chiark / gitweb /
Add more documentation, fix commandline slicing.
[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         def __init__(self, portName):
19                 self._comm = None
20                 self._gcodeList = []
21
22                 self._comm = machineCom.MachineCom(portName, callbackObject=self)
23
24         def mcLog(self, message):
25                 sys.stdout.write('log:%s\n' % (message))
26
27         def mcTempUpdate(self, temp, bedTemp, targetTemp, bedTargetTemp):
28                 sys.stdout.write('temp:%s\n' % json.dumps(temp))
29
30         def mcStateChange(self, state):
31                 if self._comm is None:
32                         return
33                 sys.stdout.write('state:%d:%s\n' % (state, self._comm.getStateString()))
34
35         def mcMessage(self, message):
36                 sys.stdout.write('message:%s\n' % (message))
37
38         def mcProgress(self, lineNr):
39                 sys.stdout.write('progress:%d\n' % (lineNr))
40
41         def mcZChange(self, newZ):
42                 sys.stdout.write('changeZ:%d\n' % (newZ))
43
44         def monitorStdin(self):
45                 while not self._comm.isClosed():
46                         line = sys.stdin.readline().strip()
47                         line = line.split(':', 1)
48                         if line[0] == 'STOP':
49                                 self._comm.cancelPrint()
50                                 self._gcodeList = ['M110']
51                         elif line[0] == 'G':
52                                 self._gcodeList.append(line[1])
53                         elif line[0] == 'START':
54                                 self._comm.printGCode(self._gcodeList)
55                         else:
56                                 sys.stderr.write(str(line))
57
58 def main():
59         if len(sys.argv) != 2:
60                 return
61         sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
62         portName = sys.argv[1]
63         comm = serialComm(portName)
64         comm.monitorStdin()
65
66 if __name__ == '__main__':
67         main()