-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLCFactory.cs
754 lines (693 loc) · 27.8 KB
/
PLCFactory.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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Unitronics.ComDriver.Executers;
using Unitronics.ComDriver.Messages;
using System.Xml.Serialization;
namespace Unitronics.ComDriver
{
public static class PLCFactory
{
#region Locals
private static List<Channel> m_channels = new List<Channel>();
private static object objectLocker = new object();
private static bool m_ActivationService = false;
private static bool m_WorkWithXmlInternally = false;
private static ushort messageDelay = 0;
#endregion
#region IPLCFactory Members
public static PLC GetPLC(Channel channel, int unitId, bool forceJazz = false)
{
bool isConnected = false;
return getPLC(ref channel, unitId, ref isConnected, forceJazz);
}
/// <summary>
/// if plcName is not null then after connecting to the PLC object
/// it checks if the PLC Name matches plcName. If not then
/// ComDriveException.PlcNameMismatch is being throws
/// </summary>
public static PLC GetPLC(Channel channel, int unitId, string plcName, bool forceJazz = false)
{
bool isConnected = false;
PLC plc;
plc = getPLC(ref channel, unitId, ref isConnected, forceJazz);
// if plcName is not null then after connecting to the PLC object
// it checks if the PLC Name matches plcName. If not then exception
// is being throws
// Prevent checking for PLC Name for U90 PLCs or when the PLC does not support Binary Commands
// (Like in Preboot and such...)
var plcVersion = plc.Version;
if (plcVersion.SupportedExecuters[ExecutersType.BasicBinary] == true)
{
if (plcName != null)
{
string plcNameOnPlc = plc.PlcName;
string plcNameWithoutEncoding = Encoding.UTF7.GetString(ASCIIEncoding.Default.GetBytes(plcName));
if (plcNameWithoutEncoding != plcNameOnPlc)
{
try
{
if ((!isConnected) && (channel.GetType() != typeof(EthernetListener)))
channel.Disconnect();
}
catch
{
}
throw new ComDriveExceptions(
"The given PLC name: '" + plcName +
"' doesn't match the actual PLC Name of the PLC you tried to connect to.",
ComDriveExceptions.ComDriveException.PlcNameMismatch);
}
}
}
else if (plcVersion.SuppressEthernetHeader && plcName != null)
{
string plcNameOnPlc = plc.PlcName;
string plcNameWithoutEncoding = Encoding.UTF7.GetString(ASCIIEncoding.Default.GetBytes(plcName));
if (plcNameWithoutEncoding != plcNameOnPlc)
{
try
{
if ((!isConnected) && (channel.GetType() != typeof(EthernetListener)))
channel.Disconnect();
}
catch
{
}
throw new ComDriveExceptions(
"The given PLC name: '" + plcName +
"' doesn't match the actual PLC Name of the PLC you tried to connect to.",
ComDriveExceptions.ComDriveException.PlcNameMismatch);
}
}
return GetPLC(channel, unitId, forceJazz);
}
public static void GetPLC(EthernetListener ethernetListener)
{
try
{
bool isConnected = false;
Channel plcChannel = getChannel(ethernetListener, ref isConnected);
EthernetListener listener = plcChannel as EthernetListener;
if (listener == null)
throw new ComDriveExceptions("Ethernet Listener could not be intialized due to unexpected error",
ComDriveExceptions.ComDriveException.UnexpectedError);
listener.Listen();
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
public static void GetPLC(ListenerServer litenerServer)
{
try
{
bool isConnected = false;
Channel plcChannel = getChannel(litenerServer, ref isConnected);
ListenerServer listener = plcChannel as ListenerServer;
if (listener == null)
throw new ComDriveExceptions("Listener Server could not be intialized due to unexpected error",
ComDriveExceptions.ComDriveException.UnexpectedError);
listener.Listen();
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
public static PLC GetPLC(string xmlFilePath)
{
PlcComConfig plcComConfig;
using (FileStream fs = new FileStream(xmlFilePath, FileMode.Open))
{
XmlSerializer xs = new XmlSerializer(typeof(PlcComConfig));
plcComConfig = (PlcComConfig) xs.Deserialize(fs);
}
Channel channel = plcComConfig.Channel;
int unitId = plcComConfig.UnitID;
if (plcComConfig.RequirePlcName)
{
return GetPLC(channel, unitId, plcComConfig.PlcName);
}
else
{
return GetPLC(channel, unitId);
}
}
public static PLC GetPLC(PlcComConfig plcComConfig)
{
Channel channel = plcComConfig.Channel;
int unitId = plcComConfig.UnitID;
bool forceJazz = false;
if (channel is Ethernet || channel is EthernetListener || channel is ListenerClient)
{
forceJazz = plcComConfig.ForceJazz;
}
if (plcComConfig.RequirePlcName)
{
return GetPLC(channel, unitId, plcComConfig.PlcName, forceJazz);
}
else
{
return GetPLC(channel, unitId, forceJazz);
}
}
internal static PLC getPLC(ref Channel channel, int unitId, ref bool isConnected, bool suppressEthernetHeader)
{
try
{
isConnected = false;
if (unitId < 0 || unitId > 127)
{
throw new ComDriveExceptions("UnitID out of range! The value must be between 0 and 127.",
ComDriveExceptions.ComDriveException.InvalidUnitID);
}
Channel plcChannel = getChannel(channel, ref isConnected);
channel = plcChannel;
try
{
return new PLC(unitId, plcChannel, suppressEthernetHeader);
}
catch (ComDriveExceptions ex)
{
if ((!isConnected) && (channel.GetType() != typeof(EthernetListener)))
plcChannel.Disconnect();
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
public static Channel GetChannel(Channel channel)
{
if (channel is Serial)
{
Serial serial = channel as Serial;
return GetChannel(serial.PortName);
}
else if (channel is Ethernet)
{
Ethernet ethernet = channel as Ethernet;
return GetChannel(ethernet.RemoteIP, ethernet.RemotePort);
}
else if (channel is EthernetListener)
{
EthernetListener listener = channel as EthernetListener;
return GetChannel(listener.LocalPort);
}
else if (channel is ListenerServer)
{
ListenerServer listenerServer = channel as ListenerServer;
ListenerServer result;
GetChannel(listenerServer.LocalPort, out result);
return result;
}
else
{
return null;
}
}
public static void GetChannel(int port, out ListenerServer listenerServer)
{
try
{
foreach (Channel c in m_channels)
{
if (c.GetType() == typeof(ListenerServer))
{
ListenerServer listener = c as ListenerServer;
if (listener.LocalPort == port)
{
listenerServer = listener;
return;
}
}
}
listenerServer = null;
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
public static EthernetListener GetChannel(int port)
{
try
{
foreach (Channel c in m_channels)
{
if (c.GetType() == typeof(EthernetListener))
{
EthernetListener listener = c as EthernetListener;
if (listener.LocalPort == port)
return listener;
}
}
return null;
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
public static Ethernet GetChannel(string remoteIp, Int32 remotePort)
{
try
{
foreach (Channel c in m_channels)
{
if (c.GetType() == typeof(Ethernet))
{
Ethernet ethernet = c as Ethernet;
if ((ethernet.RemoteIP == remoteIp) && (ethernet.RemotePort == remotePort))
return ethernet;
}
}
return null;
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
public static Serial GetChannel(SerialPortNames portName)
{
try
{
foreach (Channel c in m_channels)
{
if (c.GetType() == typeof(Serial))
{
Serial serial = c as Serial;
if (serial.PortName == portName)
return serial;
}
}
return null;
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
internal static bool ValidateChannelPropertyChange(Serial channel, SerialPortNames newPortName)
{
if (!m_channels.Contains(channel))
{
return true;
}
else
{
Serial serial = GetChannel(newPortName);
if (serial == null)
{
return true;
}
else
{
return false;
}
}
}
internal static bool ValidateChannelPropertyChange(Ethernet channel, string newIp, int newPort)
{
if (!m_channels.Contains(channel))
{
return true;
}
else
{
Ethernet ethernet = GetChannel(newIp, newPort);
if (ethernet == null)
{
return true;
}
else
{
return false;
}
}
}
internal static bool ValidateChannelPropertyChange(EthernetListener channel, int newPort)
{
if (!m_channels.Contains(channel))
{
return true;
}
else
{
EthernetListener listener = GetChannel(newPort);
if (listener == null)
{
return true;
}
else
{
return false;
}
}
}
internal static bool ValidateChannelPropertyChange(ListenerServer channel, int newPort)
{
if (!m_channels.Contains(channel))
{
return true;
}
else
{
ListenerServer listener;
GetChannel(newPort, out listener);
if (listener == null)
{
return true;
}
else
{
return false;
}
}
}
public static bool WorkWithXmlInternally
{
get { return m_WorkWithXmlInternally; }
set { m_WorkWithXmlInternally = value; }
}
public static ushort MessageDelay
{
get { return messageDelay; }
set { messageDelay = value; }
}
#endregion
#region ActivationService
public static void ActivationService(Object activationService)
{
try
{
if (activationService is String)
{
string activationString = activationService as String;
System.Security.Cryptography.MD5CryptoServiceProvider MD5 =
new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] encodedBytes = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(activationString));
activationString = Utils.HexEncoding.GetHexTwoCharsPerByte(encodedBytes);
if (activationString == "7C765F7F05BC73663FFA9ED33B9998D5")
{
m_ActivationService = true;
}
else
{
m_ActivationService = false;
}
}
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
internal static bool ActivationServiceEnabled
{
get { return m_ActivationService; }
}
#endregion
#region ComDriverLogger
public static void EnableLogger()
{
try
{
ComDriverLogger.Enable();
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
public static void EnableLogger(LogTypesConfig logTypesConfig)
{
try
{
ComDriverLogger.Enable(logTypesConfig);
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
public static void DisableLogger()
{
try
{
ComDriverLogger.Disable();
}
catch (ComDriveExceptions ex)
{
throw;
}
catch (Exception ex)
{
string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace;
ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
throw;
}
}
public static void LoadLoggerSettingsFromConfig()
{
ComDriverLogger.Disable();
}
#endregion
#region Private
private static Channel getChannel(Channel channel, ref bool isConnected)
{
lock (objectLocker)
{
if (m_channels.Contains(channel) || channel is ListenerClient)
{
//there is already a channel with the same configuration
try
{
isConnected = channel.Connected;
}
catch
{
}
return channel;
}
else
{
if (m_channels.Count == 0) //no channels
{
try
{
isConnected = channel.Connected;
}
catch
{
}
if (channel.GetType() == typeof(EthernetListener))
{
EthernetListener listener = channel as EthernetListener;
//listener.Listen();
listener.AlreadyInitialized = false;
m_channels.Add(channel);
return channel;
}
else
{
channel.Connect();
channel.AlreadyInitialized = false;
m_channels.Add(channel);
return channel;
}
}
else
{
foreach (Channel c in m_channels)
{
if (c.GetType() == channel.GetType())
{
if (c.IsEquivalentChannel(channel))
{
try
{
isConnected = c.Connected;
}
catch
{
}
// if Serial port name is the same or Ethernet IP:Port is the same then...
if (c.GetType() == typeof(Serial))
{
Serial openedChannel = c as Serial;
Serial newChannel = channel as Serial;
if (c.Connected == true)
{
c.AlreadyInitialized = true;
if ((openedChannel.PortName == newChannel.PortName) &&
((openedChannel.BaudRate != newChannel.BaudRate) ||
(openedChannel.Retry != newChannel.Retry) ||
(openedChannel.TimeOut != newChannel.TimeOut) ||
(openedChannel.Parity != newChannel.Parity) ||
(openedChannel.DataBits != newChannel.DataBits) ||
(openedChannel.StopBits != newChannel.StopBits)))
throw new ComDriveExceptions(
"Opened Serial connection parameters cannot be modified",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
else
{
// return the c channel and change its properties
c.AlreadyInitialized = false;
openedChannel.BaudRate = newChannel.BaudRate;
openedChannel.Parity = newChannel.Parity;
openedChannel.DataBits = newChannel.DataBits;
openedChannel.StopBits = newChannel.StopBits;
openedChannel.TimeOut = newChannel.TimeOut;
openedChannel.Retry = newChannel.Retry;
openedChannel.AutoDetectComParams = newChannel.AutoDetectComParams;
openedChannel.Connect();
}
}
else if (c.GetType() == typeof(Ethernet))
{
Ethernet newChannel = channel as Ethernet;
Ethernet openedChannel = c as Ethernet;
if (c.Connected == true)
{
c.AlreadyInitialized = true;
if ((openedChannel.Retry != newChannel.Retry) ||
(openedChannel.TimeOut != newChannel.TimeOut) ||
(openedChannel.Protocol != newChannel.Protocol))
{
throw new ComDriveExceptions(
"Opened Ethernet parameters cannot be modified",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
}
else
{
// return the c channel and change its properties
c.AlreadyInitialized = false;
openedChannel.Protocol = newChannel.Protocol;
openedChannel.TimeOut = newChannel.TimeOut;
openedChannel.Retry = newChannel.Retry;
openedChannel.Connect();
}
}
else if (c.GetType() == typeof(EthernetListener))
{
EthernetListener newChannel = channel as EthernetListener;
EthernetListener openedChannel = c as EthernetListener;
if (c.Connected == true)
{
c.AlreadyInitialized = true;
if ((openedChannel.Retry != newChannel.Retry) ||
(openedChannel.TimeOut != newChannel.TimeOut))
{
throw new ComDriveExceptions(
"Opened Ethernet parameters cannot be modified",
ComDriveExceptions.ComDriveException.CommunicationParamsException);
}
}
else
{
// return the c channel and change its properties
c.AlreadyInitialized = false;
openedChannel.TimeOut = newChannel.TimeOut;
openedChannel.Retry = newChannel.Retry;
//openedChannel.Listen();
}
}
return c;
}
}
}
isConnected = channel.Connected;
if (!channel.Connected)
{
channel.Connect();
channel.AlreadyInitialized = false;
m_channels.Add(channel);
return channel;
}
return null;
}
}
}
}
#endregion
}
}