yogeshbhimani
yogeshbhimani

Reputation: 59

Read AT COMMAND output using c code?

I want to read output of my AT command which is sent to GSM modem using c code.I made a code but in this code buffer is not showing proper output.please help me. I want to print the cell information using this code.AT command which is I use is "AT+CCED=0,2".

int main()
{
  int fd;  // File descriptor
  int n,i;
  char buf[1000]={"\0"}; 
  char com[20]={"at+cced=0,2\r"};       
  fd = open_port();

  // Read the configureation of the port

  struct termios options;
  tcgetattr( fd, &options );

  /* SEt Baud Rate */

  cfsetispeed( &options, B115200 );
  cfsetospeed( &options, B115200 );

  //I don't know what this is exactly

  options.c_cflag |= ( CLOCAL | CREAD );

  // Set the Charactor size

  options.c_cflag &= ~CSIZE; /* Mask the character size bits */
  options.c_cflag |= CS8;    /* Select 8 data bits */

  // Set parity - No Parity (8N1)

  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;

  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

  // Disable Software Flow control

  options.c_iflag &= ~(IXON | IXOFF | IXANY);

  // Chose raw (not processed) output

  options.c_oflag &= ~OPOST;

  if ( tcsetattr( fd, TCSANOW, &options ) == -1 )
    printf ("1Error with tcsetattr = %s\n", strerror ( errno ) );
  else
    printf ( "%s\n", "tcsetattr succeed" );

  fcntl(fd, F_SETFL, FNDELAY);


  // Write some stuff !!!

  n = write(fd, com, strlen(com));
  if (n < 0)
    fputs("write() of 4 bytes failed!\n", stderr);
  else
    printf ("Write succeed n  = %i\n", n );

  n=0;
  i=0;
  while (1) 
  {
    n = read( fd, buf, sizeof(buf) );

    if(n>0)
    {   
      printf("%s", buf);    
      fflush(stdout);
    }
    //   i=i+1;
  }

  close( fd );
  return 0;
}           

int open_port(void)
{
  int fd; /* File descriptor for the port */

  fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1)
  {
    perror("open_port: Unable to open /dev/ttyUSB0 - ");
  }
  else
    fcntl(fd, F_SETFL, FNDELAY);

  printf ( "In Open port fd = %i\n", fd); 
  return (fd);
}

Upvotes: 1

Views: 4213

Answers (1)

AK_
AK_

Reputation: 2059

I've done a project like this before but I used Java! It was very similar (and painful).

I will give you a walk through of what I did to make it work:

  1. Did you set the correct rates and bit flags of the serial port?
  2. Use Putty or Super Terminal to ensure that you can communicate with your GSM modem BEFORE trying to code anything.
  3. Ensure you give some idle time to wait for the GSM modem to start (aka: warming up time).
  4. Some special chars cause problems - are you handling them correctly?
  5. Are you sending EOF, \r\n, or \0 (depends on the GSM modem) at the end of your communication? -> Sometimes you won't get any messages until you send this special char.

Also make use of tested libraries:

Alternatively you can use online GSM/SMS services if your whole project depends on the service rather than the modem itself.

Upvotes: 1

Related Questions