【问题标题】:Python-Arduino WebPython-Arduino 网络
【发布时间】:2021-04-20 20:35:33
【问题描述】:

所以我要离开名为“Arduino 的 Python 编程”的书,我目前在使用书中的 web ex 2 时遇到了麻烦。我知道github有这个链接的代码:https://github.com/Python-programming-Arduino/ppfa-code/tree/master/Chapter%2008/Exercise%202%20-%20Webpy%20Serial

但是,我仍然遇到问题。我知道 python 2.7 已停产,但我认为我必须将所有内容保留在 python 2.7 中,因为这就是本书所使用的。

每当我运行 python 代码时发生的错误:

raise SerialException("无法打开端口 {!r}: {!r}".format(self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port 'COM4': WindowsError(2, 'The system cannot find the file specified.')

正在使用的代码:

import web
from web import form
import serial


port = serial.Serial('COM4', 9600, timeout=1)

render = web.template.render('templates')

urls = (
    '/', 'index')


class index:
    submit_form = form.Form(
        form.Textbox('Temperature', description='Temperature'),
        form.Button('submit', type="submit", description='submit')
    )

    def GET(self):
        f = self.submit_form()
        f.validates()
        line = port.readline()
        if line:
            data = float(line)
            humidity = relativeHumidity(line, 25)
            return render.base(f, humidity)
        else:
            return render.base(f, "Not valid data")

    def POST(self):
        f = self.submit_form()
        f.validates()
        temperature = f['Temperature'].value
        line = port.readline()
        if line:
            data = float(line)
            humidity = relativeHumidity(line, float(temperature))
            return render.base(f, humidity)
        else:
            return render.base(f, "Not valid data")


def relativeHumidity(data, temperature):
    volt = float(data) / 1024 * 5.0
    sensorRH = 161.0 * volt / 5.0 - 25.0
    trueRH = sensorRH / (1.0546 - 0.0026 * temperature)
    return trueRH


if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

我尝试在运行我的 python 程序的同一文件夹中创建模板文件夹,我唯一确定可行的是 Arduino 代码,因为串行监视器工作正常。

感谢您提供任何帮助,并且我尝试尽可能全面地了解我的经验,以便获得更好的帮助,而不是确保我的端口已连接和更新,因为这已经完成了。

更新

我已验证我正在与正确的端口通信,但现在我收到此错误:

Traceback (most recent call last):
  File "C:/Users/Edgar Castillo/PycharmProjects/WebCH8/webEx2.py", line 51, in <module>
    if __name__() == "__main__":
TypeError: 'str' object is not callable

我已经修改了代码,不记得是否做了任何令人难以置信的显着更改,但这是我为上述错误运行的代码。

import web
from web import form
import serial

port = serial.Serial('COM4', 9600, timeout=1)


render = web.template.render('templates')

urls = (
    '/', 'index')


class index:
    submit_form = form.Form(
        form.Textbox('Temperature', description='Temperature'),
        form.Button('submit', type="submit", description='submit')
    )

    def GET(self):
        f = self.submit_form()
        f.validates()
        line = port.readline()
        if line:
            data = float(line)
            humidity = relativeHumidity(line, 25)
            return render.base(f, humidity)
        else:
            return render.base(f, "Not valid data")

    def POST(self):
        f = self.submit_form()
        f.validates()
        temperature = f['Temperature'].value
        line = port.readline()
        if line:
            data = float(line)
            humidity = relativeHumidity(line, float(temperature))
            return render.base(f, humidity)
        else:
            return render.base(f, "Not valid data")


def relativeHumidity(data, temperature):
    volt = float(data) / 1024 * 5.0
    sensorRH = 161.0 * volt / 5.0 - 25.0
    trueRH = sensorRH / (1.0546 - 0.0026 * temperature)
    return trueRH


if __name__() == "__main__":
    app = web.application(urls, globals())
    app.run()

我看到第 25 行和第 37 行中没有使用数据,我不知道为什么会这样。如果有人可以帮助我,将不胜感激!

【问题讨论】:

    标签: python-2.7 sensors arduino-uno web.py


    【解决方案1】:

    此错误通常表示您尝试使用错误的 COM 端口。

    尝试在控制台中运行这些单行代码之一,以确定 Arduino 是哪个端口:

    python -m serial.tools.list_ports
    python -c "from serial.tools.list_ports import comports; print(comports())"
    

    这有点远,因为你说你设法通过 Arduino 串行监视器与 Arduino 对话,所以你一定已经知道它在 COM4 上。但也许您在此期间断开/重新连接它,它最终在不同的端口上?

    如果上述单行代码对您有用,您可以将其合并到您的代码中,例如通过向用户显示可用设备并要求他们选择一个:

    com_ports = {i: port for i, port in enumerate(comports())}
    print("+------------------------")
    print("| Choose a serial port from the below list.")
    print("+------------------------")
    print("| Available serial ports:")
    print("+------------------------")
    for i, port in com_ports.items():
        print("| {}: {}".format(i, port))
    print("+------------------------")
    choice = input("  Port: ")
    
    conn = serial.Serial(com_ports[int(choice)].device, BAUD_RATE)
    ...
    

    您还可以尝试通过枚举所有可用设备并检查它们的元数据来使程序自动找到 Arduino。有关可用字段,请参阅 the docs

    【讨论】:

    • 我很确定我使用了正确的 COM,因为我检查了设备管理器并且它是唯一正在使用的 COM。
    猜你喜欢
    • 2013-12-14
    • 1970-01-01
    • 2013-07-30
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多