-
Notifications
You must be signed in to change notification settings - Fork 2
/
AsyncSerial.cs
622 lines (553 loc) · 20.5 KB
/
AsyncSerial.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
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
#region Licence
//MIT License(MIT)
/* AsyncSerial.cs Version 4.3.0 */
/* Copyright(c) 2018 Mike Simpson */
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#endregion
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.IO.Ports;
namespace AsyncSerial
{
#region classRegion
/// <summary>
/// This class allows a serial port to be set up with different parameters.
/// It contains functions to open and close the port safely
/// Implements IDisposable
/// </summary>
public class AsyncSerial : IDisposable
{
#region Varibles
/// <summary>
/// Underlying SerialPort object supported by .NET
/// </summary>
SerialPort dotNETPort;
/// <summary>
/// Contains an error message if any of the functions return false
/// </summary>
private string portErrorMessage = "";
#endregion
#region Constructo
/// <summary>
/// Constructor take 5 arguments to correctly configure the serial port
/// </summary>
/// <param name="portName"> Sets the port name of the serial port (string) </param>
/// <param name="baudRate"> Sets the baud rate of the serial port (int) </param>
/// <param name="parity"> Sets the parity bit system to use (string) </param>
/// <param name="dataBits"> Sets the number of data bits of the serial port (int) </param>
/// <param name="stopBits"> Sets the number of stop bits of the serial port (string) </param>
public AsyncSerial(string portName, int baudRate, string parity, int dataBits, string stopBits)
{
dotNETPort = new SerialPort();
if (portName != null & parity != null & stopBits != null)
{
try
{
switch (stopBits.ToLower(CultureInfo.CurrentCulture))
{
case "none": dotNETPort.StopBits = StopBits.None; break;
case "onepointfive": dotNETPort.StopBits = StopBits.OnePointFive; break;
case "two": dotNETPort.StopBits = StopBits.Two; break;
default: dotNETPort.StopBits = StopBits.One; break;
}
switch (parity.ToLower(CultureInfo.CurrentCulture))
{
case "even": dotNETPort.Parity = System.IO.Ports.Parity.Even; break;
case "odd": dotNETPort.Parity = System.IO.Ports.Parity.Odd; break;
case "mark": dotNETPort.Parity = System.IO.Ports.Parity.Mark; break;
case "space": dotNETPort.Parity = System.IO.Ports.Parity.Space; break;
default: dotNETPort.Parity = System.IO.Ports.Parity.None; break;
}
dotNETPort.PortName = portName;
dotNETPort.BaudRate = baudRate;
dotNETPort.DataBits = dataBits;
}
catch (IOException e)
{
exceptionCatch(e);
}
catch (ArgumentNullException e)
{
exceptionCatch(e);
}
catch (ArgumentOutOfRangeException e)
{
exceptionCatch(e);
}
catch (ArgumentException e)
{
exceptionCatch(e);
}
catch (InvalidOperationException e)
{
exceptionCatch(e);
}
}
}
#endregion
#region Methods
/// <summary>
/// Calls the .NET IDisposable method on the serial port and cleans up with the garbage collector
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Uses closes the port properly and disposes using the serial port IDisposable method
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
try
{
dotNETPort.Close();
}
catch (IOException e)
{
exceptionCatch(e);
}
dotNETPort.Dispose();
}
// free native resources if there are any
}
/// <summary>
/// Open the serial port with defined values, returns false and puts error message in 'portError' if the port fails to open
/// </summary>
public bool StartReceive()
{
if (dotNETPort != null)
{
try
{
dotNETPort.Open();
}
catch (InvalidOperationException Ex)
{
exceptionCatch(Ex);
return false;
}
catch (IOException Ex)
{
exceptionCatch(Ex);
return false;
}
catch (UnauthorizedAccessException Ex)
{
exceptionCatch(Ex);
return false;
}
catch (ArgumentOutOfRangeException Ex)
{
exceptionCatch(Ex);
return false;
}
catch (ArgumentException Ex)
{
exceptionCatch(Ex);
return false;
}
finally
{
dotNETPort.DataReceived += new SerialDataReceivedEventHandler(port_OnDataRecived); //sets up new event handler
}
return true;
}
else
{
exceptionCatch("Port Null");
return false;
}
}
/// <summary>
/// Exceptions can be passed to be recorded in the port error message
/// </summary>
/// <param name="Ex"></param>
private void exceptionCatch(Exception Ex)
{
portErrorMessage = defaltErrorText() + Ex.Message;
}
/// <summary>
/// Strings can be passed to be recorded in the port error message
/// </summary>
/// <param name="Ex"></param>
private void exceptionCatch(string Ex)
{
portErrorMessage = defaltErrorText() + Ex;
}
/// <summary>
/// Returns a stringing containing defalt text about errors which specific error information can be appended to
/// </summary>
/// <returns></returns>
private string defaltErrorText()
{
return "An error occurred while trying to open the serial port " + dotNETPort.PortName.ToString() + "\nPlease check the port settings and try again\n" + "Error message - ";
}
/// <summary>
/// Safely closes the port if open
/// </summary>
public void StopReceive()
{
try
{
if (dotNETPort != null)
{
if (dotNETPort.IsOpen)
{
dotNETPort.Close();
}
}
}
catch (ArgumentException Ex)
{
exceptionCatch(Ex);
}
catch (IOException Ex)
{
exceptionCatch(Ex);
}
}
/// <summary>
/// This function can be used to get the port status externally
/// </summary>
public bool IsPortOpen
{
get
{
if (dotNETPort != null)
{
return dotNETPort.IsOpen;
}
else
{
return false;
}
}
}
/// <summary>
/// Returns an empty/null string if there is no error, or the error message as a string to be displayed to the user, auto clears once read
/// </summary>
public string CurrentPortError
{
get
{
string returnError = portErrorMessage;
portErrorMessage = "";
return returnError;
}
}
/// <summary>
/// Transmitts a string over the serial port using the Serial.Write() method
/// Encoding should be considered before passing string
/// </summary>
/// <param name="sendData"></param>
public bool SendSerialData(string sendData)
{
if (IsPortOpen & sendData != null)
{
try
{
dotNETPort.Write(sendData);
return true;
}
catch (InvalidOperationException Ex)
{
exceptionCatch(Ex);
return false;
}
catch (ArgumentNullException Ex)
{
exceptionCatch(Ex);
return false;
}
catch (OverflowException Ex)
{
exceptionCatch(Ex);
return false;
}
}
else
{
exceptionCatch("Port nto open");
return false;
}
}
/// <summary>
/// Transmitts a byte array over the serial port using the Serial.write() method
/// Can be used to send custom data formats without text overhead
/// </summary>
/// <param name="sendData"></param>
public bool SendSerialData(byte[] sendData)
{
if (IsPortOpen & sendData != null)
{
try
{
dotNETPort.Write(sendData, 0, sendData.Length);
return true;
}
catch (InvalidOperationException Ex)
{
exceptionCatch(Ex);
return false;
}
catch (ArgumentNullException Ex)
{
exceptionCatch(Ex);
return false;
}
catch (ArgumentOutOfRangeException Ex)
{
exceptionCatch(Ex);
return false;
}
catch (ArgumentException Ex)
{
exceptionCatch(Ex);
return false;
}
catch(OverflowException Ex)
{
exceptionCatch(Ex);
return false;
}
}
else
{
exceptionCatch("Port not open");
return false;
}
}
#endregion
#region Events
/// <summary>
/// This function executes when data is recived on the serial port and triggers the event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void port_OnDataRecived(object sender, SerialDataReceivedEventArgs e)
{
int lengthToRead = dotNETPort.BytesToRead;
byte[] rxBytes = new byte[lengthToRead];
dotNETPort.Read(rxBytes, 0, lengthToRead);
OnPacketReceived(rxBytes);
}
/// <summary>
/// The event that triggers when ever data is recived on the serial port
/// Operations started from this even run on their own thread
/// </summary>
public event EventHandler<PacketEventArgs> PacketReceived;
/// <summary>
/// Sends packets to calling function to be processed
/// </summary>
/// <param name="packet"></param>
protected virtual void OnPacketReceived(byte[] packet)
{
PacketReceived?.Invoke(this, new PacketEventArgs(packet));
}
}
/// <summary>
/// Custom class for packet recived events, based on EventArgs
/// </summary>
public class PacketEventArgs : EventArgs
{
private byte[] packet;
/// <summary>
/// Allows new packets to be read by and set by constructor
/// </summary>
public byte[] Packet()
{
return packet;
}
/// <summary>
/// Constructor for PacketEventArgs, reads new packets
/// </summary>
/// <param name="inPacket"></param>
public PacketEventArgs(byte[] inPacket)
{
packet = inPacket;
}
}
#endregion
#endregion
#region PortParameters
/// <summary>
/// Provides string arrays containing common (or enforced) port parameters which you can use to populate menus in foreach loops etc
/// ReadOnlyCollections can be cast to a array using .ToArray()
/// </summary>
public static class SerialPortParameters
{
private static string[] commonBaudRates = new string[] {"110", "300", "600", "1200", "2400", "4800", "9600", "14400", "19200", "28800", "38400", "56000",
"57600", "115200", "128000", "153600", "230400", "256000", "460800", "921600", "1843200"};
private static ReadOnlyCollection<string> readOnlyCommonBaudRates = new ReadOnlyCollection<string>(commonBaudRates);
private static string[] commonStopBits = new string[] { "none", "one", "onepointfive", "two" };
private static ReadOnlyCollection<string> readOnlyCommonStopBits = new ReadOnlyCollection<string>(commonStopBits);
private static string[] commonParityBits = new string[] { "none", "odd", "even", "mark", "space" };
private static ReadOnlyCollection<string> readOnlyCommonParityBits = new ReadOnlyCollection<string>(commonParityBits);
private static string[] commonDataBits = new string[] { "7", "8" };
private static ReadOnlyCollection<string> readOnlyCommonDataBits = new ReadOnlyCollection<string>(commonDataBits);
/// <summary>
/// Commonly used serial baud rates, system dependant but usually produced by a 1.843200MHz oscillator
/// </summary>
public static ReadOnlyCollection<string> BaudRates
{
get
{
return readOnlyCommonBaudRates;
}
}
/// <summary>
/// Only supported stop bits as of .NET 4.6.1
/// </summary>
public static ReadOnlyCollection<string> StopBits
{
get
{
return readOnlyCommonStopBits;
}
}
/// <summary>
/// Only supported parity bits as of .NET 4.6.1
/// </summary>
public static ReadOnlyCollection<string> ParityBits
{
get
{
return readOnlyCommonParityBits;
}
}
/// <summary>
/// Genrally supported data bits (7 for ASCII, 8 for ease of use)
/// </summary>
public static ReadOnlyCollection<string> DataBits
{
get
{
return readOnlyCommonDataBits;
}
}
/// <summary>
/// Gets currently recognised serial port names connected to the computer, can be called when ever refreshed list is needed
/// </summary>
/// <returns></returns>
public static ReadOnlyCollection<string> GetSerialPorts
{
get
{
ReadOnlyCollection<string> portNames = new ReadOnlyCollection<string>(SerialPort.GetPortNames());
return portNames;
}
}
}
#endregion
#region ExampleCode
// <summary>
// WINFORMS EXAMPLE
// SerialPortParameters can be used to fill form elements for easier user selection
// </summary>
//private void Form_Load(object sender, EventArgs e)
//{
// serialPortBaud_CB.Items.AddRange(SerialPortParameters.BaudRates.ToArray());
// serialPortData_CB.Items.AddRange(SerialPortParameters.DataBits.ToArray());
// serialPortParity_CB.Items.AddRange(SerialPortParameters.ParityBits.ToArray());
// serialPortStop_CB.Items.AddRange(SerialPortParameters.StopBits.ToArray());
//}
// <summary>
// XAML EXAMPLE
// Adds common values for each serial port setting to a combobox and selects a default
// </summary>
//private void populateSerialSettings()
//{
// List<string> itemsToAdd = SerialPortParameters.BaudRates.ToList();
// itemsToAdd.ForEach(item => serialPortBaudRate_CB.Items.Add(item));
// serialPortBaudRate_CB.SelectedItem = "115200";
// itemsToAdd.Clear();
// itemsToAdd = SerialPortParameters.DataBits.ToList();
// itemsToAdd.ForEach(item => serialPortDataBits_CB.Items.Add(item));
// serialPortDataBits_CB.SelectedItem = "8";
// itemsToAdd.Clear();
// itemsToAdd = SerialPortParameters.ParityBits.ToList();
// itemsToAdd.ForEach(item => serialPortParity_CB.Items.Add(item));
// serialPortParity_CB.SelectedItem = "none";
// itemsToAdd.Clear();
// itemsToAdd = SerialPortParameters.StopBits.ToList();
// itemsToAdd.ForEach(item => serialPortStopBits_CB.Items.Add(item));
// serialPortStopBits_CB.SelectedItem = "one";
//}
// <summary>
// Example of how to setup the serial port and event handler
// </summary>
//private void setupPort()
//{
// string portName = serialPortName_TB.Text;
// int portBaud = Convert.ToInt32(serialPortBaud_CB.SelectedItem.ToString());
// string portParity = serialPortParity_CB.SelectedItem.ToString();
// int portData = Convert.ToInt32(serialPortData_CB.SelectedItem.ToString());
// string portStop = serialPortStop_CB.SelectedItem.ToString();
// comPort = new AsyncSerial(portName, portBaud, portParity, portData, portStop); //comPort should be declared at class level
//if (comPort.CurrentPortError != null | comPort.CurrentPortError != "")
//{
// //No Error
//}
//else
//{
// //Handle Error
// comPort.clearPortError(); //Reset for next attempt
//}
//comPort.PacketReceived += onComSerial_receive;
//if (comPort.StartReceive())
//{
//}
//else
//{
// //Port Error
//}
//}
// <summary>
// This is the code that gets called when serial data is recived
// </summary>
//private void onMySerial_receive(object sender, PacketEventArgs e)
//{
// byte[] packet = e.Packet;
// string supplyMessage = Encoding.Default.GetString(packet); //Conver to string if needed
// //WINFORM EXAMPLE
// this.Invoke((MethodInvoker)delegate
// {
// someFormItem.property = someData; //Since the port is asynchronous you cannot change UI elements without a cross thread Invoker
// });
// //XAML EXAMPLE
// Dispatcher.Invoke(() => someFormItem.property = someData); //Since the port is asynchronous you cannot change UI elements without a cross thread Invoker
//}
//private void sendSerialData()
//{
// if (comPort.IsPortOpen()) //you can check if the port is open (device could be removed etc)
// {
// comPort.SendSerialData("you can send strings or byte arrays");
// }
// else
// {
// //port isn't open
// }
//}
#endregion
}