Sei sulla pagina 1di 21

Python for Serial Communication

Python for Serial Communication


PyCon APAC 2011, Singapore
PyCon APAC 2011, Singapore
Eka A. Kurniawan
Eka A. Kurniawan
@ekaakurniawan @ekaakurniawan
Outline

Serial Communication Architecture

Drier !n"tallation

pySerial #o$ule

Demo on Con"ole

%&! 'ool Deelopment

Demo on %&!
Serial Communication
Architecture
FPGA
USB
to
UART
&A('
Connection
&S) Ca*le
Buffer
&S) to &A('
Drier
User
+P%A )oar$ ,aptop
(e-ue"t

Data +low Point o. /iew


Driver Installation

Silicon ,a*" CP2100 &S) to &A(' )ri$ge /CP Drier"


Serial Communication
Architecture with ySerial
FPGA
USB
to
UART
&A('
Connection
&S) Ca*le
Buffer
&S) to &A('
Drier
pySerial
User
+P%A )oar$ ,aptop
(e-ue"t

Data +low Point o. /iew


ySerial !o"ule
pySerial mo$ule encap"ulate" the acce"" .or the "erial port. !t
proi$e" *acken$" .or Python running on 1in$ow", ,inu2, )SD
3po""i*ly any P4S!5 compliant "y"tem6, 7ython an$ !ronPython
3.8E' an$ #ono6. 'he mo$ule name$ 9"erial: automatically "elect"
the appropriate *acken$. ; Chri" ,iechti
)ene.it"<

(un on multi;plat.orm

100= Python

Ea"y to in"tall

Ea"y to u"e
ySerial !o"ule
Installation

+rom Source
%et the archie 3py"erial;2.y.tar.g>6 .rom
http<??pypi.python.org?pypi?py"erial. &npack it, go to py"erial;2.y
$irectory an$ run<
python setup.py install

&*untu 10.10
ySerial !o"ule
Basic Functions

!mporting pySerial #o$ule


import serial

Serial Cla""
ser = serial.Serial('/dev/ttyUSB0', 9600)

open an$ i"4pen +unction"


ser.open()
ser.isOpen()

write +unction
ser.rite('!')
ySerial !o"ule
Basic Functions

in1aiting +unction
ser.in"aitin#()

rea$ +unction
ser.read($%%)
ser.read(ser.in"aitin#())

clo"e +unction
ser.&lose()

pySerial AP!
http<??py"erial."ource.orge.net?py"erial@api.html
Demo on Console
GUI Tool Develoment

&"ing PyAt

SPPyAt 'ool

'ip 1< Checking .or 8ew Data

'ip 2< Detecting 8ew Deice


Ti #$ Chec%in& for 'ew Data
4pen Connection
i" *u..er emptyB
Di"play Data
%et Data
i" u"er inputB Sen$ Data
8o
Ce"
Ce"
8o

Polling #etho$

A$antage< Ea"y to
Deelop

Di"a$antage"<
(e"ource !ne..iciency
an$ Signal )locking
A
t

E
n
g
i
n
e
Ti #$ Chec%in& for 'ew Data

'imer #etho$

A$antage< (e"ource
E..iciency

Di"a$antage<
Chance o. 'riggering
)u..er 4er.low
4pen Connection
Di"play Data
%et Data
Sen$ Data
8o
Ce"
A
t

E
n
g
i
n
e
i" *u..er emptyB
Ti #$ Chec%in& for 'ew Data

Co$e !mplementation .or 'imer #etho$ &"ing PyAt

During @@init@@
sel'.lo#(imer = )one

!n"i$e connect +unction


sel'.lo#(imer = *timer()
*O+,e&t.&onne&t(sel'.lo#(imer, S-.)/0(1timeout()1),
sel'.&he&2Bu''er)
sel'.lo#(imer.start(!00)

!n"i$e $i"connect +unction


sel'.lo#(imer.stop()
Ti #$ Chec%in& for 'ew Data

'hrea$ #etho$

A$antage< 8o
)locking Signal

'hrea$ Com*ine$
with (ea$ )locking
Proi$e" (e"ource
E..iciency
ser.3ead(!)
4pen
Connection
(ea$er
Sen$ Data
Clo"e
Connection
1riter
'hrea$"
Ti #$ Chec%in& for 'ew Data

Co$e !mplementation .or 'hrea$ with (ea$ )locking #etho$


&"ing PyAt

(ea$er 'hrea$ Keep" ,ooping on +ollowing Co$e


data = sel'.ser.read(!)
n = sel'.ser.in"aitin#()
i' n4
data = data 5 sel'.ser.read(n)
sel'.emit(S-.)/0(1ne6ata(*Strin#)1), data)

1riter 'hrea$ E2ecute" +ollowing Co$e


sel'.ser.rite(str(sel'.&md))
Chec%in& for 'ew Data
CPU Usa&e

'imer #etho$ 31m"6

'imer #etho$ 310m"6


CP&2 CP&1

'hrea$ Polling #etho$

'hrea$ with (ea$ )locking


Ti ($ Detectin& 'ew Device

Detecting 8ew Deice in ,inu2

Serial Port Communication


/dev/ttyS7

&S);to;Serial Communication
/dev/ttyUSB7

Co$e !mplementation to Detect 8ew Deice &"ing Python


import #lo+
#lo+.#lo+(1/dev/ttyS81)
#lo+.#lo+(1/dev/ttyUSB81)
Demo on GUI
)in%s

Silicon ,a*" CP2100 &S) to &A(' )ri$ge /CP Drier"

Downloa$ing pySerial

pySerial Documentation

pySerial AP!

PyAt (e.erence %ui$e

SPPyAt 'ool ProDect Eome


Secial Than%s

Chri" ,iechti 3pySerial Deeloper6

,oke Kwan 8g

Potrebbero piacerti anche