0

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();
  }
LtWorf
  • 7,286
  • 6
  • 31
  • 45
dali1985
  • 3,263
  • 13
  • 49
  • 68
  • "during the compiling" - how do you intend to examine runtime information at compile time? –  May 23 '13 at 07:36
  • or else to give me the results as mentioned above Ok, Ok, Ok, Ok etc at the end of compilation. – dali1985 May 23 '13 at 07:40

1 Answers1

1

You could make a function called like sendAT, which would do something like this:

int sendAT(char* command,int fd) {
  size_t cmdlen = strlen(command);

  int n = write(fd,command, cmdlen);
  if (n != cmdlen)
      return -1
  char reply[40];
  n = read(fd,reply,sizeof(reply));
  reply[n] = 0; //Terminate the string
  if (strcmp(reply, "OK")==0) 
      return 0; //All went well
  else
      return -1; //Some error occurred
}

At the moment you have a lot of duplicated code that does the same thing for every command that you send to the phone.

LtWorf
  • 7,286
  • 6
  • 31
  • 45
  • Thanks a lot. I will use your code to test it. One more question, is there any way this code to send me not only 0 or -1 but also the results OK, CME Error etc? I mean to send me exactly the error which maybe I will have. – dali1985 May 23 '13 at 08:20
  • You should pass a buffer as a parameter and use it in the read, so the calling function can see the result of the last command, but I'd keep the integer return code because doing strcmp(buf,"OK") at every command would again be duplicating code. – LtWorf May 23 '13 at 10:15