并发无关的serialio API
项目描述
serialio
一个python并发无关的串行行库。
当处理通过串行行工作并实现简单的REQ-REP通信协议的仪器时很有帮助(例如:SCPI)。
除了本地串行行,serialio还支持通过RFC2217协议的串行行、原始TCP套接字和tango。
关于RFC2217,它应该与以下兼容
- ser2net桥接程序与telnet(RFC2217)和原始配置
- gserial[ser2tcp]桥接程序(RFC2217)
关于tango,它应该与tango类兼容
使用asyncio编写的基类实现,支持不同的并发模型
- asyncio
- 经典阻塞API
- 基于future的API
以下是预期实现的内容摘要
并发 | 本地 | RFC2217 | 原始TCP | Tango |
---|---|---|---|---|
asyncio | Y | Y | Y | Y |
经典同步 | Y | Y | Y | Y |
并发未来 | Y | Y | Y | Y |
安装
在您最喜欢的Python环境中
pip install serialio
用法
asyncio
import asyncio
import serialio.aio.tcp
async def main():
sl = serialio.serial_for_url("serial-tcp://lab1.acme.org:5000")
# or the equivalent:
# sl = serialio.aio.tcp.Serial("lab1.acme.org", 5000)
await sl.open()
# Assuming a SCPI complient on the other end we can ask for:
reply = await sl.write_readline(b"*IDN?\n")
print(reply)
await sl.close()
asyncio.run(main())
本地串行线
import serialio.aio.posix
sl = serialio.aio.posix.Serial("/dev/ttyS0")
# or the equivalent
sl = serialio.serial_for_url("serial:///dev/ttyS0")
原始TCP套接字
import serialio.aio.tcp
sl = serialio.aio.tcp.Serial("lab1.acme.org:5000")
# or the equivalent
sl = serialio.serial_for_url("serial+tcp://lab1.acme.org:5000")
RFC2217(telnet)
import serialio.aio.rfc2217
sl = serialio.aio.rfc2217.Serial("lab1.acme.org:5000")
# or the equivalent
sl = serialio.serial_for_url("rfc2217://lab1.acme.org:5000")
Tango
(需要pip install serialio[tango]
安装)
import serialio.aio.tango
sl = serialio.aio.tango.Serial("lab/01/serial-01")
# or the equivalent
sl = serialio.serial_for_url("serial+tango://lab/01/serial-01")
经典
from serialio.aio.tcp import Serial
sl = Serial("lab1.acme.org", 5000)
reply = sl.write_readline(b"*IDN?\n")
print(reply)
concurrent.futures
from serialio.sio.tcp import Serial
sl = Serial("lab1.acme.org", 5000, resolve_futures=False)
reply = sl.write_readline(b"*IDN?\n").result()
print(reply)
与serial的API差异
- 基于协程的API
open()
协程必须在使用串行线之前显式调用- 通过函数而不是属性设置参数(即:使用
await ser_line.set_XXX(value)
而不是ser_line.XXX = value
(例如:await ser_line.set_baudrate()
)) - 自定义
eol
字符(串行默认为b"\n"
) - 包含REQ/REP原子函数(
write_read()
系列)
特性
serialio串行对象的主要目标是便于与连接到串行线的仪器进行通信。
最常见的情况包括期望ASCII协议如SCPI的REQ/REP语义的仪器。在这些情况下,大多数命令都转换为主机和仪器之间交换的小数据包。
REQ-REP语义
许多仪器都采用请求-应答协议。serialio串行提供了有用的write_read
方法系列,这些方法简化了与这些仪器的通信。
自定义EOL
在线性协议中,有时人们认为\n
不是一个好的EOL字符。serialio可以使用不同的EOL字符进行自定义。例如,XIA-PFCU总是以;\r\n
回复,我们可以使用以下片段进行配置
sl = serialio.serial_for_url("serial:///dev/ttyS0", eol=b";\r\n")
await sl.open()
初始EOL字符可以在任何readline
方法中重写。示例
await sl.write_readline(b"*IDN?\n", eol=b"\r")
流
待定:编写本章
项目详情
关闭
serialio-2.4.0.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | b3a4e2e173b521ae54b70995c88a01be50bab9cc718c05da89857d4ce480aaff |
|
MD5 | f25340f4afed7f24b9c33f98f67dd231 |
|
BLAKE2b-256 | 6b0482273fdf56bfac20c28a22c94085afb6e6c13dfdf57911ea594eb1d3204a |