-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
136 lines (110 loc) · 3.42 KB
/
Copy pathserver.py
File metadata and controls
136 lines (110 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from flask import Flask, render_template, jsonify, request
import serial
import serial.tools.list_ports
import time
import json
import os
app = Flask(__name__)
BAUD_RATE = 115200
SERIAL_TIMEOUT = 5.0
serial_conn = None
homed = False
position = {"x": 0, "y": 0, "z": 0}
speed = 1000
DATA_DIR = os.path.dirname(os.path.abspath(__file__))
SEQ_FILE = os.path.join(DATA_DIR, "sequences.json")
def connect_serial():
global serial_conn
if serial_conn and serial_conn.is_open:
return True
ports = list(serial.tools.list_ports.comports())
for p in ports:
desc = (p.description or "").upper()
name = (p.name or "").upper()
if "USB" in desc or "ACM" in name:
try:
serial_conn = serial.Serial(p.device, BAUD_RATE, timeout=SERIAL_TIMEOUT)
return True
except serial.SerialException:
pass
if ports:
try:
serial_conn = serial.Serial(ports[0].device, BAUD_RATE, timeout=SERIAL_TIMEOUT)
return True
except serial.SerialException:
pass
return False
def disconnect_serial():
global serial_conn
if serial_conn and serial_conn.is_open:
serial_conn.close()
serial_conn = None
def send_gcode(cmd):
global serial_conn
if not serial_conn or not serial_conn.is_open:
if not connect_serial():
return False
try:
serial_conn.write((cmd + "\n").encode())
serial_conn.flush()
return True
except:
return False
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/connect", methods=["POST"])
def api_connect():
if connect_serial():
return jsonify({"status": "ok", "port": serial_conn.name})
return jsonify({"status": "error", "message": "No serial port found"}), 400
@app.route("/api/disconnect", methods=["POST"])
def api_disconnect():
disconnect_serial()
return jsonify({"status": "ok"})
@app.route("/api/home", methods=["POST"])
def api_home():
global homed, position
if send_gcode("G28"):
homed = True
position = {"x": 700, "y": 0, "z": 0}
return jsonify({"status": "ok"})
return jsonify({"status": "error"}), 400
@app.route("/api/move", methods=["POST"])
def api_move():
global position
req = request.get_json()
x = req.get("x")
y = req.get("y")
z = req.get("z")
spd = req.get("speed", speed)
cmd = "G0"
if x is not None:
cmd += " X{}".format(x - 700)
position["x"] = x
if y is not None:
cmd += " Y{}".format(-y)
position["y"] = y
if z is not None:
cmd += " Z{}".format(-z)
position["z"] = z
cmd += " F{}".format(spd)
if send_gcode(cmd):
return jsonify({"status": "ok"})
return jsonify({"status": "error"}), 400
@app.route("/api/gcode", methods=["POST"])
def api_gcode():
req = request.get_json()
cmd = req.get("cmd", "").strip()
if not cmd:
return jsonify({"status": "error"}), 400
if send_gcode(cmd):
resp = ""
if serial_conn and serial_conn.is_open:
time.sleep(0.3)
if serial_conn.in_waiting:
resp = serial_conn.read(serial_conn.in_waiting).decode(errors="ignore")
return jsonify({"status": "ok", "response": resp.strip()})
return jsonify({"status": "error"}), 400
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)