我编写了以下代码,该代码使用 GSM SM5100B 向我的手机发送一条简单的消息。但这不起作用。我想用 C++ 代码检查每个 printf 行的输出。
例如
AT+CMFG=1
ok
AT+CMGS="69******"
ok
ETC。
有什么理由要实施这个吗?
My code
#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);
}
最严重的是,发送 AT 命令后您并没有等待最终结果代码。使用sleep
不是一个有效的解决方案。你MUST修复此问题以正确解析从调制解调器返回的响应。有关更多详细信息,请参阅以下答案,answer 1 https://stackoverflow.com/a/16404193/23118, answer 2 https://stackoverflow.com/a/16446523/23118.
那么你的init_gsm
函数应该有一个int fd
用于发送 AT 命令以及读取响应的参数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)