python-3.x - Rpi Python 接口 RS232 到 RS485 转换器问题
问题描述
我正在使用 raspberry Pi 与 ADAM-4520 设备建立串行通信,使用 pyserial lib 使用 USB 到串行转换器。1,我使用超级终端在 PC 上测试设备,并使用 Gtkterm 与 Pi 测试设备,使用命令“#04”从设备读取传感器值。这里我首先给出 Gtkterm 的输出。
#04
>+261.25+310.76+049.09+206.77+126.80+049.79
#04
>+261.25+310.76+049.09+206.75+126.80+049.79
但是当我尝试通过 Pyserial 程序时它不起作用。
这是我的代码:
import serial
import time
s=serial.Serial(port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1)
st="#04"
st=''.join(str(ord(c)) for c in st)
x=st.encode('ascii')
while True:
s.write(x)
print(x)
time.sleep(0.2)
text=s.readline()
temp=text.decode('ascii')
#text=text.decode('utf-8')
#text=text[5:-1]
print(temp)
time.sleep(2)
由于超级终端和 gtkterm 都使用 ascii,我也尝试将我的命令转换为 ascii,但没有结果。我是python新手,请指导找到问题
pi@raspberrypi:~ $ sudo python3 helloworld.py
b'354852'
b'354852'
b'354852'
解决方案
推荐阅读
- c++ - 将 DLL API 调用重新路由到 C++ 中的另一个 DLL
- html - 如何在jsp中使用自定义标签更改列表?
- android - 如何从单选按钮将适配器中的数据传递给活动
- python - 一起使用 virtualenvwrapper 和 pyenv
- sql - 获取 hive 中每个 id 的连续 t 日期范围
- go - 如何从 golang 中的 []interface{} 中提取切片
- c# - 如何在 Xamarin 中列出我的 android 设备中的所有 .mp3 文件
- java - 当我单击一个按钮时,如何对 javaFX 中的对话框说不关闭窗口
- html - 如何在图像中水平和垂直对齐文本以保持响应?
- kubernetes - 我们如何根据内存利用率参数限制给定工作节点 (VM) 中的 pod 数量并触发节点扩展