def sending_end(self): """ Manages the end of upload. If some commands are waiting, run them at the end. """ self.send_thread.read_allowed.disconnect() self.serial_manager.send_confirm.disconnect() if self.send_thread and self.send_thread.user_stop: self.print("Stopped by user.", "error") logger.error("Upload stopped by user.") elif self.send_thread and self.send_thread.error: self.print("Error while sending.", "error") logger.error("Error while sending.") else: self.print("Done.", "info") logger.info("Upload done.") self.sending_progress.setValue(0) self.btn_send_current_file.setText( _translate("MainWindow", "Send current file")) self.btn_send_current_file.clicked.disconnect() self.btn_send_current_file.clicked.connect(self.send_file) self.tabWidget.setEnabled(True) self.send_thread = None if len(self.waiting_cmd) > 0: self.run_thread(**self.waiting_cmd.pop(0))
def open(self, baudrate, serial_port, timeout): """ Opens the serial port with the given parameters. :param baudrate: The baudrate. :param serial_port: The port to be used. :param timeout: Timeout for reading and writing. """ logger.info("Opening {} with baudrate {}, timeout {}".format( repr(serial_port), baudrate, timeout)) self.send_print.emit("Opening {} with baudrate {}, timeout {}".format( repr(serial_port), baudrate, timeout), "info") self.serial.port = serial_port self.serial.baudrate = baudrate self.serial.timeout = timeout self.serial.write_timeout = timeout try: self.serial.open() self.is_open = True except serial.serialutil.SerialException: self.is_open = False logger.error("Could not open serial port.") self.send_print.emit("Could not open serial port.", "error") return False logger.debug(self.serial.timeout) return True
def sendMsg(self, msg): """ Sends a message using the serial port if fake_mode is False. :param msg: The message to be sent. :returns: True if no error occurred, else False. """ if not self.fake_mode and not (self.serial and self.serial.isOpen()): self.send_print.emit("Error, no serial port to write.", "error") logger.error("Serial manager has no serial opened to send data.") return False elif self.fake_mode: self.send_print.emit(msg, "operator") logger.info( "Sending {} through fake serial port".format(repr(msg))) self.something_sent = True return True else: logger.info("Sending {} through {}".format(repr(msg), self.serial)) self.send_print.emit(msg, "operator") if msg[-1] != '\n': msg += '\n' try: self.serial.write(bytes(msg, encoding='utf8')) except serial.serialutil.SerialException as e: logger.error(e) return True
def readMsg(self): """ Reads a line from the serial port. And emit the send_print or send_confirm signals if necessary. """ if self.fake_mode: if self.something_sent: logger.info("Received {}".format(repr("ok"))) self.send_print.emit("ok", "machine") self.something_sent = False self.send_confirm.emit(True) return elif not (self.serial and self.serial.isOpen()): self.send_print.emit("Error, no serial port to read.", "error") logger.error("Serial manager has no serial opened to read data.") self.send_confirm.emit(False) return try: waiting = self.serial.in_waiting except OSError as e: logger.error("Serial error : {}".format(e)) self.send_print.emit("Serial error while reading.", "error") self.serial_fatal_error.emit() return if not waiting: return txt = "" try: txt = self.serial.readline().decode('ascii') except serial.serialutil.SerialException as e: logger.error("Serial error : {}".format(e)) self.send_print.emit("Serial error while reading.", "error") except UnicodeDecodeError as e: logger.error("Serial error : {}".format(e)) self.send_print.emit("Serial error while reading.", "error") except OSError as e: logger.error("Serial error : {}".format(e)) self.send_print.emit("Serial error while reading.", "error") self.serial_fatal_error.emit() if txt: if "error" in txt.lower(): self.send_print.emit(txt, "error") else: self.send_print.emit("m> {}".format(txt), "machine") self.send_confirm.emit("ok" in txt.lower())
def parse_error(self, line): """ Handles parsing errors. :param line: The line where the error occurred. """ self.chk_display_current_line.setChecked(False) self.code_edit.setExtraSelections([]) QMessageBox.critical( self, _translate("MainWindow", "Error."), _translate("MainWindow", "An error occurred during parsing.")) logger.error("While parsing line {}".format(line)) highlight = QTextEdit.ExtraSelection() highlight.cursor = QTextCursor( self.code_edit.document().findBlockByLineNumber(line)) highlight.format.setProperty(QTextFormat.FullWidthSelection, True) highlight.format.setBackground(Qt.red) self.code_edit.setTextCursor(highlight.cursor) self.code_edit.setExtraSelections([highlight])