python串⼝库函数_python串⼝模块pyserial简单介绍⼯作实践
真知分享ITp。。。
前⾯已经介绍过ubuntu下安装ch341的驱动程序下⾯安装python的串⼝模块,这样就直接可以将协调器发⽣送过来的所有数据,通过python 串⼝读出来,然后写⼊到数据库中(可以使⽤python操作数据库的),有关数据库的远程同步(搜索mysql 同步)这个以后再说.
⾸先要有⼏点认识:
Features
same class based interface on all supported platforms
access to the port settings through Python 2.2+ properties
port numbering starts at zero, no need to know the port name in the user program
port string (device name) can be specified if access through numbering is inappropriate
support for different bytesizes, stopbits, parity and flow control with RTS/CTS and/or Xon/Xoff
working with or without receive timeout
file like API with “read” and “write” (“readline” etc. also supported)
The files in this package are 100% pure Python. They depend on non standard but common packages on Windows
(pywin32) and Jython (JavaComm). POSIX (Linux, BSD) uses only modules from the standard Python distribution)
The port is set up for binary transmission. No NULL byte stripping, CR-LF translation etc. (which are many times enabled
for POSIX.) This makes this module universally useful.
⿊体字的部分注意⼀下,特别是最后⼀项.全部是⼆进制传输.
sudo easy_install pyserial
然后测试⼀下
sudo python(这是为了⼀会给串⼝读写权限)
>>> import serial
>>> ser=serial.Serial('/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0')
>>> ser
Serial(port='/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
python怎么读取串口数据>>> ser.timeout=1
>>> ser
Serial(port='/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False, dsrdtr=False)
有时候如果你使⽤/dev/ttyUSB0⾏不通的话,就直接使⽤/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0
timeout=None, # set a timeout value, None for waiting forever
上⾯timeout的意思是,如果是none,那么程序永远会死在这⾥.⼀直等待
timeout=0 # non-blocking mode (return immediately on read)
timeout = 0: ⾮阻塞形式 (读完之后就返回,费时的io操作,就直接交给后台处理了)
timeout=x # set timeout to x seconds (float allowed)
超时时间⼀到,程序就是继续执⾏
————————–没错我是分割线–开始————————————
⼩知识:
读写串⾏⼝时,既可以同步执⾏,也可以重叠(异步)执⾏。
在同步执⾏时,函数直到操作完成后才返回。这意味着在同步执⾏时线程会被阻塞,从⽽导致效率下降。
在重叠执⾏时,即使操作还未完成,调⽤的函数也会⽴即返回。费时的I/O操作在后台进⾏,这样线程就可以⼲别的事情。
例如,线程可以在不同的句柄上同时执⾏I/O操作,甚⾄可以在同⼀句柄上同时进⾏读写操作。”重叠”⼀词的含义就在于此。
阻塞的定义:
对于read,block指当串⼝输⼊缓冲区没有数据的时候,read函数将会阻塞在这⾥,⼀直到串⼝输⼊缓冲区中有数据可读取,read读到了需要的字节数之后,返回值为读到的字节数;
对于write,block指当串⼝输出缓冲区满,或剩下的空间⼩于将要写⼊的字节数,则write将阻塞,⼀直到串⼝输出缓冲区中剩下的空间⼤于等于将要写⼊的字节数,执⾏写⼊操作,返回写⼊的字节数。
⾮阻塞的定义:
对于read,no block指当串⼝输⼊缓冲区没有数据的时候,read函数⽴即返回,返回值为0。
对于write,no block指当串⼝输出缓冲区满,或剩下的空间⼩于将要写⼊的字节数,则write将进⾏写操作,写⼊当前串⼝输出缓冲区剩下空间允许的字节数,然后返回写⼊的字节数。
—————————-没错我是分割线–结束———————————-
Be carefully when using “readline”. Do specify a timeout when opening the serial port otherwise it could block foreverif 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
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)
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.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论