PySerial 最小 RFC2217 示例:将从串口接收的数据复制到 stdout
另请查看本地串口的相同示例:PySerial 最小示例:将从串口接收的数据复制到 stdout
此示例连接到 10.1.2.3 端口 1234 上的 RFC2217 远程串口。它不向串口发送数据,只将从串口接收的数据复制到 stdout。
pyserial_rfc2217_copy.py
#!/usr/bin/env python3
import serial
with serial.serial_for_url("rfc2217://10.1.2.3:1234", baudrate=115200) as ser:
try:
while True:
response = ser.read()
if response:
print(response.decode("iso-8859-1"), end="")
finally:
ser.close()pyserial_rfc2217_copy.py
#!/usr/bin/env python3
import serial
with serial.serial_for_url("rfc2217://10.1.2.3:1234", baudrate=115200) as ser:
try:
while True:
response = ser.read()
if response:
print(response.decode("iso-8859-1"), end="")
finally:
ser.close()通过使用 iso-8859-1 解码,我们确保即使二进制字节也以某种方式解码,不会导致异常。
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow