对 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)