# LdfParser **Repository Path**: mtoooo/ldf-parser ## Basic Information - **Project Name**: LdfParser - **Description**: ldf数据库解析 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2023-02-01 - **Last Updated**: 2024-09-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: ldf, Lin, Database ## README # LdfParser #### 介绍 ldf数据库解析 #### 软件架构 软件架构说明 ### 程序使用(C# ldf数据库解析demo) - 参考工程CsharpDemo - string output = p.StandardOutput.ReadToEnd(); //output参考下一项数据结构,里面包含了所有的ldf数据 ![img.png](img.png) ```CSharp using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.Diagnostics; using System.Xml.Linq; namespace WpfTest2 { /// /// MainWindow.xaml 的交互逻辑 /// class Nosws { } public class LdfNode { static void Main(string[] args) { //string debugPath = System.Environment.CurrentDirectory; //此c#项目的debug文件夹路径 //string pyexePath = @"d:\git\ling-shu\ldfparser_lib\py2exe\dist\ldfparserpy\ldfparserpy.exe"; //private const string DbcFilePath = "../../src/EH6466.dbc"; String bin_path = System.IO.Directory.GetCurrentDirectory(); var bin_path_list = bin_path.Split("\\"); var base_path = ""; foreach ( var item in bin_path_list) { if (item == "bin") { break; } base_path += item + "\\"; }; Console.WriteLine(base_path); string pyexePath = "src\\LdfParserPy\\LdfParserPy.exe"; pyexePath = base_path + pyexePath; Console.WriteLine(pyexePath); //string pyexePath = @"D:\GIT\ling-shu\py2exe\dist\test_ldf.exe"; //python文件所在路径,一般不使用绝对路径,此处仅作为例子,建议转移到debug文件夹下 Process p = new Process(); p.StartInfo.FileName = pyexePath;//需要执行的文件路径 p.StartInfo.UseShellExecute = false; //必需 p.StartInfo.RedirectStandardOutput = true;//输出参数设定 p.StartInfo.RedirectStandardInput = true;//传入参数设定 p.StartInfo.CreateNoWindow = true; p.StartInfo.Arguments = base_path + "src\\all_lin.ldf";//参数以空格分隔,如果某个参数为空,可以传入”” //p.StartInfo.Arguments = "1 参数二是一个字符串";//参数以空格分隔,如果某个参数为空,可以传入”” p.Start(); string output = p.StandardOutput.ReadToEnd(); Console.WriteLine($"{output}"); Console.WriteLine($"返回参数类型为:{output.GetType()}"); Console.WriteLine("执行完毕!"); //p.WaitForExit();//关键,等待外部程序退出后才能往下执行} ////Console.Write(output);//输出 var dict = JsonConvert.DeserializeObject>(output); //取值方式 var nodes = dict["nodes"] as JObject; var frames = dict["frames"] as JObject; var signals = dict["signals"] as JObject; var master_node = nodes["master"] as JObject; var master_node_name = master_node["name"]; Console.WriteLine($"master node name is {master_node_name.ToString()}"); Console.WriteLine(dict["header"].ToString()); p.Close(); p.Close(); } } } ``` ### 数据结构 - 返回值是一个字典 - 数据结构如下 ```JSON { "header": "lin_description_file", "protocol_version": "2.2", "language_version": "2.2", "speed": 19200, "nodes": { "master": {"name": "BGW", "timebase": 0.005, "jitter": 0.0001}, "slaves": ["RCM", "RLS", "KSM"] }, "signals": [ {"name": "VehSpd", "width": 13, "init_value": 0, "publisher": "BGW", "subscribers": ["RLS"]}, {"name": "RiReadingLampSwErr", "width": 1, "init_value": 0, "publisher": "RCM","subscribers": ["BGW"]}, {"name": "KsEnable", "width": 1, "init_value": 1, "publisher": "BGW", "subscribers": ["KSM"]} ], "diagnostic_signals": [ {"name": "MasterReqB0", "width": 8, "init_value": 0}, {"name": "MasterReqB1", "width": 8, "init_value": 0}, {"name": "MasterReqB2", "width": 8, "init_value": 0}, {"name": "MasterReqB3", "width": 8, "init_value": 0}, {"name": "MasterReqB4", "width": 8, "init_value": 0}, ], "frames": [ {"name": "BGW_RLS", "frame_id": 7, "publisher": "BGW", "length": 6, "signals": [{"signal": "VehSpd", "offset": 0}, {"signal": "VehSpdSts", "offset": 13},] } ], "sporadic_frames": [ {"name": "test2", "frames": ["KSM_BGW"]} ], "event_triggered_frames": [ {"name": "test1", "collision_resolving_schedule_table": "NormalTable", "frame_id": 0, "frames": ["KSM_BGW"]} ], "diagnostic_frames": [ {"name": "MasterReq", "frame_id": 60, "signals": [ {"signal": "MasterReqB0", "offset": 0}, {"signal": "MasterReqB1", "offset": 8}, {"signal": "MasterReqB2", "offset": 16}, {"signal": "MasterReqB3", "offset": 24}, {"signal": "MasterReqB4", "offset": 32}, {"signal": "MasterReqB5", "offset": 40}, {"signal": "MasterReqB6", "offset": 48}, {"signal": "MasterReqB7", "offset": 56} ] } ], "node_attributes": [ {"name": "RCM", "lin_protocol": "2.2", "configured_nad": 17, "initial_nad": 17, "product_id": {"supplier_id": 100, "function_id": 67, "variant": 1}, "response_error": "RCMRespErr", "P2_min": 0.05, "ST_min": 0.0, "N_As_timeout": 1.0, "N_Cr_timeout": 1.0, "configurable_frames": ["BGW_RCM", "RCM_BGW"] } ], "schedule_tables": [ {"name": "NormalTable", "schedule": [ {"command": {"type": "frame", "frame": "BGW_RLS"}, "delay": 0.01}, {"command": {"type": "frame", "frame": "RLS_BGW"}, "delay": 0.02}, {"command": {"type": "frame", "frame": "BGW_RCM"}, "delay": 0.01}, {"command": {"type": "frame", "frame": "RCM_BGW"}, "delay": 0.02}, {"command": {"type": "frame", "frame": "BGW_KSM"}, "delay": 0.01}, {"command": {"type": "frame", "frame": "KSM_BGW"}, "delay": 0.01} ] }, {"name": "DiagReqOnly", "schedule": [ {"command": {"type": "master_request"}, "delay": 0.01}] }, ], "signal_encoding_types": [ {"name": "Vtsig_AmbLiIllmn_RLS", "values": [{"type": "physical", "min": 0, "max": 65535, "scale": 1.0, "offset": 0.0, "unit": None}] }, {"name": "Vtsig_AmbLiStsRoofConsole", "values": [{"type": "logical", "value": 0, "text": "off"}, {"type": "logical", "value": 1, "text": "on"}, {"type": "logical", "value": 2, "text": "reserved"}, {"type": "logical", "value": 3, "text": "invalid"} ] }, ], "signal_representations": [ {"encoding": "Vtsig_AmbLiIllmn_RLS", "signals": ["AmbLiIllmn"]}, {"encoding": "Vtsig_AmbLiStsRoofConsole", "signals": ["AmbLiStsRoofConsole"]}, {"encoding": "Vtsig_AmbTVld_BGW", "signals": ["AmbTVld"]}, {"encoding": "Vtsig_BGWRLS_Reserved_01_BGW", "signals": ["BGWRLS_Reserved_01"]}, ] } ``` ### C#获取dictionary内容 - 一个嵌套字典的调用方式(dict\list\string等类型) ```CSharp // 将string类型转为 Dictionary类型 Console.WriteLine("*****************************"); var json = @"{ "DisplayName": "新一代算法模型", "CustomerType": 1, "Report": { "TotalCustomerCount": 1000, "TotalTradeCount": 50 }, "CustomerIDHash": [1,2,3,4,5] }"; var dict = JsonConvert.DeserializeObject>(py_print); //取值方式 Console.WriteLine(dict["DisplayName"].ToString()); Console.WriteLine($"第二个传入的参数为:{dict["var2"].ToString()}"); //打印传入的参数2 var report = dict["Report"] as JObject; var totalCustomerCount = report["TotalCustomerCount"]; Console.WriteLine($"totalCustomerCount={totalCustomerCount}"); var arr = dict["CustomerIDHash"] as JArray; var list = arr.Select(m => m.Value()).ToList(); Console.WriteLine($"list={string.Join(",", list)}"); ```