python 串口 serial

2017-03-26  本文已影响0人  熊猫掰棒子

开始

想用pi的串口接收串口数据,然后显示,记录,处理。
python的串口通信模块 serial,还不知道怎么查看serial的内部函数,help不出来,感觉作者没有写,网上找到些资料。
参考资料


Be carefully when using “readline”. Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that “readlines” only works with a timeout. “readlines” depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly.

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

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.

  1. The serial port settings are set on the command line when starting the program.
  2. There is no possibility to change settings from remote.
  3. All data is passed through as-is.
上一篇 下一篇

猜你喜欢

热点阅读