AT命令C++代码的输出 [英] output of AT command c++ code

查看:51
本文介绍了AT命令C++代码的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码,它使用 GSM SM5100B 向我的手机发送一条简单的消息.但它不起作用.我想用 C++ 代码检查每个 printf 行的输出.例如

I wrote the following code which sends a simple message to my mobile with GSM SM5100B. But it does not work. I would like to check the outputs of each printf line with c++ code. For example

AT+CMFG=1
ok
AT+CMGS="69******"
ok

等等.有什么原因要实现这个吗?

etc. Is there any why to implement this?

我的代码

#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. 
");
}
else
{
    fcntl(fd, F_SETFL, 0);
    printf("port is open.
");
}

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
");
 sleep(3);
 printf("AT+CMGS="+34603****"
");
 sleep(3);
 //printf("Hello
%c",26); 
 printf("Hellox1A");
 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 不是一个有效的解决方案.您必须修复此问题以正确解析从调制解调器返回的响应.有关更多详细信息,请参阅以下答案,答案 1答案 2.

Most seriously, you are not waiting for the final result code after sending an AT command. Using sleep is not a valid solution. You MUST fix this to do proper parsing of the response you get back from the modem. See the following answers for more details, answer 1, answer 2.

那么你的 init_gsm 函数应该有一个 int fd 参数,用于发送 AT 命令以及从中读取响应.

Then your init_gsm function ought to have an int fd argument to use for sending the AT commands to as well as reading the response from.

这篇关于AT命令C++代码的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆