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:
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).
PySerial is a library which provides support for serial connections ("RS-232") over a variety of different devices: old-style serial ports, Bluetooth dongles, infra-red ports, and so on.
timeout = None : wait forever / until requested number of bytes are received. timeout = 0 : non-blocking mode, return immediately in any case, returning zero or more, up to the requested number of bytes.
PySerial is a useful package for problem solvers because it allows us to exchange data between computers and pieces of external hardware such as voltmeters, oscilloscopes, strain gauges, flow meters, actuators, and lights. PySerial provides an interface to communicate over the serial communication protocol.
This module encapsulates the access for the serial port. It provides backends for Python running on Windows, OSX, Linux, BSD (possibly any POSIX compliant system) and IronPython. The module named "serial" automatically selects the appropriate backend.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With