摘要:python
pyserial module: https://github.com/tbusf/pyserialgit
Python使用pyserial進行串口通訊:http://blog.csdn.net/log1100/article/details/54380325github
串口通信的python模塊——pySerial :http://blog.csdn.net/dainiao01/article/details/5885122shell
Parameters for the Serial classwindows
ser = serial.Serial( port=None, # number of device, numbering starts at # zero. if everything fails, the user # can specify a device string, note # that this isn't portable anymore # if no port is specified an unconfigured # an closed serial port object is created baudrate=9600, # baud rate bytesize=EIGHTBITS, # number of databits parity=PARITY_NONE, # enable parity checking stopbits=STOPBITS_ONE, # number of stopbits timeout=None, # set a timeout value, None for waiting forever xonxoff=0, # enable software flow control rtscts=0, # enable RTS/CTS flow control interCharTimeout=None # Inter-character timeout, None to disable )
Methods of Serial instancessocket
open() # open port close() # close port immediately 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) # write the string s to the port flushInput() # flush input buffer, discarding all it's contents flushOutput() # flush output buffer, abort output sendBreak() # send break condition 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
Attributes of Serial instances
readonly函數
portstr # device name BAUDRATES # list of valid baudrates BYTESIZES # list of valid byte sizes PARITIES # list of valid parities STOPBITS # list of valid stop bit widths
New values can be assigned to the following attributes, the port will be reconfigured, even if it’s opened at that time:(即便是打開的狀況下也會從新配置???liub)this
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
竟然還有這麼多好東西,看看下面:spa
This program opens a TCP/IP port. When a connection is made to that port (e.g. with telnet) it forwards all data to the serial port and vice versa..net
This example only exports a raw socket connection. The next example below gives the client much more control over the remote serial port.
打開一個python shell
import serial導入模塊
而後就能夠用了
ser = serial.Serial(0) 是打開第一個串口
print ser.portstr 能看到第一個串口的標識,windows下是COM1
ser.write(「hello") 就是往串口裏面寫數據
ser.close() 就是關閉ser表示的串口
ser.open() 會打開這個串口
ser = serial.Serial('COM1', 115200) 來設置波特率,固然還有專門的函數
data = ser.read()能夠讀一個字符
data = ser.read(20) 是讀20個字符
data = ser.readline() 是讀一行,以/n結束,要是沒有/n就一直讀,阻塞。
data = ser.readlines()和ser.xreadlines()都須要設置超時時間
ser.baudrate = 9600 設置波特率
ser 來查看當前串口的狀態
ser.isOpen() 看看這個串口是否已經被打開
import pyserial t=serial.Serial() t.port = 3 t.baudrate = 115200 t.open() t.write(chr(0x03)) #向串口輸入ctrl+c