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

pya2lmodel-0.0.1.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

pya2lmodel-0.0.1-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file pya2lmodel-0.0.1.tar.gz.

File metadata

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

File hashes

Hashes for pya2lmodel-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4ac1e6d596d1c80f269e5deca36877e47fdf4e0930fa3298fdd13ac00cd4929d
MD5 a98c999bd923c2730f761f6c9ca5cb60
BLAKE2b-256 111128c595682a1099e5420e964d6d01e260ce5276331f3638c515febb9e082e

See more details on using hashes here.

File details

Details for the file pya2lmodel-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pya2lmodel-0.0.1-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 pya2lmodel-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9644c99c0a57c17a4c85cead7df82b48fdf55103d769e934b4df57df753edbcb
MD5 5a78a4fc907f68781270b90d5151bf34
BLAKE2b-256 70fadd2e3b98411ca6db57be742f3bdfdad61d1f58dbf71327f4105b1cc2cfcd

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