0% found this document useful (0 votes)
330 views

Python For Serial Communication PDF

Python for Serial Communication PyCon APAC 2011, Singapore Outline Serial Communication Architecture Data +low Point o. / iew &S) to &A(' Dri er &s) Ca le Buffer User Driver Installation Serial Communication Architecture with ySerial 'o"ule Basic Functions Demo on Console GUI Tool Develo ment 'ool 'ip 1 Checking.or 8ew Data 2 Detecting 8ew De ice Ti

Uploaded by

Helder Pinto
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
330 views

Python For Serial Communication PDF

Python for Serial Communication PyCon APAC 2011, Singapore Outline Serial Communication Architecture Data +low Point o. / iew &S) to &A(' Dri er &s) Ca le Buffer User Driver Installation Serial Communication Architecture with ySerial 'o"ule Basic Functions Demo on Console GUI Tool Develo ment 'ool 'ip 1 Checking.or 8ew Data 2 Detecting 8ew De ice Ti

Uploaded by

Helder Pinto
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Python for Serial Communication

PyCon APAC 2011, Singapore

Eka A. Kurniawan
@ekaakurniawan

Outline

Serial Communication Architecture Dri er !n"tallation pySerial #o$ule Demo on Con"ole %&! 'ool De elopment Demo on %&!

Serial Communication Architecture

Data +low Point o. /iew


&S) to &A(' Dri er

FPGA
&A(' Connection

USB to UART
+P%A )oar$

&S) Ca*le

Buffer

(e-ue"t

User

,aptop

Driver Installation

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

Serial Communication Architecture with ySerial

Data +low Point o. /iew


&S) to &A(' Dri er pySerial

FPGA
&A(' Connection

USB to UART
+P%A )oar$

&S) Ca*le

Buffer

(e-ue"t

User

,aptop

ySerial !o"ule
pySerial mo$ule encap"ulate" the acce"" .or the "erial port. !t pro i$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 archi e 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"[email protected]

Demo on Console

GUI Tool Develo ment

&"ing PyAt SPPyAt 'ool 'ip 1< Checking .or 8ew Data 'ip 2< Detecting 8ew De ice

Ti #$ Chec%in& for 'ew Data

Polling #etho$ A$ antage< Ea"y to De elop


At Engine

4pen Connection

%et Data i" *u..er emptyB Ce"

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

8o

Di"play Data

i" u"er inputB 8o

Ce"

Sen$ Data

Ti #$ Chec%in& for 'ew Data

'imer #etho$ A$ antage< (e"ource E..iciency


At Engine

4pen Connection

Di"a$ antage< Chance o. 'riggering )u..er 4 er.low

i" *u..er emptyB Ce"

8o

%et Data Di"play Data

Sen$ Data

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 Pro i$e" (e"ource E..iciency
ser.3ead(!)

4pen Connection

(ea$er Sen$ Data 1riter Clo"e Connection '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(1ne 6ata(*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

'hrea$ Polling #etho$

'imer #etho$ 310m"6

'hrea$ with (ea$ )locking

CP&1

CP&2

Ti ($ Detectin& 'ew Device

Detecting 8ew De ice in ,inu2 Serial Port Communication


/dev/ttyS7

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

Co$e !mplementation to Detect 8ew De ice &"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 Dri er" Downloa$ing pySerial pySerial Documentation pySerial AP! PyAt (e.erence %ui$e SPPyAt 'ool ProDect Eome

S ecial Than%s

Chri" ,iechti 3pySerial De eloper6 ,oke Kwan 8g

You might also like