I wrote the following code in which I am trying to send a message from a SM5100b GSM(which is connected to a Rasberry Pi) to my mobile. It is working but I can check the results of AT commands for example Ok, Ok, +CME ERROR: 4, Ok only when I have Cutecom emulator opened. How can I write a "read" function in this code to give me these results during the compiling line by line? I tried something like out = read(fd, n, sizeof(n)) but I did not have results. I am using Raspian a Debian OS and Codeblocks.
#include <stdio.h> /* Standard input/output definitions */
#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 definitions */
int open_port(void)
{
int fd; /* File descriptor for the port */
int n,d,e,f,o,out;
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/* Could not open the port. */
perror("open_port: Unable to open /dev/ttyAMA0");
}
else
fcntl(fd, F_SETFL, 0);
sleep(2);
n = write(fd, "AT\r\n", 4);
if (n < 0)
fputs("write() of 4 bytes failed!\n", stderr);
sleep(2);
d = write(fd, "AT+CMGF=1\r", 10);
if (d < 0)
fputs("write() of 10 bytes failed!\n", stderr);
sleep(2);
e = write(fd, "AT+CMGS=\"6034****\"\r", 20);
if (e < 0)
fputs("write() of 20 bytes failed!\n", stderr);
sleep(2);
f = write(fd, "hello\r\x1A", 10);
if (f < 0)
fputs("write() of 10 bytes failed!\n", stderr);
sleep(2);
return (fd);
}
int main(void)
{
open_port();
}