def program(self): try: if self.portaSerial.readAvailable: # Le conjunto de dados da porta serial read_buffer, command = self.portaSerial.read_buffer(['T', 't'], 40) msg = CanMessage(read_buffer, command) msg.print_msg() #print(read_buffer[0:8]) finally: # Chama de novo funcao prgram() depois de update_time segundos QtCore.QTimer.singleShot(update_time, self.program)
def parse_data(file_name): """Read the CSV data and parse it""" log_reader = csv.reader(open(file_name, 'r'), delimiter=',') for row in log_reader: # Data is logged in the format (timestamp, (can_id, data, len(data))). timestamp = row[0] can_id = int(row[1], 16) can_data = int(row[2], 16) data_len = int(row[3]) msg = CanMessage(can_id, can_data.to_bytes(data_len, 'big')) print(timestamp) msg.parse()
def run(self): """Start reading from the CAN data source. This reads messages from the data source, parses each one and prints to stdout, before logging into a log file until the process terminates. Args: None Returns: Does not return. """ while True: # CAN ID, data, DLC can_id, data = self.get_packet() if can_id not in self.masked: can_msg = CanMessage(can_id, data) can_msg.parse() logging.info('{},{},{}'.format(can_id, data, len(data))) #pylint: disable=logging-format-interpolation
def write_subscribe_files(self, header_path: str, source_path: str) -> None: """ :param path: The path to the directory to put the files into. The filenames will be subscribe.c and subscribe.h. :return: None """ include_guard_name = "__{0}_SUBSCRIBE_H_".format(self.name.upper()) header_f = open("{0}/subscribe.h".format(header_path), 'w') source_f = open("{0}/subscribe.c".format(source_path), 'w') header_f.write("/*\n" " * Generated by {0}. Do not manually edit this file.\n" " */\n".format(os.path.basename(__file__))) header_f.write("\n") header_f.write("#ifndef {0}\n" "#define {0}\n".format(include_guard_name)) header_f.write("\n") # header_f.write("#ifdef __cplusplus\n" # "extern \"C\" {\n" # "#endif\n") # header_f.write("\n") header_f.write("#include <stdint.h>\n" "#include <stdbool.h>\n" "#include \"stm32f4xx_hal.h\"\n" "#include \"stm32f4xx_hal_can.h\"\n" "#include \"task/task_can_bus.h\"\n" "#include <shared_can_defs.h>\n") header_f.write("\n") header_f.write("void eat_new_data(CAN_rx_t* recv);\n\n") source_f.write("/*\n" " * Generated by {0}. Do not manually edit this file.\n" " */\n".format(os.path.basename(__file__))) source_f.write("\n") source_f.write("#include <stdint.h>\n" "#include <stdbool.h>\n" "#include \"stm32f4xx_hal.h\"\n" "#include \"stm32f4xx_hal_can.h\"\n" "#include \"task/task_can_bus.h\"\n" "#include <shared_can_defs.h>\n" "#include <string.h>\n" "#include \"util.h\"\n" "#include \"subscribe.h\"\n") source_f.write("\n") for msg in self.subscribe: struct_type = CanMessage.make_c_data_struct_type(msg) struct_name = CanMessage.make_c_data_struct_instance_name(msg) # Write the declaration of the data variable header_f.write("extern {0} {1};\n".format(struct_type, struct_name)) source_f.write("{0} {1};\n".format(struct_type, struct_name)) # Write the declaration of the timestamp variable header_f.write("extern uint32_t {0}_rx_time;\n".format(msg)) source_f.write("uint32_t {0}_rx_time;\n".format(msg)) # Blank line for spacing header_f.write("\n") source_f.write("\n") # header_f.write("#ifdef __cplusplus\n" # "}\n" # "#endif\n") # header_f.write("\n") header_f.write("#endif\n") source_f.write("void eat_new_data(CAN_rx_t* recv)\n" "{\n" "\tvoid* dest = 0;\n" "\tuint32_t* time_address = 0;\n") if_block_template = "if (recv->header.ExtId == {0})\n" \ "\t{{\n" \ "\t\tdest = &{1};\n" \ "\t\ttime_address = &{2};\n" \ "\t}}" blocks = [] for msg in self.subscribe: id_name = "CAN_ID_" + msg struct_name = CanMessage.make_c_data_struct_instance_name(msg) time_name = msg + "_rx_time" blocks.append(if_block_template.format(id_name, struct_name, time_name)) source_f.write("\n\t") source_f.write("\n\telse ".join(blocks)) source_f.write("\n\n") source_f.write("\tif (dest != 0) {\n" "\t\tmemcpy(dest, &(recv->data), recv->header.DLC);\n" "\t\t*time_address = get_timestamp();\n" "\t}") source_f.write("\n}\n") header_f.close() source_f.close()
def test_get_c_data_struct_name(self): test_msg = CanMessage(msg_id=0x1, priority=0, interval=50, fields=None) test_name = "TEST_MSG" test_msg.set_name(test_name) assert test_msg.gen_c_data_struct_name() == "CAN_MSG_{0}_T".format(test_name)
def test_get_c_id_define(self): test_msg = CanMessage(msg_id=0x1, priority=0, interval=50, fields=None) test_name = "TEST_MSG" test_msg.set_name(test_name) assert test_msg.gen_c_id_define() == "#define CAN_ID_{0} ({1})".format(test_name, "0x1")