alesi2000

ASAP2Library PythonNET Example

Sep 19th, 2021 (edited)
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. #
  2. # Shows how to access the ASAP2Library from Python using PythonNET.
  3. #
  4. # Prerequisites
  5. # 1) Python installed (you may check this by entering 'python --version' in a console)
  6. # 2) execute "pip install pythonnet" in a console
  7. #
  8. # Execute this by entering 'python PythonNETExample.py'
  9. #
  10. # Demonstrates
  11. # 1) How to open an A2L File
  12. # 2) Get an element by type from the A2L model
  13. # 3) Get all elements by type from the A2L model
  14. # 4) Delete an element from the A2L model
  15. # 5) Create an new measurement and add it to the A2L model
  16. # 6) Write the resulting model to a new A2L file
  17. #
  18.  
  19. import clr
  20. clr.AddReference("System")
  21. clr.AddReference("ASAP2")
  22. clr.AddReference("ASAP2If")
  23.  
  24. from System import EventHandler, Enum, Byte
  25. from jnsoft.ASAP2 import *      # import all types from the ASAP2Library
  26. from jnsoft.Helpers import *    # import all types from the ASAP2Library interface
  27.  
  28. # Parser message event handler
  29. def parser_message_handler(sender, args):
  30.   print(args.TimeStamp.ToShortTimeString()
  31.     + "\t" + Enum.GetName(MessageType, args.Type)
  32.     + "(" + Enum.GetName(A2LType, args.Source.Type)
  33.     + ")\t" + args.Message)
  34.  
  35. # Create parser, read A2L
  36. print("Opening file ASAP2Example.a2l")
  37. a2lParser = A2LParser()
  38. # subscribe to the ParserMessage event
  39. a2lParser.ParserMessage += EventHandler[ParserEventArgs](parser_message_handler)
  40. a2lParser.parse("..\..\A2LExamples\ASAP2Example.a2l")
  41.  
  42. # Get the first A2L module
  43. module = a2lParser.Project.getNode[A2LMODULE](False)
  44. print("\nA2L Module:" + module.Name + " (" + module.Description + ")")
  45.  
  46. # Get and print all A2L measurements from the module
  47. measurements = module.getNodeList[A2LMEASUREMENT](False)
  48. for m in measurements:
  49.   print("Measurement:" + m.Name + " DataType:" + Enum.GetName(DATA_TYPE, m.DataType) + " Limits:" + str(m.LowerLimit) + ".." + str(m.UpperLimit))
  50.  
  51. # Get and print all A2L characteristics from the module
  52. characteristics = module.getNodeList[A2LCHARACTERISTIC](False)
  53. for c in characteristics:
  54.   print("Characteristic:" + c.Name + " Type:" + Enum.GetName(CHAR_TYPE, c.CharType) + " Limits:" + str(c.LowerLimit) + ".." + str(c.UpperLimit))
  55.  
  56. # delete a measurement from the A2L model
  57. measurement = None
  58. found, measurement = a2lParser.Project.MeasDict.TryGetValue("speed_by_formula", measurement)
  59. if(found):
  60.     A2LNODE.deleteFromModel(measurement)
  61.     print(measurement.Name + " deleted.")
  62.  
  63. # build a measurement and add it to the A2L model
  64. measurement = A2LMEASUREMENT()
  65. measurement.Name = "Measurement 1"
  66. measurement.Description = "Measurement Description"
  67. measurement.DataType = DATA_TYPE.UBYTE
  68. measurement.LowerLimit = 0
  69. measurement.UpperLimit = Byte.MaxValue
  70. measurement.Conversion = "IDENTITY"
  71. measurement.Address = 0x400
  72. module.addChild(measurement);
  73.  
  74. # store the modified A2L file
  75. a2lParser.write("ASAP2Example.out.a2l")
  76.  
  77. print("Done.")
  78.  
Add Comment
Please, Sign In to add comment