gsm — вывод команды AT c ++

Я написал следующий код, который отправляет простое сообщение на мой мобильный телефон с GSM SM5100B. Но это не работает. Я хотел бы проверить выходные данные каждой строки printf с кодом 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);

}

0

Решение

Если серьезно, вы не ждете окончательного кода результата после отправки AT-команды. С помощью sleep не является действительным решением. Вы ДОЛЖЕН исправить это, чтобы сделать правильный анализ ответа, который вы получаете от модема. Смотрите следующие ответы для более подробной информации, ответ 1, ответ 2.

Тогда ваш init_gsm функция должна иметь int fd Аргумент, используемый для отправки команд AT, а также для чтения ответа от.

0

Другие решения

Метод C ++ заключается в том, что вы используете комбинацию stringstream а также write,

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

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector