-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtableServer.py
More file actions
78 lines (63 loc) · 2.65 KB
/
tableServer.py
File metadata and controls
78 lines (63 loc) · 2.65 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
import zmq
import serial
import time
class SerialServer:
def __init__(self, port, device):
context = zmq.Context()
self.socket = context.socket(zmq.REP)
self.socket.bind("tcp://*:%d"%port)
self.instr = serial.Serial(device, 115200, timeout=0)
# Wake up grbl
wakeUp = "\r\n\r\n"
self.instr.write(wakeUp.encode())
time.sleep(2) # Wait for grbl to initialize
self.instr.flushInput() # Flush startup text in serial input
#Homing cycle
command = "$H\n"
self.instr.write(command.encode()) # Send g-code block to grbl
grbl_out = self.instr.readline()
#set coordinateds to 0,0
command = "G10 P0 L20 X0 Y0 Z0\n"
self.instr.write(command.encode()) # Send g-code block to grbl
grbl_out = self.instr.readline()
self.globalX = 0.0
self.globalY = 0.0
print("SerialServer listening at *:%d relaying to %s" % (port,device))
def loop(self):
while True:
msg = self.socket.recv().decode("utf-8").strip()
print("Processing %s" % msg)
msg = msg.split()
reply = ""
if "delta" in msg:
self.globalX += float(msg[1])
self.globalY += float(msg[2])
command = "G90 G0 X"+str(self.globalX)+" Y"+str(self.globalY)+"\n"
self.instr.write(command.encode()) # Send g-code block to grbl
time.sleep(0.2)
reply = self.instr.readline()
self.socket.send(reply) #no need to decode and encode
if "go" in msg:
self.globalX = float(msg[1])
self.globalY = float(msg[2])
command = "G90 G0 X"+str(self.globalX)+" Y"+str(self.globalY)+"\n"
self.instr.write(command.encode()) # Send g-code block to grbl
time.sleep(0.2)
reply = self.instr.readline()
self.socket.send(reply) #no need to decode and encode
if "get" in msg:
reply = str(self.globalX)+" "+str(self.globalY)
self.socket.send(reply.encode())
if "unlock" in msg:
command = "$x\n"
self.instr.write(command.encode()) # Send g-code block to grbl
time.sleep(0.2)
reply = self.instr.readline()
self.socket.send(reply) #no need to decode and encode
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-p","--port")
parser.add_option("-d","--device")
(options,args)=parser.parse_args()
myServer = SerialServer(int(options.port), options.device)
myServer.loop()