Question

I want to send the next command "at" to my gprs through the serial port. The gprs should respond "ok", but I cannot figure it out my code below. When I send the command in gtkterm (in my Debian) "at" and I press enter the gprs responds "ok" without problem but in my code something is wrong. I thought that it is the \r but I don't know.

New code but with the same result:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <stdbool.h>

int main(int argc,char** argv)
{
    char comando[]={'a','t','\r','\0'}; 
        comunicacion(comando);

        return EXIT_SUCCESS;
}
comunicacion(char data[])
{
    struct termios tio;
        struct termios stdio;
        struct termios old_stdio;
        int tty_fd;

        unsigned char c ='d';
        tcgetattr(STDOUT_FILENO,&old_stdio);

        printf("Please start with /dev/ttyS1 (for example)\n");
        memset(&stdio,0,sizeof(stdio));
        stdio.c_iflag=0;
        stdio.c_oflag=0;
        stdio.c_cflag=0;
        stdio.c_lflag=0;
        stdio.c_cc[VMIN]=1;
        stdio.c_cc[VTIME]=0;
        tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
        tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
        fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);       // make the reads non-blocking

        memset(&tio,0,sizeof(tio));
        tio.c_iflag=0;
        tio.c_oflag=0;
        tio.c_cflag=CS8|CREAD|CLOCAL;           // 8n1, see termios.h for more information
        tio.c_lflag=0;
        tio.c_cc[VMIN]=1;
        tio.c_cc[VTIME]=5;

        tty_fd=open("/dev/ttyUSB0", O_RDWR | O_NONBLOCK);      
        cfsetospeed(&tio,B115200);            // 115200 baud
        cfsetispeed(&tio,B115200);            // 115200 baud

        tcsetattr(tty_fd,TCSANOW,&tio);
        // enter \r


        int i =0;
        char caracter = ' ';
        bool ciclo = true;

        while(ciclo)
        {
            c=data[i];
            i++;
            if(c != '\0')
            {
               write(tty_fd,&c,1);    
            }
            else
            {
               if(read(tty_fd,&c,1)>0)
               {
                   write(STDOUT_FILENO,&c,1);
               } 
               else
               {
                   ciclo = false;
               }
            }
        }

        // if new data is available on the serial port, print it out
        // if new data is available on the console, send it to the serial port

        close(tty_fd);
        tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);
}
Was it helpful?

Solution

Your loop can easily run off the end of you data array causing undefined behavior. You need to ALWAYS set ciclo=false when data[i] is 0, so you should get rid of the (mostly useless) ciclo variable and just use while (data[i] != 0) as your loop.

If you want to send your data and then read the response (which seems likely), then you need TWO loops -- first a loop to write the command, then a second non-nested loop to read the response.

OTHER TIPS

Your loop while (ciclo) is infinite: ciclo never becomes false. Basically, you should send command and then read the response (and the response may contain more than one character per each sent character).

Also, I'm not sure how you detect the end of data: currently your command contains only 3 characters, without terminating '\0'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top