chiark / gitweb /
Increment version number
[cura.git] / Cura / serialCommunication.py
1 """
2 Serial communication with the printer for printing is done from a separate process,
3 this to ensure that the PIL does not block the serial printing.
4
5 This file is the 2nd process that is started to handle communication with the printer.
6 And handles all communication with the initial process.
7 """
8
9 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
10 import sys
11 import threading
12 import time
13 import os
14 import json
15
16 from Cura.util import machineCom
17
18 class serialComm(object):
19         """
20         The serialComm class is the interface class which handles the communication between stdin/stdout and the machineCom class.
21         This interface class is used to run the (USB) serial communication in a different process then the GUI.
22         """
23         def __init__(self, portName, baudrate):
24                 self._comm = None
25                 self._gcodeList = []
26
27                 try:
28                         baudrate = int(baudrate)
29                 except ValueError:
30                         baudrate = 0
31                 self._comm = machineCom.MachineCom(portName, baudrate, callbackObject=self)
32
33         def mcLog(self, message):
34                 sys.stdout.write('log:%s\n' % (message))
35
36         def mcTempUpdate(self, temp, bedTemp, targetTemp, bedTargetTemp):
37                 sys.stdout.write('temp:%s:%s:%f:%f\n' % (json.dumps(temp), json.dumps(targetTemp), bedTemp, bedTargetTemp))
38
39         def mcStateChange(self, state):
40                 if self._comm is None:
41                         return
42                 sys.stdout.write('state:%d:%s\n' % (state, self._comm.getStateString()))
43
44         def mcMessage(self, message):
45                 sys.stdout.write('message:%s\n' % (message))
46
47         def mcProgress(self, lineNr):
48                 sys.stdout.write('progress:%d\n' % (lineNr))
49
50         def mcZChange(self, newZ):
51                 sys.stdout.write('changeZ:%f\n' % (newZ))
52
53         def monitorStdin(self):
54                 while not (self._comm.isClosed() or sys.stdin.closed):
55                         line = sys.stdin.readline()
56                         # If readline returns an empty string it means that
57                         # we've hit stdin's EOF, probably because the parent was
58                         # closed, so we need to exit as well instead of spamming stderr.
59                         if line == '':
60                                 break
61                         line = line.strip().split(':', 1)
62                         if line[0] == 'STOP':
63                                 self._comm.cancelPrint()
64                                 self._gcodeList = ['M110']
65                         elif line[0] == 'G':
66                                 self._gcodeList.append(line[1])
67                         elif line[0] == 'C':
68                                 self._comm.sendCommand(line[1])
69                         elif line[0] == 'START':
70                                 self._comm.printGCode(self._gcodeList)
71                         elif line[0] == 'PAUSE':
72                                 self._comm.setPause(True)
73                         elif line[0] == 'RESUME':
74                                 self._comm.setPause(False)
75                         else:
76                                 sys.stderr.write(str(line))
77
78 def startMonitor(portName, baudrate):
79         sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
80         comm = serialComm(portName, baudrate)
81         thread = threading.Thread(target=comm.monitorStdin)
82         thread.start()
83         while thread.is_alive():
84                 time.sleep(0.1)
85
86 def main():
87         if len(sys.argv) != 3:
88                 return
89         portName, baudrate = sys.argv[1], sys.argv[2]
90         startMonitor(portName, baudrate)
91
92 if __name__ == '__main__':
93         main()