【发布时间】:2020-04-15 17:09:51
【问题描述】:
我有一个 GSM 调制解调器并通过 putty 对其进行测试。有用。然后我用我的 c++ 程序发送 AT,但调制解调器回复 AT。它只是回应我的命令,没有回答 OK。
这是我的代码:
#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 -qws
open_port: 成功打开端口 /dev/ttyUSB0
开始阅读.....
我的错误在哪里?
【问题讨论】:
-
找到一些可以让您准确查看通过串行端口传输的字节的工具可能会很有用。我有一个硬件设备,可以与串行电缆串联并捕获数据,并且存在允许您在数据通过串行端口驱动程序时查看数据的软件。观察线上的实际数据对于解决此类问题非常有用。您可能会看到来自您的程序的字节与您期望的不太一样,或者“OK”真的回来了,就像其他人建议的那样迟到了。
-
如何查看串口的字节传输?我认为这是我的问题。 (我的意思是我发送的数据可能有问题)。当我用我的电脑发送 AT 到我的调制解调器时,它立即回答 OK。
标签: c++ gsm at-command