【发布时间】:2019-01-08 09:20:00
【问题描述】:
我正在尝试使用 pySerial 通过串行方式读取和写入传感器。我没有软件或硬件流控制。
我能够向设备发送一个十六进制字符串,但我只收到一个字节,而不是我应该看到的两到十个字节。传感器正在工作——我已经使用 Realterm 验证了这一点。
我尝试过使用 ser.readline()(而不是 inWaiting 循环)和 ser.read(2);这只会导致程序挂起。我也尝试过增加睡眠时间,并尝试了不同的波特率(在 PC 和传感器上),但似乎没有任何效果。
有人有什么建议吗?
import time
import serial
# configure the serial connections
ser = serial.Serial(
port='COM1',
baudrate=115200,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
ser.isOpen()
print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
while 1 :
# get keyboard input
data_in = raw_input(">> ")
if data_in == 'exit':
ser.close()
exit()
else:
# send the character to the device
ser.write(data_in.decode('hex') + '\r\n')
out = ''
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != '':
print ">>" + " ".join(hex(ord(n)) for n in out)
(我对Full examples of using pySerial package上的代码稍作修改)
【问题讨论】:
标签: python serial-port pyserial