forked from PrajinkyaPimpalghare/LDF-File-Parser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
191 lines (189 loc) · 8.36 KB
/
Program.cs
File metadata and controls
191 lines (189 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*==========================================================================
INFORMATION ABOUT CODE
============================================================================
>LDF File Parsing
Author: Prajinkya Pimpalghare
Date: 7-October-2017
Version: 1.0
Input Variable: Signal Name| Path of .LDF file
OutPUT: Message name , MinValue, Maximum Value, Scaling , Offset , Unit , Range [Min-Max]
============================================================================*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LDF_FILEPARSER
{
class Program
{
public static void SignalValidation(String InputSignalName, StreamReader reader)
{
//Objective: Validate The Signal::It is Present or Not in LDF file
//Input : SignalName and OpenFile Stream
//OutPut : Returns Signal Is Present Or Not
string line;
String SignalStatus = "Signal Not Exist";
reader.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);
while ((line = reader.ReadLine()) != null)
{
if (line == "Signals {")
{
while ((line = reader.ReadLine()) != "}")
{
string[] Signal = line.Split(':');
if (Signal[0].Trim() == InputSignalName)
SignalStatus = "Signal Exist In The Provided Ldf File";
}
}
}
if (SignalStatus == "Signal Not Exist")
{
Console.WriteLine("Signal Not Exist in Provided LDF file");
Environment.Exit(0);
}
}
public String SignalMesasageSearch(String InputSignalName, StreamReader reader)
{
//Objective: Search Related message to the input Signal
//Input : SignalName and OpenFile Stream
//OutPut : Returns Message Related to the input Signal
string line;
String EncodedSignal = null;
reader.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);
while ((line = reader.ReadLine()) != null)
{
if (line == "Signal_representation {")
{
while ((line = reader.ReadLine()) != "}")
{
string[] Signal = line.Split(':');
string[] SignalSplit = Signal[1].Split(',');
foreach (string Sig in SignalSplit)
{
if (Sig.Trim(new Char[] { ';', ' ' }) == InputSignalName)
EncodedSignal = Signal[0].Trim();
}
}
}
}
return (EncodedSignal+" {");// In Signal Encoding It Searches for Message and {
}
public Dictionary<string,String> SignalEncoding(String SignalMessageName, StreamReader reader)
{
//Objective: Encodes the signal and Provides Physical and Logical Value
//Input : Encoded Message
//OutPut : Physical
Dictionary<string, string> dict = new Dictionary<string, string>();
string line;
reader.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);
while ((line = reader.ReadLine()) != null)
{
if (line == "Signal_encoding_types {")
{
while ((line = reader.ReadLine()) != "}")
{
if (line.Trim() == SignalMessageName)
{
while ((line = reader.ReadLine()) != "\t}")
{
string[] Signal = line.Split(',');
if (Signal[0].Trim() == "physical_value")
{
dict.Add("MinValue", Signal[1]);
dict.Add("MaxValue", Signal[2]);
dict.Add("Scaling", Signal[3]);
Program.StringToFloat(Signal[1]);
dict.Add("Offset", Signal[4].Trim(new Char[] { ';', ' ' }));
try
{
dict.Add("Unit", Signal[5].Trim(new Char[] { ';', ' ' }));
}
catch
{
dict.Add("Unit", null);
}
float[] UpdateValue = Program.MaxValueConversion(Program.StringToFloat(Signal[1]), Program.StringToFloat(Signal[2]),Program.StringToFloat(Signal[3]), Program.StringToFloat(Signal[4]), dict["Unit"]);
dict.Add("MinRange", UpdateValue[0].ToString());
dict.Add("MaxRange", UpdateValue[1].ToString());
}
}
}
}
}
}
return (dict);
}
public static float[] MaxValueConversion(float InitValue, float DefaultValue, float Offset, float MinValue, String Unit)
{
//Objective: For Finding The Range Of Signal
//Input : Important=Unit of signal
//OutPut : Range of the signal
if (InitValue == 4)//For Special Kinds Of Signal:ST_DIAG_OBD
{
MinValue = InitValue;
}
if (Unit == null)
{
return new float[] { MinValue, DefaultValue };
}
else if (Unit == "\"?C\"" || Unit == "\"A\"")
{
return new float[] { MinValue, DefaultValue-Offset};
}
else if (Unit == "\"1/min\"" || Unit == "\"V\"")
{
return new float[] { MinValue, Offset * DefaultValue };
}
else
{
return new float[] { MinValue, DefaultValue };//For Encoding Not Possible with LDF
}
}
public static float StringToFloat(String StringValue)
{
//Objective: String To Float Value Conversion
float Temp;
float.TryParse(StringValue, out Temp);
return (Temp);
}
public static void PathValidation(String Path)
{
//It Validates The Path
if (System.IO.File.Exists(Path))
{
Console.WriteLine("Path Exist");
}
else
{
Console.WriteLine("Please Enter Correct Path Of LDF File");
Environment.Exit(0);
}
}
static void Main(string[] args)
{
//Main Program Starts here::
//This module will parse the LDF LIn file and provid ethe Physical value of the signal.
Console.WriteLine("Please Provide The Signal Name");
string InputSignalName = Console.ReadLine();
Console.WriteLine("Please Provide The LDF File Path");
string LDFFilePath = Console.ReadLine();
Program.PathValidation(LDFFilePath);
StreamReader reader = File.OpenText(LDFFilePath);
Program.SignalValidation(InputSignalName, reader);
Program SearchSignal = new Program();
String SignalMessageName=SearchSignal.SignalMesasageSearch(InputSignalName, reader);
Console.WriteLine("InputSignalName = " + InputSignalName);
Dictionary<string, string> dict=SearchSignal.SignalEncoding(SignalMessageName, reader);
if (dict.Count != 0)//For Printing Values, Can bbe removed when integrated with other programs
{
foreach (KeyValuePair<string, string> item in dict)
Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
}
else
Console.WriteLine("This Signal Having Only Logical Values");
Console.ReadLine();
}
}
}