chiark / gitweb /
Add version 0.1 of serial communication with printerConnection.
[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
10 from Cura.util import machineCom
11
12 class serialComm(object):
13         def __init__(self, portName):
14                 self._comm = None
15                 self._gcodeList = []
16
17                 self._comm = machineCom.MachineCom(portName, callbackObject=self)
18
19         def mcLog(self, message):
20                 sys.stdout.write('log:%s\n' % (message))
21
22         def mcTempUpdate(self, temp, bedTemp, targetTemp, bedTargetTemp):
23                 sys.stdout.write('temp:%s\n' % str(temp))
24
25         def mcStateChange(self, state):
26                 if self._comm is None:
27                         return
28                 sys.stdout.write('state:%d:%s\n' % (state, self._comm.getStateString()))
29
30         def mcMessage(self, message):
31                 sys.stdout.write('message:%s\n' % (message))
32
33         def mcProgress(self, lineNr):
34                 sys.stdout.write('progress:%d\n' % (lineNr))
35
36         def mcZChange(self, newZ):
37                 sys.stdout.write('changeZ:%d\n' % (newZ))
38
39         def monitorStdin(self):
40
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 = []
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()