0

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

}
dali1985
  • 3,263
  • 13
  • 49
  • 68
  • 1
    Could you please be more specific? Just saying "it does not work" doesn't really tell us much. – Some programmer dude May 21 '13 at 06:57
  • Although it might have something to do with you not writing anything to the modem? – Some programmer dude May 21 '13 at 06:58
  • I mean I do not have results...I have only port is open and the 3 printf lines. I think I need a code to read buffer – dali1985 May 21 '13 at 07:06
  • What you need is code to write to the modem ([`write`](http://pubs.opengroup.org/onlinepubs/009695399/functions/write.html)) and read from it ([`read`](http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html)). Experiment with those functions first. – Some programmer dude May 21 '13 at 07:11

2 Answers2

0

The C++ method is that you use a combination of stringstream and 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());
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
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.

Community
  • 1
  • 1
hlovdal
  • 26,565
  • 10
  • 94
  • 165