chiark / gitweb /
For now prevent UltiGCode from using connectionManager.
[cura.git] / Cura / serialCommunication.py
1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2
3 # Serial communication with the printer for printing is done from a separate process,
4 # this to ensure that the PIL does not block the serial printing.
5
6 import sys
7 import time
8 import os
9 import json
10
11 from Cura.util import machineCom
12
13 class serialComm(object):
14         def __init__(self, portName):
15                 self._comm = None
16                 self._gcodeList = []
17
18                 self._comm = machineCom.MachineCom(portName, callbackObject=self)
19
20         def mcLog(self, message):
21                 sys.stdout.write('log:%s\n' % (message))
22
23         def mcTempUpdate(self, temp, bedTemp, targetTemp, bedTargetTemp):
24                 sys.stdout.write('temp:%s\n' % json.dumps(temp))
25
26         def mcStateChange(self, state):
27                 if self._comm is None:
28                         return
29                 sys.stdout.write('state:%d:%s\n' % (state, self._comm.getStateString()))
30
31         def mcMessage(self, message):
32                 sys.stdout.write('message:%s\n' % (message))
33
34         def mcProgress(self, lineNr):
35                 sys.stdout.write('progress:%d\n' % (lineNr))
36
37         def mcZChange(self, newZ):
38                 sys.stdout.write('changeZ:%d\n' % (newZ))
39
40         def monitorStdin(self):
41                 while not self._comm.isClosed():
42                         line = sys.stdin.readline().strip()
43                         line = line.split(':', 1)
44                         if line[0] == 'STOP':
45                                 self._comm.cancelPrint()
46                                 self._gcodeList = ['M110']
47                         elif line[0] == 'G':
48                                 self._gcodeList.append(line[1])
49                         elif line[0] == 'START':
50                                 self._comm.printGCode(self._gcodeList)
51                         else:
52                                 sys.stderr.write(str(line))
53
54 def main():
55         if len(sys.argv) != 2:
56                 return
57         sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
58         portName = sys.argv[1]
59         comm = serialComm(portName)
60         comm.monitorStdin()
61
62 if __name__ == '__main__':
63         main()