Python 3.x Rpi Python接口RS232到RS485转换器问题
我正在使用raspberry Pi与ADAM-4520设备建立串行通信,使用pyserial lib,使用usb到串行转换器。 首先,我用PC和Gtkterm测试设备,PC使用超级终端,Pi使用Gtkterm, 使用命令“#04”从设备读取传感器值。 这里我首先给出Gtkterm的输出Python 3.x Rpi Python接口RS232到RS485转换器问题,python-3.x,serial-port,raspberry-pi3,Python 3.x,Serial Port,Raspberry Pi3,我正在使用raspberry Pi与ADAM-4520设备建立串行通信,使用pyserial lib,使用usb到串行转换器。 首先,我用PC和Gtkterm测试设备,PC使用超级终端,Pi使用Gtkterm, 使用命令“#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 但当
#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)
由于hyper terminal和gtkterm都使用ascii,我也尝试将命令转换为ascii,但没有结果。
我是python新手,请指导查找问题
pi@raspberrypi:~ $ sudo python3 helloworld.py
b'354852'
b'354852'
b'354852'
我对Adam一无所知,但我怀疑您是否需要进行编码(“ascii”)。我认为更可能的情况是,Adam正在等待线路终止。试试st='#04\r'或'#04\r\n'(其中'\r'是回车,而'\n'是换行)。@electrogas,谢谢你的建议。这真的很有帮助,我的代码开始使用
(st=“#04\r”)
。