Python For Serial Communication
Python For Serial Communication
Eka A. Kurniawan
@ekaakurniawan
Outline
Driver Installation
pySerial Module
Demo on Console
Demo on GUI
Serial Communication
Architecture
FPGA
UART
Connection
USB
to
UART
FPGA Board
USB Cable
Buffer
Request
User
Laptop
Driver Installation
Serial Communication
Architecture with pySerial
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
Serial Class
ser = serial.Serial('/dev/ttyUSB0', 9600)
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
Using PyQt
SPPyQt Tool
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
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
During __init__
self.logTimer = None
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
CPU1
CPU2
USB-to-Serial Communication
/dev/ttyUSBx
Demo on GUI
Links
Downloading pySerial
pySerial Documentation
pySerial API
Special Thanks
Loke Kwan Ng