-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPrinterSerial.py
191 lines (182 loc) · 6.46 KB
/
PrinterSerial.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from serial import Serial
from serial.serialutil import SerialException
from threading import *
import time
import re
import os
from utils import *
class PrinterSerial(Serial, EventDispatcher):
def __init__(self, port, baud):
Serial.__init__(self)
EventDispatcher.__init__(self)
self.port = port
self.baudrate = baud
self.listeners = {}
self.waiting = True
self.detected = False
self.readyMsg = ""
self.repeatsWaiting = False
self.messageEnd = b""
self.inUse = False
self.notFound = False
self.connectionError = False
self.lastSpeed = 1000
self.statusRequest = False
self.busy = False
self._stopping = False
self.connecting = True
try:
self.open()
except SerialException as se:
if os.name == "nt":
if str(se).find('FileNotFoundError') >= 0:
self.notFound = True
self.dispatch('connection-error')
elif str(se).find('PermissionError') >= 0:
self.inUse = True
self.connectionError = True
self.dispatch('connection-error')
return
detectThread = Thread(target=self.detectSetup)
detectThread.start()
def detectSetup(self):
time.sleep(2)
currBytes = self.read(self.inWaiting())
newlineRegex = re.compile(b"(\r\n|\n)")
if re.search(b'grbl(?i)', currBytes) is not None:
print("matched grbl")
self.detected = True
self.statusRequest = "?"
self.readyMsg = b"(idle|hold)(?i)"
self.readyRegex = re.compile(self.readyMsg)
self.write("G91")
self.dispatch('connected')
return
found = re.search(newlineRegex, currBytes)
if found is not None:
self.messageEnd = found.group(0)
else:
self.baudError = True
self.dispatch("connection-error")
return
self.write("G91")
time.sleep(.05)
self.accepted = False
if self.inWaiting() >=1:
self.accepted = self.readline()
self.clearBuffer()
time.sleep(3)
if self.inWaiting() >= 1:
repeatCount = 0
self.waitingMessage = self.readline()
while self.inWaiting() >= 1:
tempWaitMsg = self.readline()
if self.waitingMessage != tempWaitMsg:
self.waitingMessage = tempWaitMsg
repeatCount = 0
repeatCount+=1
if repeatCount >= 2:
self.repeatsWaiting = True
self.readyRegex = re.compile(self.waitingMessage)
print("repeating", repeatCount, self.waitingMessage)
self.detected = True
self.connecting = False
self.dispatch('connected')
def write(self, command):
super(PrinterSerial, self).write(str(command + "\n").encode("ASCII"))# + self.messageEnd)
def moveZ(self, distance, speed=1500):
if self._stopping == True and self.busy == False:
return
distance = float(distance)
speed = float(speed)
self.busy = True
'''
Temporary force speed to prevent issues
Need further testing
'''
self.write("G1 F" + str(speed))
self.lastSpeed = speed
'''
end temp code
'''
self.dispatch("move-start")
self.write("G1 Z" + str(distance) + " F" + str(speed))
timeToMove = abs(distance) / ((speed + self.lastSpeed) / 2) * 60
if self.repeatsWaiting or self.statusRequest:
Thread(target=self._sleepWait, args=[timeToMove]).start()
self.lastSpeed = speed
def _sleepWait(self, t):
print("_sleepWait")
time.sleep(abs(t))
moveCompleted = False
self.clearBuffer()
if self.statusRequest == False and self.repeatsWaiting == False:
moveCompleted = True
elif self.statusRequest != False:
self.write(self.statusRequest)
while moveCompleted == False:
bytesReceived = 0
loopCount = 0
while bytesReceived == 0:
bytesReceived = self.inWaiting()
time.sleep(.01)
loopCount += 1
if loopCount == 4 and self.statusRequest != False:
self.write(self.statusRequest)
loopCount = 0
status = self.readline()
print(status)
if re.search(self.readyRegex, status) is not None:
moveCompleted = True
self.busy = False
self.dispatch("move-complete")
def stopAndClose(self):
self.write('M18')
self.write('M2')
self._stopping = True
self.unbind('move-complete')
self.unbind('move-start')
if self.busy == True:
self.bind('move-complete', self._moveCompleteClose)
elif self.busy == False:
self.close()
def _moveCompleteClose(self, evt):
self.close()
def close(self):
super(PrinterSerial, self).close()
self.dispatch('connection-close')
def clearBuffer(self):
if self.inWaiting() >= 1:
self.read(self.inWaiting())
if __name__ == "__main__":
class TestObject():
def dude(self, evt):
self.count = 0
print(evt['target'].readyMsg)
print(evt['target'].inWaiting())
evt['target'].moveZ(5, 400)
print(evt['target'].inWaiting())
if evt['target'].readline() == evt['target'].readyMsg:
print("DUDEZ!")
def complete(self, evt):
print("complete")
if self.count == 0:
evt['target'].moveZ(-5, 400)
elif self.count == 1:
evt['target'].moveZ(-5, 200)
elif self.count == 2:
evt['target'].moveZ(-5, 500)
elif self.count == 3:
evt['target'].moveZ(5, 500)
elif self.count == 4:
evt['target'].moveZ(5, 300)
self.count+=1
def moveStart(self, evt):
print("start")
ps = PrinterSerial("COM6", 9600)
to = TestObject()
ps.bind("move-start", to.moveStart)
ps.bind("move-complete", to.complete)
ps.bind("connected", to.dude)
print("asdf")
#ps.moveZ(-5)