1

I do have open a native serial port on linux using termios.
The port is opened in blocking mode and with the settings VMIN = 10 and VTIME = 5. I thought this should mean the blocking read function is returning after half a second if the timeout hits.

Although I found this one here:

A read() is satisfied when either VMIN characters have been transferred to the caller's buffer, or when VTIME tenths expire between characters. Since this timer is not started until the first character arrives, this call can block indefinitely if the serial line is idle. This is the most common mode of operation, and we consider VTIME to be an intercharacter timeout, not an overall one. his call should never return zero bytes read.

So my question is now, how to make the call return on this timeout if the line is idle?

Thanks!

p0fi
  • 1,056
  • 12
  • 28
  • (1) VMIN and VTIME are only used when non-canonical input is configured. Did you do that? (2) *"how to make the call return on this timeout if the line is idle?"* -- That all depends on whether you want to deal with possibly reading zero characters, or will (sensibly) wait/block for at least one character. Study the full web page http://www.unixwiz.net/techtips/termios-vmin-vtime.html (that you should have credited). – sawdust May 29 '15 at 08:44
  • (1) I was searching now for a while and I can't find how to know am I using non-canonical or canonical input mode. Could you give me a hint how to find out and/or set the mode? (2) reading zero's would not be a problem – p0fi May 29 '15 at 17:46
  • (1) For non-canonical mode, ICANON (plus others) should be cleared in the c_lflag member of the termios structure. See [Serial Programming Guide for POSIX Operating Systems](http://www.cmrr.umn.edu/~strupp/serial.html). (2) I did not write *"reading zero's"*, but meant receiving no data from the read() syscall (i.e. the return code is 0). For a "pure timed read", use `VMIN = 0`. – sawdust May 29 '15 at 19:40
  • Oh my bad then, sorry! But it would be also no problem to receive no data from the call since I initialize the read buffer with 0's. So if no data would be read, it would still be all 0. I'm fine with that. A pure timed read isn't what I want. So given that, how can I make the read() call return after 10 bytes read or a timeout? – p0fi May 29 '15 at 22:04
  • 1
    *"how can I make the read() call return after 10 bytes read or a timeout?"* -- You haven't analyzed this properly. What is supposed to happen if there's only 9 bytes? Should the read() continue to block or should it return just the 9 bytes instead of the full 10? *"since I initialize the read buffer with 0's."* -- Do not use initialized buffer contents to determine valid data when you have a 100% accurate read byte-count. – sawdust May 30 '15 at 00:51
  • If there are only 9 bytes read it should block. Until the timeout from VTIME hits. Okay I could not use initialized buffer but why not? If read() gets any data the 0's will be overwritten, if not the buffer will contain 0's. – p0fi May 30 '15 at 09:59
  • I suspect a simple Linux **read()** cannot do what you expect. You'll probably have to write your own **read_message()** to ensure that you have assembled a message of 10 bytes that is properly byte aligned (or report a timeout or incomplete message). Do not try to use **read()** alone and expect to always obtain byte-aligned messages. You may have to use **select()** with a timeout as well as **read()**. *"If read() gets any data the 0's will be overwritten"* -- What happens when the data are zeroes (or whatever the buffer is initialized with)? You have a 100% accurate read byte-count. – sawdust Jun 01 '15 at 02:33
  • I know the exact sequence of bytes which should be inside the buffer after a successful read. So if there is anything another than this, I can report an error. – p0fi Jun 01 '15 at 03:17
  • Go ahead, learn the hard way by writing bad code. It's inefficient. It's data and/or device dependent. It's not portable. You can made it work for today, but not the next project or change in device. It's the kind of code that works most of the time, but fails occasionally. – sawdust Jun 01 '15 at 05:18
  • I don't want to write bad code. I am here to ask how to make it better. It is a check to find out if that specific device is connected to the port. So I know what to send and in case of success I know the exact answer. If there is anything else in the buffer it's not that device and I'm okay with that. – p0fi Jun 01 '15 at 06:05

0 Answers0