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 set_read_allowed(self, st): """ Allows or not the thread to read. :param st: Is it allowed ? """ logger.debug("Set reading allowed to {}".format(st)) self.read_allowed = st
def confirm(self, st): """ Receive confirmation from the readThread. :param st: Everything ok ? """ self.error = not st self.confirmed = True logger.debug("Confirmation : {}.".format(st))
def list_serials(self): """ Lists available serials ports. """ logger.debug("Listing available serial ports.") l = serial_ports() for i in range(self.serial_ports_list.count()): self.serial_ports_list.removeItem(0) for i in l: logger.info("Found {}".format(i)) self.serial_ports_list.addItem(i)
def list_configs(self): """ Lists available configurations. """ for _ in range(self.config_list.count()): self.config_list.removeItem(0) logger.debug("Listing available configurations.") for f in os.listdir(settings.CONFIG_DIR): if f.endswith(".json"): logger.debug("Found {}".format(f)) self.config_list.addItem(f[:-5]) self.config_list.addItem(_translate("MainWindow", "New configuration"))
def set_serial_mode(self, mode): """ Change serial mode. :param mode: can be "manual" or "file" :type mode: str """ if mode == "manual": logger.debug("Setting manual mode.") self.btn_set_origin.setEnabled(True) self.grp_cmd.setEnabled(True) self.grp_auto.setEnabled(True) # self.btn_y_plus.pressed.connect() elif mode == "file": logger.debug("Setting file mode.") self.btn_set_origin.setEnabled(False) self.grp_cmd.setEnabled(False) self.grp_auto.setEnabled(False)
def save_config(self, filename=None): """ Saves a configuration. :param filename: The name of the file. :type filename: str """ logger.info("Saving configuration.") logger.debug("Filename given : {}".format(filename)) current_config = self.config_list.currentIndex() nb_config = self.config_list.count() if current_config == nb_config-1 and not filename: self.save_config_as() else: if not filename: file = self.config_list.currentText() + ".json" file = os.path.join(settings.CONFIG_DIR, file) else: file = filename config = self.config_as_dict() with open(file, "w") as f: json.dump(config, f)
def remove_useless(self): """ Remove useless things in the code according to the UI options. """ rm_nums = self.chk_del_num.isChecked() rm_comm = self.chk_del_comments.isChecked() minimize = self.chk_optimize_bounding_box.isChecked() if minimize: x, y = self.get_minimize_bounding_box() r = '' last_g00 = None line = 0 for i in parse(self.gcode): if 'M' in i['name'] or 'G' in i['name']: if not rm_nums and 'N' in i['args']: r += 'N' + str(int(i['args']['N'])) + ' ' r += i['name'] + str(int(i['value'])) + ' ' for a in i['args']: if (not minimize and a in 'XY') or (a in 'ZIJKF'): r += a + str(i['args'][a]) + ' ' elif minimize and a == 'X': r += a + str(i['args'][a] - x) + ' ' elif minimize and a == 'Y': r += a + str(i['args'][a] - y) + ' ' if i['name'] == 'G' and i['value'] == 0: last_g00 = line r += '\n' line += 1 if 'comment' in i['args'] and not rm_comm: logger.debug(i) r += '(' + i['args']['comment'] + ')\n' line += 1 if last_g00: r = r.split('\n') r[last_g00] = 'G00 X0.0000 Y0.0000' r = '\n'.join(r) self.output.setText(r) self.gcode = r
def connectUi(self): """ Connects The UI signals and slots. """ logger.debug("Connecting Ui.") self.btn_serial_ports_list.clicked.connect(self.list_serials) self.btn_y_plus.pressed.connect(self.start_continuous_y_forward) self.btn_y_minus.pressed.connect(self.start_continuous_y_backward) self.btn_x_plus.pressed.connect(self.start_continuous_x_forward) self.btn_x_minus.pressed.connect(self.start_continuous_x_backward) self.btn_z_plus.pressed.connect(self.start_continuous_z_forward) self.btn_z_minus.pressed.connect(self.start_continuous_z_backward) self.btn_y_plus.released.connect(self.stop_y) self.btn_y_minus.released.connect(self.stop_y) self.btn_x_plus.released.connect(self.stop_x) self.btn_x_minus.released.connect(self.stop_x) self.btn_z_plus.released.connect(self.stop_z) self.btn_z_minus.released.connect(self.stop_z) self.btn_set_origin.clicked.connect(self.set_origin) self.btn_go_to_zero.clicked.connect(self.goto_origin) self.btn_emergency_stop.clicked.connect(self.emergency_stop) self.auto_cmd_type.currentIndexChanged.connect( self.manage_auto_cmd_number) self.btn_run_auto_cmd.clicked.connect(self.auto_cmd) self.manage_auto_cmd_number(self.auto_cmd_type.currentIndex()) self.btn_run_custom_cmd.clicked.connect(self.run_custom_cmd) self.chk_fake_serial.stateChanged.connect( self.manage_emulate_serial_port) self.btn_send_current_file.clicked.connect(self.send_file) self.btn_command.clicked.connect(self.send_cmd) self.config_list.currentIndexChanged.connect(self.update_config) self.btn_save_config.clicked.connect(self.save_config) self.btn_save_config_as.clicked.connect(self.save_config_as) self.btn_send_config.clicked.connect(self.send_config) self.btn_connect.clicked.connect(self.manage_connection) self.btn_file.clicked.connect(self.choose_file) self.btn_reload.clicked.connect(self.load_file) self.btn_save_file.clicked.connect(self.save_file) self.btn_save_as.clicked.connect(self.save_file_as) self.redraw.clicked.connect(self.draw_file) self.btn_close.clicked.connect(self.close_file) self.serial_manager.send_print.connect(self.print) self.btn_preprocessor.clicked.connect(self.run_preprocessor) self.reverse_display_x.clicked.connect(self.update_drawing) self.reverse_display_y.clicked.connect(self.update_drawing) self.reverse_display_z.clicked.connect(self.update_drawing) self.btn_license.clicked.connect(self.about_license) self.btn_about_qt.clicked.connect(self.about_qt) self.code_edit.cursorPositionChanged.connect( self.highlight_selected_path) self.view_3D.parse_error.connect(self.parse_error) self.serial_manager.serial_fatal_error.connect(self.manage_connection)
def get_minimize_bounding_box(self): """ Computes a new origin for the drawing. """ min_x, min_y = 0, 0 set_x, set_y = False, False x_o, y_o = 0, 0 x, y = 0, 0 z = 0 r = '' for t in parse(self.gcode): if t['name'] is not 'G': continue if 'Z' in t['args']: z = t['args']['Z'] if (z > 0 or t['value'] not in (1, 2, 3) or not ('X' in t['args'] or 'Y' in t['args'])): x_o = t['args'].get('X', x_o) y_o = t['args'].get('Y', y_o) continue if 'X' in t['args']: x = t['args']['X'] if not set_x: min_x = x set_x = True if x < min_x: logger.debug( "min_x becomes {x} from {t}".format(**locals())) min_x = min(min_x, x) if 'Y' in t['args']: y = t['args']['Y'] if not set_y: min_y = y set_y = True if y < min_y: logger.debug( "min_y becomes {y} from {t}".format(**locals())) min_y = min(min_y, y) if t['value'] in (2, 3): i = t['args'].get('I', 0) j = t['args'].get('J', 0) k = t['args'].get('K', 0) clockwise = (t['value'] == 2) segments = arc_to_segments((x_o, y_o), (i, j), (x, y), clockwise) for xc, yc in segments: if not set_x: min_x = xc set_x = True if not set_y: min_y = yc set_y = True if xc < min_x: logger.debug( "min_x becomes {xc} from {t}".format(**locals())) if yc < min_y: logger.debug( "min_y becomes {yc} from {t}".format(**locals())) min_x = min(min_x, xc) min_y = min(min_y, yc) x_o, y_o = x, y logger.debug("Minimizing by setting origin to {},{}".format( min_x, min_y)) return min_x, min_y