简体   繁体   English

AT命令C ++代码的输出

[英]output of AT command c++ code

I wrote the following code which sends a simple message to my mobile with GSM SM5100B. 我编写了以下代码,该代码通过GSM SM5100B向手机发送了一条简单消息。 But it does not work. 但这行不通。 I would like to check the outputs of each printf line with c++ code. 我想用c ++代码检查每个printf行的输出。 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);

}

The C++ method is that you use a combination of stringstream and write . C ++方法是结合使用stringstreamwrite

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

Most seriously, you are not waiting for the final result code after sending an AT command. 最严重的是,发送AT命令后,您不必等待最终的结果代码。 Using sleep is not a valid solution. 使用sleep不是有效的解决方案。 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 . 有关更多详细信息,请参见以下答案, 答案1答案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. 然后,您的init_gsm函数应该具有一个int fd参数,可用于发送AT命令以及从中读取响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM