Boelle

Untitled

Sep 16th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.51 KB | None | 0 0
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3.  
  4. from .avr_isp import stk500v2, ispBase, intelHex
  5. import serial
  6. import threading
  7. import time
  8. import queue
  9. import re
  10. import functools
  11.  
  12. from UM.Application import Application
  13. from UM.Logger import Logger
  14. from cura.PrinterOutputDevice import PrinterOutputDevice, ConnectionState
  15. from UM.Message import Message
  16.  
  17. from PyQt5.QtCore import QUrl, pyqtSlot, pyqtSignal, pyqtProperty
  18.  
  19. from UM.i18n import i18nCatalog
  20. catalog = i18nCatalog("cura")
  21.  
  22.  
  23. class USBPrinterOutputDevice(PrinterOutputDevice):
  24.  
  25. def __init__(self, serial_port):
  26. super().__init__(serial_port)
  27. self.setName(catalog.i18nc("@item:inmenu", "USB printing"))
  28. self.setShortDescription(catalog.i18nc("@action:button", "Print via USB"))
  29. self.setDescription(catalog.i18nc("@info:tooltip", "Print via USB"))
  30. self.setIconName("print")
  31. self.setConnectionText(catalog.i18nc("@info:status", "Connected via USB"))
  32.  
  33. self._serial = None
  34. self._serial_port = serial_port
  35. self._error_state = None
  36.  
  37. self._connect_thread = threading.Thread(target = self._connect)
  38. self._connect_thread.daemon = True
  39.  
  40. self._end_stop_thread = None
  41. self._poll_endstop = False
  42.  
  43. # The baud checking is done by sending a number of m105 commands to the printer and waiting for a readable
  44. # response. If the baudrate is correct, this should make sense, else we get giberish.
  45. self._required_responses_auto_baud = 3
  46.  
  47. self._listen_thread = threading.Thread(target=self._listen)
  48. self._listen_thread.daemon = True
  49.  
  50. self._update_firmware_thread = threading.Thread(target= self._updateFirmware)
  51. self._update_firmware_thread.daemon = True
  52. self.firmwareUpdateComplete.connect(self._onFirmwareUpdateComplete)
  53.  
  54. self._heatup_wait_start_time = time.time()
  55.  
  56. ## Queue for commands that need to be send. Used when command is sent when a print is active.
  57. self._command_queue = queue.Queue()
  58.  
  59. self._is_printing = False
  60. self._is_paused = False
  61.  
  62. ## Set when print is started in order to check running time.
  63. self._print_start_time = None
  64. self._print_start_time_100 = None
  65.  
  66. ## Keep track where in the provided g-code the print is
  67. self._gcode_position = 0
  68.  
  69. # List of gcode lines to be printed
  70. self._gcode = []
  71.  
  72. # Check if endstops are ever pressed (used for first run)
  73. self._x_min_endstop_pressed = False
  74. self._y_min_endstop_pressed = False
  75. self._z_min_endstop_pressed = False
  76.  
  77. self._x_max_endstop_pressed = False
  78. self._y_max_endstop_pressed = False
  79. self._z_max_endstop_pressed = False
  80.  
  81. # In order to keep the connection alive we request the temperature every so often from a different extruder.
  82. # This index is the extruder we requested data from the last time.
  83. self._temperature_requested_extruder_index = 0
  84.  
  85. self._current_z = 0
  86.  
  87. self._updating_firmware = False
  88.  
  89. self._firmware_file_name = None
  90. self._firmware_update_finished = False
  91.  
  92. self._error_message = None
  93. self._error_code = 0
  94.  
  95. onError = pyqtSignal()
  96.  
  97. firmwareUpdateComplete = pyqtSignal()
  98. firmwareUpdateChange = pyqtSignal()
  99.  
  100. endstopStateChanged = pyqtSignal(str ,bool, arguments = ["key","state"])
  101.  
  102. def _setTargetBedTemperature(self, temperature):
  103. Logger.log("d", "Setting bed temperature to %s", temperature)
  104. self._sendCommand("M140 S%s" % temperature)
  105.  
  106. def _setTargetHotendTemperature(self, index, temperature):
  107. Logger.log("d", "Setting hotend %s temperature to %s", index, temperature)
  108. self._sendCommand("M104 T%s S%s" % (index, temperature))
  109.  
  110. def _setHeadPosition(self, x, y , z, speed):
  111. self._sendCommand("G0 X%s Y%s Z%s F%s" % (x, y, z, speed))
  112.  
  113. def _setHeadX(self, x, speed):
  114. self._sendCommand("G0 X%s F%s" % (x, speed))
  115.  
  116. def _setHeadY(self, y, speed):
  117. self._sendCommand("G0 Y%s F%s" % (y, speed))
  118.  
  119. def _setHeadZ(self, z, speed):
  120. self._sendCommand("G0 Y%s F%s" % (z, speed))
  121.  
  122. def _homeHead(self):
  123. self._sendCommand("G28")
  124.  
  125. def _homeBed(self):
  126. self._sendCommand("G28 Z")
  127.  
  128. def startPrint(self):
  129. self.writeStarted.emit(self)
  130. gcode_list = getattr( Application.getInstance().getController().getScene(), "gcode_list")
  131. self._updateJobState("printing")
  132. self.printGCode(gcode_list)
  133.  
  134. def _moveHead(self, x, y, z, speed):
  135. self._sendCommand("G91")
  136. self._sendCommand("G0 X%s Y%s Z%s F%s" % (x, y, z, speed))
  137. self._sendCommand("G90")
  138.  
  139. ## Start a print based on a g-code.
  140. # \param gcode_list List with gcode (strings).
  141. def printGCode(self, gcode_list):
  142. if self._progress or self._connection_state != ConnectionState.connected:
  143. self._error_message = Message(catalog.i18nc("@info:status", "Unable to start a new job because the printer is busy or not connected."))
  144. self._error_message.show()
  145. Logger.log("d", "Printer is busy or not connected, aborting print")
  146. self.writeError.emit(self)
  147. return
  148.  
  149. self._gcode.clear()
  150. for layer in gcode_list:
  151. self._gcode.extend(layer.split("\n"))
  152.  
  153. # Reset line number. If this is not done, first line is sometimes ignored
  154. self._gcode.insert(0, "M110")
  155. self._gcode_position = 0
  156. self._print_start_time_100 = None
  157. self._is_printing = True
  158. self._print_start_time = time.time()
  159.  
  160. for i in range(0, 4): # Push first 4 entries before accepting other inputs
  161. self._sendNextGcodeLine()
  162.  
  163. self.writeFinished.emit(self)
  164.  
  165. ## Get the serial port string of this connection.
  166. # \return serial port
  167. def getSerialPort(self):
  168. return self._serial_port
  169.  
  170. ## Try to connect the serial. This simply starts the thread, which runs _connect.
  171. def connect(self):
  172. if not self._updating_firmware and not self._connect_thread.isAlive():
  173. self._connect_thread.start()
  174.  
  175. ## Private function (threaded) that actually uploads the firmware.
  176. def _updateFirmware(self):
  177. self._error_code = 0
  178. self.setProgress(0, 100)
  179. self._firmware_update_finished = False
  180.  
  181. if self._connection_state != ConnectionState.closed:
  182. self.close()
  183. hex_file = intelHex.readHex(self._firmware_file_name)
  184.  
  185. if len(hex_file) == 0:
  186. Logger.log("e", "Unable to read provided hex file. Could not update firmware")
  187. self._updateFirmwareFailedMissingFirmware()
  188. return
  189.  
  190. programmer = stk500v2.Stk500v2()
  191. programmer.progress_callback = self.setProgress
  192.  
  193. try:
  194. programmer.connect(self._serial_port)
  195. except Exception:
  196. pass
  197.  
  198. # Give programmer some time to connect. Might need more in some cases, but this worked in all tested cases.
  199. time.sleep(1)
  200.  
  201. if not programmer.isConnected():
  202. Logger.log("e", "Unable to connect with serial. Could not update firmware")
  203. self._updateFirmwareFailedCommunicationError()
  204. return
  205.  
  206. self._updating_firmware = True
  207.  
  208. try:
  209. programmer.programChip(hex_file)
  210. self._updating_firmware = False
  211. except serial.SerialException as e:
  212. Logger.log("e", "SerialException while trying to update firmware: <%s>" %(repr(e)))
  213. self._updateFirmwareFailedIOError()
  214. return
  215. except Exception as e:
  216. Logger.log("e", "Exception while trying to update firmware: <%s>" %(repr(e)))
  217. self._updateFirmwareFailedUnknown()
  218. return
  219. programmer.close()
  220.  
  221. self._updateFirmwareCompletedSucessfully()
  222. return
  223.  
  224. ## Private function which makes sure that firmware update process has failed by missing firmware
  225. def _updateFirmwareFailedMissingFirmware(self):
  226. return self._updateFirmwareFailedCommon(4)
  227.  
  228. ## Private function which makes sure that firmware update process has failed by an IO error
  229. def _updateFirmwareFailedIOError(self):
  230. return self._updateFirmwareFailedCommon(3)
  231.  
  232. ## Private function which makes sure that firmware update process has failed by a communication problem
  233. def _updateFirmwareFailedCommunicationError(self):
  234. return self._updateFirmwareFailedCommon(2)
  235.  
  236. ## Private function which makes sure that firmware update process has failed by an unknown error
  237. def _updateFirmwareFailedUnknown(self):
  238. return self._updateFirmwareFailedCommon(1)
  239.  
  240. ## Private common function which makes sure that firmware update process has completed/ended with a set progress state
  241. def _updateFirmwareFailedCommon(self, code):
  242. if not code:
  243. raise Exception("Error code not set!")
  244.  
  245. self._error_code = code
  246.  
  247. self._firmware_update_finished = True
  248. self.resetFirmwareUpdate(update_has_finished = True)
  249. self.progressChanged.emit()
  250. self.firmwareUpdateComplete.emit()
  251.  
  252. return
  253.  
  254. ## Private function which makes sure that firmware update process has successfully completed
  255. def _updateFirmwareCompletedSucessfully(self):
  256. self.setProgress(100, 100)
  257. self._firmware_update_finished = True
  258. self.resetFirmwareUpdate(update_has_finished = True)
  259. self.firmwareUpdateComplete.emit()
  260.  
  261. return
  262.  
  263. ## Upload new firmware to machine
  264. # \param filename full path of firmware file to be uploaded
  265. def updateFirmware(self, file_name):
  266. Logger.log("i", "Updating firmware of %s using %s", self._serial_port, file_name)
  267. self._firmware_file_name = file_name
  268. self._update_firmware_thread.start()
  269.  
  270. @property
  271. def firmwareUpdateFinished(self):
  272. return self._firmware_update_finished
  273.  
  274. def resetFirmwareUpdate(self, update_has_finished = False):
  275. self._firmware_update_finished = update_has_finished
  276. self.firmwareUpdateChange.emit()
  277.  
  278. @pyqtSlot()
  279. def startPollEndstop(self):
  280. if not self._poll_endstop:
  281. self._poll_endstop = True
  282. if self._end_stop_thread is None:
  283. self._end_stop_thread = threading.Thread(target=self._pollEndStop)
  284. self._end_stop_thread.daemon = True
  285. self._end_stop_thread.start()
  286.  
  287. @pyqtSlot()
  288. def stopPollEndstop(self):
  289. self._poll_endstop = False
  290. self._end_stop_thread = None
  291.  
  292. def _pollEndStop(self):
  293. while self._connection_state == ConnectionState.connected and self._poll_endstop:
  294. self.sendCommand("M119")
  295. time.sleep(0.5)
  296.  
  297. ## Private connect function run by thread. Can be started by calling connect.
  298. def _connect(self):
  299. Logger.log("d", "Attempting to connect to %s", self._serial_port)
  300. self.setConnectionState(ConnectionState.connecting)
  301. programmer = stk500v2.Stk500v2()
  302. try:
  303. programmer.connect(self._serial_port) # Connect with the serial, if this succeeds, it's an arduino based usb device.
  304. self._serial = programmer.leaveISP()
  305. except ispBase.IspError as e:
  306. Logger.log("i", "Could not establish connection on %s: %s. Device is not arduino based." %(self._serial_port,str(e)))
  307. except Exception as e:
  308. Logger.log("i", "Could not establish connection on %s, unknown reasons. Device is not arduino based." % self._serial_port)
  309. if not self._serial:
  310. Logger.log("d", "Stk500v2 opened serial port %s", self._serial_port)
  311. else:
  312. Logger.log("d", "Stk500v2 could not handle serial port %s", self._serial_port)
  313.  
  314. # If the programmer connected, we know its an atmega based version.
  315. # Not all that useful, but it does give some debugging information.
  316. for baud_rate in self._getBaudrateList(): # Cycle all baud rates (auto detect)
  317. Logger.log("d", "Attempting to connect to printer with serial %s on baud rate %s", self._serial_port, baud_rate)
  318. if self._serial is None:
  319. try:
  320. self._serial = serial.Serial(str(self._serial_port), baud_rate, timeout = 3, writeTimeout = 10000)
  321. except serial.SerialException:
  322. Logger.log("d", "Could not open port %s" % self._serial_port)
  323. continue
  324. else:
  325. if not self.setBaudRate(baud_rate):
  326. continue # Could not set the baud rate, go to the next
  327.  
  328. Logger.log("d", "Serial port %s opened at baudrate %s", self._serial_port, baud_rate)
  329. time.sleep(2) # Ensure that we are not talking to the bootloader. 1.5 seconds seems to be the magic number
  330. sucesfull_responses = 0
  331. timeout_time = time.time() + 5
  332. self._serial.write(b"\n")
  333. Logger.log("d", "Sending M105")
  334. self._sendCommand("M105") # Request temperature, as this should (if baudrate is correct) result in a command with "T:" in it
  335. while timeout_time > time.time():
  336. line = self._readline()
  337. if line is None:
  338. Logger.log("d", "No response from serial connection received.")
  339. # Something went wrong with reading, could be that close was called.
  340. self.setConnectionState(ConnectionState.closed)
  341. return
  342.  
  343. Logger.log("d", "Response: %s" % line)
  344. if b"T:" in line:
  345.  
  346. Logger.log("d", "Correct response for auto-baudrate detection received.")
  347. self._serial.timeout = 0.5
  348. sucesfull_responses += 1
  349. if sucesfull_responses >= self._required_responses_auto_baud:
  350. self._serial.timeout = 2 # Reset serial timeout
  351. self.setConnectionState(ConnectionState.connected)
  352. self._listen_thread.start() # Start listening
  353. Logger.log("i", "Established printer connection on port %s" % self._serial_port)
  354. return
  355.  
  356. Logger.log("d", "Sending M105")
  357. self._sendCommand("M105") # Send M105 as long as we are listening, otherwise we end up in an undefined state
  358. Logger.log("d", "Timeout reached before getting a valid response from serial port %s at baudrate %s", self._serial_port, baud_rate)
  359.  
  360. Logger.log("e", "Baud rate detection for %s failed", self._serial_port)
  361. self.close() # Unable to connect, wrap up.
  362. self.setConnectionState(ConnectionState.closed)
  363.  
  364. ## Set the baud rate of the serial. This can cause exceptions, but we simply want to ignore those.
  365. def setBaudRate(self, baud_rate):
  366. try:
  367. self._serial.baudrate = baud_rate
  368. return True
  369. except Exception as e:
  370. return False
  371.  
  372. ## Close the printer connection
  373. def close(self):
  374. Logger.log("d", "Closing the USB printer connection.")
  375. if self._connect_thread.isAlive():
  376. try:
  377. self._connect_thread.join()
  378. except Exception as e:
  379. Logger.log("d", "PrinterConnection.close: %s (expected)", e)
  380. pass # This should work, but it does fail sometimes for some reason
  381.  
  382. self._connect_thread = threading.Thread(target = self._connect)
  383. self._connect_thread.daemon = True
  384.  
  385. self.setConnectionState(ConnectionState.closed)
  386. if self._serial is not None:
  387. try:
  388. self._listen_thread.join()
  389. except:
  390. pass
  391. self._serial.close()
  392.  
  393. self._listen_thread = threading.Thread(target = self._listen)
  394. self._listen_thread.daemon = True
  395. self._serial = None
  396.  
  397. ## Directly send the command, withouth checking connection state (eg; printing).
  398. # \param cmd string with g-code
  399. def _sendCommand(self, cmd):
  400. if self._serial is None:
  401. return
  402.  
  403. if "M109" in cmd or "M190" in cmd:
  404. self._heatup_wait_start_time = time.time()
  405.  
  406. try:
  407. command = (cmd + "\n").encode()
  408. self._serial.write(b"\n")
  409. self._serial.write(command)
  410. except serial.SerialTimeoutException:
  411. Logger.log("w","Serial timeout while writing to serial port, trying again.")
  412. try:
  413. time.sleep(0.5)
  414. self._serial.write((cmd + "\n").encode())
  415. except Exception as e:
  416. Logger.log("e","Unexpected error while writing serial port %s " % e)
  417. self._setErrorState("Unexpected error while writing serial port %s " % e)
  418. self.close()
  419. except Exception as e:
  420. Logger.log("e","Unexpected error while writing serial port %s" % e)
  421. self._setErrorState("Unexpected error while writing serial port %s " % e)
  422. self.close()
  423.  
  424. ## Send a command to printer.
  425. # \param cmd string with g-code
  426. def sendCommand(self, cmd):
  427. if self._progress:
  428. self._command_queue.put(cmd)
  429. elif self._connection_state == ConnectionState.connected:
  430. self._sendCommand(cmd)
  431.  
  432. ## Set the error state with a message.
  433. # \param error String with the error message.
  434. def _setErrorState(self, error):
  435. self._updateJobState("error")
  436. self._error_state = error
  437. self.onError.emit()
  438.  
  439. def requestWrite(self, node, file_name = None, filter_by_machine = False):
  440. Application.getInstance().showPrintMonitor.emit(True)
  441. self.startPrint()
  442.  
  443. def _setEndstopState(self, endstop_key, value):
  444. if endstop_key == b"x_min":
  445. if self._x_min_endstop_pressed != value:
  446. self.endstopStateChanged.emit("x_min", value)
  447. self._x_min_endstop_pressed = value
  448. elif endstop_key == b"y_min":
  449. if self._y_min_endstop_pressed != value:
  450. self.endstopStateChanged.emit("y_min", value)
  451. self._y_min_endstop_pressed = value
  452. elif endstop_key == b"z_min":
  453. if self._z_min_endstop_pressed != value:
  454. self.endstopStateChanged.emit("z_min", value)
  455. self._z_min_endstop_pressed = value
  456.  
  457. ## Listen thread function.
  458. def _listen(self):
  459. Logger.log("i", "Printer connection listen thread started for %s" % self._serial_port)
  460. temperature_request_timeout = time.time()
  461. ok_timeout = time.time()
  462. while self._connection_state == ConnectionState.connected:
  463. line = self._readline()
  464. if line is None:
  465. break # None is only returned when something went wrong. Stop listening
  466.  
  467. if time.time() > temperature_request_timeout:
  468. if self._num_extruders > 0:
  469. self._temperature_requested_extruder_index = (self._temperature_requested_extruder_index + 1) % self._num_extruders
  470. self.sendCommand("M105 T%d" % (self._temperature_requested_extruder_index))
  471. else:
  472. self.sendCommand("M105")
  473. temperature_request_timeout = time.time() + 5
  474.  
  475. if line.startswith(b"Error:"):
  476. # Oh YEAH, consistency.
  477. # Marlin reports a MIN/MAX temp error as "Error:x\n: Extruder switched off. MAXTEMP triggered !\n"
  478. # But a bed temp error is reported as "Error: Temperature heated bed switched off. MAXTEMP triggered !!"
  479. # So we can have an extra newline in the most common case. Awesome work people.
  480. if re.match(b"Error:[0-9]\n", line):
  481. line = line.rstrip() + self._readline()
  482.  
  483. # Skip the communication errors, as those get corrected.
  484. if b"Extruder switched off" in line or b"Temperature heated bed switched off" in line or b"Something is wrong, please turn off the printer." in line:
  485. if not self.hasError():
  486. self._setErrorState(line[6:])
  487.  
  488. elif b" T:" in line or line.startswith(b"T:"): # Temperature message
  489. try:
  490. self._setHotendTemperature(self._temperature_requested_extruder_index, float(re.search(b"T: *([0-9\.]*)", line).group(1)))
  491. except:
  492. pass
  493. if b"B:" in line: # Check if it's a bed temperature
  494. try:
  495. self._setBedTemperature(float(re.search(b"B: *([0-9\.]*)", line).group(1)))
  496. except Exception as e:
  497. pass
  498. #TODO: temperature changed callback
  499. elif b"_min" in line or b"_max" in line:
  500. tag, value = line.split(b":", 1)
  501. self._setEndstopState(tag,(b"H" in value or b"TRIGGERED" in value))
  502.  
  503. if self._is_printing:
  504. if line == b"" and time.time() > ok_timeout:
  505. line = b"ok" # Force a timeout (basically, send next command)
  506.  
  507. if b"ok" in line:
  508. ok_timeout = time.time() + 5
  509. if not self._command_queue.empty():
  510. self._sendCommand(self._command_queue.get())
  511. elif self._is_paused:
  512. line = b"" # Force getting temperature as keep alive
  513. else:
  514. self._sendNextGcodeLine()
  515. elif b"resend" in line.lower() or b"rs" in line: # Because a resend can be asked with "resend" and "rs"
  516. try:
  517. self._gcode_position = int(line.replace(b"N:",b" ").replace(b"N",b" ").replace(b":",b" ").split()[-1])
  518. except:
  519. if b"rs" in line:
  520. self._gcode_position = int(line.split()[1])
  521.  
  522. # Request the temperature on comm timeout (every 2 seconds) when we are not printing.)
  523. if line == b"":
  524. if self._num_extruders > 0:
  525. self._temperature_requested_extruder_index = (self._temperature_requested_extruder_index + 1) % self._num_extruders
  526. self.sendCommand("M105 T%d" % self._temperature_requested_extruder_index)
  527. else:
  528. self.sendCommand("M105")
  529.  
  530. Logger.log("i", "Printer connection listen thread stopped for %s" % self._serial_port)
  531.  
  532. ## Send next Gcode in the gcode list
  533. def _sendNextGcodeLine(self):
  534. if self._gcode_position >= len(self._gcode):
  535. return
  536. if self._gcode_position == 100:
  537. self._print_start_time_100 = time.time()
  538. line = self._gcode[self._gcode_position]
  539.  
  540. if ";" in line:
  541. line = line[:line.find(";")]
  542. line = line.strip()
  543. try:
  544. if line == "M0" or line == "M1":
  545. line = "M105" # Don't send the M0 or M1 to the machine, as M0 and M1 are handled as an LCD menu pause.
  546. if ("G0" in line or "G1" in line) and "Z" in line:
  547. z = float(re.search("Z([0-9\.]*)", line).group(1))
  548. if self._current_z != z:
  549. self._current_z = z
  550. except Exception as e:
  551. Logger.log("e", "Unexpected error with printer connection: %s" % e)
  552. self._setErrorState("Unexpected error: %s" %e)
  553. checksum = functools.reduce(lambda x,y: x^y, map(ord, "N%d%s" % (self._gcode_position, line)))
  554.  
  555. self._sendCommand("N%d%s*%d" % (self._gcode_position, line, checksum))
  556. self._gcode_position += 1
  557. self.setProgress((self._gcode_position / len(self._gcode)) * 100)
  558. self.progressChanged.emit()
  559.  
  560. ## Set the state of the print.
  561. # Sent from the print monitor
  562. def _setJobState(self, job_state):
  563. if job_state == "pause":
  564. self._is_paused = True
  565. self._updateJobState("paused")
  566. elif job_state == "print":
  567. self._is_paused = False
  568. self._updateJobState("printing")
  569. elif job_state == "abort":
  570. self.cancelPrint()
  571.  
  572. ## Set the progress of the print.
  573. # It will be normalized (based on max_progress) to range 0 - 100
  574. def setProgress(self, progress, max_progress = 100):
  575. self._progress = (progress / max_progress) * 100 # Convert to scale of 0-100
  576. if self._progress == 100:
  577. # Printing is done, reset progress
  578. self._gcode_position = 0
  579. self.setProgress(0)
  580. self._is_printing = False
  581. self._is_paused = False
  582. self._updateJobState("ready")
  583. self.progressChanged.emit()
  584.  
  585. ## Cancel the current print. Printer connection wil continue to listen.
  586. def cancelPrint(self):
  587. self._gcode_position = 0
  588. self.setProgress(0)
  589. self._gcode = []
  590.  
  591. # Turn off temperatures, fan and steppers
  592. self._sendCommand("M140 S0")
  593. self._sendCommand("M104 S0")
  594. self._sendCommand("M107")
  595. self._sendCommand("M84")
  596. self._is_printing = False
  597. self._is_paused = False
  598. self._updateJobState("ready")
  599. Application.getInstance().showPrintMonitor.emit(False)
  600.  
  601. ## Check if the process did not encounter an error yet.
  602. def hasError(self):
  603. return self._error_state is not None
  604.  
  605. ## private read line used by printer connection to listen for data on serial port.
  606. def _readline(self):
  607. if self._serial is None:
  608. return None
  609. try:
  610. ret = self._serial.readline()
  611. except Exception as e:
  612. Logger.log("e", "Unexpected error while reading serial port. %s" % e)
  613. self._setErrorState("Printer has been disconnected")
  614. self.close()
  615. return None
  616. return ret
  617.  
  618. ## Create a list of baud rates at which we can communicate.
  619. # \return list of int
  620. def _getBaudrateList(self):
  621. ret = [115200, 250000, 230400, 57600, 38400, 19200, 9600]
  622. return ret
  623.  
  624. def _onFirmwareUpdateComplete(self):
  625. self._update_firmware_thread.join()
  626. self._update_firmware_thread = threading.Thread(target = self._updateFirmware)
  627. self._update_firmware_thread.daemon = True
  628.  
  629. self.connect()
Add Comment
Please, Sign In to add comment