Skip to content

Instantly share code, notes, and snippets.

@bkralik
Last active April 3, 2025 12:40
Show Gist options
  • Select an option

  • Save bkralik/5115ab131125371354f59b6840de1102 to your computer and use it in GitHub Desktop.

Select an option

Save bkralik/5115ab131125371354f59b6840de1102 to your computer and use it in GitHub Desktop.
Read Racelogic REF file
from loguru import logger
import zlib
filename = "Dodge-Charger-LD-2018.REF"
f = open(filename, "rb")
l = f.readline().decode("utf-8").rstrip()
logger.debug("First line read: {}", l)
valid_headers = [
"Racelogic Can Data File V1",
"Racelogic Can Data File V2",
"Racelogic Can Data File V1a",
"Racelogic Can Data File V2a"
]
if not (l in valid_headers):
logger.error("Valid header not found")
quit(1)
l2 = f.readline().decode("utf-8").rstrip()
logger.debug("Second line read: {}", l2)
def getCodedDataLength(f):
buf = f.read(2)
return buf[0] << 8 | buf[1]
l3 = getCodedDataLength(f)
logger.debug("Coded data length 1: {}", l3)
d3 = f.read(l3)
d3d = zlib.decompress(d3)
l4 = getCodedDataLength(f)
logger.debug("Coded data length 2: {}", l4)
logger.debug("SignalName,ID,Units,StartBit,Length,Offset,Scale,Maximum,Minimum,Dataformat,ByteOrder,DLC")
logger.debug("if ByteOrder == Motorola, StartBit = 56 - StartBit / 8 * 16 + StartBit")
for i in range(0, l4):
l5 = getCodedDataLength(f)
d5 = f.read(l5)
d5d = zlib.decompress(d5).decode()
logger.info(d5d)
@al-locke1
Copy link

for i in range(0, l4):
    l5 = getCodedDataLength(f)
    d5 = f.read(l5)
    d5d = zlib.decompress(d5).decode()

    fields = d5d.split(",")

    try:
        start_bit = int(fields[3])  # StartBit is at index 3
        byte_order = fields[10]  # ByteOrder is at index 10

        # Apply transformation if ByteOrder is Motorola
        if byte_order.strip() == "Motorola":
            start_bit = 56 - start_bit
            fields[3] = str(start_bit)  # Update the StartBit field

    except (IndexError, ValueError) as e:
        logger.error(f"Error processing line: {d5d} -> {e}")
        continue

    logger.info(",".join(fields))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment