Skip to content

Commit

Permalink
Parsed Correctly for test log
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiran Jojare committed Jul 19, 2024
1 parent df87c04 commit ed611b6
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 21 deletions.
72 changes: 60 additions & 12 deletions test/data/test_log.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,61 @@
WinNo Identifier Flg Dlc Name Time Dir
CAN 1 123456 X 8 ECU1.Message1 44709.992868 R
-> Signal1 100.0000 units
-> Signal2 200.0000 units
CAN 1 123456 X 8 ECU1.Message1 44710.002938 R
-> Signal1 110.0000 units
-> Signal2 210.0000 units
CAN 1 654321 X 8 ECU1.Message2 44710.012918 R
-> Signal3 300.0000 units
-> Signal4 400.0000 units
CAN 1 654321 X 8 ECU1.Message2 44710.022908 R
-> Signal3 310.0000 units
-> Signal4 410.0000 units
CAN 1 000064 X 8 AccelerationCommandTest 44709.992868 R
-> ACC_AccelerationTest 0.0000 Bool
-> ACC_CurrentTest 1.0000 Bool
-> ACC_SurgeTest 12345.0000 RPM
-> ACC_AccelTest 67890.0000 RPM/sec
-> ACC_MaxCurrentTest 100.0000 Amp
CAN 1 000065 X 8 FeedbackTest 44710.002938 R
-> FB_SafetyModeTest 1.0000 Enum
-> FB_TorqueTest 2345.6000 Nm
-> FB_VoltageTest 3456.7000 Volts
-> FB_CurrentTest 4567.8000 Amps
-> FB_SpeedTest 5678.9000 rpm
CAN 1 000066 X 8 SafetyFeedbackTest 44710.012918 R
-> SAF_SafetyModeTest 0.0000 Enum
-> SAF_TorqueTest 567.0000 Nm
-> SAF_VoltageTest 6789.0000 Volts
-> SAF_CurrentTest 7890.1000 Amps
-> SAF_SpeedTest 8901.2000 rpm
CAN 1 000067 X 8 CommandTest 44710.022908 R
-> CMD_ControlModeTest 2.0000 Enum
-> CMD_CommandCounterTest 3.0000 Integer
-> CMD_ActiveDischargeTest 0.0000 Bool
-> CMD_UseRawSpeedTest 1.0000 Bool
-> CMD_DirOfRotationTest 0.0000 Enum
CAN 1 000068 X 8 StatusTest 44710.032888 R
-> STS_NotEnabledTest 1.0000 Bool
-> STS_OverCurrentTest 0.0000 Bool
-> STS_OverBusTest 1.0000 Bool
-> STS_OverPhaseTest 0.0000 Bool
-> STS_UnderVoltageTest 1.0000 Bool
CAN 1 000064 X 8 AccelerationCommandTest 44710.042868 R
-> ACC_AccelerationTest 1.0000 Bool
-> ACC_CurrentTest 0.0000 Bool
-> ACC_SurgeTest 54321.0000 RPM
-> ACC_AccelTest 98765.0000 RPM/sec
-> ACC_MaxCurrentTest 150.0000 Amp
CAN 1 000065 X 8 FeedbackTest 44710.052938 R
-> FB_SafetyModeTest 0.0000 Enum
-> FB_TorqueTest 1234.5000 Nm
-> FB_VoltageTest 4567.8000 Volts
-> FB_CurrentTest 5678.9000 Amps
-> FB_SpeedTest 6789.0000 rpm
CAN 1 000066 X 8 SafetyFeedbackTest 44710.062918 R
-> SAF_SafetyModeTest 1.0000 Enum
-> SAF_TorqueTest 890.0000 Nm
-> SAF_VoltageTest 5678.0000 Volts
-> SAF_CurrentTest 6789.1000 Amps
-> SAF_SpeedTest 7890.2000 rpm
CAN 1 000067 X 8 CommandTest 44710.072908 R
-> CMD_ControlModeTest 3.0000 Enum
-> CMD_CommandCounterTest 4.0000 Integer
-> CMD_ActiveDischargeTest 1.0000 Bool
-> CMD_UseRawSpeedTest 0.0000 Bool
-> CMD_DirOfRotationTest 1.0000 Enum
CAN 1 000068 X 8 StatusTest 44710.082888 R
-> STS_NotEnabledTest 0.0000 Bool
-> STS_OverCurrentTest 1.0000 Bool
-> STS_OverBusTest 0.0000 Bool
-> STS_OverPhaseTest 1.0000 Bool
-> STS_UnderVoltageTest 0.0000 Bool
96 changes: 87 additions & 9 deletions test/test_log_parsing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,90 @@
import re
import cantools
import os
import sys

def parse_log_file(log_path):
with open(log_path, 'r') as file:
log_data = file.readlines()
return log_data
def parse_dbc(file_path):
try:
db = cantools.database.load_file(file_path)
print(f"Successfully parsed DBC file: {file_path}")
return db
except Exception as e:
print(f"Error parsing {file_path}: {e}")
return None

def parse_log(db, log_file_path):
try:
parsed_data = []
with open(log_file_path, 'r') as log_file:
current_message = None
for line in log_file:
# print(f"Reading line: {line.strip()}") # Commented for clean output
if line.startswith("CAN"):
if current_message:
process_message(db, current_message, parsed_data)
current_message = [line.strip()]
# print("Detected start of a new message") # Commented for clean output
elif line.strip().startswith("->"):
# print("Detected start of line with ->")
current_message.append(line.strip())
# print(f"Appending signal line: {line.strip()}") # Commented for clean output
if current_message:
process_message(db, current_message, parsed_data)
print_parsed_data(parsed_data)
except Exception as e:
print(f"Error parsing log file {log_file_path}: {e}")

def process_message(db, message_lines, parsed_data):
try:
# print(f"Processing message lines: {message_lines}") # Commented for clean output
main_line = message_lines[0].split()
# print(f"Main line parts: {main_line}") # Commented for clean output
can_id = main_line[2]
message_name = main_line[5]
timestamp = main_line[6]
direction = main_line[7]

signals = []

for signal_line in message_lines[1:]:
parts = signal_line.split()
signal_name = parts[1]
signal_value = parts[2]
signals.append((signal_name, signal_value, timestamp))

parsed_data.append({
"message_name": message_name,
"can_id": can_id,
"timestamp": timestamp,
"direction": direction,
"signals": signals
})

except Exception as e:
print(f"Error processing message: {e}")

def print_parsed_data(parsed_data):
for data in parsed_data:
print(f"Message: {data['message_name']}, CAN ID: {data['can_id']}, Timestamp: {data['timestamp']}, Direction: {data['direction']}")
for signal in data['signals']:
signal_name, signal_value, signal_timestamp = signal
print(f" Signal: {signal_name}, Value: {signal_value}, Timestamp: {signal_timestamp}")

if __name__ == "__main__":
log_path = 'test/data/test_log.txt'
log_data = parse_log_file(log_path)
for line in log_data:
print(line.strip())
if len(sys.argv) != 2:
print("Usage: python test_log_parsing.py [test|main]")
sys.exit(1)

mode = sys.argv[1]
if mode == "test":
dbc_file = "C:\\Github\\CAN-Log-Parser\\test\\data\\test.dbc"
log_file = "C:\\Github\\CAN-Log-Parser\\test\\data\\test_log.txt"
elif mode == "main":
dbc_file = "C:\\Github\\CAN-Log-Parser\\data\\1200G_CAN-DBC_v01.01.00.dbc"
log_file = "C:\\Github\\CAN-Log-Parser\\data\\DMW_Message_Timeout_CAN_Log.txt"
else:
print("Invalid mode. Use 'test' or 'main'.")
sys.exit(1)

db = parse_dbc(dbc_file)
if db:
parse_log(db, log_file)

0 comments on commit ed611b6

Please sign in to comment.