August 07, 2009 at 14:57 Tags Python , Serial port

The previous couple of posts about the PySerial module for serial communications with Python were just a basic introduction. Let's now see something much more useful.

PySerial makes Python a great tool for serial communications from a computer, as it can be easily combined with other powerful Python libraries to create full-scale applications. In my case, I'm using PyQt with its plotting supplementary PyQwt to create nice "live" plotting applications, that can be combined with the serial port. Here's a demo:

Download plotting_data_monitor- it's a small demo application written in Python that requires the following modules to be installed on your machine:

PyQt

PyQwt

PySerial

What does it do? Well, it basically shows how to combine all these powers of Python into a single application in a simple way. You can choose a serial port and then run the monitor (all via the menu). If another program is sending data to the specified port, you'll see the plot updating "in real time":

If you have nothing sending data to your machine, no worries. If you've installed com0com or a similar virtual port emulator like I explained here, configure it to connect two ports together.

Then, download sender_sim.py which is a very simple data-sending script (the data it provides is a pleasant pseudo-randomized sinusoid). You may want to change the port name hard-coded in it, if your port numbers are different.

When both this sender and the monitor run on the same machine, you'll be able to see the live plotting. Note that I've added a couple of extra features from PyQwt:

A "thermo" bar that shows the average temperature

A knob that sets how often the monitor updates the screen

These widgets, and a few others, make PyQwt quite a nice library for emulating Labview-type "lab control" applications in Python. I recommend it highly.