为什么我的 AT 命令在我的 GSM 调制解调器中回显?
Why my AT command echo in my GSM modem?
我有一个 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();
}
输出是这样的:
Go to open port open_port:
succesfully open port /dev/ttyUSB0 port
[root@FriendlyARM /]#./SerialPort -qws
open_port: succesfully open port /dev/ttyUSB0
start reading .....
我哪里错了?
您没有终止字符串 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));
至:
char w[5];
w[0]='A';
w[1]='T';
w[2]=13;
w[3]=10;
w[4]=0; // <<<
//sprintf(i,"2f9",k);
std::cout << w;
write(fd1,w,strlen(w)); // <<<
或者更简洁:
char w[] = "AT\r\n"
std::cout << w;
write(fd1,w,strlen(w));
调制解调器回复需要时间OK
,请尝试等待一段时间再读取,或者将读取置于循环中。
默认 GSM 模块 returns 相同的“AT”命令在 GSM 调制解调器中回显。因此,您可以通过发送命令“ATE0”来关闭回显,并通过发送命令“AT&W”将设置保存到永久内存,然后"OK"。
如果您再次需要,可以通过发送命令“ATE1”重新启用回显。
我有一个 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();
}
输出是这样的:
Go to open port open_port:
succesfully open port /dev/ttyUSB0 port
[root@FriendlyARM /]#./SerialPort -qws
open_port: succesfully open port /dev/ttyUSB0
start reading .....
我哪里错了?
您没有终止字符串 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));
至:
char w[5];
w[0]='A';
w[1]='T';
w[2]=13;
w[3]=10;
w[4]=0; // <<<
//sprintf(i,"2f9",k);
std::cout << w;
write(fd1,w,strlen(w)); // <<<
或者更简洁:
char w[] = "AT\r\n"
std::cout << w;
write(fd1,w,strlen(w));
调制解调器回复需要时间OK
,请尝试等待一段时间再读取,或者将读取置于循环中。
默认 GSM 模块 returns 相同的“AT”命令在 GSM 调制解调器中回显。因此,您可以通过发送命令“ATE0”来关闭回显,并通过发送命令“AT&W”将设置保存到永久内存,然后"OK"。 如果您再次需要,可以通过发送命令“ATE1”重新启用回显。