我是 Python 3.7 的新手,我正在尝试使用以下代码从串行端口读取字节。我正在使用 pySerial 模块和 read()函数返回字节
。
self.uart = serial.Serial()
self.uart.port = '/dev/tty/USB0'
self.uart.baudrate = 115200
self.uart.open()
# buffer for received bytes
packet_bytes = bytearray()
# read and process data from serial port
while True:
# read single byte from serial port
current_bytes = self._uart.read()
if current_bytes is B'$':
self.process_packet(packet_bytes)
packet_bytes = bytearray()
else:
packet_bytes.append(current_bytes) <- Error occurs here
我收到以下错误:
TypeError: an integer is required
知道如何解决吗?
最佳答案
packet_bytes += bytearray(current_bytes)
关于python - 如何在 Python 3.7 中向 bytearray 添加字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55940660/