Dec-08-2018, 08:41 PM
Hello guys! I am working on a code that takes lines from ASC log file that logs CAN messages from vehicle. I get this error even tho my programs runs good. I would appreciate any help given here to resolve this issue. Thank you!
This is example of message where "d" is on position 4.
And in the next quotes is way to get this error.
Firstly program is reading file line by line and splitting it in a list like is quoted up there.
Quote:Traceback (most recent call last):
File "C:/Users/Luka/Desktop/Diplomski/CAN_parser.py", line 290, in <module>
main()
File "C:/Users/Luka/Desktop/Diplomski/CAN_parser.py", line 285, in main
message_type_recognizer(message_split)
File "C:/Users/Luka/Desktop/Diplomski/CAN_parser.py", line 219, in message_type_recognizer
elif can_message[4] == "d":
IndexError: list index out of range
This is example of message where "d" is on position 4.
Quote:['20.688593', '1', '9', 'Rx', 'd', '8', '04', '00', '00', '00', '00', '00', '00', '00', 'Length', '=', '242000', 'BitCount', '=', '125', 'ID', '=', '9']
And in the next quotes is way to get this error.
Firstly program is reading file line by line and splitting it in a list like is quoted up there.
def main(): line_counter = 1 # ASCOpen = open(ASCFilePath, 'r') with open(ASCFilePath) as ASCOpen: for line in ASCOpen: line = line.strip() message_split = line.split(' ') message_split = list(filter(lambda a: a != '', message_split)) print(line_counter) print(message_split) message_type_recognizer(message_split) line_counter = line_counter + 1This is part of code where it says index out of range.
def message_type_recognizer(can_message): if can_message[0] == "date" or can_message[0] == "base" or can_message[0] == "internal" or can_message[0] == "//" or can_message[0] == "Begin" or can_message[1] == "Start": header_parser(can_message) elif can_message[4] == "d": print(len(can_message)) can_message_parser(can_message)And now the fun part where I think is mistake but I am not sure. the next part of the code takes message and if (i.e can_message[5] = 8 then connect next 8 values in one string like '04', '00', '00', '00', '00', '00', '00' connect them in 04000.. )
def can_message_parser(can_message): can_payload_number = int(can_message[5]) starting_range = 6 ending_range = starting_range + can_payload_number - 1 can_payload_value = "" for x in range(starting_range, ending_range + 1): can_payload_value = can_payload_value + can_message[starting_range] starting_range = starting_range + 1 length_position = 6 + int(can_message[5]) + 2 bitcount_position = length_position + 3 CANMessageObject = CANDataMessage(can_message, can_payload_value, length_position, bitcount_position)And down below is class for one CANDataMessage
class CANDataMessage: def __init__(self, can_msg, can_payload_number, length_position, bit_count_position): self.Time = can_msg[0] self.Channel = can_msg[1] self.ID = can_msg[2] self.Dir = can_msg[3] self.FrameType = can_msg[4] self.Dlc = can_msg[5] self.Payload = can_payload_number self.Length = can_msg[length_position] self.BitCount = can_msg[bit_count_position]