pySerial - 只读取一个字节
pySerial - Only reading one byte
我正在尝试使用 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 上的代码)
您的读取语句明确请求 1 个字节:
ser.read(1)
如果你知道要读取多少字节,你可以在这里指定。如果您不确定,那么您可以指定一个更大的数字。例如,做
ser.read(10)
将读取最多 10 个字节。如果只有8个可用,那么它将只有return 8个字节(超时后,见下文)。
还值得设置一个超时来防止程序挂起。只需向您的 Serial 构造函数添加一个超时参数。以下将为您提供 2 秒超时:
ser = serial.Serial(
port='COM1',
baudrate=115200,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=2
)
文档指出:
read(size=1)
Read size
bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read.
因此,如果您不知道预期有多少字节,请设置一个较小的超时时间(如果可能),这样您的代码就不会挂起。
如果您的代码没有 return 预期的完整字节数,那么您连接的设备可能没有发送您预期的所有字节。既然你已经验证它应该单独工作,你是否验证了你发送的数据是正确的?也许首先使用 struct.pack() 编码为字节。例如,发送一个十进制值为 33(十六进制 0x21)
的字节
import struct
bytes_to_send = struct.pack('B', 33)
我也从未发现有必要在发送前将行尾字符 \r\n
附加到消息中
我正在尝试使用 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 上的代码)
您的读取语句明确请求 1 个字节:
ser.read(1)
如果你知道要读取多少字节,你可以在这里指定。如果您不确定,那么您可以指定一个更大的数字。例如,做
ser.read(10)
将读取最多 10 个字节。如果只有8个可用,那么它将只有return 8个字节(超时后,见下文)。
还值得设置一个超时来防止程序挂起。只需向您的 Serial 构造函数添加一个超时参数。以下将为您提供 2 秒超时:
ser = serial.Serial(
port='COM1',
baudrate=115200,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=2
)
文档指出:
read(size=1)
Readsize
bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read.
因此,如果您不知道预期有多少字节,请设置一个较小的超时时间(如果可能),这样您的代码就不会挂起。
如果您的代码没有 return 预期的完整字节数,那么您连接的设备可能没有发送您预期的所有字节。既然你已经验证它应该单独工作,你是否验证了你发送的数据是正确的?也许首先使用 struct.pack() 编码为字节。例如,发送一个十进制值为 33(十六进制 0x21)
的字节import struct
bytes_to_send = struct.pack('B', 33)
我也从未发现有必要在发送前将行尾字符 \r\n
附加到消息中