-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerial.cs
699 lines (601 loc) · 22.3 KB
/
Serial.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
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Runtime.Serialization;
using System.Threading;
using Unitronics.ComDriver.Messages;
using System.IO;
namespace Unitronics.ComDriver
{
public class Serial : Channel
{
#region Locals
private SerialPort m_serialPort = new SerialPort();
private const String STX_STRING = "/_OPLC";
private SerialPortNames m_SerialPortNames;
private bool m_AutoDetectComParams = true;
private bool messageReceived = false;
private byte[] resultBytes = new byte[0];
#endregion
#region Constructors
public Serial()
{
m_serialPort.PortName = SerialPortNames.COM1.ToString();
}
public Serial(SerialPortNames portName, BaudRate baudRate, int retry, int timeOut, DataBits dataBits,
Parity parity, StopBits stopBit)
: base(retry, timeOut)
{
m_SerialPortNames = portName;
m_serialPort.BaudRate = (int) baudRate;
m_serialPort.DataBits = (int) dataBits;
m_serialPort.Parity = parity;
m_serialPort.StopBits = stopBit;
}
public Serial(SerialPortNames portName, BaudRate baudRate, int retry, int timeOut, DataBits dataBits,
Parity parity, StopBits stopBit, bool autoDetectComParams)
: base(retry, timeOut)
{
m_SerialPortNames = portName;
m_serialPort.BaudRate = (int) baudRate;
m_serialPort.DataBits = (int) dataBits;
m_serialPort.Parity = parity;
m_serialPort.StopBits = stopBit;
m_AutoDetectComParams = autoDetectComParams;
}
public Serial(SerialPortNames portName, BaudRate baudRate)
: base(3, 3000)
{
m_SerialPortNames = portName;
m_serialPort.DataBits = 8;
m_serialPort.Parity = Parity.None;
m_serialPort.StopBits = StopBits.One;
m_serialPort.BaudRate = (int) baudRate;
}
#endregion
#region Public
internal override string GetLoggerChannelText()
{
return Utils.HelperComDriverLogger.GetLoggerChannel(this);
}
internal override void SendString(string text, byte messageEnumerator, bool isIdMessage = false,
bool suppressEthernetHeader = false)
{
if (!m_serialPort.IsOpen)
m_serialPort.Open();
try
{
//m_serialPort.DiscardInBuffer();
//m_serialPort.DiscardOutBuffer();
m_serialPort.Write(text);
}
catch (InvalidOperationException)
{
throw;
}
catch (ArgumentOutOfRangeException)
{
throw;
}
catch (ArgumentException)
{
throw;
}
catch (TimeoutException)
{
throw;
}
//finally
//{
// System.Threading.Thread.Sleep(10);
//}
}
internal override void SendBytes(byte[] bytes, byte messageEnumerator)
{
if (!m_serialPort.IsOpen)
m_serialPort.Open();
try
{
//m_serialPort.DiscardInBuffer();
//m_serialPort.DiscardOutBuffer();
m_serialPort.Write(bytes, 0, bytes.Length);
}
catch (InvalidOperationException)
{
throw;
}
catch (ArgumentOutOfRangeException)
{
throw;
}
catch (ArgumentException)
{
throw;
}
catch (TimeoutException)
{
throw;
}
//finally
//{
// System.Threading.Thread.Sleep(10);
//}
}
internal override string ReceiveString()
{
messageReceived = false;
resultBytes = new byte[0];
m_serialPort.ReadTimeout = TimeOut;
while (!messageReceived)
{
byte[] receivedData = new byte[1024];
int received = m_serialPort.Read(receivedData, 0, receivedData.Length);
serialPortReceiveString(receivedData, received);
}
if (!messageReceived)
{
throw new TimeoutException();
}
return ASCIIEncoding.ASCII.GetString(resultBytes);
}
private void serialPortReceiveString(byte[] incomingBytes, int count)
{
bool bStxFound = false;
bool bEtxFound = false;
int index = 0;
int checksum = 0;
string resultString;
if (count > 0)
{
byte[] temp = new byte[count];
Array.Copy(incomingBytes, 0, temp, 0, count);
ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
Utils.HelperComDriverLogger.GetLoggerChannel(this), temp);
byte[] tmpBuffer = new byte[resultBytes.Length + count];
Array.Copy(resultBytes, 0, tmpBuffer, 0, resultBytes.Length);
Array.Copy(temp, 0, tmpBuffer, resultBytes.Length, count);
resultBytes = tmpBuffer;
}
resultString = ASCIIEncoding.ASCII.GetString(resultBytes);
if (resultString.Length > 0)
{
index = resultString.IndexOf("/"); // find the STX
if (index >= 0)
{
resultString = resultString.Substring(index, resultString.Length - index);
byte[] tempBuffer = new byte[resultBytes.Length - index];
Array.Copy(resultBytes, index, tempBuffer, 0, resultBytes.Length - index);
resultBytes = tempBuffer;
bStxFound = true;
}
else
{
if (resultString.Length > 100)
{
throw new ComDriveExceptions("STX is missing",
ComDriveExceptions.ComDriveException.CommunicationTimeout);
}
}
index = resultString.IndexOf("\r"); // find the ETX
if (index >= 0)
{
resultString = resultString.Substring(0, index + 1);
bEtxFound = true;
}
}
else
{
return;
}
if (!bStxFound || !bEtxFound)
return;
for (int i = 2; i < resultString.Length - 3; i++)
{
checksum += resultBytes[i];
}
string CRC = Utils.DecimalToHex(checksum % 256);
if (CRC != resultString.Substring(resultString.Length - 3, 2))
{
throw new ComDriveExceptions("Wrong Data Checksum", ComDriveExceptions.ComDriveException.ChecksumError);
}
messageReceived = true;
}
internal override byte[] ReceiveBytes()
{
messageReceived = false;
resultBytes = new byte[0];
m_serialPort.ReadTimeout = TimeOut;
while (!messageReceived)
{
byte[] receivedData = new byte[1024];
int received = m_serialPort.Read(receivedData, 0, receivedData.Length);
serialPortReceiveBytes(receivedData, received);
}
if (!messageReceived)
{
throw new TimeoutException();
}
return resultBytes;
}
private void serialPortReceiveBytes(byte[] incomingBytes, int count)
{
bool bStxFound = false;
byte[] tempBuffer;
string bufferAsString;
int index = 0;
UInt16 pcCheckSum;
int totalLength = 0;
const int HEADER_LENGTH = 24;
if (count > 0)
{
byte[] temp = new byte[count];
Array.Copy(incomingBytes, 0, temp, 0, count);
ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
Utils.HelperComDriverLogger.GetLoggerChannel(this), temp);
byte[] tmpBuffer = new byte[resultBytes.Length + count];
Array.Copy(resultBytes, 0, tmpBuffer, 0, resultBytes.Length);
Array.Copy(temp, 0, tmpBuffer, resultBytes.Length, count);
resultBytes = tmpBuffer;
}
if (resultBytes.Length > 0)
{
bufferAsString = ASCIIEncoding.ASCII.GetString(resultBytes);
index = bufferAsString.IndexOf(STX_STRING);
if (index >= 0)
{
tempBuffer = new byte[resultBytes.Length - index];
Array.Copy(resultBytes, index, tempBuffer, 0, resultBytes.Length - index);
resultBytes = tempBuffer;
bStxFound = true;
}
else
{
if (resultBytes.Length > 100)
{
throw new ComDriveExceptions("STX is missing",
ComDriveExceptions.ComDriveException.CommunicationTimeout);
}
}
}
else
{
return;
}
if (!bStxFound)
return;
if (resultBytes.Length < HEADER_LENGTH)
return;
pcCheckSum = Utils.calcCheckSum(ref resultBytes, 0, 21);
if (pcCheckSum != BitConverter.ToUInt16(resultBytes, 22))
{
throw new ComDriveExceptions("Wrong Header Checksum",
ComDriveExceptions.ComDriveException.ChecksumError);
}
totalLength = BitConverter.ToUInt16(resultBytes, 20) +
HEADER_LENGTH + 3; // 3 for data checksum + ETX
if (resultBytes.Length < totalLength)
return;
tempBuffer = new byte[totalLength];
Array.Copy(resultBytes, 0, tempBuffer, 0, totalLength);
resultBytes = tempBuffer;
tempBuffer = null;
pcCheckSum = Utils.calcCheckSum(ref resultBytes, 24, totalLength - 4);
if (pcCheckSum != BitConverter.ToUInt16(resultBytes, totalLength - 3))
{
throw new ComDriveExceptions("Wrong Data Checksum", ComDriveExceptions.ComDriveException.ChecksumError);
}
if (resultBytes[totalLength - 1] != 92) // 92 is '\' which is the ETX
{
throw new ComDriveExceptions("ETX is missing", ComDriveExceptions.ComDriveException.ETXMissing);
}
messageReceived = true;
}
internal override void Connect()
{
if (!m_serialPort.IsOpen)
{
CheckPortName(m_SerialPortNames);
m_serialPort.PortName = m_SerialPortNames.ToString();
try
{
m_serialPort.DtrEnable = true;
m_serialPort.RtsEnable = true;
m_serialPort.Open();
}
catch (Exception ex)
{
throw new ComDriveExceptions(ex.Message, ComDriveExceptions.ComDriveException.PortInUse);
}
string text = ConnectionStatus.ConnectionOpened + " with BR" + m_serialPort.BaudRate.ToString() +
"(" + m_serialPort.DataBits.ToString() + ", " + m_serialPort.Parity.ToString() + ", " +
m_serialPort.StopBits.ToString() + ")";
ComDriverLogger.LogConnectionState(DateTime.Now,
Utils.HelperComDriverLogger.GetLoggerChannel(this), text);
}
}
public override void Disconnect()
{
if (base.QueueCount != 0)
return;
if (m_serialPort.IsOpen)
{
m_serialPort.Close();
AlreadyInitialized = false;
try
{
ComDriverLogger.LogConnectionState(DateTime.Now,
Utils.HelperComDriverLogger.GetLoggerChannel(this),
ConnectionStatus.ConnectionClosed.ToString());
}
catch
{
}
m_serialPort.Dispose();
}
}
public void DtrSwitch()
{
m_serialPort.DtrEnable = !m_serialPort.DtrEnable;
}
public bool z_SendBreak()
{
try
{
bool isOpened = m_serialPort.IsOpen;
if (!m_serialPort.IsOpen)
m_serialPort.Open();
m_serialPort.BreakState = true;
Thread.Sleep(500);
m_serialPort.BreakState = false;
Thread.Sleep(1000);
if (!isOpened)
m_serialPort.Close();
return true;
}
catch
{
return false;
}
}
internal bool BreakState
{
get { return m_serialPort.BreakState; }
set { m_serialPort.BreakState = value; }
}
internal string SetPlcConnetionParamsU90(int dataBits, System.IO.Ports.Parity parity,
System.IO.Ports.StopBits stopBits)
{
// The same as the break command, but for U90 PLCs
string SetPlcConnetionParamsU90 = "CPSR";
int nibble = 0;
SetPlcConnetionParamsU90 += "A"; // Flow control + Timeout 60 seconds
SetPlcConnetionParamsU90 += "7"; // CANBus baudrate = 10Kb
nibble += dataBits - 7;
switch (parity)
{
case System.IO.Ports.Parity.Even:
nibble += 0;
break;
case System.IO.Ports.Parity.Odd:
nibble += 2;
break;
case System.IO.Ports.Parity.None:
nibble += 4;
break;
default:
throw new ComDriveExceptions("Invalid Parity for U90 PLC Connection!",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
switch (stopBits)
{
case System.IO.Ports.StopBits.One:
nibble += 0;
break;
case System.IO.Ports.StopBits.Two:
nibble += 8;
break;
default:
throw new ComDriveExceptions("Invalid Stop Bits for U90 PLC Connection!",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
SetPlcConnetionParamsU90 += nibble.ToString("X");
switch (BaudRate)
{
case BaudRate.BR110:
SetPlcConnetionParamsU90 += "1";
break;
case BaudRate.BR300:
SetPlcConnetionParamsU90 += "2";
break;
case BaudRate.BR600:
SetPlcConnetionParamsU90 += "3";
break;
case BaudRate.BR1200:
SetPlcConnetionParamsU90 += "4";
break;
case BaudRate.BR2400:
SetPlcConnetionParamsU90 += "5";
break;
case BaudRate.BR4800:
SetPlcConnetionParamsU90 += "6";
break;
case BaudRate.BR9600:
SetPlcConnetionParamsU90 += "7";
break;
case BaudRate.BR19200:
SetPlcConnetionParamsU90 += "8";
break;
case BaudRate.BR38400:
SetPlcConnetionParamsU90 += "9";
break;
case BaudRate.BR57600:
SetPlcConnetionParamsU90 += "A";
break;
case BaudRate.BR115200:
// Don't throw an exception... since if it's not a U90 then this exception is wrong
SetPlcConnetionParamsU90 += "A";
break;
default:
throw new ComDriveExceptions("Invalid Baudrate for U90 PLC Connection!",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
return SetPlcConnetionParamsU90;
}
internal string BreakCommand()
{
string breakCommand = "CPC";
breakCommand += "1"; // plc port
breakCommand += "T"; // Temp change
switch (BaudRate)
{
case BaudRate.BR110:
breakCommand += "01";
break;
case BaudRate.BR300:
breakCommand += "02";
break;
case BaudRate.BR600:
breakCommand += "03";
break;
case BaudRate.BR1200:
breakCommand += "04";
break;
case BaudRate.BR2400:
breakCommand += "05";
break;
case BaudRate.BR4800:
breakCommand += "06";
break;
case BaudRate.BR9600:
breakCommand += "07";
break;
case BaudRate.BR19200:
breakCommand += "08";
break;
case BaudRate.BR38400:
breakCommand += "09";
break;
case BaudRate.BR57600:
breakCommand += "0A";
break;
case BaudRate.BR115200:
breakCommand += "0B";
break;
}
breakCommand += "FF"; // Timeout - no change
breakCommand += "FF"; // Flow Control - no change
return breakCommand;
}
internal override bool IsEquivalentChannel(Channel anotherChanel)
{
Serial serial = anotherChanel as Serial;
if (serial.PortName == PortName)
{
return true;
}
else
{
return false;
}
}
#endregion
#region Properties
public BaudRate BaudRate
{
get { return (BaudRate) m_serialPort.BaudRate; }
set { m_serialPort.BaudRate = (int) value; }
}
public DataBits DataBits
{
get { return (DataBits) m_serialPort.DataBits; }
set { m_serialPort.DataBits = (int) value; }
}
public Parity Parity
{
get { return m_serialPort.Parity; }
set { m_serialPort.Parity = value; }
}
public StopBits StopBits
{
get { return m_serialPort.StopBits; }
set { m_serialPort.StopBits = value; }
}
public SerialPortNames PortName
{
get { return m_SerialPortNames; }
set
{
if (m_SerialPortNames != value)
{
if (PLCFactory.ValidateChannelPropertyChange(this, value))
{
m_SerialPortNames = value;
}
else
{
throw new ComDriveExceptions(
"Cannot change Serial port name since it will result a duplicated channel with the same Port name",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
}
}
}
public bool AutoDetectComParams
{
get { return m_AutoDetectComParams; }
set { m_AutoDetectComParams = value; }
}
public override bool Connected
{
get { return m_serialPort.IsOpen; }
}
#endregion
#region Private
private void CheckPortName(SerialPortNames portName)
{
string[] portNames = SerialPort.GetPortNames();
if (!portNames.Contains(portName.ToString()))
{
throw new ComDriveExceptions("Specified Serial Port doesn't exist!",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
}
private void appendReceivedDataToArray(ref byte[] incomingData)
{
byte[] tempBuffer;
byte[] copyOfBuffer;
int bytesToRead;
bytesToRead = m_serialPort.BytesToRead;
if (bytesToRead > 0)
{
tempBuffer = new byte[bytesToRead];
m_serialPort.Read(tempBuffer, 0, bytesToRead);
copyOfBuffer = incomingData;
incomingData = new byte[copyOfBuffer.Length + tempBuffer.Length];
Array.Copy(copyOfBuffer, 0, incomingData, 0, copyOfBuffer.Length);
Array.Copy(tempBuffer, 0, incomingData, copyOfBuffer.Length, tempBuffer.Length);
ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
Utils.HelperComDriverLogger.GetLoggerChannel(this), tempBuffer);
}
}
private void appendReceivedDataToString(ref string incomingData)
{
byte[] tempBuffer;
string bufferAsString;
int bytesToRead;
bytesToRead = m_serialPort.BytesToRead;
if (bytesToRead > 0)
{
tempBuffer = new byte[bytesToRead];
m_serialPort.Read(tempBuffer, 0, bytesToRead);
bufferAsString = ASCIIEncoding.ASCII.GetString(tempBuffer);
incomingData += bufferAsString;
ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
Utils.HelperComDriverLogger.GetLoggerChannel(this), tempBuffer);
}
}
#endregion
}
}