【问题标题】:Serial.serial is not working, thonny: module 'serial' has no attribute 'Serial'Serial.serial 不起作用,thonny:模块 \'serial\' 没有属性 \'Serial\'
【发布时间】:2022-08-12 16:59:10
【问题描述】:

我刚开始使用 Ardunino、Python 以及 RPI 和 Ardunio 之间的串行通信(使用 Thonny)。我查看了一些教程(例如 https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/)来运行我的代码。但即使是标准代码也没有运行。

那是我的代码:

if __name__ == \'__main__\':
    ser = serial.Serial(\'/dev/ttyACM0\', 9600, timeout=1)
    ser.reset_input_buffer()
    while True:
        if ser.in_waiting > 0:
            line = ser.readline().decode(\'utf-8\').rstrip()
            print(line)```

This error keeps occuring: *AttributeError: module \'serial\' has no attribute \'Serial\'*

Your program tries to call method Serial of an object of type serial, but this type does not have such method. 

I have installed the library serial, the name of my file is : *Comm_testb.py*. 

I have tried also to do some other codes, which I found on stackoverflow ;). But either I failed to adapt them or there is something wrong. I also can not figure out other causes. So, I hope you can help me :).

    标签: python serialization arduino thonny


    【解决方案1】:

    你确定你下载了正确的库吗? 在 python 中,既有“串行”又有“pyserial”,要与 Arduino 通信,您需要后者。

    你最好卸载 'serial' 库,因为导入 pyserial 的行是:

    import serial
    

    请让我知道这是否有效

    【讨论】:

    • 非常感谢!我的 Arduino 现在向我问好 :)!
    • 无论如何,对于任何可能有同样问题的新手来说:安装 pyserial 包并通过 Import serial 导入它:)。不要对名字感到困惑:)
    猜你喜欢
    • 2017-05-03
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 2015-08-10
    • 2012-11-27
    相关资源
    最近更新 更多