IOManager.cs
11.4 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
using log4net;
using TSA_V.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace TSA_V.DeviceLibrary
{
public class IOManager
{
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static Dictionary<string, ConfigIO> DIList = new Dictionary<string, ConfigIO>();
public static Dictionary<string, ConfigIO> DOList = new Dictionary<string, ConfigIO>();
public static string KNDIP = "";
public static ushort KNDPort = 0;
public static void ConnectionIO()
{
if (!ISConnection())
{
IOBase.instance.ConnectionIP(KNDIP, KNDPort);
System.Threading.Thread.Sleep(100);
}
}
public static bool ISConnection()
{
return IOBase.instance.IsConnection(KNDIP);
}
private static StringBuilder builder = new StringBuilder("\r\n");
static IOManager()
{
KNDIP = ConfigAppSettings.GetValue(Setting_Init.IO_IP);
KNDPort = (ushort)ConfigAppSettings.GetIntValue(Setting_Init.IO_Port);
byte slaveId = (byte)0;
IOBase.Init();
builder = new StringBuilder("\r\n");
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, IOBase.instance.DIStartAddress, "SuddenStop", "X01_急停"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 1), "Footrest_Single", "X02_启动"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 2), "AirCheck_Single", "X03_气压检测"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 3), "SMEMA_Up", "X04_SMEMA前机送料"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 4), "SMEMA_Down", "X05_SMEMA后机要料"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 5), "LineInCheck", "X06_线体入口检测"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 6), "LineWorkCheck", "X07_线体工作区域检测"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 7), "LineOutCheck", "X08_线体出口检测"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 8), "StopCylinder_Up", "X09_挡停气缸上升端"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 9), "StopCylinder_Down", "X10_挡停气缸下降端"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 10), "SideCylinder_Forward", "X11_侧挡气缸前进端"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 11), "SideCylinder_Back", "X12_侧挡气缸后退端"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 12), "BottomCylinder_Down1", "X13_底部定位气缸1下降端"));
AddDI(new ConfigIO(ConfigItemType.DI, KNDIP, slaveId, (ushort)(IOBase.instance.DIStartAddress + 13), "BottomCylinder_Down2", "X14_底部定位气缸2下降端"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, IOBase.instance.DoStartAddress, "Run_Single", "Y01_设备运行指示灯"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 1), "Line_FStart", "Y02_线体皮带线运转"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 2), "SMEMA_Up", "Y03_SMEMA前机要料请求"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 3), "SMEMA_Down", "Y04_SMEMA后机送料请求"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 4), "StopCylinder_Up", "Y05_挡停气缸上升SOL"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 5), "StopCylinder_Down", "Y06_挡停气缸下降SOL"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 6), "SideCylinder_Forward", "Y07_侧挡气缸前进SOL"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 7), "SideCylinder_Back", "Y08_侧挡气缸后退SOL"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 8), "BottomCylinder_Up", "Y09_底部定位气缸上升SOL"));
AddDO(new ConfigIO(ConfigItemType.DO, KNDIP, slaveId, (ushort)(IOBase.instance.DoStartAddress + 9), "BottomCylinder_Down", "Y10_底部定位气缸下降SOL"));
if (String.IsNullOrEmpty(builder.ToString()).Equals(false) && builder.ToString().Equals("\r\n").Equals(false))
{
// LogUtil.info(builder.ToString());
}
}
private static void AddDI(ConfigIO io)
{
DIList.Add(io.ProName, io);
builder.Append(" /// <summary>\r\n");
builder.Append(" /// " + io.Explain + "\r\n");
builder.Append(" /// </summary>\r\n");
builder.Append(" public static string " + io.ProName + "=\"" + io.ProName + "\";\r\n");
}
private static void AddDO(ConfigIO io)
{
DOList.Add(io.ProName, io);
builder.Append(" /// <summary>\r\n");
builder.Append(" /// " + io.Explain + "\r\n");
builder.Append(" /// </summary>\r\n");
builder.Append(" public static string " + io.ProName + "=\"" + io.ProName + "\";\r\n");
}
/// <summary>
/// X01_急停
/// </summary>
public static string SuddenStop = "SuddenStop";
/// <summary>
/// X02_启动
/// </summary>
public static string Footrest_Single = "Footrest_Single";
/// <summary>
/// X03_气压检测
/// </summary>
public static string AirCheck_Single = "AirCheck_Single";
/// <summary>
/// X04_SMEMA前机送料
/// </summary>
public static string SMEMA_Up = "SMEMA_Up";
/// <summary>
/// X05_SMEMA后机要料
/// </summary>
public static string SMEMA_Down = "SMEMA_Down";
/// <summary>
/// X06_线体入口检测
/// </summary>
public static string LineInCheck = "LineInCheck";
/// <summary>
/// X07_线体工作区域检测
/// </summary>
public static string LineWorkCheck = "LineWorkCheck";
/// <summary>
/// X08_线体出口检测
/// </summary>
public static string LineOutCheck = "LineOutCheck";
/// <summary>
/// X09_挡停气缸上升端
/// </summary>
public static string StopCylinder_Up = "StopCylinder_Up";
/// <summary>
/// X10_挡停气缸下降端
/// </summary>
public static string StopCylinder_Down = "StopCylinder_Down";
/// <summary>
/// X11_侧挡气缸前进端
/// </summary>
public static string SideCylinder_Forward = "SideCylinder_Forward";
/// <summary>
/// X12_侧挡气缸后退端
/// </summary>
public static string SideCylinder_Back = "SideCylinder_Back";
/// <summary>
/// X13_底部定位气缸1下降端
/// </summary>
public static string BottomCylinder_Down1 = "BottomCylinder_Down1";
/// <summary>
/// X14_底部定位气缸2下降端
/// </summary>
public static string BottomCylinder_Down2 = "BottomCylinder_Down2";
/// <summary>
/// Y01_设备运行指示灯
/// </summary>
public static string Run_Single = "Run_Single";
/// <summary>
/// Y02_线体皮带线运转
/// </summary>
public static string Line_FStart = "Line_FStart";
/// <summary>
/// Y09_底部定位气缸上升SOL
/// </summary>
public static string BottomCylinder_Up = "BottomCylinder_Up";
/// <summary>
/// Y10_底部定位气缸下降SOL
/// </summary>
public static string BottomCylinder_Down = "BottomCylinder_Down";
#region IO控制方法
public static void IOCheckAndMove(string ioType,IO_VALUE ioValue)
{
if (!IOValue(ioType).Equals(ioValue))
{
IOMove(ioType, ioValue);
}
}
public static void IOMove(string ioType, IO_VALUE ioValue)
{
try
{
if (DOList.ContainsKey(ioType))
{
ConfigIO io = DOList[ioType];
IOBase.instance.WriteSingleDO(io.DeviceName, io.SlaveID, io.IOIndex, ioValue);
Thread.Sleep(60);
}
else
{
LogUtil.error(LOGGER, "没有DO=" + ioType);
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "KNDIOMove错误ioType=" + ioType + ",value=" + ioValue + ",错误信息:" + ex.ToString());
}
}
public static IO_VALUE IOValue(string ioType)
{
IO_VALUE ioValue = IO_VALUE.LOW;
if (DIList.ContainsKey(ioType))
{
ConfigIO configIo = DIList[ioType];
ioValue = IOBase.instance.GetDIValue(configIo.DeviceName, configIo.SlaveID, configIo.IOIndex);
//UpdateDoValue(ioType, ioValue);
}
else if (DOList.ContainsKey(ioType))
{
ConfigIO configIo = DOList[ioType];
ioValue = IOBase.instance.GetDOValue(configIo.DeviceName, configIo.SlaveID, configIo.IOIndex);
}
else
{
LogUtil.error(LOGGER, "没有DO=" + ioType);
}
return ioValue;
}
#endregion
public static ConfigIO getWaitIO(string p)
{
if (DIList.ContainsKey(p))
{
return DIList[p];
}
else if (DOList.ContainsKey(p))
{
return DOList[p];
}
return null;
}
public static bool ShuddenOK()
{
if (ISConnection().Equals(false))
{
return false;
}
if (IOValue(IOManager.SuddenStop).Equals(IO_VALUE.LOW))
{
return false;
}
return true;
}
}
public enum IO_VALUE
{
/// <summary>
/// 低位
/// </summary>
LOW = 0,
/// <summary>
/// 高位
/// </summary>
HIGH = 1,
}
public static class ConfigItemType
{
public static string DI = "DI";
public static string DO = "DO";
public static string AXIS = "AXIS";
public static string PRO = "PRO";
}
}