Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Shows how to access the ASAP2Library from Python using PythonNET.
- #
- # Prerequisites
- # 1) Python installed (you may check this by entering 'python --version' in a console)
- # 2) execute "pip install pythonnet" in a console
- #
- # Execute this by entering 'python PythonNETExample.py'
- #
- # Demonstrates
- # 1) How to open an A2L File
- # 2) Get an element by type from the A2L model
- # 3) Get all elements by type from the A2L model
- # 4) Delete an element from the A2L model
- # 5) Create an new measurement and add it to the A2L model
- # 6) Write the resulting model to a new A2L file
- #
- import clr
- clr.AddReference("System")
- clr.AddReference("ASAP2")
- clr.AddReference("ASAP2If")
- from System import EventHandler, Enum, Byte
- from jnsoft.ASAP2 import * # import all types from the ASAP2Library
- from jnsoft.Helpers import * # import all types from the ASAP2Library interface
- # Parser message event handler
- def parser_message_handler(sender, args):
- print(args.TimeStamp.ToShortTimeString()
- + "\t" + Enum.GetName(MessageType, args.Type)
- + "(" + Enum.GetName(A2LType, args.Source.Type)
- + ")\t" + args.Message)
- # Create parser, read A2L
- print("Opening file ASAP2Example.a2l")
- a2lParser = A2LParser()
- # subscribe to the ParserMessage event
- a2lParser.ParserMessage += EventHandler[ParserEventArgs](parser_message_handler)
- a2lParser.parse("..\..\A2LExamples\ASAP2Example.a2l")
- # Get the first A2L module
- module = a2lParser.Project.getNode[A2LMODULE](False)
- print("\nA2L Module:" + module.Name + " (" + module.Description + ")")
- # Get and print all A2L measurements from the module
- measurements = module.getNodeList[A2LMEASUREMENT](False)
- for m in measurements:
- print("Measurement:" + m.Name + " DataType:" + Enum.GetName(DATA_TYPE, m.DataType) + " Limits:" + str(m.LowerLimit) + ".." + str(m.UpperLimit))
- # Get and print all A2L characteristics from the module
- characteristics = module.getNodeList[A2LCHARACTERISTIC](False)
- for c in characteristics:
- print("Characteristic:" + c.Name + " Type:" + Enum.GetName(CHAR_TYPE, c.CharType) + " Limits:" + str(c.LowerLimit) + ".." + str(c.UpperLimit))
- # delete a measurement from the A2L model
- measurement = None
- found, measurement = a2lParser.Project.MeasDict.TryGetValue("speed_by_formula", measurement)
- if(found):
- A2LNODE.deleteFromModel(measurement)
- print(measurement.Name + " deleted.")
- # build a measurement and add it to the A2L model
- measurement = A2LMEASUREMENT()
- measurement.Name = "Measurement 1"
- measurement.Description = "Measurement Description"
- measurement.DataType = DATA_TYPE.UBYTE
- measurement.LowerLimit = 0
- measurement.UpperLimit = Byte.MaxValue
- measurement.Conversion = "IDENTITY"
- measurement.Address = 0x400
- module.addChild(measurement);
- # store the modified A2L file
- a2lParser.write("ASAP2Example.out.a2l")
- print("Done.")
Add Comment
Please, Sign In to add comment