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