LDF Parser

0.26.0 · active · verified Thu Apr 16

ldfparser is a Python library designed to parse LIN Description Files (LDF), enabling extraction of signal names, frame definitions, and facilitating encoding and decoding of LIN messages. It supports LIN standards 1.3, 2.0, 2.1, and 2.2A. Currently, the library is in a pre-release state, which means that features might undergo changes between minor versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse an LDF file, retrieve network parameters like baudrate, access specific frames, and then encode and decode LIN messages using defined signals. A temporary LDF file is created for a runnable example.

import ldfparser
import os
import tempfile
import binascii

# Create a dummy LDF file for demonstration
ldf_content = """
LIN_description_file;
LIN_protocol_version = "2.1";
LIN_language_version = "2.1";
LIN_speed = 19.2 kbps;
Nodes {
    Master: MasterNode, 5 ms, 0.1 ms;
    Slaves: SlaveNode1;
}
Signals {
    EngineRPM: 12, 0, MasterNode, MasterNode;
    CoolantTemp: 8, 12, MasterNode, MasterNode;
}
Frames {
    EngineFrame: 0x10, 8, MasterNode;
    Signals {
        EngineRPM, 0;
        CoolantTemp, 2;
    }
}
Signal_encoding_types {
    MotorRPM_encoding:
        physical_value,
        0, 255, 1, 0, "RPM";
}
Signal_representation {
    MotorRPM_encoding: EngineRPM;
}
"""

with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.ldf') as temp_ldf_file:
    temp_ldf_file.write(ldf_content)
    ldf_path = temp_ldf_file.name

try:
    # Load LDF
    ldf = ldfparser.parse_ldf(path=ldf_path)
    print(f"Successfully parsed LDF from: {ldf_path}")

    # Get baudrate
    print(f"Baudrate: {ldf.get_baudrate()} kbps")

    # Get an unconditional frame
    frame = ldf.get_unconditional_frame('EngineFrame')
    print(f"Frame 'EngineFrame' ID: {frame.frame_id}, Length: {frame.length} bytes")

    # Encode signal values
    signal_values = {"EngineRPM": 1500, "CoolantTemp": 95}
    message = frame.encode(signal_values)
    print(f"Encoded message for {signal_values}: 0x{binascii.hexlify(message).decode('utf-8')}")

    # Decode message
    decoded_values = frame.decode(message)
    print(f"Decoded message: {decoded_values}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the temporary LDF file
    if os.path.exists(ldf_path):
        os.remove(ldf_path)

view raw JSON →