C++ AT命令c++;代码

C++ AT命令c++;代码,c++,gsm,at-command,C++,Gsm,At Command,我编写了以下代码,它通过GSM SM5100B向我的手机发送一条简单的消息。但它不起作用。我想用C++代码检查每个打印输出线的输出。 比如说 AT+CMFG=1 ok AT+CMGS="69******" ok 等等。 为什么要实施这一点 我的代码 #include <stdio.h> // standard input / output functions #include <string.h> // string function definitions #incl

我编写了以下代码,它通过GSM SM5100B向我的手机发送一条简单的消息。但它不起作用。我想用C++代码检查每个打印输出线的输出。 比如说

AT+CMFG=1
ok
AT+CMGS="69******"
ok
等等。 为什么要实施这一点

我的代码

#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitionss
#include <time.h>   // time calls

int open_port(void)
{
int fd; // file description for the serial port
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) // if open is unsucessful
{
    printf("open_port: Unable to open /dev/ttyAMA0. \n");
}
else
{
    fcntl(fd, F_SETFL, 0);
    printf("port is open.\n");
}

return(fd);
} //open_port

int configure_port(int fd)      // configure the port
{
struct termios port_settings;      // structure to store the port settings in

cfsetispeed(&port_settings, B9600);    // set baud rates
cfsetospeed(&port_settings, B9600);

port_settings.c_cflag &= ~PARENB;    // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;

tcsetattr(fd, TCSANOW, &port_settings);    // apply the settings to the port
return(fd);

}  

void init_gsm()
{
 printf("AT+CMGF=1\r\n");
 sleep(3);
 printf("AT+CMGS=\"+34603****\"\r\n");
 sleep(3);
 //printf("Hello\r\n%c",26); 
 printf("Hello\x1A");
 sleep(3);
// printf("\x1A");

}
int main(void)
{
int fd = open_port();
configure_port(fd);
sleep(5);
//query_modem(fd);
    init_gsm();
return(0);

}
#包括//标准输入/输出函数
#包含//字符串函数定义
#包含//UNIX标准函数定义
#包含//文件控制定义
#包括//错误号定义
#包括//POSIX终端控制定义
#包括//定时通话
int open_端口(无效)
{
int fd;//串行端口的文件说明
fd=开放(“/dev/ttyam0”,O|RDWR | O|NOCTTY | O|NDELAY);
if(fd=-1)//如果打开不成功
{
printf(“打开端口:无法打开/dev/ttyam0。\n”);
}
其他的
{
fcntl(fd,F_SETFL,0);
printf(“端口已打开。\n”);
}
返回(fd);
}//打开\u端口
int configure_port(int fd)//配置端口
{
struct termios port_settings;//存储端口设置的结构
cfsetispeed(&port_settings,B9600);//设置波特率
cfsetospeed(端口设置和B9600);
port_settings.c_cflag&=~PARENB;//设置无奇偶校验、停止位、数据位
端口设置.c\u cflag&=~CSTOPB;
端口设置.c\u cflag&=~CSIZE;
端口_settings.c_cflag |=CS8;
tcsetattr(fd、TCSANOW和port_设置);//将设置应用于端口
返回(fd);
}  
void init_gsm()
{
printf(“AT+CMGF=1\r\n”);
睡眠(3);
printf(“AT+CMGS=\”+34603****\”\r\n);
睡眠(3);
//printf(“你好\r\n%c”,26);
printf(“Hello\x1A”);
睡眠(3);
//printf(“\x1A”);
}
内部主(空)
{
int fd=打开_端口();
配置_端口(fd);
睡眠(5);
//查询调制解调器(fd);
init_gsm();
返回(0);
}

<代码> > P> C++方法是使用代码< StrugSuth和p>
stringstream ss;
string number;

cout << "Enter phone number to send to:");  
cout.flush();
cin >> number;
ss << "AT+CMGS=\"" << number << "\"\r\n";
string str = ss.str();
write(fd, str.c_str(), str.length());
stringstreamss;
字符串编号;
数量;

ss最严重的是,在发送AT命令后,您没有等待最终的结果代码。使用
睡眠
不是有效的解决方案。必须解决此问题,才能正确解析从调制解调器返回的响应。有关更多详细信息,请参阅以下答案


那么您的
init\u gsm
函数应该有一个
int fd
参数,用于向发送AT命令以及从中读取响应。

您能更具体一些吗?仅仅说“它不工作”并不能告诉我们太多。虽然这可能与您没有向调制解调器写入任何内容有关?我的意思是我没有结果…我只有打开的端口和3条printf线。我想我需要一个读取缓冲区的代码。你需要的是写入调制解调器()并从中读取()的代码。首先对这些函数进行实验。