Exemple #1
0
 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
Exemple #2
0
 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
Exemple #3
0
 def testSplitWithBytesTwice(self):
     teststr2 = "$1Aqwer$23"
     expected = ["", "$1A", "qwer", "$23", ""]
     self.assertEqual(common_functions.split_with_bytes(teststr2), expected)
Exemple #4
0
 def testSplitWithBytesNo(self):
     teststr3 = "&12qwer$1qwer"
     self.assertEqual(common_functions.split_with_bytes(teststr3),
                      [teststr3])
Exemple #5
0
 def testSplitWithBytesSimple(self):
     teststr1 = "qwer$1Aqwer"
     expected = ["qwer", "$1A", "qwer"]
     self.assertEqual(common_functions.split_with_bytes(teststr1), expected)