chiark / gitweb /
Merge pull request #828 from Dim3nsioneer/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, baudrate):
23                 self._comm = None
24                 self._gcodeList = []
25
26                 try:
27                         baudrate = int(baudrate)
28                 except ValueError:
29                         baudrate = 0
30                 self._comm = machineCom.MachineCom(portName, baudrate, callbackObject=self)
31
32         def mcLog(self, message):
33                 sys.stdout.write('log:%s\n' % (message))
34
35         def mcTempUpdate(self, temp, bedTemp, targetTemp, bedTargetTemp):
36                 sys.stdout.write('temp:%s:%s:%f:%f\n' % (json.dumps(temp), json.dumps(targetTemp), bedTemp, bedTargetTemp))
37
38         def mcStateChange(self, state):
39                 if self._comm is None:
40                         return
41                 sys.stdout.write('state:%d:%s\n' % (state, self._comm.getStateString()))
42
43         def mcMessage(self, message):
44                 sys.stdout.write('message:%s\n' % (message))
45
46         def mcProgress(self, lineNr):
47                 sys.stdout.write('progress:%d\n' % (lineNr))
48
49         def mcZChange(self, newZ):
50                 sys.stdout.write('changeZ:%d\n' % (newZ))
51
52         def monitorStdin(self):
53                 while not self._comm.isClosed():
54                         line = sys.stdin.readline().strip()
55                         line = line.split(':', 1)
56                         if line[0] == 'STOP':
57                                 self._comm.cancelPrint()
58                                 self._gcodeList = ['M110']
59                         elif line[0] == 'G':
60                                 self._gcodeList.append(line[1])
61                         elif line[0] == 'C':
62                                 self._comm.sendCommand(line[1])
63                         elif line[0] == 'START':
64                                 self._comm.printGCode(self._gcodeList)
65                         else:
66                                 sys.stderr.write(str(line))
67
68 def startMonitor(portName, baudrate):
69         sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
70         comm = serialComm(portName, baudrate)
71         comm.monitorStdin()
72
73 def main():
74         if len(sys.argv) != 3:
75                 return
76         portName, baudrate = sys.argv[1], sys.argv[2]
77         startMonitor(portName, baudrate)
78
79 if __name__ == '__main__':
80         main()