def process_read_data(self, data: bytes): """ functions to process byte data and show it :param data: byte data from comport :return: """ self.counter += len(data) read = self.decode_input(data) if read: # if timestamps if self.text_settings.timestamps: delta = datetime.datetime.now() - self.start_time lines = read.split('\r') start = str(delta) + ': ' if not self.time_shown else "" if len(lines) == 1 or lines[-1] not in ['', '\n']: self.time_shown = True else: self.time_shown = False if self.CBHex.isChecked(): lines = [common_functions.hexify(line) for line in lines] read_to_show = ('\r' + str(delta) + ': ').join(lines) if lines[-1] not in ['', '\n'] else \ ('\r' + str(delta) + ': ').join(lines[:-1]) + lines[-1] read_to_show = start + read_to_show else: read_to_show: str = common_functions.hexify( read) if self.CBHex.isChecked() else read.replace( "\n", "") self.move_cursor_and_write( read_to_show, QtGui.QColor(*self.colors['font-receive'])) self.show_graph_data(read) self.LblCounterData.setText(str(self.counter)) if self.text_settings.scroll: scroll = self.TxtBuffer.verticalScrollBar().maximum() self.TxtBuffer.verticalScrollBar().setValue(scroll)
def write_data(self, text: Union[str, bytes], encode=True, hexes=True) -> int: """ writes data to comport :param hexes: convert hexes or not :param encode: True if we need to encode :param text: text to send :return: -1 if failed 0 if ok """ commands = common_functions.split_with_bytes(text) if hexes and encode else [text] res_command = bytes() for command in commands: if common_functions.is_byte(command): add_command = bytes.fromhex(command[1:]) if encode else command else: add_command = bytes(command, encoding='utf-8') if encode else command res_command += add_command res = self.serial_port.write(res_command) self.serial_port.flush() if res != len(res_command): common_functions.error_message('Data was not sent correctly') return -1 if self.text_settings.show_sent and encode: self.TxtBuffer.setStyleSheet("background-color:rgb%s" % str(self.colors['background-color'])) self.TxtBuffer.setTextBackgroundColor(QtGui.QColor(*self.colors['background-color'])) for command in commands: if common_functions.is_byte(command): self.TxtBuffer.setTextColor(QtGui.QColor(*self.colors['bytes-color'])) else: self.TxtBuffer.setTextColor(QtGui.QColor(*self.colors['font-transmit'])) command_to_show: str = common_functions.hexify(command) if self.CBHex.isChecked() else command self.TxtBuffer.insertPlainText(command_to_show) if self.text_settings.scroll: scroll = self.TxtBuffer.verticalScrollBar().maximum() self.TxtBuffer.verticalScrollBar().setValue(scroll) return 0
def read_data(self): """ reads data from comport :return: """ read: str = "" try: read += self.serial_port.readAll().data().decode('utf-8') self.statusbar.clearMessage() except UnicodeDecodeError: self.statusbar.showMessage("Unicode decode error") if read: self.TxtBuffer.setTextBackgroundColor(QtGui.QColor(*self.colors['background-color'])) self.TxtBuffer.setTextColor(QtGui.QColor(*self.colors['font-receive'])) self.counter += len(read) self.LblCounterData.setText(str(self.counter)) read_to_show: str = common_functions.hexify(read) if self.CBHex.isChecked() else read if self.text_settings.timestamps: delta = datetime.datetime.now() - self.start_time if self.counter == 0: read_to_show = '\n' + str(delta) + ': ' + read_to_show read_to_show = read_to_show.replace("\r", "\r\n%s: " % delta) self.TxtBuffer.insertPlainText(read_to_show) if self.text_settings.scroll: scroll = self.TxtBuffer.verticalScrollBar().maximum() self.TxtBuffer.verticalScrollBar().setValue(scroll)
def write_data(self, text: Union[str, bytes], encode=True, hexes=True, use_variables=True) -> int: """ writes data to comport :param use_variables: use variables replace or not :param hexes: convert hexes or not :param encode: True if we need to encode :param text: text to send :return: -1 if failed 0 if ok """ # saving settings before writing self.settings_form = settings.Settings(self.port_settings, self.text_settings, self.colors, self.current_font) self.settings_form.save_settings() if use_variables: text = common_functions.replace_variables(text, self.var_form.var_dict) commands = common_functions.split_with_bytes( text) if hexes and encode else [text] res_command = bytes() for command in commands: if common_functions.is_byte(command): add_command = bytes.fromhex(command[1:]) if encode else command else: add_command = bytes(command, encoding='utf-8') if encode else command res_command += add_command if len(res_command) % data_types.endpoint != 0: res_write = self.write_to_port(res_command) if res_write == -1: return -1 else: # last packet is lost if its lendth is even to endpoint res_first = self.write_to_port(res_command[:-data_types.endpoint // 2]) res_second = self.write_to_port(res_command[-data_types.endpoint // 2:]) if res_first == -1 or res_second == -1: return -1 if self.text_settings.show_sent and encode: for command in commands: if common_functions.is_byte(command): color = QtGui.QColor(*self.colors['bytes-color']) else: color = QtGui.QColor(*self.colors['font-transmit']) command_to_show: str = common_functions.hexify( command) if self.CBHex.isChecked() else command self.move_cursor_and_write(command_to_show, color) return 0
def read_data(self): """ reads data from comport :return: """ read: str = "" data: bytes = self.serial_port.readAll().data() self.TxtBuffer.setTextBackgroundColor( QtGui.QColor(*self.colors['background-color'])) # decoding block try: read += data.decode('ascii') self.statusbar.clearMessage() except UnicodeDecodeError: if self.text_settings.decode == 1: for byte in data: try: if byte < 128: char = chr(byte) self.TxtBuffer.setTextColor( QtGui.QColor(*self.colors['font-receive'])) else: char = '?' self.TxtBuffer.setTextColor(QtGui.QColor( 255, 0, 0)) except ValueError: char = '?' self.TxtBuffer.setTextColor(QtGui.QColor(255, 0, 0)) self.TxtBuffer.insertPlainText(char) self.counter += 1 if self.text_settings.decode == 2: try: read = data.decode('cp1251') except UnicodeDecodeError: self.counter += len(data) self.statusbar.showMessage("Unicode decode error") if read: self.TxtBuffer.setTextColor( QtGui.QColor(*self.colors['font-receive'])) self.counter += len(read) # if timestamps if self.text_settings.timestamps: delta = datetime.datetime.now() - self.start_time lines = read.split('\r') start = str(delta) + ': ' if not self.time_shown else "" if len(lines) == 1 or lines[-1] not in ['', '\n']: self.time_shown = True else: self.time_shown = False if self.CBHex.isChecked(): lines = [common_functions.hexify(line) for line in lines] read_to_show = ('\r' + str(delta) + ': ').join(lines) if lines[-1] not in ['', '\n'] else \ ('\r' + str(delta) + ': ').join(lines[:-1]) + lines[-1] read_to_show = start + read_to_show else: read_to_show: str = common_functions.hexify( read) if self.CBHex.isChecked() else read.replace( "\n", "") self.TxtBuffer.insertPlainText(read_to_show) if self.graph and self.graph_form: lines = (self.tail + read).split('\r') logger.debug(lines) self.tail = lines[-1] # lines = (self.tail + stub[self.count]).split('\r') # self.count += 1 if len(lines) > 1: for line in lines[:-1]: logger.debug(line) if line.strip(): try: data_graph = line.strip().split(';') data_x = float(data_graph[0].strip()) data_y = [ float(value) for value in data_graph[1:] if value.strip() ] if self.graph_form.data_x: self.graph_form.update_plot_data( data_x, data_y) else: self.graph_form.create_graph( data_x, data_y) self.graph_form.show() except (ValueError, IndexError): pass self.LblCounterData.setText(str(self.counter)) if self.text_settings.scroll: scroll = self.TxtBuffer.verticalScrollBar().maximum() self.TxtBuffer.verticalScrollBar().setValue(scroll)
def testHexify(self): self.assertTrue(common_functions.hexify('ping\r\n'), '0x700x690x6e0x67')