Wrong data with Read from a serial port.


 
Thread Tools Search this Thread
Top Forums Programming Wrong data with Read from a serial port.
# 1  
Old 11-13-2012
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:


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

     counter_uart2 =0;

     for (int i=0;i<6000;i++)
         array_uart2[i]='\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:


Code:
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..
# 2  
Old 11-13-2012
Wouldn't that command be suitable for your needs ?

Code:
man netcat

Code:
man nc

# 3  
Old 11-14-2012
You could easily be reading fewer bytes than you asked for. If that happens, you have to read again to get more.
# 4  
Old 11-14-2012
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?
This User Gave Thanks to achenle For This Post:
# 5  
Old 11-15-2012
Quote:
Originally Posted by achenle
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?
how checking the return values for my calls? thanks?

I've modified my Read Function:

Code:
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

Last edited by enaud; 11-15-2012 at 04:55 AM..
# 6  
Old 11-15-2012
Quote:
Originally Posted by enaud
how checking the return values for my calls?
Code:
if(system_function() != 0)
{
        perror("system_function didn't work right");
}

# 7  
Old 11-15-2012
Quote:
Originally Posted by Corona688
Code:
if(system_function() != 0)
{
        perror("system_function didn't work right");
}

Smilie simply...but why i have error in my packet message? SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Cabling and adapters to communicate to service processor serial port from Windows PC with USB port.

Hello, I have an unloaded T5140 machine and want to access the ILOM for the first time and subsequently the network port after that., and then load Solaris 10 the final January 2011 build. The first part is what confuses me -the cabling. I am coming from a Windows machine (w/appropriate... (5 Replies)
Discussion started by: joboy
5 Replies

2. Debian

Reading data from a serial port

Dear List - I am trying to capture data from a serial port and write it to a file. /var/www$ cat /dev/ttyS0 > scale_value.html cat: /dev/ttyS0: Device or resource busy /var/www# cat /proc/tty/driver/serial serinfo:1.0 driver revision: 0: uart:16550A port:000003F8 irq:4 tx:90... (11 Replies)
Discussion started by: Meow613
11 Replies

3. Programming

Read from serial port

Hi I try to communicate with a GSM modem, from C, for sending SMS. I use standart AT-commands. Working well with terminal. There is no problem writing ti the port. But when I try to read I only get a echo, I write "ATI" and get "ATI" back, I should get somthing like "SIEMENS 35... (4 Replies)
Discussion started by: dmiller
4 Replies

4. Programming

unable to send read and write serial port

hey frns pls help me out !! i hav a code of c that i have to include in my project. i am using a device (geomeda) that has unix based OS. it also support SIM card for connecting to server . I need to send SMS to user from this device.. below code is not working .. i am unable to send sms and the... (7 Replies)
Discussion started by: yashwantkumar
7 Replies

5. Solaris

How to enable Serial port on ILOM, when Network Port is enabled in parallel

Hi Everyone, In my environment, I have few T5220. On the iLOM Management Card, I have both Network and Serial port are cabled, I don't have any issues while I try to connect using Network Management port, but when I try to connect the serial port for the same server which is actually connected... (3 Replies)
Discussion started by: bobby320
3 Replies

6. Shell Programming and Scripting

Need help with serial port

Hi, I have a external board connected to my serial port. I need to execute "shutdown -r now" command when system boot up. When system boots up it requires a username ans password. Then I need to run my command. I can use rc script but that is rebooting system before it asks for username and... (0 Replies)
Discussion started by: charlie.arya
0 Replies

7. Programming

Problem with read data from serial device

I have problem with C programming. I want to send & receive data through serial communication. I send data(command) to device to get data from device but when receive data, it can't get altogether of data. It get only some data. What should I do to get altogether of data? If all of... (7 Replies)
Discussion started by: noppon_s
7 Replies

8. SCO

data transfer from serial port

dear sir, pls. can you help me ? , my os is unix sco 5.0.4 and ,server dat derive (1,4gb) not working, now i want to transfer my server data in other machine (unix/other possible) by serial port/other port comminication. thanks pankaj raval (2 Replies)
Discussion started by: pankajbraval
2 Replies

9. Linux

Urgent - serial port

Hi All, I am a begineer in Linux, I have 4 ports and 3 are operational port 1,port 2 and port 4 (when I plug in serial device I can see it working) but port 3 seems it is not working. I am sure the hardware is fine. when I give command dmesg | grep tty I get, serial 8250:ttyS0 at... (0 Replies)
Discussion started by: vr_82
0 Replies
Login or Register to Ask a Question