10

Previously interCharTimeout, since version 3.0 inter_byte_timeout.

API: http://pyserial.readthedocs.org/en/latest/pyserial_api.html#serial.Serial.inter_byte_timeout

I suspect that the following is the difference between the regular timeout and inter_byte_timeout is the following:

  • timeout: The countdown starts when calling a read function. Even if the bytes keep coming, it will stop reading / throw exception when the specified amount of time has passed from when the read function was called.
  • inter_byte_timeout: Starts the count down clock over, every time a byte is received. If the characters come in continuous stream with fx 1ms between bytes, it can go on forever if the inter_byte_timeout is just greater than 1ms.

Am I right about this?

I suspect not, because I just can't make the function timeout. I have tried with the example below. I was expecting the inter_byte_timeout to make it read and print one "string" at at time, as they are written by the arduino, one per second. Instead it timeouts every three seconds and then prints what came in for that period.

Arduino sketch that writes to serial:

void setup() {
  Serial.begin(9600);
  while(!Serial); //wait for connection
  for (int i=0;true;i++){
    Serial.printf("=== iteration %d ===\n", i);
    delay(1000); //ms
  }
}

void loop() {
}

Python script:

import serial
ser=serial.Serial(port='/dev/ttyACM0', timeout=3,inter_byte_timeout=0.01)
for i in range(100):
    a = ser.read(10000)
    print i, len(a), repr(a)

Command line output:

~$ python test.py
0 60 '=== iteration 0 ===\n=== iteration 1 ===\n=== iteration 2 ===\n'
1 60 '=== iteration 3 ===\n=== iteration 4 ===\n=== iteration 5 ===\n'
2 60 '=== iteration 6 ===\n=== iteration 7 ===\n=== iteration 8 ===\n'

I am using serial 3.0.1, on Ubuntu. The serial device on the other end is a Teensy (Arduino analog).

Mads Skjern
  • 5,648
  • 6
  • 36
  • 40

1 Answers1

9

Your understanding seems correct (see this page about UNIX termios VMIN and VTIME). I took a closer look at the code of serialposix.py and I see that on POSIX systems (like Linux and android) inter_byte_timeout is set in tenths of a second. So when you ask for a value of 0.01 this line of code in pyserial...

vtime = int(self._inter_byte_timeout * 10)

...converts it to a vtime of zero and that's why your code fails. Set it to 0.1 or higher and it should work.

You may also wish to look at the url_handlers documentation and specifically the alt:// handler and see if you can successfully use the PosixPollSerial alternative handler for read().


P.S.: I didn't check the code for non POSIX systems.

ndemou
  • 4,691
  • 2
  • 30
  • 33
  • 2
    Wow, ndemou! Nice find, and that to me is a candidate for a patch to pyserial. I would think that any nonzero value specified should be set as the minimum possible nonzero value, (if too low), rather than zero! like: `vtime = max(int(self._inter_byte_timeout*10),1) if self._inter_byte_timeout else 0` – RufusVS Sep 01 '17 at 16:50