-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSerialDeviceInput.cs
More file actions
670 lines (609 loc) · 22.5 KB
/
Copy pathSerialDeviceInput.cs
File metadata and controls
670 lines (609 loc) · 22.5 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
using Windows.Storage.Streams;
using NLog;
using System.Text;
namespace SerialDeviceLib
{
public class SerialDeviceInput
{
internal static Logger logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// Private variables
/// </summary>
private SerialDevice serialPort = null;
DataWriter dataWriteObject = null;
DataReader dataReaderObject = null;
private CancellationTokenSource ReadCancellationTokenSource;
#region Private Fields
private string _portName;
private uint _baudrate = 115200;
private ushort _dataBits = 8;
private SerialStopBitCount _stopbits= SerialStopBitCount.One;
private SerialParity _parity=SerialParity.None;
private object accessLock = new object();
/// <summary>
/// 状态
/// </summary>
private string Status { get; set; }
/// <summary>
/// 串口获取状态,
/// true:有可用串口
/// false:无可用串口
/// </summary>
private bool IsEnabled;//
private bool IsConnect=false;//
#endregion
#region Public Events
/// <summary>
/// SerialDeviceInput里边运行的当前状态
/// </summary>
public string SerialDeviceInputSatus { get => Status; }
/// <summary>
/// 获取可用串口状态
/// </summary>
public bool SerialPortIsEnabled { get => IsEnabled; }
#endregion
#region Public Events
/// <summary>
/// ComPort received event.
/// </summary>
public delegate void ComPortWasFoundEventHandler(object sender, ComPortWasFoundEventArgs args);
/// <summary>
/// Occurs when message received.
/// </summary>
public event ComPortWasFoundEventHandler ComPortReceived;
/// <summary>
/// Connected state changed event.
/// </summary>
public delegate void ConnectionStatusChangedEventHandler(object sender, ConnectionStatusChangedEventArgs args);
/// <summary>
/// Occurs when connected state changed.
/// </summary>
public event ConnectionStatusChangedEventHandler ConnectionStatusChanged;
/// <summary>
/// Message received event.
/// </summary>
public delegate void MessageReceivedEventHandler(object sender, MessageReceivedEventArgs args);
/// <summary>
/// Occurs when message received.
/// </summary>
public event MessageReceivedEventHandler MessageReceived;
#endregion
#region Public Members
/// <summary>
///
/// </summary>
/// <param name="portname"></param>
/// <param name="baudrate"></param>
/// <param name="parity"></param>
/// <param name="DataBits"></param>
/// <param name="stopbits"></param>
public void SetPort(string portname, uint baudrate = 115200, SerialParity parity = SerialParity.None, ushort DataBits=8, SerialStopBitCount stopbits = SerialStopBitCount.One)
{
if (portname != null)
{
this._portName = portname;
this._baudrate = baudrate;
this._stopbits = stopbits;
this._parity = parity;
Status = "串口设置成功";
}
else
{
Status = "串口号为空";
}
}
/// <summary>
/// - Create a DataReader object
/// - Create an async task to read from the SerialDevice InputStream
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void Listen()
{
try
{
if (serialPort != null)
{
dataReaderObject = new DataReader(serialPort.InputStream);
// keep reading the serial input
while (true)
{
await ReadAsync(ReadCancellationTokenSource.Token);
}
}
}
catch (TaskCanceledException tce)
{
Status = "Reading task was cancelled, closing device and cleaning up";
CloseDevice();
}
catch (Exception ex)
{
Status = ex.Message;
}
finally
{
// Cleanup once complete
if (dataReaderObject != null)
{
dataReaderObject.DetachStream();
dataReaderObject = null;
}
}
}
//转换数据
private string LoadData(uint bytesRead)
{
StringBuilder str_builder = new StringBuilder();
//转换缓冲区数据为16进制
while (dataReaderObject.UnconsumedBufferLength > 0)
{
str_builder.Append(dataReaderObject.ReadByte().ToString("x2"));
}
return str_builder.ToString().ToUpper();
}
/// <summary>
/// ReadAsync: Task that waits on data and reads asynchronously from the serial device InputStream
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task ReadAsync(CancellationToken cancellationToken)
{
//Task<UInt32> loadAsyncTask;
uint ReadBufferLength = 1024;
//// If task cancellation was requested, comply
//cancellationToken.ThrowIfCancellationRequested();
//// Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
//dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
//using (var childCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
//{
// // Create a task object to wait for data on the serialPort.InputStream
// loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(childCancellationTokenSource.Token);
// // Launch the task and wait
// UInt32 bytesRead = await loadAsyncTask;
// if (bytesRead > 0)
// {
// rcvdText.Text = dataReaderObject.ReadString(bytesRead);
// status.Text = "bytes read successfully!";
// }
//}
Task<UInt32> loadAsyncTask;
dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
//读取数据
loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask();
uint bytesRead = await loadAsyncTask;
//判断获取数据长度
if (bytesRead > 0)
{
//转换十六进制数据
string res = LoadData(bytesRead);
OnMessageReceived(new MessageReceivedEventArgs(res));
// Status = "Recived Data:"+ res;
}
}
public async void Connect()
{
try
{
serialPort = await SerialDevice.FromIdAsync(this._portName);
if (serialPort == null) return;
//串口已连接
IsConnect = true;
OnConnectionStatusChanged(new ConnectionStatusChangedEventArgs(true));
// Configure serial settings
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(200);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(200);
serialPort.BaudRate =this._baudrate;
serialPort.Parity = this._parity;
serialPort.StopBits =this._stopbits;
serialPort.DataBits =this._dataBits;
serialPort.Handshake = SerialHandshake.None;
// Display configured settings
Status = "串口: ";
Status += serialPort.BaudRate + "-";
Status += serialPort.DataBits + "-";
Status += serialPort.Parity.ToString() + "-";
Status += serialPort.StopBits;
// Create cancellation token object to close I/O operations when closing the device
ReadCancellationTokenSource = new CancellationTokenSource();
Listen();
}
catch (Exception ex)
{
Status = ex.Message;
}
}
/// <summary>
/// CloseDevice:
/// - Disposes SerialDevice object
/// - Clears the enumerated device Id list
/// </summary>
public void CloseDevice()
{
if (serialPort != null)
{
serialPort.Dispose();
IsConnect = false;
OnConnectionStatusChanged(new ConnectionStatusChangedEventArgs(false));
Status = "串口关闭";
}
serialPort = null;
}
/// <summary>
/// 检测到可用串口以对象集合返回,dis[i]选择,dis.Count查看总数,serialPort = await SerialDevice.FromIdAsync(dis[i].Id);
/// </summary>
/// <returns></returns>
public async void GetAvailablePorts_Dis()
{
DeviceInformationCollection dis = null;
try
{
string aqs = SerialDevice.GetDeviceSelector();
dis = await DeviceInformation.FindAllAsync(aqs);
Status = "检测到可用串口";
this.IsEnabled = true;
ComPortWasFound(new ComPortWasFoundEventArgs(dis));
}
catch (Exception ex)
{
throw ex;
}
}
private char[] HexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f' };
private bool CharInArray(char aChar, char[] charArray)
{
return (Array.Exists<char>(charArray, delegate (char a) { return a == aChar; }));
}
/// <summary>
/// 十六进制字符串转换字节数组
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public byte[] HexStringToByteArray(string s)
{
// s = s.Replace(" ", "");
if (s.Length % 2 != 0)
{
s = "0" + s;
}
StringBuilder sb = new StringBuilder(s.Length);
foreach (char aChar in s)
{
if (CharInArray(aChar, HexDigits))
sb.Append(aChar);
}
s = sb.ToString();
int bufferlength;
if ((s.Length % 2) == 1)
bufferlength = s.Length / 2 + 1;
else bufferlength = s.Length / 2;
byte[] buffer = new byte[bufferlength];
for (int i = 0; i < bufferlength - 1; i++)
buffer[i] = (byte)Convert.ToByte(s.Substring(2 * i, 2), 16);
if (bufferlength > 0)
buffer[bufferlength - 1] = (byte)Convert.ToByte(s.Substring(2 * (bufferlength - 1), (s.Length % 2 == 1 ? 1 : 2)), 16);
return buffer;
}
/// <summary>
/// 字节数组转换十六进制字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
return sb.ToString().ToUpper();
}
/// <summary>
///十六进制字符串转换为ASCII
/// </summary>
/// <param name="hexstring">一条十六进制字符串</param>
/// <returns>返回一条ASCII码</returns>
public string HexStringToAscii(string hexstring)
{
byte[] ss = HexStringToByteArray(hexstring);
string AA = null;
AA= new ASCIIEncoding().GetString(ss,0,ss.Length);
return AA;
}
/**/
/// <summary>
/// 16进制字符串转换为二进制数组
/// </summary>
/// <param name="hexstring">用空格切割字符串</param>
/// <returns>返回一个二进制字符串</returns>
private byte[] HexStringToBinary(string hexstring)
{
string[] tmpary = hexstring.Trim().Split(' ');
byte[] buff = new byte[tmpary.Length];
for (int i = 0; i < buff.Length; i++)
{
buff[i] = Convert.ToByte(tmpary[i], 16);
}
return buff;
}
/// <summary>
/// 文本方式发送字符串
/// </summary>
public async void SendMessage_StringAsText(string sendStr)
{
try
{
if (serialPort != null && IsConnect== true)
{
// Create the DataWriter object and attach to OutputStream
dataWriteObject = new DataWriter(serialPort.OutputStream);
//Launch the WriteAsync task to perform the write
await WriteStringAsync(sendStr);
}
else
{
Status ="串口未打开,发送数据失败";
}
}
catch (Exception ex)
{
Status = ex.Message;
}
finally
{
// Cleanup once complete
if (dataWriteObject != null)
{
dataWriteObject.DetachStream();
dataWriteObject = null;
}
}
}
/// <summary>
/// 输入16进制字符串按照16进制字节数组发送
/// </summary>
/// <param name="sendStr">16进制字符串</param>
public async void SendMessage_HexStringAsHexBytes(string sendStr)
{
try
{
if (serialPort != null && IsConnect == true)
{
// Create the DataWriter object and attach to OutputStream
dataWriteObject = new DataWriter(serialPort.OutputStream);
//Launch the WriteAsync task to perform the write
await WriteBytesAsync(HexStringToByteArray(sendStr));
}
else
{
Status = "串口未打开,发送数据失败";
}
}
catch (Exception ex)
{
Status = ex.Message;
}
finally
{
// Cleanup once complete
if (dataWriteObject != null)
{
dataWriteObject.DetachStream();
dataWriteObject = null;
}
}
}
/// <summary>
/// byte[]数组转为对应字符串发送
/// </summary>
/// <param name="sendStr"></param>
public async void SendMessage_BytesAsHexString(byte[] sendStr)
{
try
{
if (serialPort != null && IsConnect == true)
{
// Create the DataWriter object and attach to OutputStream
dataWriteObject = new DataWriter(serialPort.OutputStream);
//Launch the WriteAsync task to perform the write
await WriteStringAsync(ByteArrayToHexString(sendStr));
}
else
{
Status = "串口未打开,发送数据失败";
}
}
catch (Exception ex)
{
Status = ex.Message;
}
finally
{
// Cleanup once complete
if (dataWriteObject != null)
{
dataWriteObject.DetachStream();
dataWriteObject = null;
}
}
}
/// <summary>
/// byte[]数组直接发送
/// </summary>
/// <param name="sendBytes"></param>
public async void SendMessage_BytesAsBytes(byte[] sendBytes)
{
try
{
if (serialPort != null && IsConnect == true)
{
// Create the DataWriter object and attach to OutputStream
dataWriteObject = new DataWriter(serialPort.OutputStream);
//Launch the WriteAsync task to perform the write
await WriteBytesAsync(sendBytes);
}
else
{
Status = "串口未打开,发送数据失败";
}
}
catch (Exception ex)
{
Status = ex.Message;
}
finally
{
// Cleanup once complete
if (dataWriteObject != null)
{
dataWriteObject.DetachStream();
dataWriteObject = null;
}
}
}
/// <summary>
/// WriteAsync: Task that asynchronously writes data from the input text box 'sendText' to the OutputStream
/// </summary>
/// <returns></returns>
private async Task WriteStringAsync(string sendDataStr)
{
Task<UInt32> storeAsyncTask;
if (sendDataStr.Length != 0)
{
// Load the text from the sendText input text box to the dataWriter object
dataWriteObject.WriteString(sendDataStr);
// Launch an async task to complete the write operation
storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
UInt32 bytesWritten = await storeAsyncTask;
if (bytesWritten > 0)
{
Status = "Send String:" + sendDataStr;
Status += "bytes written successfully!";
}
sendDataStr = "";
}
else
{
Status = "Enter the text you want to write and then click on 'WRITE'";
}
}
/// <summary>
/// WriteAsync: Task that asynchronously writes data from the input text box 'sendText' to the OutputStream
/// </summary>
/// <returns></returns>
private async Task WriteBytesAsync(byte[] sendDataByte)
{
Task<UInt32> storeAsyncTask;
if (sendDataByte.Length != 0)
{
// Load the text from the sendText input text box to the dataWriter object
dataWriteObject.WriteBytes(sendDataByte);
// Launch an async task to complete the write operation
storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
UInt32 bytesWritten = await storeAsyncTask;
if (bytesWritten > 0)
{
Status = "Send String:" + BitConverter.ToString(sendDataByte);
Status += "bytes written successfully!";
}
sendDataByte = null;
}
else
{
Status = "Enter the text you want to write and then click on 'WRITE'";
}
}
#region Events Raising
/// <summary>
/// Raises the connected state changed event.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void OnConnectionStatusChanged(ConnectionStatusChangedEventArgs args)
{
logger.Debug(args.ConnectStatus);
if (ConnectionStatusChanged != null)
ConnectionStatusChanged(this, args);
}
/// <summary>
/// Raises the message received event.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void OnMessageReceived(MessageReceivedEventArgs args)
{
logger.Debug(args.Data.ToString());
if (MessageReceived != null)
MessageReceived(this, args);
}
/// <summary>
/// Raises the Comport can use event.
/// </summary>
/// <param name="args">ObservableCollection<DeviceInformation></param>
protected virtual void ComPortWasFound(ComPortWasFoundEventArgs args)
{
logger.Debug(args.DeviceInformationCollection[0].Id.ToString());
if (ComPortReceived != null)
ComPortReceived(this, args);
}
#endregion
}
}
/// <summary>
/// Connected state changed event arguments.
/// </summary>
public class ConnectionStatusChangedEventArgs
{
/// <summary>
/// The connected state.
/// </summary>
public readonly bool ConnectStatus;
/// <summary>
/// Initializes a new instance of the <see cref="SerialDeviceLib.ConnectionStatusChangedEventArgs"/> class.
/// </summary>
/// <param name="state">State of the connection (true = connected, false = not connected).</param>
public ConnectionStatusChangedEventArgs(bool state)
{
ConnectStatus = state;
}
}
/// <summary>
/// Message received event arguments.
/// </summary>
public class MessageReceivedEventArgs
{
/// <summary>
/// The data.
/// </summary>
public readonly string Data;
/// <summary>
/// Initializes a new instance of the <see cref="SerialDeviceLib.MessageReceivedEventArgs"/> class.
/// </summary>
/// <param name="data">Data:返回数据</param>
public MessageReceivedEventArgs(string data)
{
Data = data;
}
}
/// <summary>
/// 发现可用串口事件
/// </summary>
public class ComPortWasFoundEventArgs
{
/// <summary>
/// DeviceInformationCollection[i].ID串口号
/// DeviceInformationCollection.Count
/// </summary>
public readonly DeviceInformationCollection DeviceInformationCollection;
/// <summary>
/// Initializes a new instance of the <see cref="SerialDeviceLib.ComPortReceivedEventArgs"/> class.
/// </summary>
/// <param name="DeviceInformationCollection">DeviceInformationCollection.ID为串口号</param>
public ComPortWasFoundEventArgs(DeviceInformationCollection data)
{
DeviceInformationCollection = data;
}
}
#endregion