ValueError:“端口”必须为None或字符串,而不是<class'serial.serialposix.serial'=“”>

时间:2019-04-27 21:33:59

标签: python python-3.x raspberry-pi3 pyserial nextion

我不明白该错误,在寻找解决方案5小时后,我终于放弃了。

我正在尝试打开串行连接,但是显然我的端口不是刺痛的吗?

同一端口在另一段代码中可以正常工作...

不工作

import serial
import pynextion


class NextionApp:

    def __init__(self):
        ser = serial.Serial("/dev/ttyAMA0", 9600, timeout=0)
        pages = [
            {"id": "0", "name": "page0()page",
                "components": [
                    {"id": "1", "type": "text", "name": "txt_tmp"},
                    {"id": "3", "type": "text", "name": "txt_hum"},
                    {"id": "0", "type": "text", "name": "txt_co2"},
                    {"id": "4", "type": "button", "name": "btn_test"},
                ]
            }
        ]
        self.nextion = pynextion.Nextion(ser, pages)
        print("Serial connected")

nextionApp = NextionApp()

工作

#!/usr/bin/env python


import time
import serial


ser = serial.Serial("/dev/ttyAMA0", 9600, timeout=1)
counter=0


while 1:
   x=ser.readline()
   print (x)

1 个答案:

答案 0 :(得分:0)

Python Nexation库希望接收设备路径作为输入,而不是serial对象的实例(它自己创建)。您应该致电:

self.nextion = pynextion.Nextion("/dev/ttyAMA0", pages)