Skip to main content

Parse and model ASAP2 A2L files with bidirectional conversion

Project description

PyA2lModel

A comprehensive Python library for parsing and modeling ASAP2 (.a2l) files with bidirectional conversion capabilities. This library extracts A2L content into structured Python data models and supports exporting models back to A2L format.

Features

Parsing Capabilities

  • Project and Module information extraction
  • XCPplus Protocol Layer configuration parsing
  • DAQ (Data Acquisition) configuration with event handling
  • XCP_ON_CAN transport layer parameters (including CAN FD)
  • Memory segments with page information
  • AXIS_PTS (calibration axes) parsing
  • MEASUREMENTS data extraction
  • CHARACTERISTICS for calibration parameters
  • RECORD_LAYOUTs for data structure definitions
  • COMPU_METHODs and COMPU_VTABs for conversion methods
  • GROUPs and FUNCTIONs with local measurements and references

Export Capabilities

  • Bidirectional Conversion: Parse A2L files to Python objects and export back to A2L format
  • Programmatic Modification: Edit parsed data and regenerate A2L files
  • Customizable Output: Control indentation and formatting
  • Complete Round-trip: Parse → Modify → Export workflow

Installation

Ensure Python 3.7+ is installed.

pip install pya2lmodel

Basic Usage

Parsing A2L Files

from a2lparser import A2LParser, A2LModel

# Parse an A2L file
parser = A2LParser()
model = parser.parse_file("your_file.a2l")

# Access parsed data
print(f"Project: {model.project_name}")
print(f"Module: {model.module_name}")
print(f"Measurements: {len(model.measurements)}")
print(f"Characteristics: {len(model.characteristics)}")
print(f"DAQ events: {len(model.daq_events)}")

# Export to JSON-like dict
model_dict = model.to_dict()
import json
print(json.dumps(model_dict, indent=2))

Exporting to A2L Format

# Export model to A2L string
a2l_content = model.to_a2l()
print("Generated A2L content:")
print(a2l_content[:500])  # Show first 500 characters

# Export directly to file
model.to_file("exported_file.a2l")

# Customize indentation
model.to_file("exported_file_spaces.a2l", indent="    ")

Advanced Usage: Modify and Re-export

# Parse existing file
model = parser.parse_file("original.a2l")

# Modify data
if model.measurements:
    model.measurements[0].description = "Modified measurement description"

# Add new characteristics
from a2lparser.a2l_model import Characteristic
new_char = Characteristic(
    name="NEW_CALIBRATION",
    description="New calibration parameter",
    char_type="VALUE",
    address=0x1000,
    record_layout="RL_1D",
    compu_method="CM_IDENTICAL",
    lower_limit=0.0,
    upper_limit=100.0
)
model.characteristics.append(new_char)

# Export modified model
model.to_file("modified.a2l")

API Reference

A2LParser Class

  • parse_file(path: str | Path) -> A2LModel: Parse A2L file from path
  • parse_text(text: str) -> A2LModel: Parse A2L content from string

A2LModel Class

  • to_dict() -> Dict[str, Any]: Convert model to JSON-like dictionary
  • to_a2l(indent: str = "\t") -> str: Export to A2L format string
  • to_file(filepath: str, indent: str = "\t") -> None: Export to A2L file

Data Classes

  • ProtocolLayer, DaqConfig, DaqEvent
  • XcpOnCanConfig, XcpOnCanFdConfig
  • MemorySegment, SegmentInfo, PageInfo
  • AxisPts, Measurement, Characteristic
  • CompuMethod, CompuVTab
  • RecordLayout, Group, Function

Export Features

Supported Blocks in Export

  • Complete PROJECT and MODULE structure
  • XCPplus protocol layer configuration
  • DAQ configuration with events
  • Memory segments and pages
  • AXIS_PTS, MEASUREMENT, and CHARACTERISTIC blocks
  • COMPU_METHODs and COMPU_VTABs
  • RECORD_LAYOUTs, GROUPs, and FUNCTIONs

File Format Control

  • Line endings: Uses CRLF (

) for Windows compatibility

  • Indentation: Configurable (default: tab)
  • Encoding: UTF-8 with proper error handling
  • Structure: Maintains ASAP2 version compatibility

Example Output

Project: MyECUProject
Module: ECU_Module
Protocol Layer parsed: True
DAQ events: 3
XCP on CAN parsed: True
Memory segments: 5
AXIS_PTS: 12
Measurements: 150
Characteristics: 80
Record layouts: 8

Generated A2L preview:
ASAP2_VERSION 1 70

/begin PROJECT "MyECUProject" ""
	/begin HEADER ""
		VERSION "1"
		PROJECT_NO No
	/end HEADER
	/begin MODULE "ECU_Module" ""
	/begin A2ML
	/end A2ML
...

Limitations

  • This is a minimal parser and does not support full ASAP2 grammar
  • Tailored to specific A2L dialects - may require adjustments for variants
  • Large or complex A2L files may need optimization

License

MIT

Contact

For issues or improvements, please raise an issue or contribute via pull requests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

a2lmodel-0.0.2.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

a2lmodel-0.0.2-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file a2lmodel-0.0.2.tar.gz.

File metadata

  • Download URL: a2lmodel-0.0.2.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for a2lmodel-0.0.2.tar.gz
Algorithm Hash digest
SHA256 566fbd6d36fd0e5df239557ff3acda50c7cde70a607034c866ce28e1994c3e53
MD5 4825ac5788df6bee5b5db53d635fc3fa
BLAKE2b-256 4446bffa2c70821f1107a850da4e8a2260e6a19cf6c8b40265d24b6ff47a94f4

See more details on using hashes here.

File details

Details for the file a2lmodel-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: a2lmodel-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for a2lmodel-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9b8b2313e0a25eed0ec09c7a6301f5715e2f774f86d3ac6fa71e87e2414ce5ef
MD5 0d45c1fb3e152dd55ce6fdc0b50f7d25
BLAKE2b-256 00bed832916e50a4d55a11d786142e5b65ef9ec118844bf89cd6a334794ee9b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page