Python Pyserial Readline Example

Opening serial ports¶

Do also have a look at the example files in the examples directory in the source distribution or online. Note The eol parameter for readline is no longer supported when pySerial is run with newer Python versions (V2.6+) where the module io is available. Welcome to pySerial’s documentation¶. 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.

Open port at “9600,8,N,1”, no timeout:

Open named port at “19200,8,N,1”, 1s timeout:

Open port at “38400,8,E,1”, non blocking HW handshaking:

Configuring ports later¶

Get a Serial instance and configure/open it later:

Also supported with context manager:

Readline¶

Python Pyserial Readline Example

Be careful when using readline(). Do specify a timeout when opening theserial port otherwise it could block forever if no newline character isreceived. Also note that readlines() only works with a timeout.readlines() depends on having a timeout and interprets that as EOF (endof file). It raises an exception if the port is not opened correctly.

Do also have a look at the example files in the examples directory in thesource distribution or online.

Note

Pyserial example code

The eol parameter for readline() is no longer supported whenpySerial is run with newer Python versions (V2.6+) where the moduleio is available.

EOL¶

To specify the EOL character for readline() or to use universal newlinemode, it is advised to use io.TextIOWrapper:

Testing ports¶

Listing ports¶

python-mserial.tools.list_ports will print a list of available ports. Itis also possible to add a regexp as first argument and the list will onlyinclude entries that matched.

Note

The enumeration may not work on all operating systems. It may beincomplete, list unavailable ports or may lack detailed descriptions of theports.

Accessing ports¶

pySerial includes a small console based terminal program calledserial.tools.miniterm. It can be started with python-mserial.tools.miniterm<port_name>(use option -h to get a listing of all options).

It’s useful to be able to read and plot serial data in real time (for example, you might want to monitor the output of a laser scanner or IMU). While this is a trivial task in MATLAB or LabVIEW, I wondered if there was a low effort way to do it for free.

I’ve known for a while that Python has an easy-to-use serial library, but I wasn’t sure what kinds of plotting/graphing options might exist for Python. A quick search turned up Matplotlib – a MATLAB-like plotting API for Python. As it turns out, Matplotlib includes an animation API and a function called FuncAnimation, which can be used to animate data over time (or update a graph with some sensor data over time).

I’m using this page to document my attempt(s) to use Matplotlib to create a real time graph of data read from a serial port. For a proper introduction to Matplotlib, I’d recommend sentdex’s Matplotlib video series.

Items used

  • Laptop or PC running Ubuntu 18
  • Python 3.x
  • Matplotlib
  • pyserial
  • Arduino (or any programmable device with a serial port)

Installing matplotlib and pyserial on Ubuntu 18

Generating some fake serial data with an Arduino

Python Pyserial Readline Example

Pyserial Readline Example

Python pyserial read example

To test my code, I used an Arduino to put some data on the serial port. In the example code below, the arduino simulates a coin toss using the function random.

Python Pyserial Readline Example Python

Python Pyserial Readline Example

On each iteration of the main loop, a random integer between 0 and 1 is generated, and the relative frequency of getting one side of the coin or the other is updated. This data is then sent to the serial port as comma delimitted line, where the termination character is ‘/n’. That is, the serial data looks like this:

The idea is that I’m putting some data on a serial port over time, and now I can write a python script to read and plot it.

Creating the real time plot

References

Python Pyserial Readline Example Java

  • https://pythonhosted.org/pyserial/shortintro.html
  • https://www.youtube.com/watch?v=ZmYPzESC5YY
  • https://matplotlib.org/
  • https://matplotlib.org/api/_as_gen/matplotlib.animation.FuncAnimation.html#matplotlib.animation.FuncAnimation