python串口通讯数据过长_Python 串口通讯
摘要:
pyserial module:?https://github.com/tbusf/pyserial
Python使用pyserial進行串口通信:http://blog.csdn.net/log1100/article/details/54380325
串口通訊的python模塊——pySerial?:http://blog.csdn.net/dainiao01/article/details/5885122
Parameters for the Serial class
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 instances
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 instancesreadonly
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)
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
居然還有這么多好東西,看看下面:
TCP/IP – serial bridge
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.
This example only exports a raw socket connection. The next example below gives the client much more control over the remote serial port.
The serial port settings are set on the command line when starting the program.
There is no possibility to change settings from remote.
All data is passed through as-is.
打開一個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() 看看這個串口是否已經被打開
importpyserial
t=serial.Serial()
t.port= 3t.baudrate= 115200t.open()
t.write(chr(0x03)) #向串口輸入ctrl+c
總結
以上是生活随笔為你收集整理的python串口通讯数据过长_Python 串口通讯的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: conda和pip的区别
- 下一篇: python删除重复值所在的行数_pyt