Skip to content

Instantly share code, notes, and snippets.

@edsammy
Created January 24, 2025 21:54
Show Gist options
  • Save edsammy/c0ed55c7d05e0adc88e724c9f8431bda to your computer and use it in GitHub Desktop.
Save edsammy/c0ed55c7d05e0adc88e724c9f8431bda to your computer and use it in GitHub Desktop.
Convert panda CAN logger to savvyCAN format
# usage: python logger_to_savvy.py <input_file> <output_file>
# load from CANSavvy as GVRET logger format
import csv
import sys
import os
def convert_can_data(input_file, output_file):
with open(input_file, 'r') as infile, open(output_file, 'w', newline='') as outfile:
reader = csv.DictReader(infile)
writer = csv.writer(outfile)
# Write header
writer.writerow(["Time Stamp", "ID", "Extended", "Bus", "LEN", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8"])
for row in reader:
time_stamp = int(float(row['Time']) * 1000000) # Convert to microseconds
can_id = int(row['MessageID'], 16)
extended = 'true' if can_id > 0x7FF else 'false'
bus = int(row['Bus']) + 1 # Increment bus number
length = int(row['MessageLength'])
message = row['Message'][2:] # Remove '0x' prefix
# Split message into bytes
data = [message[i:i+2] for i in range(0, len(message), 2)]
# Pad data with empty strings if less than 8 bytes
data += [''] * (8 - len(data))
output_row = [
str(time_stamp),
f"{can_id:08X}",
extended,
str(bus),
str(length)
] + data
writer.writerow(output_row)
def main():
if len(sys.argv) != 3:
print("Usage: python script.py <input_file> <output_file>")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
if not os.path.exists(input_file):
print(f"Error: Input file '{input_file}' does not exist.")
sys.exit(1)
try:
convert_can_data(input_file, output_file)
print(f"Conversion complete. Output saved to {output_file}")
except Exception as e:
print(f"An error occurred: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment