Wrong data with Read from a serial port.

hi, I've a problem on my C/C++ program with Posix Library.

I have to read data from the serial but I have incorrect data, in fact I get a bunch of zeros:
"2953.3174, 2785.2126, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 , 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 , 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 , 0, 0,0, "

I should get something like this:

3087,3102,2492,1860,1403,1074,1004,1468,2152,2645,3007,3143,2714,2006,1503,1148,971,1308,1998,2536,2 925,3163,2884,2182,1625,1234,979,1170,1820,2411,2834,3135,3013,2381,1721,1328,1021,1076,1646,2290,27 46,3079,3115,2589,1901,1430,1095,1001,1442,2128,2630,2994,3156,2758 ETC...

My class that i use is this:

int cClassSerialLIB::ReadPort(/*QChar * Response*/)
{
     long int iIn;

     counter_uart2 =0;

     for (int i=0;i<6000;i++)
         array_uart2='\0';

     
       if (fd < 1)
       {
           printf(" port is not open\n");
           return -1;
       } 

       iIn = read(fd, array_uart2, sizeof(array_uart2));

       static int count=0;

       if (iIn < 0)
       {
           if (errno == EAGAIN)
           {         
                   //qDebug("Read nothing");
                   return 0; // assume that command generated no response
           }
           else
           {
                   printf("read error %d %s\n", errno, strerror(errno));
           } 
       }

       counter_uart2=iIn;
       return iIn;
}

and open function:

bool cClassSerialLIB::OpenPort(bool bloccante)
{
      // make sure port is closed
      ClosePort();
      //ttymxc4
      QByteArray Porta_byte_array = Porta.toLatin1();
      const char* Porta_char=Porta_byte_array.data();
      fd = open(Porta_char/*"/dev/ttymxc4"*/, O_RDWR |
                          O_NOCTTY | /* tells UNIX that this program doesn't want to be the "controlling terminal" for that port*/
                          O_NDELAY); /* tells UNIX that this program doesn't care what state the DCD signal line is in */

      if (fd < 0)
      {
          qDebug("errore in apertura %d %s\n", errno, strerror(errno));
          return false;
      }
      else
      {
          qDebug("ok %d",fd);
          struct termios options;

          /* The FNDELAY option causes the read function to return 0 if no
            characters are available on the port. To restore normal (blocking) behavior,
            call fcntl() without the FNDELAY option: */

          if(bloccante)
          {
              printf("bloccante\n");
              fcntl(fd, F_SETFL, 0);
          }
          else
          {
             printf("NON bloccante");
             fcntl(fd, F_SETFL, FNDELAY);
          }

          //cfmakeraw(&options); //ABILITARLO NEL CASO DI ERRORE!


          tcgetattr(fd, &options);

          //set port speed 115200
          cfsetispeed(&options, B115200);
          cfsetospeed(&options, B115200);

          options.c_cflag |= (CLOCAL | CREAD);

          //set 8n1

          options.c_cflag &= ~PARENB; /* Enable parity bit */
          options.c_cflag &= ~CSTOPB; /* 2 stop bits (1 otherwise) */
          options.c_cflag &= ~CSIZE; /* Mask the character size bits */
          options.c_cflag |= CS8; /* Select 8 data bits */

          options.c_iflag &= ~IXON; //RIMUOVE X0N/XOFF control
          options.c_iflag &= ~IXOFF;
          options.c_iflag &= ~IGNCR;

          //set raw input
          options.c_lflag &= ~(ICANON |   /*Enable canonical input (else raw)*/
                                  ECHO |  /*Enable echoing of input characters*/
                                  ECHOE | /*Echo erase character as BS-SP-BS*/
                                  ISIG);  /*Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals*/


          tcsetattr(fd, TCSANOW, &options); /*Make changes now without waiting for data to complete*/

      }
      return true;
}

how i must set option in open function? thanks..

Wouldn't that command be suitable for your needs ?

man netcat
man nc

You could easily be reading fewer bytes than you asked for. If that happens, you have to read again to get more.

Also, the read() library call returns "ssize_t", not "long int". They might coincidentally be the same on your system's architecture, but they are not the same by definition. You then truncate the "long int" value by returning an "int" from your readPort() method.

Also, you are not checking the return values for your calls to fcntl(), tcgetattr(), cfsetispeed(), cfsetospeed(), or tcsetattr(). How do you know if those calls succeeded?

1 Like

how checking the return values for my calls? thanks?

I've modified my Read Function:

int cClassSerialLIB::ReadPort_poll(){
 
    int iIn;
    struct pollfd pfds[5];
 
    pfds[0].fd     = fd;
    pfds[0].events = POLLIN;
    int nfds = 1;
 
    while(1){
        /* poll aspetta indefinitivamente (-1) */
        int pollret = poll(pfds, nfds, -1);
 
        if (pollret == -1) {
           //perror("poll");
           continue;
        }
 
        /* 2) timeout: this should never happen really... */
        if (pollret == 0) {
 
           continue;
        }
 
        if(pfds[0].revents == POLLIN){
               iIn = read(fd, array_uart2, sizeof(array_uart2));
               if (iIn < 0)
               {
                   if (errno == EAGAIN)
                   {
                           continue; // assume that command generated no response
                   }
                   else
                   {
                           printf("read error %d %s\n", errno, strerror(errno));
                   } // end if
               }
 
               aggiorna1();
        }
    }
}

aggiorna() is decodified function of my packets data, infact i receive a packet data(310 byte at max) every 50ms and so (115200 8n1) i have 1byte every 87us and so 27 ms for trasmission!

With this function i have error in my arrayuart2(the work's buffer) infact i have error on byte for example len payload is wrongor other similar errors:

My protocol:

M A M <-- header first 3 bytes
len payload <-- 2 byte
payload <--1--300 byte
EOM='m' <--1 byte
checksum<--1 byte

If i write
if(pfds[0].revents == POLLIN){
sleep(1);
iIn = read(fd, array_uart2, sizeof(array_uart2));

It work without errors but i think that i lose some packets of data because i receive data every 50ms and i wait 1second...

Thanks
Regards

if(system_function() != 0)
{
        perror("system_function didn't work right");
}

:smiley: simply...but why i have error in my packet message? :(:confused:

Well, for starters, is the port getting set up the way you think it is? That's why we've been bothering you to check your return values...

But is correct use poll function or i must use IOCTL function?

i can use IOCTL every 2 ms with a TIMER (in Qt--> QTimer) and if for 5 time number of byte is the same then read!

Is correct ?
:smiley:

poll() is not reccomended. On many systems it can't work on devices at all.

serial devices have their own read timeouts, in any case, you could perhaps use those. See man termios.

Of course, you'd have to set them up using the termios calls you're not sure are working yet. Seriously, it would be really useful to know if those worked at all.

and what i must use?
how modified my READ function? thanks

Have you checked the return value on those calls yet?

If you set up read timeouts on the serial device, read() will timeout by itself.

I've checked all system call in open function and i dont have errors!

Now how modified read function?

thanks

Understand that, since we don't have your device handy to test with, it's really hard for us to tell at a distance. I'd really hoped you were having trouble with your setup instead.

From man termios:

   Canonical and non-canonical mode
       The  setting of the ICANON canon flag in c_lflag determines whether the
       terminal is operating in canonical mode (ICANON set)  or  non-canonical
       mode (ICANON unset).  By default, ICANON set.

       In canonical mode:

       * Input  is  made  available  line by line.  An input line is available
         when one of the line delimiters is typed (NL, EOL, EOL2;  or  EOF  at
         the start of line).  Except in the case of EOF, the line delimiter is
         included in the buffer returned by read(2).

       * Line editing is enabled (ERASE, KILL; and if the IEXTEN flag is  set:
         WERASE,  REPRINT,  LNEXT).   A  read(2)  returns  at most one line of
         input; if the read(2) requested fewer bytes than are available in the
         current line of input, then only as many bytes as requested are read,
         and the remaining characters will be available for a future read(2).

       In non-canonical mode input is available immediately (without the  user
       having  to  type  a line-delimiter character), and line editing is dis-
       abled.  The settings of MIN (c_cc[VMIN]) and TIME (c_cc[VTIME])  deter-
       mine  the  circumstances  in  which a read(2) completes; there are four
       distinct cases:

       * MIN == 0; TIME == 0: If data is available,  read(2)  returns  immedi-
         ately,  with the lesser of the number of bytes available, or the num-
         ber of bytes requested.  If no data is available, read(2) returns 0.

       * MIN > 0; TIME == 0: read(2) blocks until the lesser of MIN  bytes  or
         the  number  of bytes requested are available, and returns the lesser
         of these two values.

       * MIN == 0; TIME > 0: TIME specifies the limit for a timer in tenths of
         a  second.   The  timer  is  started when read(2) is called.  read(2)
         returns either when at least one byte of data is available,  or  when
         the  timer  expires.  If the timer expires without any input becoming
         available, read(2) returns 0.

       * MIN > 0; TIME > 0: TIME specifies the limit for a timer in tenths  of
         a second.  Once an initial byte of input becomes available, the timer
         is restarted after each further byte is  received.   read(2)  returns
         either  when  the lesser of the number of bytes requested or MIN byte
         have been read, or when the inter-byte timeout expires.  Because  the
         timer  is  only  started after the initial byte becomes available, at
         least one byte will be read.

So you'd want to add these to your setup:

bool cClassSerialLIB::OpenPort(bool bloccante)
{
      // make sure port is closed
      ClosePort();
      //ttymxc4
      QByteArray Porta_byte_array = Porta.toLatin1();
      const char* Porta_char=Porta_byte_array.data();
      fd = open(Porta_char/*"/dev/ttymxc4"*/, O_RDWR |
                          O_NOCTTY | /* tells UNIX that this program doesn't want to be the "controlling terminal" for that port*/
                          O_NDELAY); /* tells UNIX that this program doesn't care what state the DCD signal line is in */

      if (fd < 0)
      {
          qDebug("errore in apertura %d %s\n", errno, strerror(errno));
          return false;
      }
      else
      {
          qDebug("ok %d",fd);
          struct termios options;

          /* The FNDELAY option causes the read function to return 0 if no
            characters are available on the port. To restore normal (blocking) behavior,
            call fcntl() without the FNDELAY option: */

          if(bloccante)
          {
              printf("bloccante\n");
              fcntl(fd, F_SETFL, 0);
          }
          else
          {
             printf("NON bloccante");
             fcntl(fd, F_SETFL, FNDELAY);
          }

          //cfmakeraw(&options); //ABILITARLO NEL CASO DI ERRORE!


          tcgetattr(fd, &options);

          //set port speed 115200
          cfsetispeed(&options, B115200);
          cfsetospeed(&options, B115200);

          options.c_cflag |= (CLOCAL | CREAD);

          //set 8n1

          options.c_cflag &= ~PARENB; /* Enable parity bit */
          options.c_cflag &= ~CSTOPB; /* 2 stop bits (1 otherwise) */
          options.c_cflag &= ~CSIZE; /* Mask the character size bits */
          options.c_cflag |= CS8; /* Select 8 data bits */

          options.c_iflag &= ~IXON; //RIMUOVE X0N/XOFF control
          options.c_iflag &= ~IXOFF;
          options.c_iflag &= ~IGNCR;

          //set raw input
          options.c_lflag &= ~(ICANON |   /*Enable canonical input (else raw)*/
                                  ECHO |  /*Enable echoing of input characters*/
                                  ECHOE | /*Echo erase character as BS-SP-BS*/
                                  ISIG);  /*Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals*/

          options.c_cc[VMIN]=0;
          options.c_cc[VTIME]=5; // For half-second timeout


          tcsetattr(fd, TCSANOW, &options); /*Make changes now without waiting for data to complete*/

      }
      return true;
}

...and then read() will timeout by itself in the manner described above.

What do do with it depends on what you need.

Does the device write entire lines, or just a stream without newlines?

etc, etc.

i have a packet of data very 50 ms without newlines

If there's no newlines, how can you tell when the packet ends? Is it a fixed size? I suspect you're catching packets in the middle, and never finding the end properly.

For that matter, it's not all going to arrive at once, either. Serial data comes in serially if it's a serial port (is it, really? is it actually some USB device?)

Every 50 milliseconds is fast enough to not bother with timeouts, poll(), select(), or anything. Few things are going to be fine-grained enough to handle it, and there's enough buffers to confuse the matter. Just read() will do.

My protocol:

M A M <-- header first 3 bytes
len payload <-- 2 byte
payload <--1--300 byte
End Of Message='m' <--1 byte
checksum<--1 byte

Read 1 byte at at a time, hutning for the 'm' character so you know when to stop reading.

you say:

unsigned char c='\0';
int i=0;
int flag=0;
while(1){
if(pfds[0].revents == POLLIN){
               iIn = read(fd, c, 1);
               if(c != 'm'){
               packet_array=c;
               i++;
               }
               else{
                    break;
                    i=0;
                    flag=1;
               }

}
if (flag==1){
        aggiorna1();
        flag=0;
}

??

No, I'm saying, don't use poll or read timeouts at all. Just read() and return once you've got a complete packet.