Python for Serial Communication
PyCon APAC 2011, Singapore
Eka A. Kurniawan
@ekaakurniawan
Outline
Serial Communication Architecture
Driver Installation
pySerial Module
Demo on Console
GUI Tool Development
Demo on GUI
Serial Communication
Architecture
Data Flow Point of View
USB to UART
Driver
FPGA
UART
Connection
USB
to
UART
FPGA Board
USB Cable
Buffer
Request
User
Laptop
Driver Installation
Silicon Labs CP2103 USB to UART Bridge VCP Drivers
Serial Communication
Architecture with pySerial
Data Flow Point of View
USB to UART
Driver
FPGA
pySerial
UART
Connection
USB
to
UART
FPGA Board
USB Cable
Buffer
Request
User
Laptop
pySerial Module
pySerial module encapsulates the access for the serial port. It
provides backends for Python running on Windows, Linux, BSD
(possibly any POSIX compliant system), Jython and IronPython
(.NET and Mono). The module named serial automatically selects
the appropriate backend. - Chris Liechti
Benefits:
Run on multi-platform
100% Python
Easy to install
Easy to use
pySerial Module
Installation
From Source
Get the archive (pyserial-x.y.tar.gz) from
http://pypi.python.org/pypi/pyserial. Unpack it, go to pyserial-x.y
directory and run:
python setup.py install
Ubuntu 10.10
pySerial Module
Basic Functions
Importing pySerial Module
import serial
Serial Class
ser = serial.Serial('/dev/ttyUSB0', 9600)
open and isOpen Functions
ser.open()
ser.isOpen()
write Function
ser.write('1')
pySerial Module
Basic Functions
inWaiting Function
ser.inWaiting()
read Function
ser.read(455)
ser.read(ser.inWaiting())
close Function
ser.close()
pySerial API
http://pyserial.sourceforge.net/pyserial_api.html
Demo on Console
GUI Tool Development
Using PyQt
SPPyQt Tool
Tip 1: Checking for New Data
Tip 2: Detecting New Device
Tip 1: Checking for New Data
Polling Method
Open Connection
Advantage: Easy to
Develop
Disadvantages:
Resource Inefficiency
and Signal Blocking
Get Data
Qt Engine
is buffer empty?
No
Display Data
Yes
is user input?
No
Yes
Send Data
Tip 1: Checking for New Data
Timer Method
Open Connection
Advantage: Resource
Efficiency
Disadvantage:
Chance of Triggering
Buffer Overflow
Qt Engine
is buffer empty?
No
Yes
Get Data
Display Data
Send Data
Tip 1: Checking for New Data
Code Implementation for Timer Method Using PyQt
During __init__
self.logTimer = None
Inside connect Function
self.logTimer = Qtimer()
QObject.connect(self.logTimer, SIGNAL("timeout()"),
self.checkBuffer)
self.logTimer.start(100)
Inside disconnect Function
self.logTimer.stop()
Tip 1: Checking for New Data
Thread Method
Open
Connection
Advantage: No
Blocking Signal
Thread Combined
with Read Blocking
Provides Resource
Efficiency
Reader
Send Data
Writer
ser.Read(1)
Close
Connection
Threads
Tip 1: Checking for New Data
Code Implementation for Thread with Read Blocking Method
Using PyQt
Reader Thread Keeps Looping on Following Code
data = self.ser.read(1)
n = self.ser.inWaiting()
if n:
data = data + self.ser.read(n)
self.emit(SIGNAL("newData(QString)"), data)
Writer Thread Executes Following Code
self.ser.write(str(self.cmd))
Checking for New Data
CPU Usage
Timer Method (1ms)
Thread Polling Method
Timer Method (10ms)
Thread with Read Blocking
CPU1
CPU2
Tip 2: Detecting New Device
Detecting New Device in Linux
Serial Port Communication
/dev/ttySx
USB-to-Serial Communication
/dev/ttyUSBx
Code Implementation to Detect New Device Using Python
import glob
glob.glob("/dev/ttyS*")
glob.glob("/dev/ttyUSB*")
Demo on GUI
Links
Silicon Labs CP2103 USB to UART Bridge VCP Drivers
Downloading pySerial
pySerial Documentation
pySerial API
PyQt Reference Guide
SPPyQt Tool Project Home
Special Thanks
Chris Liechti (pySerial Developer)
Loke Kwan Ng