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

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

问题描述

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

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

 <$ c 







$ b $ c> #include< stdio.h> //标准输入/输出函数
#include< string.h> // string function definitions
#include< unistd.h> // UNIX标准函数定义
#include< fcntl.h> //文件控制定义
#include< errno.h> //错误号定义
#include< termios.h> // POSIX终端控制定义
#include< time.h> //时间调用

int open_port(void)
{
int fd; //串口的文件描述
fd = open(/ dev / ttyAMA0,O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)//如果open不成功
{
printf(open_port:无法打开/ dev / ttyAMA0。\\\
);
}
else
{
fcntl(fd,F_SETFL,0);
printf(port is open.\\\
);
}

return(fd);
} // open_port

int configure_port(int fd)//配置端口
{
struct termios port_settings; //结构以存储端口设置在

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

port_settings.c_cflag& =; =〜PARENB; //设置无奇偶校验,停止位,数据位
port_settings.c_cflag& =〜CSTOPB;
port_settings.c_cflag& =;〜CSIZE;
port_settings.c_cflag | = CS8;

tcsetattr(fd,TCSANOW,& port_settings); //将设置应用到端口
return(fd);

}

void init_gsm()
{
printf(AT + CMGF = 1 \r\\\
);
sleep(3);
printf(AT + CMGS = \+34603 **** \\r\\\
);
sleep(3);
// printf(Hello\r\\\
%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 不是有效的解决方案。您必须修复此问题,以正确解析从调制解调器返回的响应。有关详情,请参阅以下答案, answer 1 answer 2



然后你的 init_gsm 函数应该有一个 int fd 参数用于发送AT命令以及从中读取响应。


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?

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);

}

解决方案

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.

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天全站免登陆