kallemooo / Asap2

Asap2 parser.
MIT License
38 stars 21 forks source link

I want know how to use this library #14

Open Kun-H-Peng opened 3 years ago

Kun-H-Peng commented 3 years ago

I want to know how to use this library and function in this library to parse asap2(a2l) ?

Could you share me some reference about the Asap2 library to use it ?

shailendrasinghrajput commented 3 years ago

which project do want to use this library as C# or C++ project. I think you are working in automotive company.

squintz commented 1 year ago

Same question for me. I'm working with C# and have never been exposed to A2L files before. We're passively monitoring CAN FD packets which contain XCP data and we want to decode the XCP. So I'm looking for an example of how to use this library to match the A2L with the CAN FD messages and get unit conversions from the A2L file. Not sure this make total sense. I'm still learning. Any examples would greatly help me.

squintz commented 1 year ago

I think I've managed to put together enough of an example with help by feeding ChatGPT the various classes and using the Test examples. I came up with this working code that I just stuck inside a button. Leaving it here in case others fine it useful.

` string fileName;

        if (a2lDialog.ShowDialog() == DialogResult.OK)
        {
            fileName = a2lDialog.FileName;
            string directoryPath = System.IO.Path.GetDirectoryName(a2lDialog.FileName);
            string fullPath = Path.Combine(directoryPath, fileName);
            var errorHandler = new ErrorHandler();
            var parser = new Asap2.Parser(fullPath, errorHandler);
            Asap2.FileComment comment = new Asap2.FileComment(Environment.NewLine + "A2l file for testing ASAP2 parser." + Environment.NewLine, true);
            var tree = parser.DoParse();

            Console.WriteLine("Press enter to serialize data.");
            Console.ReadLine();

            tree.elements.Insert(0, comment);
            var ms = new MemoryStream();
            StreamWriter stream = new StreamWriter(ms, new UTF8Encoding(true));
            parser.Serialise(tree, stream);
            ms.Position = 0;
            var sr = new StreamReader(ms);
            var myStr = sr.ReadToEnd();
            Console.WriteLine(myStr);

            // Now, you can use the foreach loop on the elements property
            foreach (Asap2Base element in tree.elements)
            {
                // Access properties or methods of the derived classes through the 'element' variable
                Console.WriteLine("Element Type: " + element.GetType().FullName);

                // Check if the element is an instance of PROJECT class
                if (element is PROJECT project)
                {
                    // Access properties of the PROJECT class
                    Console.WriteLine("Project Name: " + project.name);

                    // Now, you can use a nested foreach loop on the modules dictionary of the PROJECT
                    foreach (MODULE module in project.modules.Values)
                    {
                        // Access properties of the MODULE class
                        Console.WriteLine("Module Name: " + module.Name);
                        Console.WriteLine("Module LongIdentifier: " + module.LongIdentifier);

                        // Check if the module contains MEASUREMENT elements
                        List<MEASUREMENT> measurements = module.AxisPtsCharacteristicMeasurement.Values.OfType<MEASUREMENT>().ToList();
                        if (measurements.Count > 0)
                        {
                            // Access the MEASUREMENT elements
                            foreach (MEASUREMENT measurement in measurements)
                            {
                                // Access properties of the MEASUREMENT class
                                Console.WriteLine("Signal Name: " + measurement.Name);
                                Console.WriteLine("Description: " + measurement.LongIdentifier);
                                Console.WriteLine("Data Type: " + measurement.Datatype);
                                Console.WriteLine("Conversion: " + measurement.Conversion);
                                Console.WriteLine("Resolution: " + measurement.Resolution);
                                Console.WriteLine("Accuracy: " + measurement.Accuracy);
                                Console.WriteLine("Lower Limit: " + measurement.LowerLimit);
                                Console.WriteLine("Upper Limit: " + measurement.UpperLimit);
                                Console.WriteLine("ECU Address: 0x" + measurement.GetEcuAddress().ToString("X"));
                            }
                        }
                        else
                        {
                            Console.WriteLine("No MEASUREMENT elements found in the module: " + module.Name);
                        }
                    }
                }
            }
        }`