2

对 Python 编程真的很陌生,我正在尝试为树莓派构建一个烧瓶网络应用程序,该应用程序将通过串行端口从文本文件 DNC g 编码到较旧的 HASS cnc 磨机。磨机使用 XMODEM 协议。我一直在关注https://pypi.org/project/xmodem/上的文档。

当我从命令行运行python3 dncPi.py时,出现以下错误:

  File "dncPi.py", line 2, in <module>
    from xmodem import XMODEM
ModuleNotFoundError: No module named 'xmodem' 

跑步pip3 install xmodem节目:

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: xmodem in /home/pi/.local/lib/python3.7/site-packages (0.4.6)

我已经检查了它/home/pi/.local/lib/python3.7/site-packages是 python3 路径的一部分:

python3 -c "import sys; print(':'.join(x for x in sys.path if x))"

这是我的 dncPi.py 代码:

import serial
from xmodem import XMODEM
from flask import Flask, render_template, request

ser = serial.Serial('/dev/ttyAMA0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)

def getc(size):
    return ser.read(size) or None

def putc(data):
    return ser.write(data)

modem = XMODEM(getc, putc)

app = Flask(__name__)

@app.route("/")
def index():

    print(request.method)
    if request.method == 'POST':
        if request.form.get('Send NC') == 'Send NC':
            filename = request.form.get('Filename')
            stream = open('/mnt/CNC/DNC/' + filename + '.nc', 'rb')
            status = modem.send(stream, retry=8)
            if status:
                print(filename + ".nc Sent")
            else:
                print(filename + ".nc Send Failure")
        elif request.form.get('Abort') == 'Abort':
            modem.abort()
            print("Aborted")
        else:
            # pass # unknown
            return render_template("index.html")
    elif request.method == 'GET':
        # return render_template("index.html")
        print("No Post Back Call")
    return render_template("index.html")

if __name__ == "__main__":
   app.run(host='0.0.0.0', port=80, debug=True)
4

1 回答 1

1

Tripleee 说得对。我没想到 sudo 会改变环境。sudo pip3 install xmodem解决它。

于 2021-02-05T17:08:01.407 回答