Python读取AR488 GPIB转串口设备无返回的问题排查
Python脚本与AR488 GPIB转串口转换器通信无返回数据问题
我正在编写Python脚本与AR488 GPIB转串口转换器交互,使用Termite终端程序可正常读写设备,但Python脚本无法接收返回数据。
已通过Termite验证设备功能及通信设置正常,且这些设置与Python脚本中的配置完全一致:
import serial import time def send(port, command): time.sleep(0.1) eol_char = '\r\n' port.write((command+eol_char).encode("ASCII")) def read(port): time.sleep(0.1) reply = port.readline() decoded = reply.decode("ASCII") return decoded.strip() # 创建串口实例 ins1 = serial.Serial('COM9', baudrate = 115200, bytesize=8, timeout=1, stopbits = serial.STOPBITS_TWO, parity = serial.PARITY_NONE, ) # 请求版本信息 send(ins1,'++ver') # 读取并打印响应 ans = read(ins1) print('ver: ' + ans) # 结束后关闭串口 ins1.close()
但运行脚本时,控制台仅输出“ver: ”,变量ans为空字符串。我遗漏了什么?
编辑:按照建议将脚本修改为逐字节读取版本,但调试时发现readsingle函数中的while循环被跳过,这是否意味着输入缓冲区无数据?
import serial import time def send(port, command): time.sleep(0.1) eol_char = '\r\n' port.write((command+eol_char).encode("ASCII")) def read(port): time.sleep(0.1) reply = port.readline() decoded = reply.decode("ASCII") return decoded.strip() def readsingle(port): time.sleep(0.1) out = '' while ins1.inWaiting() > 0: out += ins1.read(1).decode("ASCII") return out # 创建串口实例 ins1 = serial.Serial('COM9', baudrate = 115200, bytesize=8, timeout=1, stopbits = serial.STOPBITS_TWO, parity = serial.PARITY_NONE, ) # 请求版本信息 send(ins1,'++ver') # 读取并打印响应 ans2 = readsingle(ins1) print('ver: ' + ans2) # 结束后关闭串口 ins1.close()
内容的提问来源于stack exchange,提问作者TedAnderson
相关产品推荐
相关产品推荐

