Pyserial操作串口


pySerial

介紹

封裝了串口通訊模塊,支持Linux、Windows、BSD(可能支持所有支持POSIX的操作系統),支持Jython(Java)和IconPython(.NET and Mono).

首頁 http://pyserial.sf.net/

特性
所有平台使用同樣的類接口
端口號默認從0開始,程序中不需要知道端口名稱
像文件讀寫一樣的API,read、write(readline等也受支持)
所有程序全由Python完成,除了標准庫外不依賴其他包,除了pywin32(windows)、JavaComm(Jython). POSIX(Linux, BSD) 只依賴Python標准庫。
依賴環境
Python2.2或更新版本
windows 上的 pywin32擴展
Java/Jython上的 "Java Communications" (JavaComm)或者兼容包
安裝
pip/easy_install

pip install pyserial

easy_install pyserial

windows

下載地址 : http://sourceforge.net/project/showfiles.php?group_id=46487

快速上手
Open port 0 at "9600,8,N,1", no timeout

>>> import serial
>>> ser = serial.Serial(0) # open first serial port
>>> print ser.portstr # check which port was really used
>>> ser.write("hello") # write a string
>>> ser.close() # close port

Open named port at "19200,8,N,1", 1s timeout

>>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
>>> x = ser.read() # read one byte
>>> s = ser.read(10) # read up to ten bytes (timeout)
>>> line = ser.readline() # read a '/n' terminated line
>>> ser.close()

Open second port at "38400,8,E,1", non blocking HW handshaking

>>> ser = serial.Serial(1, 38400, timeout=0,
... parity=serial.PARITY_EVEN, rtscts=1)
>>> s = ser.read(100) # read up to one hundred bytes
... # or as much is in the buffer

Get a Serial instance and configure/open it later

>>> ser = serial.Serial()
>>> ser.baudrate = 19200
>>> ser.port = 0
>>> ser
Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
>>> ser.open()
>>> ser.isOpen()
True
>>> ser.close()
>>> ser.isOpen()
False

如果給定端口,端口將在創建對象之后立即打開,如果沒有給定端口,可選timeout參數

timeout=None # wait forever
timeout=0 # non-blocking mode (return immediately on read)
timeout=x # set timeout to x seconds (float allowed)

Serial實例的可用方法
open() # 打開端口
close() # 立即關閉端口
setBaudrate(baudrate) # change baud rate on an open port
inWaiting() # return the number of chars in the receive buffer
read(size=1) # read "size" characters
write(s) # 把字符串s寫到該端口
flushInput() # 清除輸入緩存區,放棄所有內容
flushOutput() # 清除輸出緩沖區,放棄輸出
sendBreak() # 發送中斷條件
setRTS(level=1) # set RTS line to specified logic level
setDTR(level=1) # set DTR line to specified logic level
getCTS() # return the state of the CTS line
getDSR() # return the state of the DSR line
getRI() # return the state of the RI line
getCD() # return the state of the CD line

Serial實例的屬性
只讀

portstr # 設備名稱
BAUDRATES # list of valid baudrates
BYTESIZES # list of valid byte sizes
PARITIES # list of valid parities
STOPBITS # list of valid stop bit widths

下面屬性值被更改后端口會重新配置,即使端口已經打開

port # port name/number as set by the user
baudrate # current baud rate setting
bytesize # byte size in bits
parity # parity setting
stopbits # stop bit with (1,2)
timeout # timeout setting
xonxoff # if Xon/Xoff flow control is enabled
rtscts # if hardware flow control is enabled

異常
serial.SerialException

常量
parity:

serial.PARITY_NONE
serial.PARITY_EVEN
serial.PARITY_ODD

stopbits

serial.STOPBITS_ONE
al.STOPBITS_TWO

bytesize:

serial.FIVEBITS
serial.SIXBITS
serial.SEVENBITS
serial.EIGHTBITS

翻譯(有刪減)僅供參考

原文地址:http://blog.csdn.net/dainiao01/article/details/5885122
官方文檔:http://pyserial.sf.net/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM