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