Interfacing with a RS232 serial device is a common task when using Python in embedded applications. The easiest way to get python talking to serial ports is use the pyserial project found at http://pyserial.sourceforge.net/. This module works on most platforms and is straightforward to use (see examples on project web site). However, getting the read function in this module to operate in an optimal way takes a little study and thought. This article investigates how the pyserial module works, possible issues you might encounter, and how to optimize serial reads.
# connect to serial port: ser = serial. Serialforurl (args. SERIALPORT, donotopen = True) ser. Timeout = 3 # required so that the reader thread can exit # reset control line as no remote 'terminal' has been connected yet: ser. Dtr = False: ser. Rts = False: logging. Info ('RFC 2217 TCP/IP to Serial redirector - type Ctrl-C / BREAK to quit') try: ser. Open except serial.
We start out with several goals as to how we want the application to behave in relation to the serial port:
- application must block while waiting for data.
- for performance reasons, we want to read decent size chunks of data at a time if possible. Python function calls are expensive, so performance will be best if we can read more than one byte at a time.
- We want any data received returned in a timely fashion.
A key parameter in the pyserial Serial class is the timeout parameter. This parameter is defined as:
- To read single byte from serial device. Data = ser.read to read given number of bytes from the serial device. Data = ser.read(size=5) to read one line from serial device. Data = ser.readline to read the data from serial device while something is being written over it. #for python2.7 data = ser.read(ser.inWaiting) #for python3 ser.read(ser.
- This quick-start example shows how you can create a UART device for communicating with hardware serial devices. To use this example, you'll need something to generate the UART data. We've used a GPS! Note that the GPS will give you UART data without getting a fix on your location. You can use this example right from your desk!
- Usage examples can be found in the examples where two TCP/IP - serial converters are shown, one using threads (the single port server) and an other using select (the multi port server). Note Each new client connection must create a new instance as this object (and the RFC 2217 protocol) has internal state.
The Serial class read function also accepts a size parameter that indicates how many characters should be read. Below is the source for the read function on Posix systems (Linux, etc):
The easy way to use this module is to simply set the timeout to None, and read size to 1. This will return any data received immediately. But, this setup is very inefficient when transferring large amounts of data due to the Python processing overhead. Driver updater pro crack.
To meet our goal of reading multi-byte blocks of data at a time, we need to pass the read function a size greater than 1. However, if timeout is set to None, the read will block until size bytes have been read, which does not meet the goal of returning any data read in a timely fashion. The solution then is to:
Python Serial Readline Timeout
- set the read size high enough to get good performance
- set the timeout low enough so that any data received is returned in a reasonable timeframe, but yet the application spends most of its time blocked if there is no data.
Python Pyserial Read Example
As an example, a size of 1000 and a timeout of 1 second seems to perform well. When used this way, the pyserial module performs well and returns all data read quickly.