我正在尝试通过串行 COM 端口与探头通信。制造商在 PuTTY ec 中发出一些命令。更改测量单位或读取一些值。我在 python 中编写代码,但我什么也没收到,或者我不知道我收到了什么。这是 PuTTY 配置
接下来是制造商为 PuTTY 提供的命令示例。
这是 PuTTY 终端中的代码:
我在 Python 中的代码:
import serial
ser = serial.Serial()
ser.port = 'COM5'
ser.baudrate = 19200
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.xonxoff = 0
ser.rtscts = 0
ser.dsrdtr = 0
ser.stopbits = 1
ser.timeout = 1
ser.open()
if ser.isOpen():
print(ser.name + ' is open...')
while True:
cmd = input("Enter command or 'exit':")
if cmd == 'exit':
ser.close()
break
else:
# ser.write(cmd.encode('ascii'))
# ser.write(bytes(cmd, 'utf-8'))
ser.write(str.encode(cmd + '\r\n')) #
out = ser.readline().decode("utf-8").strip()
print('Receiving... ' + str(out))
这是我收到的:
Enter command or 'exit':UNIT
Receiving...
Enter command or 'exit':exit