Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is inter_byte_timeout (interCharTimeout) in pyserial?

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

like image 688
Mads Skjern Avatar asked Mar 08 '16 15:03

Mads Skjern


People also ask

Is PySerial the same as serial?

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.

What is timeout in python serial?

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.

What is PySerial used for?

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.

What is serial module in Python?

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.


1 Answers

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.

like image 71
ndemou Avatar answered Oct 09 '22 12:10

ndemou