c - how to read and write data on serial port using threads -


I am creating a serial port application in which I am creating two threads is WRITER THREAD which write the data serial port And a reader thread that will read data from the serial port. I know how to open, configure, read and write data on a serial port, but how to use it thread.

I am using Linux (Ubuntu) and I am trying to s in

The way I have done this in the past to open ttyS0 port programming, use port 0's VMIN and a VTIME for asynchronous I / O, say, 5 deciseconds its purpose was to give notice to the thread when It was time to close the application, because he could try to read, the time to get out, the sound coming out Is able to check, and then trying to read some more.

Here is an example read function:

  size_t myread (char * buf, size_t len) {size_t total = 0; While (lane> 0) {ssize_t bytes = read (FD, buff, lane); If (bytes == -1) {If (error! = EAGN & errno! = EINTR) / / a real error, nothing that will try again if (total> gt;) {total return; } And {return -1; }}} And if (byte == 0) {// EOF returns total; } And {total + = bytes; Buf + = Bytes; Lane = bytes; }} Total return; }   

The writing function makes you hope.

In your setup function, make sure to set:

  struct Termosio Tios; ... tios.c_cflag & amp; = ~ ICANON; Tios.c_cc [VMIN] = 0; Tios.c_cc [VTIME] = 5; // You want to tweak it; 5 = 1/2 second, 10 = 1 second, ... ...    

Comments