forked from JeremyHsiao/My_K_Line_Tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySerial.cs
339 lines (295 loc) · 10.8 KB
/
MySerial.cs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;
using BlockMessageLibrary;
namespace MySerialLibrary
{
class MySerial : IDisposable
{
// static member/function to shared aross all MySerial
static protected Dictionary<string, Object> MySerialDictionary = new Dictionary<string, Object>();
// Private member
private SerialPort _serialPort;
//
// public functions
//
enum BAUD_RATE_LIST
{
BR_9600 = 9600,
BR_K_Line = 10400,
BR_115200 = 115200,
BR_230400 = 230400,
};
enum RX_PROCESSOR
{
ENQUEUE = 0,
READLINE,
K_LINE
}
private RX_PROCESSOR Rx_Processor_Selection = RX_PROCESSOR.K_LINE; // ENQUEUE
public const int Serial_BaudRate = (int)BAUD_RATE_LIST.BR_K_Line; // BAUD_RATE_LIST.BR_115200;
public const Parity Serial_Parity = Parity.None;
public const int Serial_DataBits = 8;
public const StopBits Serial_StopBits = StopBits.One;
public MySerial(string com_port) { _serialPort = new SerialPort(com_port, Serial_BaudRate, Serial_Parity, Serial_DataBits, Serial_StopBits); }
public string GetPortName() { return _serialPort.PortName; }
public MySerial()
{
_serialPort = new SerialPort
{
BaudRate = Serial_BaudRate,
Parity = Serial_Parity,
DataBits = Serial_DataBits,
StopBits = Serial_StopBits
};
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// dispose managed resources
_serialPort.Close();
}
// free native resources
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
static public List<string> FindAllSerialPort()
{
List<string> ListSerialPort = new List<string>();
foreach (string comport_s in SerialPort.GetPortNames())
{
ListSerialPort.Add(comport_s);
}
return ListSerialPort;
}
public Boolean OpenPort()
{
Boolean bRet = false;
_serialPort.Handshake = Handshake.None;
_serialPort.Encoding = Encoding.UTF8;
_serialPort.ReadTimeout = 1000;
_serialPort.WriteTimeout = 1000;
switch (Rx_Processor_Selection)
{
case RX_PROCESSOR.ENQUEUE:
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
break;
case RX_PROCESSOR.K_LINE:
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler_KLine);
break;
case RX_PROCESSOR.READLINE:
// To-be-added.
break;
}
try
{
_serialPort.Open();
Start_SerialReadThread();
MySerialDictionary.Add(_serialPort.PortName, this);
bRet = true;
}
catch (Exception ex232)
{
Console.WriteLine("MySerial_OpenPort Exception at PORT: " + _serialPort.PortName + " - " + ex232);
bRet = false;
}
return bRet;
}
public Boolean OpenPort(string PortName)
{
Boolean bRet = false;
_serialPort.PortName = PortName;
bRet = OpenPort();
return bRet;
}
public Boolean ClosePort()
{
Boolean bRet = false;
MySerialDictionary.Remove(_serialPort.PortName);
try
{
Stop_SerialReadThread();
_serialPort.Close();
bRet = true;
}
catch (Exception ex232)
{
Console.WriteLine("Serial_ClosePort Exception at PORT: " + _serialPort.PortName + " - " + ex232);
bRet = false;
}
return bRet;
}
public Boolean IsPortOpened()
{
Boolean bRet = false;
//if ((_serialPort.IsOpen == true) && (readThread.IsAlive))
if (_serialPort.IsOpen == true)
{
bRet = true;
}
return bRet;
}
//
// Start of read part
//
private Queue<byte> Rx_byte_buffer_QUEUE = new Queue<byte>();
private Queue<string> UART_READ_MSG_QUEUE = new Queue<string>();
public Queue<string> LOG_QUEUE = new Queue<string>();
public byte GetRxByte() { byte ret_byte = Rx_byte_buffer_QUEUE.Dequeue(); return ret_byte; }
public bool IsRxEmpty() { return (Rx_byte_buffer_QUEUE.Count <= 0) ? true : false; }
public bool GetBreakState () { return _serialPort.BreakState; }
private void Start_SerialReadThread()
{
LOG_QUEUE.Clear();
UART_READ_MSG_QUEUE.Clear();
Rx_byte_buffer_QUEUE.Clear();
}
private void Stop_SerialReadThread()
{
LOG_QUEUE.Clear();
UART_READ_MSG_QUEUE.Clear();
Rx_byte_buffer_QUEUE.Clear();
}
// This Handler is for reading all input without wating for a whole line
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
// Find out which serial port --> which myserial
SerialPort sp = (SerialPort)sender;
MySerialDictionary.TryGetValue(sp.PortName, out Object myserial_serial_obj);
MySerial myserial = (MySerial)myserial_serial_obj;
//Rx_char_buffer_QUEUE
int buf_len = sp.BytesToRead;
if (buf_len > 0)
{
// Read in all char
byte[] input_buf = new byte[buf_len];
sp.Read(input_buf, 0, buf_len);
{
int ch_index = 0;
while (ch_index < buf_len)
{
byte byte_data = input_buf[ch_index];
myserial.Rx_byte_buffer_QUEUE.Enqueue(byte_data);
ch_index++;
}
}
}
}
//
// K-line portion of code
//
public List<BlockMessage> KLineBlockMessageList = new List<BlockMessage>();
public List<String> KLineRawDataInStringList = new List<String>();
private String RawDataInString = "";
private ProcessBlockMessage KLineKWP2000Process = new ProcessBlockMessage();
private bool ECU_filtering = false;
private List<byte> ECU_data_to_be_filtered = new List<byte>();
public void Enable_ECU_Filtering(bool enabled)
{
ECU_filtering = enabled;
}
public void Add_ECU_Filtering_Data(List<byte> filter_Data)
{
ECU_data_to_be_filtered.Clear();
ECU_data_to_be_filtered.AddRange(filter_Data);
}
private static void DataReceivedHandler_KLine(object sender, SerialDataReceivedEventArgs e)
{
// Find out which serial port --> which myserial
SerialPort sp = (SerialPort)sender;
MySerialDictionary.TryGetValue(sp.PortName, out Object myserial_serial_obj);
MySerial myserial = (MySerial)myserial_serial_obj;
while ( sp.BytesToRead > 0 )
{
// Read in all char
bool IsMessageReady = false;
byte byte_data = (byte)sp.ReadByte();
if (myserial.ECU_filtering == true)
{
if(myserial.ECU_data_to_be_filtered.Count>0)
{
myserial.ECU_data_to_be_filtered.RemoveAt(0);
}
if(myserial.ECU_data_to_be_filtered.Count==0)
{
myserial.ECU_filtering = false;
}
}
else
{
myserial.RawDataInString += byte_data.ToString("X2") + " ";
IsMessageReady = myserial.KLineKWP2000Process.ProcessNextByte(byte_data);
if (IsMessageReady)
{
BlockMessage new_message = myserial.KLineKWP2000Process.GetProcessedBlockMessage();
myserial.KLineBlockMessageList.Add(new_message);
myserial.KLineRawDataInStringList.Add(myserial.RawDataInString);
myserial.RawDataInString = "";
IsMessageReady = false;
//break;
}
}
}
}
//
// END of K-line portion of code
//
//
// End of read part
//
public bool SendToSerial(byte[] byte_to_sent)
{
bool return_value = false;
if (_serialPort.IsOpen == true)
{
//Application.DoEvents();
try
{
int temp_index = 0;
const int fixed_length = 16;
while ((temp_index < byte_to_sent.Length) && (_serialPort.IsOpen == true))
{
if ((temp_index + fixed_length) < byte_to_sent.Length)
{
_serialPort.Write(byte_to_sent, temp_index, fixed_length);
temp_index += fixed_length;
}
else
{
_serialPort.Write(byte_to_sent, temp_index, (byte_to_sent.Length - temp_index));
temp_index = byte_to_sent.Length;
}
}
return_value = true;
}
catch (Exception ex)
{
Console.WriteLine("BlueRatSendToSerial - " + ex);
return_value = false;
}
}
else
{
Console.WriteLine("COM is closed and cannot send byte data\n");
return_value = false;
}
return return_value;
}
//
// To process UART IO Exception
//
protected virtual void OnUARTException(EventArgs e)
{
UARTException?.Invoke(this, e);
}
public event EventHandler UARTException;
}
}