printrun-src/printrun/printcore.py

Sat, 04 Jun 2016 09:22:51 +0200

author
mbayer
date
Sat, 04 Jun 2016 09:22:51 +0200
changeset 20
03b34402d405
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Code cleanup

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 __version__ = "2015.03.10"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 from serialWrapper import Serial, SerialException, PARITY_ODD, PARITY_NONE
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 from select import error as SelectError
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import threading
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 from Queue import Queue, Empty as QueueEmpty
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 import time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 import platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 import os
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 reload(sys).setdefaultencoding('utf8')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 import traceback
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 import errno
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 import socket
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 import re
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 from functools import wraps
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 from collections import deque
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 from printrun import gcoder
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 from .utils import install_locale, decode_utf8
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 install_locale('pronterface')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 def locked(f):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 @wraps(f)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 def inner(*args, **kw):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 with inner.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 return f(*args, **kw)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 inner.lock = threading.Lock()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 return inner
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 def control_ttyhup(port, disable_hup):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 """Controls the HUPCL"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 if platform.system() == "Linux":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 if disable_hup:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 os.system("stty -F %s -hup" % port)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 os.system("stty -F %s hup" % port)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 def enable_hup(port):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 control_ttyhup(port, False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 def disable_hup(port):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 control_ttyhup(port, True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 class printcore():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 def __init__(self, port = None, baud = None, dtr=None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 """Initializes a printcore instance. Pass the port and baud rate to
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 connect immediately"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 self.baud = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 self.dtr = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 self.port = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 self.analyzer = gcoder.GCode()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 # Serial instance connected to the printer, should be None when
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 # disconnected
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 self.printer = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 # clear to send, enabled after responses
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 # FIXME: should probably be changed to a sliding window approach
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 self.clear = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 # The printer has responded to the initial command and is active
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 self.online = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 # is a print currently running, true if printing, false if paused
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 self.printing = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 self.mainqueue = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 self.priqueue = Queue(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 self.queueindex = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 self.lineno = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 self.resendfrom = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 self.paused = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 self.sentlines = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 self.log = deque(maxlen = 10000)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 self.sent = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 self.writefailures = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 self.tempcb = None # impl (wholeline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 self.recvcb = None # impl (wholeline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 self.sendcb = None # impl (wholeline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 self.preprintsendcb = None # impl (wholeline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 self.printsendcb = None # impl (wholeline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 self.layerchangecb = None # impl (wholeline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 self.errorcb = None # impl (wholeline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 self.startcb = None # impl ()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 self.endcb = None # impl ()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 self.onlinecb = None # impl ()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 self.loud = False # emit sent and received lines to terminal
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 self.tcp_streaming_mode = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 self.greetings = ['start', 'Grbl ']
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 self.wait = 0 # default wait period for send(), send_now()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 self.read_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 self.stop_read_thread = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 self.send_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 self.stop_send_thread = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 self.print_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 if port is not None and baud is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 self.connect(port, baud)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 self.xy_feedrate = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 self.z_feedrate = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 def logError(self, error):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 if self.errorcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 try: self.errorcb(error)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 except: logging.error(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 logging.error(error)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 @locked
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 def disconnect(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 """Disconnects from printer and pauses the print
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 if self.printer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 if self.read_thread:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 self.stop_read_thread = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 if threading.current_thread() != self.read_thread:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 self.read_thread.join()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 self.read_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 if self.print_thread:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 self.printing = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 self.print_thread.join()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 self._stop_sender()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 self.printer.close()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 except socket.error:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 except OSError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 self.printer = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 self.online = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 self.printing = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 @locked
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 def connect(self, port = None, baud = None, dtr=None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 """Set port and baudrate if given, then connect to printer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 if self.printer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 self.disconnect()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 if port is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 self.port = port
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 if baud is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 self.baud = baud
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 if dtr is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 self.dtr = dtr
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 if self.port is not None and self.baud is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 # Connect to socket if "port" is an IP, device if not
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 host_regexp = re.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 is_serial = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 if ":" in port:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 bits = port.split(":")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 if len(bits) == 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 hostname = bits[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 port = int(bits[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 if host_regexp.match(hostname) and 1 <= port <= 65535:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 is_serial = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 self.writefailures = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 if not is_serial:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 self.printer_tcp = socket.socket(socket.AF_INET,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 socket.SOCK_STREAM)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 self.printer_tcp.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 self.timeout = 0.25
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 self.printer_tcp.settimeout(1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 self.printer_tcp.connect((hostname, port))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 self.printer_tcp.settimeout(self.timeout)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 self.printer = self.printer_tcp.makefile()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 except socket.error as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 if(e.strerror is None): e.strerror=""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 self.logError(_("Could not connect to %s:%s:") % (hostname, port) +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 "\n" + _("Socket error %s:") % e.errno +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 "\n" + e.strerror)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 self.printer = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 self.printer_tcp = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 disable_hup(self.port)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 self.printer_tcp = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 self.printer = Serial(port = self.port,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 baudrate = self.baud,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 timeout = 0.25,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 parity = PARITY_ODD)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 self.printer.close()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 self.printer.parity = PARITY_NONE
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 try: #this appears not to work on many platforms, so we're going to call it but not care if it fails
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 self.printer.setDTR(dtr);
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 #self.logError(_("Could not set DTR on this platform")) #not sure whether to output an error message
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 self.printer.open()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 except SerialException as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 self.logError(_("Could not connect to %s at baudrate %s:") % (self.port, self.baud) +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 "\n" + _("Serial error: %s") % e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 self.printer = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 except IOError as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 self.logError(_("Could not connect to %s at baudrate %s:") % (self.port, self.baud) +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 "\n" + _("IO error: %s") % e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 self.printer = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 self.stop_read_thread = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 self.read_thread = threading.Thread(target = self._listen)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 self.read_thread.start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 self._start_sender()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 def reset(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 """Reset the printer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 if self.printer and not self.printer_tcp:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 self.printer.setDTR(1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 time.sleep(0.2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 self.printer.setDTR(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 def _readline(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 line = self.printer.readline()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 if self.printer_tcp and not line:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 raise OSError(-1, "Read EOF from socket")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 except socket.timeout:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 return ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 if len(line) > 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 self.log.append(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 if self.recvcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 try: self.recvcb(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 except: self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 if self.loud: logging.info("RECV: %s" % line.rstrip())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 return line
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 except SelectError as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 if 'Bad file descriptor' in e.args[1]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 self.logError(_(u"Can't read from printer (disconnected?) (SelectError {0}): {1}").format(e.errno, decode_utf8(e.strerror)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 self.logError(_(u"SelectError ({0}): {1}").format(e.errno, decode_utf8(e.strerror)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 raise
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 except SerialException as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 self.logError(_(u"Can't read from printer (disconnected?) (SerialException): {0}").format(decode_utf8(str(e))))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 except socket.error as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 self.logError(_(u"Can't read from printer (disconnected?) (Socket error {0}): {1}").format(e.errno, decode_utf8(e.strerror)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 except OSError as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 if e.errno == errno.EAGAIN: # Not a real error, no data was available
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 return ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 self.logError(_(u"Can't read from printer (disconnected?) (OS Error {0}): {1}").format(e.errno, e.strerror))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 def _listen_can_continue(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 if self.printer_tcp:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 return not self.stop_read_thread and self.printer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 return (not self.stop_read_thread
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 and self.printer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 and self.printer.isOpen())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 def _listen_until_online(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 while not self.online and self._listen_can_continue():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275 self._send("M105")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 if self.writefailures >= 4:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 logging.error(_("Aborting connection attempt after 4 failed writes."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 empty_lines = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 while self._listen_can_continue():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 line = self._readline()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 if line is None: break # connection problem
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 # workaround cases where M105 was sent before printer Serial
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 # was online an empty line means read timeout was reached,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 # meaning no data was received thus we count those empty lines,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 # and once we have seen 15 in a row, we just break and send a
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 # new M105
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 # 15 was chosen based on the fact that it gives enough time for
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 # Gen7 bootloader to time out, and that the non received M105
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 # issues should be quite rare so we can wait for a long time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 # before resending
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 if not line:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 empty_lines += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 if empty_lines == 15: break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 else: empty_lines = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 if line.startswith(tuple(self.greetings)) \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 or line.startswith('ok') or "T:" in line:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 self.online = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 if self.onlinecb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 try: self.onlinecb()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 except: self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 def _listen(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 """This function acts on messages from the firmware
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 if not self.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309 self._listen_until_online()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 while self._listen_can_continue():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 line = self._readline()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 if line is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 if line.startswith('DEBUG_'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 if line.startswith(tuple(self.greetings)) or line.startswith('ok'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 if line.startswith('ok') and "T:" in line and self.tempcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 # callback for temp, status, whatever
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 try: self.tempcb(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 except: self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 elif line.startswith('Error'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 self.logError(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324 # Teststrings for resend parsing # Firmware exp. result
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 # line="rs N2 Expected checksum 67" # Teacup 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 if line.lower().startswith("resend") or line.startswith("rs"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 for haystack in ["N:", "N", ":"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 line = line.replace(haystack, " ")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 linewords = line.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 while len(linewords) != 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 toresend = int(linewords.pop(0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 self.resendfrom = toresend
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 def _start_sender(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341 self.stop_send_thread = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 self.send_thread = threading.Thread(target = self._sender)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 self.send_thread.start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 def _stop_sender(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 if self.send_thread:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 self.stop_send_thread = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 self.send_thread.join()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349 self.send_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 def _sender(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 while not self.stop_send_thread:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 command = self.priqueue.get(True, 0.1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 except QueueEmpty:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 while self.printer and self.printing and not self.clear:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 time.sleep(0.001)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 self._send(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 while self.printer and self.printing and not self.clear:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 time.sleep(0.001)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 def _checksum(self, command):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 return reduce(lambda x, y: x ^ y, map(ord, command))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 def startprint(self, gcode, startindex = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 """Start a print, gcode is an array of gcode commands.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 returns True on success, False if already printing.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 The print queue will be replaced with the contents of the data array,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 the next line will be set to 0 and the firmware notified. Printing
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 will then start in a parallel thread.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373 if self.printing or not self.online or not self.printer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 self.queueindex = startindex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 self.mainqueue = gcode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 self.printing = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 self.lineno = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 self.resendfrom = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380 self._send("M110", -1, True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 if not gcode or not gcode.lines:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 self.clear = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 resuming = (startindex != 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 self.print_thread = threading.Thread(target = self._print,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 kwargs = {"resuming": resuming})
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 self.print_thread.start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 def cancelprint(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 self.pause()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 self.paused = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 self.mainqueue = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 # run a simple script if it exists, no multithreading
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 def runSmallScript(self, filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 if filename is None: return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 f = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 with open(filename) as f:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 for i in f:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 l = i.replace("\n", "")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 l = l[:l.find(";")] # remove comments
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 self.send_now(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 def pause(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 """Pauses the print, saving the current position.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412 if not self.printing: return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 self.paused = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 self.printing = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 # try joining the print thread: enclose it in try/except because we
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 # might be calling it from the thread itself
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 self.print_thread.join()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 except RuntimeError, e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 if e.message == "cannot join current thread":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 self.print_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 # saves the status
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 self.pauseX = self.analyzer.abs_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 self.pauseY = self.analyzer.abs_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 self.pauseZ = self.analyzer.abs_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 self.pauseE = self.analyzer.abs_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435 self.pauseF = self.analyzer.current_f
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 self.pauseRelative = self.analyzer.relative
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 def resume(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 """Resumes a paused print.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 if not self.paused: return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 if self.paused:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 # restores the status
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 self.send_now("G90") # go to absolute coordinates
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446 xyFeedString = ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 zFeedString = ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448 if self.xy_feedrate is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 xyFeedString = " F" + str(self.xy_feedrate)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 if self.z_feedrate is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 zFeedString = " F" + str(self.z_feedrate)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 self.send_now("G1 X%s Y%s%s" % (self.pauseX, self.pauseY,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454 xyFeedString))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455 self.send_now("G1 Z" + str(self.pauseZ) + zFeedString)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 self.send_now("G92 E" + str(self.pauseE))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 # go back to relative if needed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 if self.pauseRelative: self.send_now("G91")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460 # reset old feed rate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461 self.send_now("G1 F" + str(self.pauseF))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
462
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
463 self.paused = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
464 self.printing = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
465 self.print_thread = threading.Thread(target = self._print,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
466 kwargs = {"resuming": True})
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
467 self.print_thread.start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
468
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
469 def send(self, command, wait = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
470 """Adds a command to the checksummed main command queue if printing, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
471 sends the command immediately if not printing"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
472
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
473 if self.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
474 if self.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
475 self.mainqueue.append(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
476 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
477 self.priqueue.put_nowait(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
478 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
479 self.logError(_("Not connected to printer."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
480
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
481 def send_now(self, command, wait = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
482 """Sends a command to the printer ahead of the command queue, without a
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
483 checksum"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
484 if self.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
485 self.priqueue.put_nowait(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
486 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
487 self.logError(_("Not connected to printer."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
488
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
489 def _print(self, resuming = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
490 self._stop_sender()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
491 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
492 if self.startcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
493 # callback for printing started
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
494 try: self.startcb(resuming)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
495 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
496 self.logError(_("Print start callback failed with:") +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
497 "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
498 while self.printing and self.printer and self.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
499 self._sendnext()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
500 self.sentlines = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
501 self.log.clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
502 self.sent = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
503 if self.endcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
504 # callback for printing done
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
505 try: self.endcb()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
506 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
507 self.logError(_("Print end callback failed with:") +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
508 "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
509 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
510 self.logError(_("Print thread died due to the following error:") +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
511 "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
512 finally:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
513 self.print_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
514 self._start_sender()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
515
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
516 def process_host_command(self, command):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
517 """only ;@pause command is implemented as a host command in printcore, but hosts are free to reimplement this method"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
518 command = command.lstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
519 if command.startswith(";@pause"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
520 self.pause()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
521
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
522 def _sendnext(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
523 if not self.printer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
524 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
525 while self.printer and self.printing and not self.clear:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
526 time.sleep(0.001)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
527 # Only wait for oks when using serial connections or when not using tcp
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
528 # in streaming mode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
529 if not self.printer_tcp or not self.tcp_streaming_mode:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
530 self.clear = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
531 if not (self.printing and self.printer and self.online):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
532 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
533 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
534 if self.resendfrom < self.lineno and self.resendfrom > -1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
535 self._send(self.sentlines[self.resendfrom], self.resendfrom, False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
536 self.resendfrom += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
537 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
538 self.resendfrom = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
539 if not self.priqueue.empty():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
540 self._send(self.priqueue.get_nowait())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
541 self.priqueue.task_done()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
542 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
543 if self.printing and self.queueindex < len(self.mainqueue):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
544 (layer, line) = self.mainqueue.idxs(self.queueindex)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
545 gline = self.mainqueue.all_layers[layer][line]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
546 if self.layerchangecb and self.queueindex > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
547 (prev_layer, prev_line) = self.mainqueue.idxs(self.queueindex - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
548 if prev_layer != layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
549 try: self.layerchangecb(layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
550 except: self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
551 if self.preprintsendcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
552 if self.queueindex + 1 < len(self.mainqueue):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
553 (next_layer, next_line) = self.mainqueue.idxs(self.queueindex + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
554 next_gline = self.mainqueue.all_layers[next_layer][next_line]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
555 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
556 next_gline = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
557 gline = self.preprintsendcb(gline, next_gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
558 if gline is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
559 self.queueindex += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
560 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
561 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
562 tline = gline.raw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
563 if tline.lstrip().startswith(";@"): # check for host command
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
564 self.process_host_command(tline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
565 self.queueindex += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
566 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
567 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
568
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
569 # Strip comments
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
570 tline = gcoder.gcode_strip_comment_exp.sub("", tline).strip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
571 if tline:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
572 self._send(tline, self.lineno, True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
573 self.lineno += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
574 if self.printsendcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
575 try: self.printsendcb(gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
576 except: self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
577 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
578 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
579 self.queueindex += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
580 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
581 self.printing = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
582 self.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
583 if not self.paused:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
584 self.queueindex = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
585 self.lineno = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
586 self._send("M110", -1, True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
587
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
588 def _send(self, command, lineno = 0, calcchecksum = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
589 # Only add checksums if over serial (tcp does the flow control itself)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
590 if calcchecksum and not self.printer_tcp:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
591 prefix = "N" + str(lineno) + " " + command
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
592 command = prefix + "*" + str(self._checksum(prefix))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
593 if "M110" not in command:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
594 self.sentlines[lineno] = command
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
595 if self.printer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
596 self.sent.append(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
597 # run the command through the analyzer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
598 gline = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
599 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
600 gline = self.analyzer.append(command, store = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
601 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
602 logging.warning(_("Could not analyze command %s:") % command +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
603 "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
604 if self.loud:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
605 logging.info("SENT: %s" % command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
606 if self.sendcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
607 try: self.sendcb(command, gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
608 except: self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
609 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
610 self.printer.write(str(command + "\n"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
611 if self.printer_tcp:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
612 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
613 self.printer.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
614 except socket.timeout:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
615 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
616 self.writefailures = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
617 except socket.error as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
618 if e.errno is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
619 self.logError(_(u"Can't write to printer (disconnected ?):") +
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
620 "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
621 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
622 self.logError(_(u"Can't write to printer (disconnected?) (Socket error {0}): {1}").format(e.errno, decode_utf8(e.strerror)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
623 self.writefailures += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
624 except SerialException as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
625 self.logError(_(u"Can't write to printer (disconnected?) (SerialException): {0}").format(decode_utf8(str(e))))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
626 self.writefailures += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
627 except RuntimeError as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
628 self.logError(_(u"Socket connection broken, disconnected. ({0}): {1}").format(e.errno, decode_utf8(e.strerror)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
629 self.writefailures += 1

mercurial