Wrong data with Read from a serial port.


 
Thread Tools Search this Thread
Top Forums Programming Wrong data with Read from a serial port.
# 8  
Old 11-15-2012
Quote:
Originally Posted by enaud
Smilie simply...but why i have error in my packet message? SmilieSmilie
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...
# 9  
Old 11-15-2012
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 ?
Smilie
# 10  
Old 11-15-2012
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.
# 11  
Old 11-15-2012
and what i must use?
how modified my READ function? thanks
# 12  
Old 11-15-2012
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.
# 13  
Old 11-16-2012
Quote:
Originally Posted by Corona688
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

Last edited by enaud; 11-16-2012 at 09:46 AM..
# 14  
Old 11-16-2012
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:

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

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*/

          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.
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