Python Serial库无法获取RS-232设备响应的问题求助
RS-232设备Python通信无响应问题排查
问题背景
我有一台通过RS-232端口配置的设备,想用Python编写软件对其进行配置。当前程序能成功向串口写入字节,但设备无任何响应。设备手册显示,正确命令会返回对应字符,错误命令则返回错误码,但目前完全没有响应。
我使用Serial Monitor App分析了原Labwindows/CVI(推测为C/C++语言)软件与设备的通信数据包,已在代码中添加了将dtr和rts设置为0的操作(该操作存在于原软件通信流程中,Python默认无此设置),但问题仍未解决。
关键差异:HandFlow参数对比
原软件的HandFlow信息:
IOCTL_SERIAL_SET_HANDFLOW: Set handshake information ControlHandShake=1 FlowReplace=64 XonLimit=2048 XoffLimit=512
Python程序的HandFlow信息:
IOCTL_SERIAL_SET_HANDFLOW: Set handshake information ControlHandShake=18 FlowReplace=64 XonLimit=2048 XoffLimit=512
同一台电脑上原软件可正常通信,但Python程序不行,恳请帮助!
我的Python代码
import serial as ps ser=ps.Serial('COM5', baudrate=9600, bytesize=8, stopbits=1, write_timeout=0, timeout=0, parity=ps.PARITY_NONE, dsrdtr=True) print(ser.name) ser.dtr=0 ser.rts=0 #commands to the device ser.write(b'INST:NSEL 1\r') ser.flush() ser.write(b'*IDN?\r') #ser.write(b'INST:CNUM?\r') #ser.write(b'STAT?\r') #ser.write(b'CONF:CALG 10\r') print('Done!') #print(ser.read(10)) ser.close() print('Done!!')
已排除的可能原因
- 使用的RS-232-USB转换器在同一电脑上配合原软件可正常工作,因此转换器无问题;
- 通过Serial Monitor App获取了原软件发送的命令,Python中使用了相同命令,因此串口命令正确。
内容的提问来源于stack exchange,提问作者Marcin Łęcki
相关产品推荐
相关产品推荐

