如何创建长度为 1 的 bytes() 序列
How to create a sequence of bytes() with length 1
我正在 Python 3 中编写一个应用程序,它使用 pyserial 模块通过串行端口与设备通信。
写入设备时,pyserial 模块期望数据表示为字节序列。
如果我一次写入所有数据,这工作正常,但如果我逐字节写入数据,每个字节后都有延迟,则失败。我想逐字节写入的原因是当我必须处理只能以低速率处理数据的设备时。
我使用的代码是:
def write_timed(self, data):
"""Write data to serial port,
taking into account inter-character delay and line-delay
:param data (str): string to write; any escaped characters will be converted
e.g. \n will be output as a newline (not as characters '\' and 'n')
"""
# convert raw string to sequence of bytes
data = bytes(data.encode('latin-1').decode("unicode_escape"), "latin-1")
logging.info("TX:{}".format(repr(data)))
# only write to device if we have something to write
if data:
if data and self.char_delay_s == 0:
self.serial.write(data)
else:
for d in data:
self.serial.write(d)
time.sleep(self.char_delay_s)
time.sleep(self.line_delay_s)
# ensure all data has gone out
self.serial.flush()
我得到的错误是:
File "C:\projects\maintenance\protocoltest\protocoltest\device\device.py", line 65, in write_timed
self.serial.write(d)
File "C:\projects\newplatform\venv3_pyside2\lib\site-packages\serial\serialwin32.py", line 301, in write
data = to_bytes(data)
File "C:\projects\newplatform\venv3_pyside2\lib\site-packages\serial\serialutil.py", line 61, in to_bytes
for item in seq:
TypeError: 'int' object is not iterable
错误的原因是当我执行for d in data:
时,变量d变成了int
而不是长度为1的字节序列
如果我尝试使用 d = bytes(d)
修复该问题,我会得到一串零,即 d 值的长度。
如何将单个字节写入 pyserial?
如果我将 self.char_delay_s
设置为零,一切正常,即使是长度为 1 的字符串。如果我有一个非零值,则会出现上述错误。
不幸的是,正如您发现的那样,当您索引一个 bytes
对象时,您会得到一个 int
对象。最简单的做法就是将 int
转换回 bytes
:
for d in data:
d = bytes([d])
# d is now a bytes object and can be used as such
或者,您可以使用切片而不是索引:
for i in range(len(data)):
d = data[i : i + 1]
# d is a bytes object because it's a slice of a bytes object
我正在 Python 3 中编写一个应用程序,它使用 pyserial 模块通过串行端口与设备通信。
写入设备时,pyserial 模块期望数据表示为字节序列。
如果我一次写入所有数据,这工作正常,但如果我逐字节写入数据,每个字节后都有延迟,则失败。我想逐字节写入的原因是当我必须处理只能以低速率处理数据的设备时。
我使用的代码是:
def write_timed(self, data):
"""Write data to serial port,
taking into account inter-character delay and line-delay
:param data (str): string to write; any escaped characters will be converted
e.g. \n will be output as a newline (not as characters '\' and 'n')
"""
# convert raw string to sequence of bytes
data = bytes(data.encode('latin-1').decode("unicode_escape"), "latin-1")
logging.info("TX:{}".format(repr(data)))
# only write to device if we have something to write
if data:
if data and self.char_delay_s == 0:
self.serial.write(data)
else:
for d in data:
self.serial.write(d)
time.sleep(self.char_delay_s)
time.sleep(self.line_delay_s)
# ensure all data has gone out
self.serial.flush()
我得到的错误是:
File "C:\projects\maintenance\protocoltest\protocoltest\device\device.py", line 65, in write_timed
self.serial.write(d)
File "C:\projects\newplatform\venv3_pyside2\lib\site-packages\serial\serialwin32.py", line 301, in write
data = to_bytes(data)
File "C:\projects\newplatform\venv3_pyside2\lib\site-packages\serial\serialutil.py", line 61, in to_bytes
for item in seq:
TypeError: 'int' object is not iterable
错误的原因是当我执行for d in data:
时,变量d变成了int
而不是长度为1的字节序列
如果我尝试使用 d = bytes(d)
修复该问题,我会得到一串零,即 d 值的长度。
如何将单个字节写入 pyserial?
如果我将 self.char_delay_s
设置为零,一切正常,即使是长度为 1 的字符串。如果我有一个非零值,则会出现上述错误。
不幸的是,正如您发现的那样,当您索引一个 bytes
对象时,您会得到一个 int
对象。最简单的做法就是将 int
转换回 bytes
:
for d in data:
d = bytes([d])
# d is now a bytes object and can be used as such
或者,您可以使用切片而不是索引:
for i in range(len(data)):
d = data[i : i + 1]
# d is a bytes object because it's a slice of a bytes object