problem in serial port reading after send AT to gsm modem

hani
Hi. I have mini2440 and i connect a GSM modem to it.I want to work with my
modem and send AT command by serial port.
this is my code:
=======================================================
#include <QCoreApplication>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <iostream>
int fd;
int openport(void)
{

fd=open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY);

        if (fd==-1)
                {
                perror("open_port: unable to open port /dev/ttyUSB0\n");
                return -1;
                }
        else
                {
                printf("open_port: succesfully open port /dev/ttyUSB0\n");
                fcntl(fd,F_SETFL,0);
                return 1;
                }
}

void closeport(void)
{
    close(fd);
}

void configport(void)
{
struct termios options;
tcgetattr(fd,&options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |=  (CLOCAL | CREAD);
tcsetattr(fd,TCSANOW,&options);
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~ PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_iflag &= ~(IXON|IXOFF|IXANY);
}

void writeport1(void)
{
    char w[]="AT\r\n";
    write(fd,w,sizeof(w));
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int aa ;
    char i[10];
    aa=openport();
    if (aa==1)
    {
        configport();
        writeport1();
        printf("start reading ..... \n");
        aa=read(fd,i,sizeof(i));
        printf("%d",aa);
        i[aa]=0;//terminate the string
        //std::cout << i << std::endl;
        printf("finish reading ..... \n");

    }

    closeport();
    return a.exec();
}
==========================================
i can send AT but i cant get ok from my device.
where is my mistake?
this is my output:
==========================================
[root@FriendlyARM /]# ./SerialPort -qws
open_port: succesfully open port /dev/ttyUSB0
start reading .....

davef
Are you sure the command was received by the modem?

Search for <serial port loop-back test> so you can test your software and
the connecting lead to the modem.

hani
Hi @Devef and thanks my friend to reply.

>Are you sure the command was received by the modem?
when i test my modem over windows. its work perfectly ( I send AT and its
reply immediately OK)
but when i test it on Ubuntu putty , it has problem.
just show me the black page and nothing happen when i write AK ( enter). 

I guess my serial port is correct because it is worked on windows.

but for test my program: i trace it line by line in qt :
1-config port 
2-open port
3-write(fd,"AT\r\n",4)
4-read port ---> in this line is stop my program and nothing happen

davef
OK, you are talking to this modem using your host running Ubuntu.

Have you set Putty up the same way on the Ubuntu machine as the Windows
machine?  Just checking :)

Think I would still be trying a loop-back test to verify that Putty is
working properly on the Ubuntu machine.

hani
Hi. i think i find my answer but before write the sample program I should
say @Devef you say right. :D i have mistake to connect modem to my pc :D
---------------------------------------------------------------
this is AT program that work in mini2440:
---------------------------------------------------------------
#include <QCoreApplication>
#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 fd; /* File descriptor for the port */
/*
    * 'open_port()' - Open serial port 1.
    *
    * Returns the file descriptor on success or -1 on error.
    */

int openport(void)
{

fd=open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY);

        if (fd==-1)
                {
                perror("open_port: unable to open port /dev/ttyUSB0\n");
                return -1;
                }
        else
                {
                printf("open_port: succesfully open port /dev/ttyUSB0\n");
                fcntl(fd,F_SETFL,0);
                return 1;
                }
}
  
//========================================================================

   void closeport(void)
   {
       close(fd);
   }
   void configport(void)
   {
   struct termios options;
   tcgetattr(fd,&options);
   cfsetispeed(&options,B9600);
   cfsetospeed(&options,B9600);
   options.c_cflag |=  (CLOCAL | CREAD);
   tcsetattr(fd,TCSANOW,&options);
   options.c_cflag &= ~CSIZE;
   options.c_cflag |= CS8;
   options.c_cflag &= ~ PARENB;
   options.c_cflag &= ~CSTOPB;
   options.c_cflag &= ~CSIZE;
   options.c_cflag |= CS8;
   options.c_iflag &= ~(IXON|IXOFF|IXANY);
   }
//============================================================
   int                  /* O - 0 = MODEM ok, -1 = MODEM bad */
      init_modem()   /* I - Serial port file */
      {
        char buffer[255];  /* Input buffer */
        char *bufptr;      /* Current char in buffer */
        int  nbytes;       /* Number of bytes read */
        int  tries;        /* Number of tries so far */

        for (tries = 0; tries < 3; tries ++)
        {
         /* send an AT command followed by a CR */
      if (write(fd, "AT\r", 3) < 3)
        continue;

         /* read characters into our string buffer until we get a CR or NL
*/
      bufptr = buffer;
      while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr -
1)) > 0)
      {
        bufptr += nbytes;
        if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
              break;
      }

         /* nul terminate the string and see if we got an OK response */
      *bufptr = '\0';

      for(int c=0 ; c < sizeof(buffer) ; c++)
      {
          if(buffer[c]=='O' && buffer[c+1]=='K')
          {
              printf("OK");
              return (1);

          }
      }
        }

        return (-1);
      }
  
//==============================================================================
==
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    openport();
    configport();
    int a2;
    while(1)
    {
        a2=init_modem();
        if (a2==1)
        {
            qDebug("OK");
            break;
        }


    }
    closeport();
    return a.exec();
}
-----------------------------------------------------------------
my mistake is in read char and the size of char.

thanks all and @Devef
;)

adri
Hi hani,
Did you make any progress since you tried this code ?
I'm trying to do the same with my own gsm modem but I've seen that the
termios parameters are very unstable and responses are very random.
Sometimes you have to wait 30 sec, sometimes it responds directly etc...

hani
Hi @adri.
I leave that program.
what you mean  " I've seen that the
termios parameters are very unstable and responses are very random"

At first test your device with putty on Linux (if you use Linux OS).
OK, If it works fine ,It means your device is fine.

Then check your application with loop back to test your application is fine
or not. ( after send data to port , then receive it). loop back like this :
connect RX and TX together

If both device and you app correct ,say to me. And also put your code
completely here.

adri
Hi again,

It's resolved now, another process was working with my modem, consequently
the termios parameters were changing all the time.
Moreover my program was reading other command results that were coming out
from nowhere. 

Thanks for your code, some pieces were very useful to me. And thanks anyway
for your help :)

hani
OK, very good to resolved.
 :)

roma
hello .. 
can you help me ...
what do  you do to solve this problem please ...

problem in serial port reading after send AT to gsm modem

i need to read all sms in loop .. and when the modem read specified msg
send to the number some words as "reply" ...

then continue to read  the other massages.. 

but my prog is stop after sending and not read any thing after that ..
can you help me to solve please ?..

thanks