AIOBOXManager.cs
9.1 KB
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
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Asa.AIOBOX;
using System.Threading;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
namespace OnlineStore.DeviceLibrary
{
public class AIOBOXManager : IOManager
{
public readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public Dictionary<string, AIOBOX_32> AIOMap = new Dictionary<string, AIOBOX_32>();
private object DIMapLock = "";
private object DOMapLock = "";
public System.Timers.Timer timer = null;
public void ConnectionIP(string ioIp )
{
if (timer == null)
{
timer = new System.Timers.Timer();
timer.Interval = 300;
timer.AutoReset = true;
timer.Elapsed += timer_Elapsed;
timer.Enabled = true;
}
AIOBOX_32 aioBox = null;
if (AIOMap.ContainsKey(ioIp))
{
aioBox = AIOMap[ioIp];
if (null != aioBox)
{
aioBox.Close();
aioBox = null;
}
AIOMap.Remove(ioIp);
}
try
{
// Create new modbus master and add event functions
aioBox = new AIOBOX_32();
aioBox.IP = ioIp;
bool result = aioBox.Connect();
if (result)
{
AIOMap.Add(ioIp, aioBox);
}
else
{
LogUtil.error("连接IO模块【"+ioIp+"】失败:"+aioBox.ErrInfo);
}
Thread.Sleep(10);
//读取所有的DO
ReadAllDI(ioIp, 0);
}
catch (Exception error)
{
LogUtil.error(LOGGER, "连接IO模块[" + ioIp + "]出错:" + error.ToString());
}
}
/// <summary>
/// 判断Io模块是否连接
/// </summary>
public bool IsConnection(string ip)
{
try
{
if (AIOMap.ContainsKey(ip))
{
if (AIOMap[ip].IsConn)
{
return true;
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦:" + ex.ToString());
}
return false;
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
List<string> list = new List<string>(AIOMap.Keys);
foreach (string io in list)
{
//判断是否连接,如果没有连接自动重连
AIOBOX_32 clinet = AIOMap[io];
if (!clinet.IsConn)
{
LogUtil.error(LOGGER, io + "当前没有连上:" + clinet.ErrInfo);
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦:" + ex.ToString());
}
Thread.Sleep(1);
}
public override void ConnectionKND(List<string> DIONameList)
{
foreach (string ip in DIONameList)
{
ConnectionIP(ip );
}
}
//关闭所有的DO
public override void CloseAllDO()
{
foreach (AIOBOX_32 aio in AIOMap.Values)
{
Status[] statuses = new Status[16];
for(int i = 0; i < 16; i++)
{
statuses[i] = Status.Off;
}
aio.WriteDO(Addr.DI_1, statuses);
}
}
public override void CloseAllConnection()
{
foreach (AIOBOX_32 aio in AIOMap.Values)
{
aio.Close();
}
AIOMap.Clear();
}
public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff)
{
try
{
AIOBOX_32 aioBox = getAIO(ioIp);
if (aioBox != null)
{
Addr add = GetAddr(StartAddress);
aioBox.WriteDO(GetAddr(StartAddress), GetStatus(onOff));
}
else
{
LogUtil.error(LOGGER, "ReadSingleDO出错没有连接IO模块:" + ioIp);
}
}
catch (Exception ex)
{
LOGGER.Error("出错啦:" + ex.ToString());
}
}
public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff, int mSeconds)
{
try
{
AIOBOX_32 aioBox = getAIO(ioIp);
Status currStatus = GetStatus(onOff);
if (aioBox != null)
{
Addr add = GetAddr(StartAddress);
aioBox.WriteDO(GetAddr(StartAddress), currStatus);
//写入之后,等待指定间隔后回写
System.Timers.Timer mytimer = new System.Timers.Timer(mSeconds);
mytimer.Elapsed += (o1, e1) =>
{
try
{
aioBox.WriteDO(GetAddr(StartAddress), aioBox.ReverseStatus(currStatus));
LogUtil.debug(LOGGER, "**********定时回写入 IO【" + ioIp + "," + StartAddress + ",值" + aioBox.ReverseStatus(currStatus) + "】:");
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "**********定时回写入 出错:" + ex.StackTrace);
}
};
mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次;
mytimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件mytask;
}
else
{
LogUtil.error(LOGGER, "WriteSingleDO出错没有连接IO模块:" + ioIp);
}
}
catch (Exception ex)
{
LogUtil.error("WriteSingleDO出错:" + ioIp);
}
}
public override void ReadAllDI(string ioIp, byte slaveId)
{
}
public override void ReadAllDO(string ioIp, byte slaveId)
{
}
public override IO_VALUE GetDOValue(string ioIP, byte slaveId, ushort StartAddress)
{
IO_VALUE value = IO_VALUE.LOW;
AIOBOX_32 aioBox = getAIO(ioIP);
if (aioBox != null)
{
Status status = Status.Off;
aioBox.ReadDO(GetAddr(StartAddress), out status);
if (status.Equals(Status.On))
{
value = IO_VALUE.HIGH;
}
}
return value;
}
public override IO_VALUE GetDIValue(string ioIP, byte slaveId, ushort StartAddress)
{
IO_VALUE value = IO_VALUE.LOW;
AIOBOX_32 aioBox = getAIO(ioIP);
if (aioBox != null)
{
Status status = Status.Off;
aioBox.ReadDI(GetAddr(StartAddress), out status);
if (status.Equals(Status.On))
{
value = IO_VALUE.HIGH;
}
}
return value;
}
public override IO_VALUE GetIOValue(ConfigIO configIO)
{
IO_VALUE value = IO_VALUE.LOW;
try
{
if (configIO.ProType.Equals(ConfigItemType.DI))
{
return GetDIValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
}
else if (configIO.ProType.Equals(ConfigItemType.DO))
{
return GetDOValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "获取数据出错:" + ex.ToString());
}
return value;
}
private Addr GetAddr(ushort StartAddress)
{
return (Addr)(StartAddress - 1);
}
private Status GetStatus(IO_VALUE onOff)
{
if (onOff.Equals(IO_VALUE.HIGH))
{
return Status.On;
}
else
{
return Status.Off;
}
}
private AIOBOX_32 getAIO(string ioIp)
{
AIOBOX_32 aioBox = null;
if (AIOMap.ContainsKey(ioIp))
{
aioBox = AIOMap[ioIp];
}
return aioBox;
}
}
}