-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEthernetBoard_old.txt
735 lines (638 loc) · 28.8 KB
/
EthernetBoard_old.txt
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
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Pipeline;
using Lextm.SharpSnmpLib.Messaging;
namespace snmpd
{
public enum EthernetBoardState
{
DISCONNECTED,
CONNECTED
}
public enum EthernetBoardChannelState
{
INPUT,
OUTPUT
}
public class EthernetBoardPort
{
public int PortNo { get; set; }
public IPAddress ParentIP { get; set; }
public EthernetBoardState State { get; set; }
public List<Channel> channels { get; set; }
public class Channel
{
public int Id { get; set; }
public bool IsAnalog { get; set; }
public int Value { get; set; }
public string OID { get; set; }
public string Name { get; set; }
public int IoPort { get; set; }
public EthernetBoardChannelState Mode { get; set; }
public Channel() { }
public Channel(int _id, int _ioPort, string _oid, string _name, int _value, EthernetBoardChannelState _mode)
{
this.Id = _id;
this.IoPort = _ioPort;
this.OID = _oid;
this.Name = _name;
this.Value = _value;
this.Mode = _mode;
}
}
public EthernetBoardPort() { channels = new List<Channel>(); }
public EthernetBoardPort(string _ip, int _portNo, EthernetBoardState _state)
{
IPAddress IpObj;
channels = new List<Channel>();
if (IPAddress.TryParse(_ip, out IpObj))
{
ParentIP = IpObj;
State = _state;
PortNo = _portNo;
}
}
private void AddChannel(Channel ch)
{
if (this.channels == null)
this.channels = new List<Channel>();
this.channels.Add(ch);
}
private void AddChannel(int _id, int _ioPort, string _oid, string _name, int _value, EthernetBoardChannelState _mode)
{
Channel ch = new Channel();
ch.Id = _id; ch.IoPort = _ioPort; ch.OID = _oid; ch.Name = _name; ch.Value = _value; ch.Mode = _mode;
if (ch.Id == 1 || ch.Id == 5)
ch.IsAnalog = true;
else
ch.IsAnalog = false;
this.channels.Add(ch);
}
}
public class ErrorEventArgs : EventArgs
{
public Exception Exception { get; set; }
public string Source { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
public DateTime DateOccurred { get; set; }
public ErrorEventArgs(Exception ex)
{
Exception = ex;
Source = ex.Source;
Message = ex.Message;
StackTrace = ex.StackTrace;
DateOccurred = DateTime.Now;
}
public override string ToString()
{
return "Error: " + Message + " Source: " + Source;
}
}
public class EthernetBoard
{
// Declare EventHandler to be used in the onError event
public event EventHandler<ErrorEventArgs> ErrorEventHandler;
// Declare delegate for access to error handling functions outside of the class
public delegate void ErrorHandlerDelegate(object sender, ErrorEventArgs e);
// Declare an error event. onError is the virtual function
public event ErrorHandlerDelegate onError;
//protected virtual void onError(object sender, ErrorEventArgs e)
//{ // Error Handler
// EventHandler<ErrorEventArgs> handler = ErrorEventHandler;
// if (handler != null)
// handler(sender, e);
//}
public delegate void ErrorAddedDelegate(string error);
public event ErrorAddedDelegate onErrorRaised;
public delegate void LoggingRaisedDelegate(string message, string color);
public event LoggingRaisedDelegate onLoggingRaised;
public delegate void DigitalInputDelegate(int boardID, EthernetBoardPort.Channel channel);
public event DigitalInputDelegate onDigitalInput;
public delegate void AnalogueInputDelegate(int boardID, EthernetBoardPort.Channel channel);
public event AnalogueInputDelegate onAnalogueInput;
public delegate void InitDelegate(int boardID, List<EthernetBoardPort> ioPorts, List<EthernetBoardPort> adcPorts);
public event InitDelegate onInit;
private delegate void InitializeDelegate(int boardID, string boardName, string ipAddress, int port,
string password, int digitalInputRefreshInMs, int analogueInputRefreshInMs,
bool bAutoRefreshDigitalInput, bool bAutoRefreshAnalogueInput);
private const int LISTEN_PORT = 161;
private const int RECEIVE_PORT = 162;
private const int DIG_CHAN_START_NO = 1;
private const int DIG_CHAN_PER_PORT = 8;
private const int ALG_CHAN_START_NO = 3;
private const int ALG_CHAN_PER_PORT = 4;
private const int SNMP_TIMEOUT = 2000;
private string _BoardName;
private int _BoardID;
private int _Port;
private string _IP_Address;
private string _Community;
private bool _IsInitialized = false;
private bool _DigitalTimerActive = false;
private bool _AnalogTimerActive = false;
private IPEndPoint _EndPoint;
private IPAddress _IP;
private EthernetBoardState _ConnectionState;
// public readonly properties
string BoardName { get { return _BoardName; } }
int BoardID { get { return _BoardID; } }
int Port { get { return _Port; } }
string IP_Address { get { return _IP_Address; } }
string Community { get { return _Community; } }
public bool IsInitialized { get { return _IsInitialized; } }
bool DigitalTimerActive { get { return _DigitalTimerActive; } }
bool AnalogTimerActive { get { return _AnalogTimerActive; } }
IPEndPoint EndPoint { get { return _EndPoint; } }
IPAddress IP { get { return _IP; } }
EthernetBoardState ConnectionState { get { return _ConnectionState; } }
List<EthernetBoardPort.Channel> DigitalChannelsList
{ get { return CurrentDigitalChannelValues; } }
List<EthernetBoardPort.Channel> AnalogChannelsList
{ get { return CurrentAnalogChannelValues; } }
// Analog timer values
private int _TimeoutInMsAnalog = -1;
private int _AnalogueInputRefreshInMs = -1;
private bool _DoAnalogAutoRefresh = true;
public System.Threading.Timer AnalogTimer;
public TimerCallback AnalogTimerDelegate;
private List<EthernetBoardPort.Channel> CurrentAnalogChannelValues;
// Digital Timer values
private int _TimeoutInMsDigital = -1;
private int _DigitalInputRefreshInMs = -1;
private bool _DigitalAutoRefresh = true;
public System.Threading.Timer DigitalTimer;
public TimerCallback DigitalTimerDelegate;
private List<EthernetBoardPort.Channel> CurrentDigitalChannelValues;
// port objects
public EthernetBoardPort AdcPort1;
public EthernetBoardPort IoPort1;
public EthernetBoardPort IoPort2;
public EthernetBoard()
{ // store the current channel values for comparison when checking for new data
CurrentDigitalChannelValues = new List<EthernetBoardPort.Channel>();
CurrentAnalogChannelValues = new List<EthernetBoardPort.Channel>();
// init first port 0
IoPort1 = new EthernetBoardPort();
// set channel info
for (int ChannelCount = 0 + DIG_CHAN_START_NO; ChannelCount < DIG_CHAN_PER_PORT + DIG_CHAN_START_NO; ChannelCount++)
{
EthernetBoardPort.Channel channel = new EthernetBoardPort.Channel();
channel.IoPort = 0;
channel.Id = ChannelCount;
channel.Name = "Digital Port # 0 Channel # " + ChannelCount;
channel.OID = "1.3.6.1.4.1.19865.1.2.1." + ChannelCount + ".0";
channel.Value = 0;
IoPort1.channels.Add(channel);
CurrentDigitalChannelValues.Add(channel);
}
// init 2nd port 1
IoPort2 = new EthernetBoardPort();
for (int ChannelCount = 0 + DIG_CHAN_START_NO; ChannelCount < DIG_CHAN_PER_PORT + DIG_CHAN_START_NO; ChannelCount++)
{
EthernetBoardPort.Channel channel = new EthernetBoardPort.Channel();
channel.IoPort = 1;
channel.Id = ChannelCount;
channel.Name = "Digital Port # 1 Channel # " + ChannelCount;
channel.OID = "1.3.6.1.4.1.19865.1.2.2." + ChannelCount + ".0";
channel.Value = 0;
IoPort2.channels.Add(channel);
CurrentDigitalChannelValues.Add(channel);
}
AdcPort1 = new EthernetBoardPort();
// assign the analog ports and channels names, numbers, and other values
// 1 analog port with 4 channels
for (int C = 0 + ALG_CHAN_START_NO; C < ALG_CHAN_PER_PORT + ALG_CHAN_START_NO; C++)
{
EthernetBoardPort.Channel channel = new EthernetBoardPort.Channel();
channel.IoPort = 0;
channel.Id = C;
channel.Name = "Analogue Port # 1 Channel #" + C;
channel.OID = "1.3.6.1.4.1.19865.1.2.1." + C + ".0";
channel.Value = 0;
AdcPort1.channels.Add(channel);
CurrentAnalogChannelValues.Add(channel);
}
}
public bool init(int iBoardID, string sBoardName, string sIpAddress, int iPort, string sCommunity,
int iDigitalInputRefreshInMs, int iAnalogueInputRefreshInMs, bool bAutoRefreshDigitalInput,
bool bAutoRefreshAnalogueInput)
{
bool success = false;
_BoardID = iBoardID;
_Port = iPort;
_BoardName = sBoardName;
_IP = IPAddress.Parse(sIpAddress);
_EndPoint = new IPEndPoint(this.IP, iPort);
_IP_Address = sIpAddress;
_Community = sCommunity;
_ConnectionState = EthernetBoardState.DISCONNECTED;
// analog timer stuff - Timers dont start automatically
_TimeoutInMsAnalog = EthernetBoard.SNMP_TIMEOUT;
_AnalogueInputRefreshInMs = iAnalogueInputRefreshInMs;
_DoAnalogAutoRefresh = bAutoRefreshAnalogueInput;
AnalogTimerDelegate = new TimerCallback(OnTickAnalog);
AnalogTimer = new Timer(AnalogTimerDelegate);
StartAnalogTimer();
// digital timer stuff- Timers dont start automatically
_TimeoutInMsDigital = EthernetBoard.SNMP_TIMEOUT;
_DigitalInputRefreshInMs = iDigitalInputRefreshInMs;
_DigitalAutoRefresh = bAutoRefreshDigitalInput;
DigitalTimerDelegate = new TimerCallback(OnTickDigital);
DigitalTimer = new Timer(DigitalTimerDelegate);
StartDigitalTimer();
_IsInitialized = true;
success = true;
if (onLoggingRaised != null)
onLoggingRaised("Initialized GPIO Ethernet Board with ID : " + this.BoardID.ToString()
+ " and Name : " + this.BoardName + " completed successfully", "#0000FF");
return success;
}
/// <summary>string SnmpSet(string _sOid, string _sCommunity, int _iChannelState, int _iTimeout)
/// Sets a channel value. </summary>
/// <param name="_sOid">The OID of the channel to set.</param>
/// <param name="_sCommunity">SNMP community name. "public" is the default value.</param>
/// <param name="_iChannelValue">The new channel value.</param>
/// <param name="_iTimeout">Milliseconds to continue attempting to readthe channel. Default is 0 (Infinite).</param>
/// <returns>(string) Not exactly sure what this will return.</returns>
public string SnmpSet(string _sOid, string _sCommunity, int _iChannelValue, int _iTimeout)
{
IList<Lextm.SharpSnmpLib.Variable> result = null;
List<Variable> lstVar = new List<Variable>();
//lstVar.Add(new Variable(new ObjectIdentifier(_sOid), new OctetString("42069")));
lstVar.Add(new Variable(new ObjectIdentifier(_sOid), new OctetString(_iChannelValue.ToString())));
try
{
Debug.Print("SET Attempt - IP: " + this.IP_Address + " Port: " + this.Port + " OID: " + _sOid);
result = Messenger.Set(VersionCode.V1, this.EndPoint, new OctetString(_sCommunity), lstVar, _iTimeout);
}
catch (Exception ex)
{
if ((ex is Lextm.SharpSnmpLib.Messaging.TimeoutException) || (ex is System.Net.Sockets.SocketException))
{
ex = null; // ignore timeouts
}
else
{
LogStuff("Error setting Digital Values for GPIO Ethernet Board Name : "
+ this.BoardName + " Message: " + ex.Message);
if (onError != null)
onError(this, new ErrorEventArgs(ex));
}
}
if (result != null && result.Count > 0)
return result[0].Data.ToString();
else
return "";
}
/// <summary>string SnmpGet(string _sOid, string _sCommunity, int _iTimeout, string _sDefaultValue)
/// Retrieves values from n EndPoint (Ip, Port)
/// </summary>
/// <param name="_sOid">The OID of the channel to read.</param>
/// <param name="_sCommunity">SNMP community name. "public" is the default value.</param>
/// <param name="_iTimeout">Milliseconds to continue attempting to readthe channel. Default is 0 (Infinite).</param>
/// <param name="_sDefaultValue">The channels previous value.</param>
/// <returns>(string) The new value of the channel</returns>
public string SnmpGet(string _sOid, string _sCommunity, int _iTimeout, int _iCurrentValue)
{
IList<Variable> result = null;
try
{
Debug.Print("GET Attempt - IP: " + this.IP_Address + " Port: " + EthernetBoard.LISTEN_PORT + " OID: " + _sOid);
result = Messenger.Get(VersionCode.V1,
this.EndPoint,
new OctetString(_sCommunity),
new List<Variable> { new Variable(new ObjectIdentifier(_sOid)) },
_iTimeout);
}
catch (Exception ex)
{
if ((ex is Lextm.SharpSnmpLib.Messaging.TimeoutException) || (ex is System.Net.Sockets.SocketException))
{
ex = null; // ignore timeouts
}
else
{
LogStuff("Error reading Digital Values for GPIO Ethernet Board ID : " + this.BoardID.ToString()
+ " and Name : " + this.BoardName + " Failed !! " + ex.Message);
if (onError != null)
onError(this, new ErrorEventArgs(ex));
}
}
if (result != null && result.Count > 0)
return result[0].Data.ToString();
else
return _iCurrentValue.ToString();
}
private void analogueInputTimer_Tick(object sender, EventArgs e)
{
//if (analoguePortsThread == null && (this.BoardID == 0 || this.BoardID == 4))// && !thread.IsAlive)
//{
// analoguePortsThread = new System.Threading.Thread(analogueInputsGetAll);
// analoguePortsThread.SetApartmentState(System.Threading.ApartmentState.MTA);
// analoguePortsThread.Priority = System.Threading.ThreadPriority.Highest;
// analoguePortsThread.Start();
//}
}
private void analogueInputsGetAll()
{
foreach (EthernetBoardPort.Channel channel in CurrentAnalogChannelValues)
{
try
{
lock (this)
{
channel.Value = Convert.ToInt16(SnmpGet(channel.OID, "private", SNMP_TIMEOUT, channel.Value));
if (onAnalogueInput != null)
onAnalogueInput(this.BoardID, channel);
}
}
catch (Exception ex)
{
if ((ex is Lextm.SharpSnmpLib.Messaging.TimeoutException) || (ex is System.Net.Sockets.SocketException))
{
ex = null; // ignore timeouts
}
else
{
LogStuff("Error reading Digital Values for GPIO Ethernet Board ID : " + this.BoardID.ToString()
+ " and Name : " + this.BoardName + " Failed !! " + ex.Message);
if (onError != null)
onError(this, new ErrorEventArgs(ex));
}
}
}
//analoguePortsThread = null;
}
public string getADCPortChannel(EthernetBoardPort.Channel channel = null, int portNum = -1, int channelNum = -1)
{
string _channelOID = "";
int _channelValue = 0;
if (channel != null && portNum == -1 && channelNum == -1)
{
_channelOID = channel.OID;
_channelValue = channel.Value;
}
else if (channel == null && portNum != -1 && channelNum != -1)
{
if (portNum == 0)
{
_channelOID = IoPort1.channels[channelNum].OID;
_channelValue = IoPort1.channels[channelNum].Value;
}
else
{
_channelOID = IoPort2.channels[channelNum].OID;
_channelValue = IoPort2.channels[channelNum].Value;
}
}
return SnmpGet(_channelOID, this.Community, _TimeoutInMsAnalog, _channelValue);
}
/******************* UTILITY FUNCTIONS ****************************************/
/// <summary>Times the execution of a function and outputs both the elapsed time and the function's result.</summary>
static void Time<T>(Func<T> work)
{
var sw = Stopwatch.StartNew();
var result = work();
Console.WriteLine(sw.Elapsed + ": " + result);
Debug.Print(sw.Elapsed + ": " + result);
}
private void OutputInfo(string OID, int chValue)
{
Debug.Print("OutputInfo - OID: " + OID + " Value: " + chValue);
}
public void setIOPortChannel(int _iStateValue, EthernetBoardPort.Channel channel = null,
int portNum = -1, int channelNum = -1)
{
EthernetBoardPort.Channel _channel;
if (channel != null && portNum == -1 && channelNum == -1)
{
_channel = channel;
}
else
{
if (portNum == 0)
_channel = IoPort1.channels[channelNum];
else
_channel = IoPort2.channels[channelNum];
}
if (_iStateValue == 0)
_channel.Mode = EthernetBoardChannelState.INPUT;
if (_iStateValue == 1)
_channel.Mode = EthernetBoardChannelState.OUTPUT;
try
{
SnmpSet(_channel.OID, _Community, _iStateValue, _TimeoutInMsDigital);
}
catch (Exception ex)
{
if ((ex is Lextm.SharpSnmpLib.Messaging.TimeoutException) || (ex is System.Net.Sockets.SocketException))
{
ex = null; // ignore timeouts
}
else
{
LogStuff("Error reading Digital Values for GPIO Ethernet Board ID : " + this.BoardID.ToString()
+ " and Name : " + this.BoardName + " Failed !! " + ex.Message);
if (onError != null)
onError(this, new ErrorEventArgs(ex));
}
}
}
public void StartDigitalTimer()
{
_DigitalTimerActive = true;
DigitalTimer = new System.Threading.Timer(DigitalTimerDelegate,
this, _DigitalInputRefreshInMs, _DigitalInputRefreshInMs);
Debug.Print(this.BoardName + " Timer Started");
}
public void StopDigitalTimer()
{
_DigitalTimerActive = false;
DigitalTimer.Dispose();
Debug.Print(this.BoardName + " Timer Stopped");
}
public void StartAnalogTimer()
{
_DigitalTimerActive = true;
DigitalTimer = new System.Threading.Timer(DigitalTimerDelegate,
this, _DigitalInputRefreshInMs, _DigitalInputRefreshInMs);
Debug.Print(this.BoardName + " Timer Started");
}
public void StopAnalogTimer()
{
_DigitalTimerActive = false;
DigitalTimer.Dispose();
Debug.Print(this.BoardName + " Timer Stopped");
}
/*********************************************************************************/
/*************************** EVENT HANDLERS **************************************/
// This is the timer tick code. Executes every (_DigitalInputRefreshInMs) milliseconds
public void OnTickDigital(object ebObject)
{ // Timer Event Handler
EthernetBoard brd = (EthernetBoard)ebObject;
// Use Interlocked to set timer increment
System.Threading.Interlocked.Increment(ref this._DigitalInputRefreshInMs);
CheckForNewDigitalData(IoPort1);
CheckForNewDigitalData(IoPort2);
}
public void OnTickAnalog(object ebObject)
{
CheckForNewAnalogData(AdcPort1);
}
private void CheckForNewDigitalData(object objPort)
{
string result = String.Empty;
int ListIndex = -1;
//******************************* SCAN PORT *******************************************//
// check each channel in the port for updated data (button values) //
// Compare it to CurrentDigitalChannelValues list. Update list if new values are found //
//*************************************************************************************//
EthernetBoardPort _IoPort = (EthernetBoardPort)objPort;
foreach (EthernetBoardPort.Channel channel in _IoPort.channels)
{
try
{
result = SnmpGet(channel.OID, this._Community, EthernetBoard.SNMP_TIMEOUT, channel.Value);
ListIndex = CurrentDigitalChannelValues.FindIndex(x => x.Name == channel.Name);
if (ListIndex > -1) // item was found in list
{
if (ListIndex == 0 || ListIndex == 5) // Is analog port - remove it from list
{
lock (this)
{
Debug.Print("Lock - OID: " + channel.OID + " Value: " + channel.Value);
channel.Value = CurrentDigitalChannelValues.Max(chnl => chnl.Value);
CurrentDigitalChannelValues.Remove(channel);
}
}
else // digital port - update channel value
{
CurrentDigitalChannelValues.ElementAt(ListIndex).Value = channel.Value;
}
}
else // Not in List - So add it
{
CurrentDigitalChannelValues.Add(channel);
}
}
catch (Exception ex)
{
if ((ex is Lextm.SharpSnmpLib.Messaging.TimeoutException) || (ex is System.Net.Sockets.SocketException))
{
ex = null; // ignore timeou
}
else
{
LogStuff("Error reading Digital Values for GPIO Ethernet Board ID : " + this.BoardID.ToString()
+ " and Name : " + this.BoardName + " Failed !! " + ex.Message);
if (onError != null)
onError(this, new ErrorEventArgs(ex));
}
}
}
Debug.Print("CurrentDigitalChannelValues: " + CurrentDigitalChannelValues.ToString());
}
private void CheckForNewAnalogData(object objPort)
{
string result = String.Empty;
int ListIndex = -1;
//******************************* SCAN PORT *******************************************//
// check each channel in the port for updated data (button values) //
// Compare it to CurrentAnalogChannelValues list. Update list if new values are found //
//*************************************************************************************//
EthernetBoardPort _IoPort = (EthernetBoardPort)objPort;
foreach (EthernetBoardPort.Channel channel in _IoPort.channels)
{
try
{
result = SnmpGet(channel.OID, this._Community, EthernetBoard.SNMP_TIMEOUT, channel.Value);
ListIndex = CurrentAnalogChannelValues.FindIndex(x => x.Name == channel.Name);
if (ListIndex > -1) // item was found in list
{
if (ListIndex == 0 || ListIndex == 5) // Is analog port - remove it from list
{
lock (this)
{
Debug.Print("Lock - OID: " + channel.OID + " Value: " + channel.Value);
channel.Value = CurrentDigitalChannelValues.Max(chnl => chnl.Value);
CurrentAnalogChannelValues.Remove(channel);
}
}
else // digital port - update channel value
{
CurrentAnalogChannelValues.ElementAt(ListIndex).Value = channel.Value;
}
}
else // Not in List - So add it
{
CurrentAnalogChannelValues.Add(channel);
}
}
catch (Exception ex)
{
if ((ex is Lextm.SharpSnmpLib.Messaging.TimeoutException) || (ex is System.Net.Sockets.SocketException))
{
ex = null; // ignore timeou
}
else
{
LogStuff("Error reading Digital Values for GPIO Ethernet Board ID : " + this.BoardID.ToString()
+ " and Name : " + this.BoardName + " Failed !! " + ex.Message);
if (onError != null)
onError(this, new ErrorEventArgs(ex));
}
}
}
Debug.Print("CurrentAnalogChannelValues: " + CurrentAnalogChannelValues.ToString());
}
public void ToggleRelay(EthernetBoardPort.Channel channel)
{
if (channel != null)
{
int value = Convert.ToInt32(!Convert.ToBoolean(Convert.ToInt32(channel.Value)));
setIOPortChannel(value, channel);
}
}
public bool ToggleAnalogueInputTimer()
{
if (_AnalogTimerActive)
{
StartAnalogTimer();
return true;
}
else
{
StopAnalogTimer();
return false;
}
}
public bool ToggleDigitalInputTimer()
{
if (!_DigitalTimerActive)
{
StartDigitalTimer();
return true;
}
else
{
StopDigitalTimer();
return false;
}
}
public void LogStuff(string msg)
{
if (onLoggingRaised != null)
onLoggingRaised(msg, "#FF0000");
}
}
}