My script has the answe :TypeError: 'module' object is not callable. How I solve it?

My script has the answe :TypeError: 'module' object is not callable. How I solve it?

我正在尝试使用串口,​​但我无法使用下面的简单程序。有人可以帮助我吗?我运行脚本结果是:

TypeError: 'module' 对象不可调用

我将 de serial 更改为 Serial 并将 b => ser.write(b,'A)

我不知道解决问题的答案

import time
import serial

ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate = 9600,
    parity=0,
    stopbits=1,
    bytesize=8,
    timeout=1
)

while 1:
    ser.write(b'A')
    x=ser.readline()
    print (x)
    time.sleep(1)

首先,确保您已经通过 pip 安装了 pyserial 而不是 serial

然后,从文档 here,命令实际上是 serial.Serial,所以下面应该有效:

ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate = 9600,
    parity=0,
    stopbits=1,
    bytesize=8,
    timeout=1
)

此外,请注意 ser.write() 需要一个字节对象,因此您应该将其更改为

ser.write(b'A')

编辑:鉴于评论中的堆栈跟踪,问题是提问者的 python 文件被称为 serial.py,导致它尝试导入自身而不是串行模块。重命名他们的 py 文件可以解决这个问题。