1. Python File Readlines
  2. Python Pyserial Readline Example Java
  3. Pyserial Readline Example
  4. Python Pyserial Read Example

All the API interface documentation for pySerial can be located. Now we need verify communication works. I will post one code example from my programming below, in part 3. It is what I used to verify that my setup works correctly. There is additional code examples on the official pySerial documentation page. Part 3: Code in Python. Python Serial Control Example Download the Serial Example: DSIPythonEX (zipped.py) After python and the pyserial module has been installed on your system, this example code will send connect, send, and receive commands from our products.

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

Python documentation: Read from serial port. Initialize serial device. Data = ser.readline to read the data from serial device while something is. Do also have a look at the example files in the examples directory in the source distribution or online. Note: The eolparameter for readlineis no longer supported when pySerial is run with newer Python versions (V2.6+) where the module iois available.

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

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.

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

  • 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

Introduction

Python File Readlines

To use Python as a graphical interface for an Arduino powered robot, programmatically read the USB with the pySerial library. However, waiting for input from pySerial's Serial object is blocking, which means that it will prevent your GUI from being responsive. The process cannot update buttons or react to input because it is busy waiting for the serial to say something.

Python Pyserial Readline Example Java

Python Pyserial Readline Example

The first key is to use the root.after(milliseconds) method to run a non-blocking version of read in the tkinter main loop. Keep in mind that when TkInter gets to the root.mainloop() method, it is running its own while loop. It needs the things in there to run every now and then in order to make the interface respond to interactions. If you are running your own infinite loop anywhere in the code, the GUI will freeze up. Alternatively, you could write your own infinite loop, and call root.update() yourself occasionally. Both methods achieve basically the same goal of updating the GUI.

Pyserial Readline Example

However, the real issue is making sure that reading from serial is non-blocking. Normally, the Serial.read() and Serial.readline() will hold up the whole program until it has enough information to give. For example, a Serial.readline() won't print anything until there is a whole line to return, which in some cases might be never! Even using the after() and update() methods will still not allow the UI to be updated in this case, since the function never ends. This problem can be avoided with the timeout=0 option when enitializing the Serial object, which will cause it to return nothing unless something is already waiting in the Serial object's buffer.

Python Pyserial Read Example

Code