chiark / gitweb /
Merge branch 'SteamEngine' of github.com:daid/Cura into SteamEngine
[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 time
12 import os
13 import json
14
15 from Cura.util import machineCom
16
17 class serialComm(object):
18         """
19         The serialComm class is the interface class which handles the communication between stdin/stdout and the machineCom class.
20         This interface class is used to run the (USB) serial communication in a different process then the GUI.
21         """
22         def __init__(self, portName):
23                 self._comm = None
24                 self._gcodeList = []
25
26                 self._comm = machineCom.MachineCom(portName, callbackObject=self)
27
28         def mcLog(self, message):
29                 sys.stdout.write('log:%s\n' % (message))
30
31         def mcTempUpdate(self, temp, bedTemp, targetTemp, bedTargetTemp):
32                 sys.stdout.write('temp:%s:%s:%f:%f\n' % (json.dumps(temp), json.dumps(targetTemp), bedTemp, bedTargetTemp))
33
34         def mcStateChange(self, state):
35                 if self._comm is None:
36                         return
37                 sys.stdout.write('state:%d:%s\n' % (state, self._comm.getStateString()))
38
39         def mcMessage(self, message):
40                 sys.stdout.write('message:%s\n' % (message))
41
42         def mcProgress(self, lineNr):
43                 sys.stdout.write('progress:%d\n' % (lineNr))
44
45         def mcZChange(self, newZ):
46                 sys.stdout.write('changeZ:%d\n' % (newZ))
47
48         def monitorStdin(self):
49                 while not self._comm.isClosed():
50                         line = sys.stdin.readline().strip()
51                         line = line.split(':', 1)
52                         if line[0] == 'STOP':
53                                 self._comm.cancelPrint()
54                                 self._gcodeList = ['M110']
55                         elif line[0] == 'G':
56                                 self._gcodeList.append(line[1])
57                         elif line[0] == 'C':
58                                 self._comm.sendCommand(line[1])
59                         elif line[0] == 'START':
60                                 self._comm.printGCode(self._gcodeList)
61                         else:
62                                 sys.stderr.write(str(line))
63
64 def startMonitor(portName):
65         sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
66         comm = serialComm(portName)
67         comm.monitorStdin()
68
69 def main():
70         if len(sys.argv) != 2:
71                 return
72         portName = sys.argv[1]
73         startMonitor(portName)
74
75 if __name__ == '__main__':
76         main()