printrun-src/printrun/printcore.py

Wed, 20 Jan 2021 10:15:13 +0100

author
mdd
date
Wed, 20 Jan 2021 10:15:13 +0100
changeset 46
cce0af6351f0
parent 15
0bbb006204fc
permissions
-rw-r--r--

updated and added new files for printrun

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

mercurial