我正在尝试使用 pySerial 通过串行读取和写入传感器。我没有软件或硬件流量控制。
我能够向设备发送一串十六进制字符串,但我只收到一个字节,而不是我应该看到的两到十个字节。传感器正在工作——我已经使用 Realterm 验证了这一点。
我尝试过使用 ser.readline() (而不是 inWaiting 循环)和 ser.read(2); 这只会导致程序挂起。我还尝试增加睡眠时间,并尝试不同的波特率(在电脑和传感器上),但似乎没有任何效果。
有人有建议吗?
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 …Run Code Online (Sandbox Code Playgroud)