python - 如何在 Python 3.7 中向 bytearray 添加字节?

标签 python arrays python-3.x pyserial

我是 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/

相关文章:

Python 有时不会将类实例作为第一个参数传递 - 理解 __new__

ruby-on-rails - Array(x) 是什么样的 ruby​​ 方法调用

Java排序——全部大写优先

python - Sphinx 快速入门有限的选项

python - 解析 JSON 对象时,保留字作为数据类中的属性名称

python - 对 glPerspective 和 GL_POLYGON 的困惑

python - 遍历字典

python - 将自定义 Docker 与 Azure ML 结合使用

javascript - Angularjs在指令中绑定(bind)数组元素ng-repeat

python-3.x - 为什么 Python 3 中的索引字节返回一个 int 而不是字节?