Python is a great programming (scripting) language that is supported on all platforms. Embedded enthusiasts love it because of variety of modules that allows connecting things to internet and other crazy stuff by writing only few lines of code. Today my interest was to read serial data with using Python 3.3 and pyserial module. Python 3.3 is still new and there is a majority of modules that needs to be ported from Python 2 to Python 3, but I hope soon they will here. Serial module seems to be working fine in Windows 7 x64 and decided to share my experience.
First of all download and install Python 3 (current release is Python 3.3)
Just be sure to include “Add python.exe to Path” so you could run Python anywhere in you computer. We wont go in to details on how to setup this tool. It is pretty robust. After setup python install is placed in C:\Python33. To test if things work OK open command prompt and type python you should see python prompt:
Here you can instantly do basic stuff like math, run basic python commands and so on.
After we made sure Python is ready then we need pyserial module. It is used to implement serial communications using serial port. This is number one module used by Arduino users and microcontroller programmers who want connect devices to computer using Python. Firs of all download latest pyserial. Currently it is a pyserial-2.6.tar.gz. Place package anywhere in your computer and extract it. Using command prompt navigate to pyserial-2.6 folder where setup.py file is located. Then enter simple command:
python setup.py install
then setup automatically installs pyserial module. You can check if it the by going to C:/Python33/Lib/site-packages. Here you should see serial folder.
Make sure you run Command Prompt as Administrator! Otherwise you wont be able to use serial module. In order to check if its working in command prompt type python. Then in Python shell type import serial. If no errors occurred, then it means serial module is working.
Now fun part. From code snippets found on internet I put a small python script which lists all existing serial ports. Then prompts user to select port to use and then opens it and reads incoming data lines that are printed in console immediately:
#! /usr/bin/env python3 import serial def scan(): """scan for available ports. return a list of tuples (num, name)""" available =  for i in range(256): try: s = serial.Serial(i) available.append( (i, s.portstr)) s.close() # explicit close 'cause of delayed GC in java except serial.SerialException: pass return available if __name__=='__main__': print ("Found ports:") for n,s in scan(): print ("(%d) %s" % (n,s)) selection = input("Enter port number:") try: ser = serial.Serial(eval(selection), 9600, timeout=1) print("connected to: " + ser.portstr) except serial.SerialException: pass while True: # Read a line and convert it from b'xxx\r\n' to xxx line = ser.readline().decode('utf-8')[:-2] if line: # If it isn't a blank line print(line) ser.close()
Script can be edited with any editor you like. I find Progammer’s Notepad to be pretty good. Make sure you name file with .py extension. In order to run script, using command prompt navigate to file location and then run it by typing:
and pressing enter. You should see something similar:
As you can see program found two ports – COM1 and COM4 with indexes 0 and 3. in order to select port you need to type index number which in my case were 3. The program confirms that we are connected to COM4 and start receiving string lines.
For demonstration I used ChipKIT UNO32 board and Mpide IDE which works exactly same as Arduino. In code examples you can find ASCII table generating code. It simply sends all table via serial interface. You don’t need to edit code – just upload sketch in to ChipKIT or Arduino if you like and run it.
With this simple script we made very simple terminal. With further coding we can extract data and do some processing, storing to file or drawing graphs of numeric values.