def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_dialog() self.ui.setupUi(self) self.start_state = False self.target_device = None self.rtt2uart = None self.ui.comboBox_Interface.addItem("JTAG") self.ui.comboBox_Interface.addItem("SWD") self.ui.comboBox_Interface.addItem("cJTAG") self.ui.comboBox_Interface.addItem("FINE") for i in range(len(speed_list)): self.ui.comboBox_Speed.addItem(str(speed_list[i]) + " kHz") for i in range(len(baudrate_list)): self.ui.comboBox_baudrate.addItem(str(baudrate_list[i])) self.port_scan() self.settings = {'device': [], 'device_index': 0, 'interface': 0, 'speed': 0, 'port': 0, 'buadrate': 0} # 检查是否存在上次配置,存在则加载 if os.path.exists('settings') == True: with open('settings', 'rb') as f: self.settings = pickle.load(f) f.close() # 应用上次配置 if len(self.settings['device']): self.ui.comboBox_Device.addItems(self.settings['device']) self.target_device = self.settings['device'][self.settings['device_index']] self.ui.comboBox_Device.setCurrentIndex( self.settings['device_index']) self.ui.comboBox_Interface.setCurrentIndex( self.settings['interface']) self.ui.comboBox_Speed.setCurrentIndex(self.settings['speed']) self.ui.comboBox_Port.setCurrentIndex(self.settings['port']) self.ui.comboBox_baudrate.setCurrentIndex( self.settings['buadrate']) # 信号-槽 self.ui.pushButton_Start.clicked.connect(self.start) self.ui.pushButton_scan.clicked.connect(self.port_scan) self.ui.pushButton_Selete_Device.clicked.connect( self.target_device_selete) self.ui.comboBox_Device.currentIndexChanged.connect( self.device_change_slot) self.ui.comboBox_Interface.currentIndexChanged.connect( self.interface_change_slot) self.ui.comboBox_Speed.currentIndexChanged.connect( self.speed_change_slot) self.ui.comboBox_Port.currentIndexChanged.connect( self.port_change_slot) self.ui.comboBox_baudrate.currentIndexChanged.connect( self.buadrate_change_slot)
class MainWindow(QDialog): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_dialog() self.ui.setupUi(self) self.start_state = False self.target_device = None self.rtt2uart = None self.ui.comboBox_Interface.addItem("JTAG") self.ui.comboBox_Interface.addItem("SWD") self.ui.comboBox_Interface.addItem("cJTAG") self.ui.comboBox_Interface.addItem("FINE") for i in range(len(speed_list)): self.ui.comboBox_Speed.addItem(str(speed_list[i]) + " kHz") for i in range(len(baudrate_list)): self.ui.comboBox_baudrate.addItem(str(baudrate_list[i])) self.port_scan() self.settings = { 'device': [], 'device_index': 0, 'interface': 0, 'speed': 0, 'port': 0, 'buadrate': 0 } # 检查是否存在上次配置,存在则加载 if os.path.exists('settings') == True: with open('settings', 'rb') as f: self.settings = pickle.load(f) f.close() # 应用上次配置 if len(self.settings['device']): self.ui.comboBox_Device.addItems(self.settings['device']) self.target_device = self.settings['device'][ self.settings['device_index']] self.ui.comboBox_Device.setCurrentIndex( self.settings['device_index']) self.ui.comboBox_Interface.setCurrentIndex( self.settings['interface']) self.ui.comboBox_Speed.setCurrentIndex(self.settings['speed']) self.ui.comboBox_Port.setCurrentIndex(self.settings['port']) self.ui.comboBox_baudrate.setCurrentIndex( self.settings['buadrate']) # 信号-槽 self.ui.pushButton_Start.clicked.connect(self.start) self.ui.pushButton_scan.clicked.connect(self.port_scan) self.ui.pushButton_Selete_Device.clicked.connect( self.target_device_selete) self.ui.comboBox_Device.currentIndexChanged.connect( self.device_change_slot) self.ui.comboBox_Interface.currentIndexChanged.connect( self.interface_change_slot) self.ui.comboBox_Speed.currentIndexChanged.connect( self.speed_change_slot) self.ui.comboBox_Port.currentIndexChanged.connect( self.port_change_slot) self.ui.comboBox_baudrate.currentIndexChanged.connect( self.buadrate_change_slot) # 禁止调整窗口大小时点击关闭按钮没有反应 # self.setFixedSize(self.width(), self.height()) def __del__(self): if self.rtt2uart is not None and self.start_state == True: self.rtt2uart.stop() # 保存当前配置 with open('settings', 'wb') as f: pickle.dump(self.settings, f) f.close() def port_scan(self): # 检测所有串口,将信息存储在字典中 self.com_dict = {} port_list = list(serial.tools.list_ports.comports()) self.ui.comboBox_Port.clear() for port in port_list: self.com_dict["%s" % port[0]] = "%s" % port[1] self.ui.comboBox_Port.addItem(port[0]) def start(self): if self.start_state == False: try: if self.target_device is not None: selete_interface = self.ui.comboBox_Interface.currentText() if (selete_interface == 'JTAG'): device_interface = pylink.enums.JLinkInterfaces.JTAG elif (selete_interface == 'SWD'): device_interface = pylink.enums.JLinkInterfaces.SWD elif (selete_interface == 'cJTAG'): device_interface = None elif (selete_interface == 'FINE'): device_interface = pylink.enums.JLinkInterfaces.FINE self.rtt2uart = rtt_to_serial( self.target_device, self.ui.comboBox_Port.currentText(), self.ui.comboBox_baudrate.currentText(), device_interface, speed_list[self.ui.comboBox_Speed.currentIndex()]) self.rtt2uart.start() # 启动后不能再进行配置 self.ui.comboBox_Device.setEnabled(False) self.ui.pushButton_Selete_Device.setEnabled(False) self.ui.comboBox_Interface.setEnabled(False) self.ui.comboBox_Speed.setEnabled(False) self.ui.comboBox_Port.setEnabled(False) self.ui.comboBox_baudrate.setEnabled(False) self.ui.pushButton_scan.setEnabled(False) else: raise Exception("Please selete the target device !") except Exception as errors: msgBox = QMessageBox() msgBox.setIcon(QMessageBox.Warning) msgBox.setText(str(errors)) msgBox.setWindowTitle('Error') msgBox.exec_() else: self.start_state = True self.ui.pushButton_Start.setText("Stop") else: try: # 停止后才能再次配置 self.ui.comboBox_Device.setEnabled(True) self.ui.pushButton_Selete_Device.setEnabled(True) self.ui.comboBox_Interface.setEnabled(True) self.ui.comboBox_Speed.setEnabled(True) self.ui.comboBox_Port.setEnabled(True) self.ui.comboBox_baudrate.setEnabled(True) self.ui.pushButton_scan.setEnabled(True) self.rtt2uart.stop() self.start_state = False self.ui.pushButton_Start.setText("Start") except: logger.error('Stop rtt2uart failed', exc_info=True) pass def target_device_selete(self): device_ui = DeviceSeleteDialog() device_ui.exec_() self.target_device = device_ui.get_target_device() if self.target_device not in self.settings['device']: self.settings['device'].append(self.target_device) self.ui.comboBox_Device.addItem(self.target_device) self.ui.comboBox_Device.setCurrentIndex( len(self.settings['device']) - 1) def device_change_slot(self, index): self.settings['device_index'] = index def interface_change_slot(self, index): self.settings['interface'] = index def speed_change_slot(self, index): self.settings['speed'] = index def port_change_slot(self, index): self.settings['port'] = index def buadrate_change_slot(self, index): self.settings['buadrate'] = index
def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_dialog() self.ui.setupUi(self) self.setWindowIcon(QIcon(resource_path(r'swap_horiz_16px.ico'))) self.setting_file_path = os.path.join(os.getcwd(), "settings") self.start_state = False self.target_device = None self.rtt2uart = None self.ui.comboBox_Interface.addItem("JTAG") self.ui.comboBox_Interface.addItem("SWD") self.ui.comboBox_Interface.addItem("cJTAG") self.ui.comboBox_Interface.addItem("FINE") for i in range(len(speed_list)): self.ui.comboBox_Speed.addItem(str(speed_list[i]) + " kHz") for i in range(len(baudrate_list)): self.ui.comboBox_baudrate.addItem(str(baudrate_list[i])) self.port_scan() self.settings = { 'device': [], 'device_index': 0, 'interface': 0, 'speed': 0, 'port': 0, 'buadrate': 0 } # 检查是否存在上次配置,存在则加载 if os.path.exists(self.setting_file_path) == True: with open(self.setting_file_path, 'rb') as f: self.settings = pickle.load(f) f.close() # 应用上次配置 if len(self.settings['device']): self.ui.comboBox_Device.addItems(self.settings['device']) self.target_device = self.settings['device'][ self.settings['device_index']] self.ui.comboBox_Device.setCurrentIndex( self.settings['device_index']) self.ui.comboBox_Interface.setCurrentIndex( self.settings['interface']) self.ui.comboBox_Speed.setCurrentIndex(self.settings['speed']) self.ui.comboBox_Port.setCurrentIndex(self.settings['port']) self.ui.comboBox_baudrate.setCurrentIndex( self.settings['buadrate']) else: logger.info('Setting file not exist', exc_info=True) self.ui.comboBox_Interface.setCurrentIndex(1) self.settings['interface'] = 1 self.ui.comboBox_Speed.setCurrentIndex(19) self.settings['speed'] = 19 self.ui.comboBox_baudrate.setCurrentIndex(16) self.settings['buadrate'] = 16 # 信号-槽 self.ui.pushButton_Start.clicked.connect(self.start) self.ui.pushButton_scan.clicked.connect(self.port_scan) self.ui.pushButton_Selete_Device.clicked.connect( self.target_device_selete) self.ui.comboBox_Device.currentIndexChanged.connect( self.device_change_slot) self.ui.comboBox_Interface.currentIndexChanged.connect( self.interface_change_slot) self.ui.comboBox_Speed.currentIndexChanged.connect( self.speed_change_slot) self.ui.comboBox_Port.currentIndexChanged.connect( self.port_change_slot) self.ui.comboBox_baudrate.currentIndexChanged.connect( self.buadrate_change_slot)
class MainWindow(QDialog): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_dialog() self.ui.setupUi(self) self.setWindowIcon(QIcon(resource_path(r'swap_horiz_16px.ico'))) self.setting_file_path = os.path.join(os.getcwd(), "settings") self.start_state = False self.target_device = None self.rtt2uart = None self.ui.comboBox_Interface.addItem("JTAG") self.ui.comboBox_Interface.addItem("SWD") self.ui.comboBox_Interface.addItem("cJTAG") self.ui.comboBox_Interface.addItem("FINE") for i in range(len(speed_list)): self.ui.comboBox_Speed.addItem(str(speed_list[i]) + " kHz") for i in range(len(baudrate_list)): self.ui.comboBox_baudrate.addItem(str(baudrate_list[i])) self.port_scan() self.settings = { 'device': [], 'device_index': 0, 'interface': 0, 'speed': 0, 'port': 0, 'buadrate': 0 } # 检查是否存在上次配置,存在则加载 if os.path.exists(self.setting_file_path) == True: with open(self.setting_file_path, 'rb') as f: self.settings = pickle.load(f) f.close() # 应用上次配置 if len(self.settings['device']): self.ui.comboBox_Device.addItems(self.settings['device']) self.target_device = self.settings['device'][ self.settings['device_index']] self.ui.comboBox_Device.setCurrentIndex( self.settings['device_index']) self.ui.comboBox_Interface.setCurrentIndex( self.settings['interface']) self.ui.comboBox_Speed.setCurrentIndex(self.settings['speed']) self.ui.comboBox_Port.setCurrentIndex(self.settings['port']) self.ui.comboBox_baudrate.setCurrentIndex( self.settings['buadrate']) else: logger.info('Setting file not exist', exc_info=True) self.ui.comboBox_Interface.setCurrentIndex(1) self.settings['interface'] = 1 self.ui.comboBox_Speed.setCurrentIndex(19) self.settings['speed'] = 19 self.ui.comboBox_baudrate.setCurrentIndex(16) self.settings['buadrate'] = 16 # 信号-槽 self.ui.pushButton_Start.clicked.connect(self.start) self.ui.pushButton_scan.clicked.connect(self.port_scan) self.ui.pushButton_Selete_Device.clicked.connect( self.target_device_selete) self.ui.comboBox_Device.currentIndexChanged.connect( self.device_change_slot) self.ui.comboBox_Interface.currentIndexChanged.connect( self.interface_change_slot) self.ui.comboBox_Speed.currentIndexChanged.connect( self.speed_change_slot) self.ui.comboBox_Port.currentIndexChanged.connect( self.port_change_slot) self.ui.comboBox_baudrate.currentIndexChanged.connect( self.buadrate_change_slot) def closeEvent(self, e): if self.rtt2uart is not None and self.start_state == True: self.rtt2uart.stop() # 保存当前配置 with open(self.setting_file_path, 'wb') as f: pickle.dump(self.settings, f) f.close() e.accept() def port_scan(self): port_list = list(serial.tools.list_ports.comports()) self.ui.comboBox_Port.clear() port_list.sort() for port in port_list: try: s = serial.Serial(port[0]) s.close() self.ui.comboBox_Port.addItem(port[0]) except (OSError, serial.SerialException): pass def start(self): if self.start_state == False: try: if self.target_device is not None: selete_interface = self.ui.comboBox_Interface.currentText() if (selete_interface == 'JTAG'): device_interface = pylink.enums.JLinkInterfaces.JTAG elif (selete_interface == 'SWD'): device_interface = pylink.enums.JLinkInterfaces.SWD elif (selete_interface == 'cJTAG'): device_interface = None elif (selete_interface == 'FINE'): device_interface = pylink.enums.JLinkInterfaces.FINE self.rtt2uart = rtt_to_serial( self.target_device, self.ui.comboBox_Port.currentText(), self.ui.comboBox_baudrate.currentText(), device_interface, speed_list[self.ui.comboBox_Speed.currentIndex()], self.ui.checkBox_resettarget.isChecked()) self.rtt2uart.start() # 启动后不能再进行配置 self.ui.comboBox_Device.setEnabled(False) self.ui.pushButton_Selete_Device.setEnabled(False) self.ui.comboBox_Interface.setEnabled(False) self.ui.comboBox_Speed.setEnabled(False) self.ui.comboBox_Port.setEnabled(False) self.ui.comboBox_baudrate.setEnabled(False) self.ui.pushButton_scan.setEnabled(False) else: raise Exception("Please selete the target device !") except Exception as errors: QMessageBox.critical(self, "Errors", str(errors)) else: self.start_state = True self.ui.pushButton_Start.setText("Stop") else: try: # 停止后才能再次配置 self.ui.comboBox_Device.setEnabled(True) self.ui.pushButton_Selete_Device.setEnabled(True) self.ui.comboBox_Interface.setEnabled(True) self.ui.comboBox_Speed.setEnabled(True) self.ui.comboBox_Port.setEnabled(True) self.ui.comboBox_baudrate.setEnabled(True) self.ui.pushButton_scan.setEnabled(True) self.rtt2uart.stop() self.start_state = False self.ui.pushButton_Start.setText("Start") except: logger.error('Stop rtt2uart failed', exc_info=True) pass def target_device_selete(self): device_ui = DeviceSeleteDialog() device_ui.exec_() self.target_device = device_ui.get_target_device() if self.target_device not in self.settings['device']: self.settings['device'].append(self.target_device) self.ui.comboBox_Device.addItem(self.target_device) self.ui.comboBox_Device.setCurrentIndex( len(self.settings['device']) - 1) def device_change_slot(self, index): self.settings['device_index'] = index self.target_device = self.ui.comboBox_Device.currentText() def interface_change_slot(self, index): self.settings['interface'] = index def speed_change_slot(self, index): self.settings['speed'] = index def port_change_slot(self, index): self.settings['port'] = index def buadrate_change_slot(self, index): self.settings['buadrate'] = index
class MainWindow(QDialog): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_dialog() self.ui.setupUi(self) self.setWindowIcon(QIcon(":/swap_horiz_16px.ico")) self.setting_file_path = os.path.join(os.getcwd(), "settings") self.start_state = False self.target_device = None self.rtt2uart = None self.connect_type = None # 默认Existing Session方式接入使能Auto reconnect self.ui.checkBox__auto.setChecked(True) # 默认选择'USB'方式接入 self.ui.radioButton_usb.setChecked(True) self.usb_selete_slot() self.ui.comboBox_Interface.addItem("JTAG") self.ui.comboBox_Interface.addItem("SWD") self.ui.comboBox_Interface.addItem("cJTAG") self.ui.comboBox_Interface.addItem("FINE") for i in range(len(speed_list)): self.ui.comboBox_Speed.addItem(str(speed_list[i]) + " kHz") for i in range(len(baudrate_list)): self.ui.comboBox_baudrate.addItem(str(baudrate_list[i])) self.port_scan() self.settings = { 'device': [], 'device_index': 0, 'interface': 0, 'speed': 0, 'port': 0, 'buadrate': 0 } # 检查是否存在上次配置,存在则加载 if os.path.exists(self.setting_file_path) == True: with open(self.setting_file_path, 'rb') as f: self.settings = pickle.load(f) f.close() # 应用上次配置 if len(self.settings['device']): self.ui.comboBox_Device.addItems(self.settings['device']) self.target_device = self.settings['device'][ self.settings['device_index']] self.ui.comboBox_Device.setCurrentIndex( self.settings['device_index']) self.ui.comboBox_Interface.setCurrentIndex( self.settings['interface']) self.ui.comboBox_Speed.setCurrentIndex(self.settings['speed']) self.ui.comboBox_Port.setCurrentIndex(self.settings['port']) self.ui.comboBox_baudrate.setCurrentIndex( self.settings['buadrate']) else: logger.info('Setting file not exist', exc_info=True) self.ui.comboBox_Interface.setCurrentIndex(1) self.settings['interface'] = 1 self.ui.comboBox_Speed.setCurrentIndex(19) self.settings['speed'] = 19 self.ui.comboBox_baudrate.setCurrentIndex(16) self.settings['buadrate'] = 16 # 信号-槽 self.ui.pushButton_Start.clicked.connect(self.start) self.ui.pushButton_scan.clicked.connect(self.port_scan) self.ui.pushButton_Selete_Device.clicked.connect( self.target_device_selete) self.ui.comboBox_Device.currentIndexChanged.connect( self.device_change_slot) self.ui.comboBox_Interface.currentIndexChanged.connect( self.interface_change_slot) self.ui.comboBox_Speed.currentIndexChanged.connect( self.speed_change_slot) self.ui.comboBox_Port.currentIndexChanged.connect( self.port_change_slot) self.ui.comboBox_baudrate.currentIndexChanged.connect( self.buadrate_change_slot) self.ui.checkBox_serialno.stateChanged.connect( self.serial_no_change_slot) self.ui.radioButton_usb.clicked.connect(self.usb_selete_slot) self.ui.radioButton_existing.clicked.connect( self.existing_session_selete_slot) def closeEvent(self, e): if self.rtt2uart is not None and self.start_state == True: self.rtt2uart.stop() # 保存当前配置 with open(self.setting_file_path, 'wb') as f: pickle.dump(self.settings, f) f.close() e.accept() def port_scan(self): port_list = list(serial.tools.list_ports.comports()) self.ui.comboBox_Port.clear() port_list.sort() for port in port_list: try: s = serial.Serial(port[0]) s.close() self.ui.comboBox_Port.addItem(port[0]) except (OSError, serial.SerialException): pass def start(self): if self.start_state == False: logger.debug('click start button') try: device_interface = None # USB或者TCP/IP方式接入需要选择配置 if not self.ui.radioButton_existing.isChecked(): if self.target_device is not None: selete_interface = self.ui.comboBox_Interface.currentText( ) if (selete_interface == 'JTAG'): device_interface = pylink.enums.JLinkInterfaces.JTAG elif (selete_interface == 'SWD'): device_interface = pylink.enums.JLinkInterfaces.SWD elif (selete_interface == 'cJTAG'): device_interface = None elif (selete_interface == 'FINE'): device_interface = pylink.enums.JLinkInterfaces.FINE else: device_interface = pylink.enums.JLinkInterfaces.SWD # 启动后不能再进行配置 self.ui.comboBox_Device.setEnabled(False) self.ui.pushButton_Selete_Device.setEnabled(False) self.ui.comboBox_Interface.setEnabled(False) self.ui.comboBox_Speed.setEnabled(False) self.ui.comboBox_Port.setEnabled(False) self.ui.comboBox_baudrate.setEnabled(False) self.ui.pushButton_scan.setEnabled(False) else: raise Exception("Please selete the target device !") # 获取接入方式的参数 if self.ui.radioButton_usb.isChecked( ) and self.ui.checkBox_serialno.isChecked(): connect_para = self.ui.lineEdit_serialno.text() elif self.ui.radioButton_tcpip.isChecked(): connect_para = self.ui.lineEdit_ip.text() elif self.ui.radioButton_existing.isChecked(): connect_para = self.ui.checkBox__auto.isChecked() else: connect_para = None self.rtt2uart = rtt_to_serial( self.connect_type, connect_para, self.target_device, self.ui.comboBox_Port.currentText(), self.ui.comboBox_baudrate.currentText(), device_interface, speed_list[self.ui.comboBox_Speed.currentIndex()], self.ui.checkBox_resettarget.isChecked()) self.rtt2uart.start() except Exception as errors: QMessageBox.critical(self, "Errors", str(errors)) else: self.start_state = True self.ui.pushButton_Start.setText("Stop") else: logger.debug('click stop button') try: # Existing方式不需要选择配置,继续禁用,不恢复 if self.ui.radioButton_existing.isChecked() == False: # 停止后才能再次配置 self.ui.comboBox_Device.setEnabled(True) self.ui.pushButton_Selete_Device.setEnabled(True) self.ui.comboBox_Interface.setEnabled(True) self.ui.comboBox_Speed.setEnabled(True) self.ui.comboBox_Port.setEnabled(True) self.ui.comboBox_baudrate.setEnabled(True) self.ui.pushButton_scan.setEnabled(True) self.rtt2uart.stop() self.start_state = False self.ui.pushButton_Start.setText("Start") except: logger.error('Stop rtt2uart failed', exc_info=True) pass def target_device_selete(self): device_ui = DeviceSeleteDialog() device_ui.exec_() self.target_device = device_ui.get_target_device() if self.target_device not in self.settings['device']: self.settings['device'].append(self.target_device) self.ui.comboBox_Device.addItem(self.target_device) self.ui.comboBox_Device.setCurrentIndex( len(self.settings['device']) - 1) def device_change_slot(self, index): self.settings['device_index'] = index self.target_device = self.ui.comboBox_Device.currentText() def interface_change_slot(self, index): self.settings['interface'] = index def speed_change_slot(self, index): self.settings['speed'] = index def port_change_slot(self, index): self.settings['port'] = index def buadrate_change_slot(self, index): self.settings['buadrate'] = index def serial_no_change_slot(self): if self.ui.checkBox_serialno.isChecked(): self.ui.lineEdit_serialno.setVisible(True) else: self.ui.lineEdit_serialno.setVisible(False) def usb_selete_slot(self): self.connect_type = 'USB' self.ui.checkBox__auto.setVisible(False) self.ui.lineEdit_ip.setVisible(False) self.ui.checkBox_serialno.setVisible(True) self.serial_no_change_slot() # 通过usb方式接入,以下功能需要选择,恢复使用 self.ui.comboBox_Device.setEnabled(True) self.ui.pushButton_Selete_Device.setEnabled(True) self.ui.comboBox_Interface.setEnabled(True) self.ui.comboBox_Speed.setEnabled(True) self.ui.checkBox_resettarget.setEnabled(True) def existing_session_selete_slot(self): self.connect_type = 'EXISTING' self.ui.checkBox_serialno.setVisible(False) self.ui.lineEdit_serialno.setVisible(False) self.ui.lineEdit_ip.setVisible(False) self.ui.checkBox__auto.setVisible(True) # 通过existing_session方式接入时,以下功能无效,禁止使用 self.ui.comboBox_Device.setEnabled(False) self.ui.pushButton_Selete_Device.setEnabled(False) self.ui.comboBox_Interface.setEnabled(False) self.ui.comboBox_Speed.setEnabled(False) self.ui.checkBox_resettarget.setEnabled(False) self.ui.checkBox_resettarget.setChecked(False)