#53 Need for blocking read

v1.0 (example)

Hi - in the example wxTerminal and the serial port re-director, a blocking read is attempted prior to checking for the number of bytes in the receive buffer...

data = self.serial.read(1) # read one, blocking
n = self.serial.inWaiting() # look if there is more
if n:
data = data + self.serial.read(n) # and get as much as possible

Why is that necessary? I assume it uses less CPU effort than just checking the inWaiting count each time through the loop?



  • alex bondarenko

    alex bondarenko - 2012-03-03

    Hi. I think you can use different read Timeout to achieve this. Please try.

  • Chris Liechti

    Chris Liechti - 2012-08-16

    [re op] Exactly. Reading one byte at a time uses lots of system calls (read operations from the com port). these are expensive (CPU time) compared to other calls. So this variant tries to read as much as possible, though it uses 3 system calls to do this. Depending on situation it may also be more suitable to read a defined number of bytes (e.g. if the protocol always sends a header with 4 bytes, read(4) is more efficient. in such cases a timeout should be used to recover from faults where less than the expected bytes are available). Inter-character timeout may also be an option so that bigger reads can be made but still be returning quickly when there is not that much data available.

  • Chris Liechti

    Chris Liechti - 2013-10-11
    • status: open --> closed
    • Group: --> v1.0 (example)

Log in to post a comment.