为什么我的AT命令在我的GSM调制解调器中回显?

问题描述 投票:1回答:3

我有一个GSM调制解调器,并用腻子对其进行测试。有用。然后我用我的c ++程序发送AT,但是调制解调器回复AT。它只是呼应我的命令,没有答案。这是我的代码:

#include <QCoreApplication>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <iostream>
int fd;
int openport(void)
{

fd=open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY);

        if (fd==-1)
                {
                perror("open_port: unable to open port /dev/ttyUSB0\n");
                return -1;
                }
        else
                {
                printf("open_port: succesfully open port /dev/ttyUSB0\n");
                fcntl(fd,F_SETFL,0);
                return 1;
                }
}
void closeport(void)
{
    close(fd);
}
void configport(void)
{
struct termios options;
tcgetattr(fd,&options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |=  (CLOCAL | CREAD);
tcsetattr(fd,TCSANOW,&options);
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~ PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_iflag &= ~(IXON|IXOFF|IXANY);
}
void writeport1(void)
{
    char w[]="AT\r\n";

    //std::cout << strlen(w);
    //std::cout << w;

    write(fd,w,sizeof(w));
}



int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int aa ;
    char i[10];
    aa=openport();
    if (aa==1)
    {
        configport();
        writeport1();
        printf("start reading ..... \n");
        aa=read(fd,i,sizeof(i));
        i[aa]=0;//terminate the string
        //std::cout << i << std::endl;
        printf("finish reading ..... \n");

    }

    closeport();
    return a.exec();
}

输出是这个:

打开端口open_port:成功打开端口/ dev / ttyUSB0端口[root @ FriendlyARM /]#./SerialPort -qwsopen_port:成功打开端口/ dev / ttyUSB0开始阅读.....

我的错误在哪里?

c++ gsm at-command
3个回答
1
投票

您尚未终止字符串w,因此虽然write调用很好,但是当将其传递给cout时,您会得到不确定的行为-更改:

char w[4];
w[0]=65;
w[1]=84;
w[2]=13;
w[3]=10;
//sprintf(i,"2f9",k);
std::cout << w;
write(fd1,w,sizeof(w));

0
投票

调制解调器需要一些时间来回复OK,尝试等待一段时间然后读取,或将读取放入循环中。


0
投票

默认情况下,GSM模块返回相同的“ AT

© www.soinside.com 2019 - 2024. All rights reserved.