forked from robotsinthesun/monkeyprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkeyprintSerial.py
632 lines (554 loc) · 18.8 KB
/
monkeyprintSerial.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# -*- coding: latin-1 -*-
#
# Copyright (c) 2015-2016 Paul Bomke
# Distributed under the GNU GPL v2.
#
# This file is part of monkeyprint.
#
# monkeyprint is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# monkeyprint is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You have received a copy of the GNU General Public License
# along with monkeyprint. If not, see <http://www.gnu.org/licenses/>.
import serial
import re
import threading
import time
#class serialThread(threading.Thread):
# # Override init function.
# def __init__(self):
# # Call super class init function.
# super(serialThread, self).__init__()
class printer(threading.Thread):
def __init__(self, settings, queue, queueCommands):
# Internalise parameters.
self.settings = settings
self.queue = queue
self.queueCommands = queueCommands
# Get serial parameters from settings.
if self.settings['printOnRaspberry'].value:
self.port=self.settings['Port RasPi'].value
self.baudrate=self.settings['Baud rate RasPi'].value
else:
self.port=self.settings['Port'].value
self.baudrate=self.settings['Baud rate'].value
# Stop event.
self.stopThread = threading.Event()
# Configure and open serial.
try:
self.serial = serial.Serial(
port=self.port,
baudrate=self.baudrate,
bytesize = serial.EIGHTBITS, #number of bits per bytes
parity = serial.PARITY_NONE, #set parity check: no parity
stopbits = serial.STOPBITS_ONE,
timeout = 0 # Wait for incoming bytes forever.
)
self.queue.put("Serial port " + self.settings['Port'].value + " connected.")
# If serial port does not exist...
except serial.SerialException:
# ... define a dummy.
self.serial = None
self.queue.put("Serial port " + self.settings['Port'].value + " not found.\nMake sure your board is plugged in and you have defined the correct serial port in the settings menu.")
# Call super class init function.
super(printer, self).__init__()
# Send function. This simply puts the command inside the input queue.
# This way the send command can be initiated without having to pass a queue
# to the serial object from outside.
def send(self, command):
self.queueCommands.put(command)
# Override run function.
# Send a command string with optional value.
# Method allows to retry sending until ack is received as well
# as waiting for printer to process the command.
def run(self):
if self.serial != None:
self.queue.put("Serial waiting for commands to send.")
else:
self.queue.put('Serial not operational.')
# Start loop.
while 1 and not self.stopThread.isSet():
# Get command from queue.
if self.queueCommands.qsize():
command = self.queueCommands.get()
string = command[0]
value = command[1]
retry = command[2]
wait = command[3]
if self.serial != None:
# Cast inputs.
if value != None: value = float(value)
if wait != None: wait = int(wait)
# ... start infinite loop that sends and waits for ack.
count = 0
# Set timeout to 5 seconds.
self.serial.timeout = 5
while count < 5 and not self.stopThread.isSet():
# Create command string from string and value.
# Separate string and value by space.
if value != None:
string = string + " " + str(value)
# Place send message in queue.
self.queue.put("Sending command \"" + string + "\".")
# Send command.
self.serial.write(string)
# If retry flag is set...
if retry:
# ... listen for ack until timeout.
printerResponse = self.serial.readline()
printerResponse = printerResponse.strip()
# Compare ack with sent string. If match...
if printerResponse == string:
# Place success message in queue.
self.queue.put("Command \"" + string + "\" sent successfully.")
if wait != None:
self.queue.put("Wait for printer to finish...")
# ... exit the send loop.
break
# If retry flag is not set...
else:
# ... exit the loop.
break
# Increment counter.
count += 1
# Place giving up message in queue if necessary.
if count == 5:
self.queue.put("Printer not responding. Giving up...")
# Wait for response from printer that signals end of action.
# If wait value is provided...
if wait != 0:
# ... set timeout to one second...
self.serial.timeout = 1
count = 0
while count < wait and not self.stopThread.isSet():
# ... and listen for "done" string until timeout.
printerResponse = self.serial.readline()
printerResponse = printerResponse.strip()
# Listen for "done" string.Check if return string is "done".
if printerResponse == "done":
self.queue.put("Printer done.")
break
else:
count += 1
# In case of timeout...
if count == wait:
# ... place fail message.
self.queue.put("Printer did not finish within timeout.")
# Reset the timeout.
self.serial.timeout = None
# Put done flag into queue to signal end of command process.
self.queue.put('done')
else:
self.queue.put('Sending failed. Port does not exist.')
self.queue.put('done')
else:
time.sleep(.1)
# Send a command string with optional value.
# Method allows to retry sending until ack is received as well
# as waiting for printer to process the command.
# def sendCommand(self, string, value=None, retry=False, wait=None):
# self.run(string, value, retry, wait)
def stop(self):
self.close()
self.queue.put("Stopping serial.")
self.stopThread.set()
def join(self, timeout=None):
self.close()
self.stopThread.set()
threading.Thread.join(self, timeout)
def close(self):
if self.serial != None:
self.serial.close()
class printerStandalone:
def __init__(self, settings):
# Internalise parameters.
self.settings = settings
self.debug = self.settings['debug'].value
# Get serial parameters from settings.
if self.settings['printOnRaspberry'].value:
self.port=self.settings['portRaspi'].value
self.baudrate=self.settings['baudrateRaspi'].value
else:
self.port=self.settings['port'].value
self.baudrate=self.settings['baudrate'].value
self.terminator = "\n"
if not self.debug:
print "Opening serial on port " + self.port + " at " + str(self.baudrate) + " baud."
# Configure and open serial.
try:
self.serial = serial.Serial(
port=self.port,
baudrate=self.baudrate,
bytesize = serial.EIGHTBITS, #number of bits per bytes
parity = serial.PARITY_NONE, #set parity check: no parity
stopbits = serial.STOPBITS_ONE,
timeout = 0 # Wait for incoming bytes forever.
)
# If serial port does not exist...
except serial.SerialException:
# ... define a dummy.
self.serial = None
print "Could not open serial on port " + str(self.port) + " with baud rate " + str(self.baudrate) + "."
else:
self.flush()
else:
print "Serial in debug mode: not sending."
self.serial = None
def flush(self, timeout=1.):
oldTimeout = self.serial.timeout
self.serial.timeout = timeout
string = "Flushing incoming messages."
while string != "":
string = self.serial.readline()
print string
self.serial.timeout = oldTimeout
return string
def waitForOk(self,timeout=.5):
oldTimeout = self.serial.timeout
self.serial.timeout = timeout
for i in range(20):
printerResponse = self.serial.readline()
print printerResponse
if printerResponse.strip() == "ok":
break
elif printerResponse.strip().split(':')[-1] == "processing":
i = 0
print "Processing..."
self.serial.timeout = oldTimeout
return printerResponse
# Divide command in parts beginning with G or M.
def splitGCode(self, command):
return filter(None,re.split("([M][^MG]*|[G][^MG]*)",command))
def sendGCode(self,command):
commandList = self.splitGCode(command[0])
value = command[1]
retry = command[2]
wait = command[3]
for commandString in commandList:
self.send((commandString, value, retry, wait))
def send(self, command):
if self.settings['debug'].value:
pass
else:
# Create return value.
returnValue = True
# Process command.
if len(command) < 4:
raise ValueError('Serial command has to contain four values.')
string = command[0]
value = command[1]
retry = command[2]
wait = command[3]
# Send command.
if self.serial != None:
# Cast inputs.
# if value != None: value = int(value)
if wait != None: wait = int(wait)
# Start loop that sends and waits for ack until timeout five times.
# Set timeout to 5 seconds.
count = 0
self.serial.timeout = 5
while count < 5:
sendString = string
# Create command string from string and value.
# Separate string and value by space.
if value != None:
sendString = sendString + " " + str(value)
print "Sending: " + sendString + "."
# Send command.
self.serial.write(sendString+self.terminator)
# If retry flag is set...
# In case fo GCode, flush input until ok is received.
printerResponse = ""
printerResponse = self.waitForOk()
print "Printer response: " + printerResponse
if retry:
# ... listen for ack until timeout.
printerResponse = printerResponse.strip()
# Compare ack with sent string or with 'ok' in case of g-code board.
# If match...
if printerResponse == string or printerResponse == 'ok':
# ... set the return value to success and...
returnValue = True
# ... exit the send loop.
# print "exiting"
break
# If ack does not match string...
else:
# ... set the return value to fail.
returnValue = False
# print "resending"
# If retry flag is not set...
else:
# ... exit the loop.
break
# Increment counter.
count += 1
# Place giving up message in queue if necessary.
# if count == 5:
# self.queue.put("Printer not responding. Giving up...")
# Wait for response from printer that signals end of action.
# If wait value is provided...
if wait != None:
# If wait value is 0...
if wait == 0:
#... set the timeout value to infinity.
self.serial.timeout=0
# Else...
else:
# ... set timeout to one second.
self.serial.timeout = 1
count = 0
while count < wait:
# ... and listen for "done" string until timeout.
printerResponse = self.serial.readline()
printerResponse = printerResponse.strip()
# Listen for "done" string.Check if return string is "done".
if printerResponse == "done":
#self.queue.put("Printer done.")
break
else:
count += 1
# In case of timeout...
# if count == wait:
# ... place fail message.
#self.queue.put("Printer did not finish within timeout.")
# Reset the timeout.
self.serial.timeout = None
# Flush incoming messages.
self.flush()
# Return success info.
return returnValue
else:
# self.flush()
return False
'''
# Override run function.
# Send a command string with optional value.
# Method allows to retry sending until ack is received as well
# as waiting for printer to process the command.
def run(self):
if self.serial != None:
self.queue.put("Serial waiting for commands to send.")
else:
self.queue.put('Serial not operational.')
# Start loop.
while 1 and not self.stopThread.isSet():
# Get command from queue.
if self.queueCommands.qsize():
command = self.queueCommands.get()
string = command[0]
value = command[1]
retry = command[2]
wait = command[3]
if self.serial != None:
# Cast inputs.
if value != None: value = float(value)
if wait != None: wait = int(wait)
# ... start infinite loop that sends and waits for ack.
count = 0
# Set timeout to 5 seconds.
self.serial.timeout = 5
while count < 5 and not self.stopThread.isSet():
# Create command string from string and value.
# Separate string and value by space.
if value != None:
string = string + " " + str(value)
# Place send message in queue.
self.queue.put("Sending command \"" + string + "\".")
# Send command.
self.serial.write(string)
# If retry flag is set...
if retry:
# ... listen for ack until timeout.
printerResponse = self.serial.readline()
printerResponse = printerResponse.strip()
# Compare ack with sent string. If match...
if printerResponse == string:
# Place success message in queue.
self.queue.put("Command \"" + string + "\" sent successfully.")
if wait != None:
self.queue.put("Wait for printer to finish...")
# ... exit the send loop.
break
# If retry flag is not set...
else:
# ... exit the loop.
break
# Increment counter.
count += 1
# Place giving up message in queue if necessary.
if count == 5:
self.queue.put("Printer not responding. Giving up...")
# Wait for response from printer that signals end of action.
# If wait value is provided...
if wait != 0:
# ... set timeout to one second...
self.serial.timeout = 1
count = 0
while count < wait and not self.stopThread.isSet():
# ... and listen for "done" string until timeout.
printerResponse = self.serial.readline()
printerResponse = printerResponse.strip()
# Listen for "done" string.Check if return string is "done".
if printerResponse == "done":
self.queue.put("Printer done.")
break
else:
count += 1
# In case of timeout...
if count == wait:
# ... place fail message.
self.queue.put("Printer did not finish within timeout.")
# Reset the timeout.
self.serial.timeout = None
# Put done flag into queue to signal end of command process.
self.queue.put('done')
else:
self.queue.put('Sending failed. Port does not exist.')
self.queue.put('done')
else:
time.sleep(.1)
'''
def close(self):
if self.serial != None:
self.serial.close()
'''
# Commands.
def buildHome(self):
self.serial.write("buildHome")
self.waitForAckInfinite()
def buildBaseUp(self):
while 1:
self.serial.timeout = 5
self.serial.write("buildBaseUp")
printerResponse = self.serial.readline() # Wait for 5 sec for anything
print "PRINTER RESPONSE: " + printerResponse
printerResponse = printerResponse.strip()
if printerResponse == "done":
break
else:
print " No response from printer. Resending command..."
self.serial.timeout = None
def buildUp(self):
while 1:
self.serial.write("buildUp")
self.serial.timeout = 5
printerResponse = self.serial.readline() # Wait for 5 sec for anything
print "PRINTER RESPONSE: " + printerResponse
printerResponse = printerResponse.strip()
if printerResponse == "done":
break
else:
print " No response from printer. Resending command..."
self.serial.timeout = None
def buildTop(self):
self.serial.write("buildTop")
self.waitForAckInfinite()
def tilt(self):
self.serial.write("tilt")
self.waitForAckInfinite()
def setStart(self):
self.serial.write("printingFlag 1")
def setStop(self):
self.serial.write("printingFlag 0")
# Settings.
def setLayerHeight(self):
self.serial.write("buildLayer " + str(self.settings.getLayerHeight() * self.settings.getStepsPerMm()))
def setBaseLayerHeight(self):
self.serial.write("buildBaseLayer " + str(self.settings.getBaseLayerHeight() * self.settings.getStepsPerMm()))
def setBuildSpeed(self):
self.serial.write("buildSpeed " + str(self.settings.getBuildSpeed))
def setTiltSpeedSlow(self):
self.serial.write("tiltSpeed " + str(self.settings.getTiltSpeedSlow))
def setTiltSpeedFast(self):
self.serial.write("tiltSpeed " + str(self.settings.getTiltSpeedFast))
def setTiltAngle(self):
self.serial.write("tiltAngle " + str(self.settings.getTiltAngle))
def setNumberOfSlices(self, numberOfSlices):
self.serial.write("nSlices " + str(numberOfSlices))
def setCurrentSlice(self, currentSlice):
self.serial.write("slice " + str(currentSlice))
# def setTimeout(self, timeout):
# self.serial.timeout = timeout # Timeout for readline command. 0 is infinity, other values are seconds.
# def waitForAckFinite(self,timeout):
# self.serial.timeout = timeout
# printerResponse = self.serial.readline() # Wait for 5 sec for anything
# print "PRINTER RESPONSE: " + printerResponse
# printerResponse = printerResponse.strip()
# if printerResponse=="done":
# return 1
# elif printerResponse != "done":
# print "Got strange response from printer: " + printerResponse
# return 0
# elif printerResponse == ""
# return 0
def waitForAckInfinite(self):
print "waitForAck"
self.serial.timeout = None
printerResponse = self.serial.readline() # Wait forever for anything
print "PRINTER RESPONSE: " + printerResponse
printerResponse = printerResponse.strip()
if printerResponse=="done":
return "done"
elif printerResponse != "done":
print "Got strange response from printer: " + printerResponse
return None
else:
return None
def ping(self):
self.serial.write("ping")
self.serial.timeout = 10
printerResponse = self.serial.readline() # Wait forever for anything
printerResponse = printerResponse.strip()
if printerResponse!="ping":
return 0
else:
return 1
def close(self):
if self.serial != None:
self.serial.close()
# print "printer serial closed"
#dontNeedThis = serialPrinter.flushInput()
'''
class projector:
def __init__(self, settings):
# Internalise settings.
self.settings = settings
self.debug = self.settings['debug'].value
# Configure and open serial.
if not self.debug:
try:
self.serial = serial.Serial(
port=self.settings['projectorPort'].value,
baudrate=self.settings['projectorBaudrate'].value,
bytesize = serial.EIGHTBITS, #number of bits per bytes
parity = serial.PARITY_NONE, #set parity check: no parity
stopbits = serial.STOPBITS_ONE
)
# If serial port does not exist...
except serial.SerialException:
# ... define a dummy.
self.serial = None
else:
print "Projector serial in debug mode: not sending."
self.serial = None
def activate(self):
command = self.settings['projectorOnCommand'].value
if self.serial != None:
self.serial.write(command+'\r')
def deactivate(self):
command = self.settings['projectorOffCommand'].value
if self.serial != None:
self.serial.write(command+'\r')
def close(self):
if self.serial != None:
self.serial.close()
# print "projector serial closed"