'Serial' 对象没有属性 'is_open'

'Serial' object has no attribute 'is_open'

我一直在使用 RPi2 上的代码与 RS485 Shield 通信以驱动各种继电器。最近弄了个树莓派,之前在树莓派2上运行的代码在树莓派3上出错了

首先,我知道 uart (/dev/ttyAMA0) 在 RPi3 上用于蓝牙控制器 "stolen"。 Using this post,我将 uart 重新分配给 GPIO 接头,因此 RS485 屏蔽应该像以前一样工作。我给你这段历史,尽管我怀疑问题不在于硬件本身。

问题来了。当我在树莓派上执行下面的代码时,出现错误:

Traceback (most recent call last):
  File "serialtest.py", line 15, in <module>
    if usart.is_open:
AttributeError: 'Serial' object has no attribute 'is_open' 

显然,在 pySerial 库中,串行对象确实具有 'is_open' 属性。关于为什么抛出此错误的任何建议?我在网络搜索中没有找到任何关于此特定错误的参考资料。

#!/usr/bin/env python

import serial
import time
import binascii


data = "55AA08060100024D5E77"
usart = serial.Serial ("/dev/ttyAMA0",19200)
usart.timeout = 2
message_bytes = data.decode("hex")
try:
    usart.write(message_bytes)
    #print usart.is_open  # True for opened
    if usart.is_open:
        time.sleep(0.5)
        size = usart.inWaiting()
        if size:
            data = usart.read(size)
            print binascii.hexlify(data)
        else:
            print('no data')
    else:
        print('usart not open')
except IOError as e :
    print("Failed to write to the port. ({})".format(e))

如果您在 Raspberry Pi 上有旧版本的 pyserialpyserial 可能没有 is_open,而是 isOpen() 方法。根据文档,isOpen() 方法在 3.0 版中被描述。您可以使用 serial.VERSION.

检查 pyserial 版本