pySerial
http://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial
Overview
这个模块封装了串口的访问。它为运行在Windows、OSX、Linux、BSD(可能是任何POSIX兼容系统)和IronPython的Python提供了支持。名为“串行”的模块会自动选择合适的后端。
所有支持的平台上的基于类的接口。
通过Python属性访问端口设置。
支持不同的字节大小,停止位,奇偶校验和流控制与RTS / CTS和/或Xon / Xoff。使用或不接收超时。
类似于读写(readline等)的API。
这个包中的文件是100%纯Python。
该端口设置为二进制传输。无空字节剥离、cr - lf翻译等(对POSIX支持多次)。这使得这个模块普遍有用。与io库RFC 2217客户端(实验)兼容,在示例中提供了服务器。
兼容io库
RFC 2217客户端(实验),示例中提供的服务器。
python -m pip install pyserial、
References
• Python: http://www.python.org/
• Jython: http://www.jython.org/
• IronPython: http://www.codeplex.com/IronPython
Opening serial ports
Open port at “9600,8,N,1”, no timeout:
import serial
ser = serial.Serial(‘/dev/ttyUSB0’) # open serial port
print(ser.name) # check which port was really used
ser.write(b’hello’) # write a string
ser.close() # close port
Open named port at “19200,8,N,1”, 1s timeout:
with serial.Serial(‘/dev/ttyS1’, 19200, timeout=1) as ser:
… x = ser.read() # read one byte
… s = ser.read(10) # read up to ten bytes (timeout)
… line = ser.readline() # read a ‘\n’ terminated line
Open port at “38400,8,E,1”, non blocking HW handshaking:
ser = serial.Serial(‘COM3’, 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
使用readline()时要小心。当打开串行端口时,一定要指定一个超时,否则,如果没有接收到换行符,它将永远阻塞。
列出端口:
python -m serial.tools.list_ports
访问端口:
python -m serial.tools.miniterm
pySerial API
class serial.Serial
init(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None, exclusive=None)
Parameters: • 端口port – Device name or None.
• 波特率baudrate (int) – Baud rate such as 9600 or 115200 etc.
• 数据位bytesize – Number of data bits. Possible values: FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS
• 校验位parity – Enable parity checking. Possible values: PARITY_NONE, PARITY_EVEN, PARITY_ODD PARITY_MARK, PARITY_SPACE
• 停止位stopbits – Number of stop bits. Possible values: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
• 超时timeout (float) – Set a read timeout value.
• 软件流控制xonxoff (bool) – Enable software flow control.
• rtscts (bool) – Enable hardware (RTS/CTS) flow control.
• dsrdtr (bool) – Enable hardware (DSR/DTR) flow control.
• 写入超时write_timeout (float) – Set a write timeout value.
• 字符间超时inter_byte_timeout (float) – Inter-character timeout, None to disable (default).
• 独占访问模式exclusive (bool) – Set exclusive access mode (POSIX only). A port cannot be opened in exclusive access mode if it is already open in exclusive access mode.
Raises: • ValueError – Will be raised when parameter are out of range, e.g. baud rate, data bits.
• SerialException – In case the device can not be found or can not be configured. 如果设备无法找到或无法配置。
a successive call to open() is required.
timeout = None:一直等待,直到收到请求的字节数
timeout = 0:非阻塞模式,在任何情况下立即返回,返回0或更多,直到请求的字节数
timeout = x:当请求的字节数可用时,立即将超时设置为x秒(允许浮动),否则等待超时过期并返回所收到的所有字节。
默认情况下,write()是阻塞的,除非设置write_timeout .对于可能的值,请参考上面的超时列表。
read(size=1)
读(大小= 1)
参数:大小-读取的字节数。
返回:从端口读取的字节。
返回类型:字节
从串行端口读取大小字节。如果设置了超时,它可能返回的字符会减少。没有超时,它将阻塞,直到读取请求的字节数为止。
write(data)
将字节数据写入端口。这应该是类型字节(或者兼容,如bytearray或memoryview)。Unicode字符串必须进行编码。“你好”.encode(“utf - 8”)。
flush()
像对象一样刷新文件。在这种情况下,等待所有数据都被写入。
in_waiting
Getter:获取输入缓冲区中的字节数
类型:int
返回接收缓冲区中的字节数。
版本3.0更改:从inWaiting()变为属性
out_waiting
Getter:获取输出缓冲区中的字节数
类型:int
平台:posix
windows平台:
返回输出缓冲区中的字节数。
版本2.7更改:(Posix支持添加)
版本3.0的更改:从outWaiting()改为属性
reset_input_buffer()
刷新输入缓冲区,丢弃所有内容。
版本3.0更改:从flushInput()重新命名
reset_output_buffer()
清除输出缓冲区,终止当前输出并丢弃缓冲区中的所有内容。
注意,对于一些USB串行适配器,这可能只刷新操作系统的缓冲区,而不是所有可能出现在USB部分的数据。
版本3.0更改:从flushOutput()重命名
send_break(持续时间= 0.25)
参数:持续时间(浮点数)-激活中断条件的时间。
发送打破条件。定时,在给定时间后返回空闲状态。
break_condition
获取当前的断开状态
设置:控制中断状态
类型:bool
当设置为真正的激活中断条件时,其他禁用。TXD控件。当活动时,不可能发送
rts:设置RTS线的状态
Getter:返回RTS线的状态
类型:bool
将RTS行设置为指定的逻辑级别。在打开串口之前,可以指定此值,然后将值应用于uppon open()(带有限制,参见open())
dtr
设置:设置DTR线的状态
Getter:返回DTR线的状态
类型:bool
将DTR线设置为指定的逻辑级别。在打开串口之前,可以指定此值,然后将值应用于uppon open()(带有限制,参见open())。
get_settings()
返回:带有当前端口设置的字典。
返回类型:dict类型
获取带有端口设置的字典。这对于备份当前的设置非常有用,以便稍后可以使用apply_settings()恢复它们。
注意,控制行状态(RTS / DTR)不属于设置的一部分。
新的2.5版本中。
在3.0版本中更改:从getSettingsDict重命名
apply_settings(d)
参数:d(dict)-一个带有端口设置的字典。
应用由get_settings()创建的字典。只应用更改,当缺少密钥时,意味着设置保持不变。
注意,控制行(RTS / DTR)没有改变。
新的2.5版本中。
在3.0版本中更改:从applySettingsDict重命名
apply_settings(d)
参数:d(dict)-一个带有端口设置的字典。
应用由get_settings()创建的字典。只应用更改,当缺少密钥时,意味着设置保持不变。
注意,控制行(RTS / DTR)没有改变。
新的2.5版本中。
在3.0版本中更改:从applySettingsDict重命名
exit(exc_type exc_val exc_tb)
关闭串口(异常不能由exit处理)。
exit(exc_type exc_val exc_tb)
关闭串口(异常不能由exit处理)。
fileno()
平台:posix
返回:文件描述符。
该对象打开的端口的返回文件描述符编号。当串行端口与select一起使用时,它是有用的。
cancel_read()
平台:posix
windows平台:
从另一个线程取消挂起的读操作。一个阻塞读()调用立即中止。read()不会报告任何错误,但会返回到那个点接收到的所有数据(类似于超时)。
在Posix上,对cancel_read()的调用可能会取消将来的read()调用。
新的3.1版本中。
cancel_write()
平台:posix
windows平台:
从另一个线程取消挂起的写操作。write()方法将立即返回(没有错误指示)。但是,操作系统可能仍然会从缓冲区发送,可能需要对reset_output_buffer()进行单独调用。
在Posix上,调用cancel_write()可以取消将来的write()调用。
新的3.1版本中。
serial.threaded.ReaderThread(threading.Thread)
实现串口读取循环并发送到协议实例(例如asyncio.Protocol),但要使用线程。
调用close()将关闭串行端口,但也可以停止()此线程,否则继续使用串行端口实例。
init(serial_instance protocol_factory)
参数:
serial_instance -串口实例(打开)被使用。
协议工厂-返回一个协议实例的可调用的
初始化线程。
python -m serial.tools.list_ports 列出端口号
python -m serial.tools.list_ports –v 列出端口信息