-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLC.cs
176 lines (154 loc) · 5.33 KB
/
PLC.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
using Logix;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
namespace PLCAPI.Models
{
public class PLC
{
private Controller controller;
private string PLC_IPAddress, PLC_Path;
private int PLC_Timeout, PLC_Delay;
private const string TRUE = "TRUE";
private const string FALSE = "FALSE";
private string PLC_ERR_CON_LOST = "PLC Connection Lost";
private const string DateTime_Format = "{1}/{2}/{0} {3}:{4}:{5} {6}";
private const string B = "<b>";
private const string EB = "</b>";
private const string BR = "<br />";
private const string TAG_TIMEDATA = "PLC_TimeData";
private const string TAG_DISCRETEOUTPUT = "DiscreteOutputData";
public PLC()
{
try
{
PLC_IPAddress = WebConfigurationManager.AppSettings["PLC_IPAddress"];
PLC_Path = WebConfigurationManager.AppSettings["PLC_Path"];
PLC_Timeout = int.Parse(WebConfigurationManager.AppSettings["PLC_Timeout"]);
PLC_Delay = int.Parse(WebConfigurationManager.AppSettings["PLC_Delay"]);
controller = new Controller();
controller.IPAddress = PLC_IPAddress;
controller.Path = PLC_Path;
controller.Timeout = PLC_Timeout;
controller.CPUType = Controller.CPU.LOGIX;
if (controller.Connect() != ResultCode.E_SUCCESS)
{
Reconnect();
}
}
catch (Exception e)
{
throw e;
}
}
~PLC()
{
if (isConnected())
{
Disconnect();
}
}
public bool isConnected()
{
try
{
return controller.IsConnected;
}
catch
{
return false;
}
}
private void Disconnect()
{
controller.Disconnect();
}
private void Reconnect()
{
if (!isConnected())
{
int retry = 0;
while (retry < PLC_Timeout)
{
retry++;
if (controller.Connect() == ResultCode.E_SUCCESS) return;
System.Threading.Thread.Sleep(PLC_Delay);
}
throw new Exception(PLC_ERR_CON_LOST);
}
}
public string ReadTag(string name, Tag.ATOMIC type)
{
Reconnect();
if (isConnected())
{
Tag tag = new Tag(name);
tag.DataType = type;
controller.ReadTag(tag);
if (tag.QualityCode == ResultCode.QUAL_GOOD)
{
return tag.Value.ToString();
}
else
{
throw new Exception("Unable to read tag with quality code: " + tag.QualityCode);
}
}
else
{
throw new Exception(PLC_ERR_CON_LOST);
}
}
public bool BitArrayGet(int value, int index) {
//Get boolean value from a 'Double Integer as Boolean' (DINT).
System.Collections.BitArray bitarray = new System.Collections.BitArray(new int[] { value });
return bitarray.Get(index);
}
public string info() {
Reconnect();
string output = "";
if (isConnected()) {
try
{
output = B +"Host Name"+EB+": "+ WebConfigurationManager.AppSettings["PLC_IPAddress"] + BR;
}
catch (Exception e)
{ }
output += B+"Date Time"+ EB +": " + GetDateTime() + BR + BR;
output += "PLC Healthy Status:" + Bool2str(GetDINT(TAG_DISCRETEOUTPUT, 0));
}
return output;
}
public string Bool2str(bool val) {
if (val) return TRUE;
return FALSE;
}
public bool GetDINT(string tagname, int index) {
int x = Int32.Parse(ReadTag(tagname, Tag.ATOMIC.INT));
bool value = BitArrayGet(x, index);
return value;
}
public string GetDateTime()
{
if (isConnected())
{
string TimeStr = String.Format(DateTime_Format,
ReadTag(TAG_TIMEDATA + "[0]", Tag.ATOMIC.INT),
ReadTag(TAG_TIMEDATA + "[1]", Tag.ATOMIC.INT),
ReadTag(TAG_TIMEDATA + "[2]", Tag.ATOMIC.INT),
ReadTag(TAG_TIMEDATA + "[3]", Tag.ATOMIC.INT),
ReadTag(TAG_TIMEDATA + "[4]", Tag.ATOMIC.INT),
ReadTag(TAG_TIMEDATA + "[5]", Tag.ATOMIC.INT),
ReadTag(TAG_TIMEDATA + "[6]", Tag.ATOMIC.INT)
);
return TimeStr;
}
else
{
throw new Exception(PLC_ERR_CON_LOST);
}
}
}
}