首页 > 解决方案 > 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'

标签: python-3.xserial-portraspberry-pi3

解决方案


推荐阅读