KTKDIOManager.cs
8.5 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
using log4net;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 康泰克IO版操作类
/// </summary>
public class KTKDIOManager
{
public static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static string KTK_PI_DeviceName = ConfigAppSettings.GetValue(Setting_Init.KTK_PI_BoardName);
public static string KTK_PO_DeviceName = ConfigAppSettings.GetValue(Setting_Init.KTK_PO_BoardName);
private static Dictionary<string, KTKDeviceInfo> DIODeviceMap = new Dictionary<string, KTKDeviceInfo>();
private static Cdio cdio = new Cdio();
//每一个port的io数量
private static int portIONum=8;
private static int ret = 0;
/// <summary>
/// io卡初始化
/// </summary>
public static string DIOInit(string DeviceName)
{
if (!DIODeviceMap.ContainsKey(DeviceName))
{
short dio_Id = 0;
//初始化
ret = cdio.Init(DeviceName, out dio_Id);
short inPortName=0;
short outPortName=0;
//获取最多的端口
cdio.GetMaxPorts(dio_Id, out inPortName, out outPortName);
KTKDeviceInfo ktk = new KTKDeviceInfo(dio_Id, DeviceName, inPortName, outPortName);
DIODeviceMap.Add(DeviceName, ktk);
return DIOGetErrorString(ret);
}
return "";
}
/// <summary>
/// 判断设备是否初始化过了
/// </summary>
public static Boolean DIODeviceIsInit(string deviceName)
{
if (DIODeviceMap.ContainsKey(deviceName))
{
return true;
}
return false;
}
/// <summary>
/// 退出DIO卡
/// </summary>
public static string DIOExit(string DeviceName)
{
if (DIODeviceMap.ContainsKey(DeviceName))
{
short id = DIODeviceMap[DeviceName].DeviceId;
//退出
int ret = cdio.Exit(id);
DIODeviceMap.Remove(DeviceName);
return DIOGetErrorString(ret);
}
return "";
}
/// <summary>
/// 读取单个DI
/// </summary>
public static IO_VALUE ReadSingDI(string DeviceName, short ioIndex)
{
IO_VALUE value = (IO_VALUE)ReadSingDIValue(DeviceName,ioIndex);
return value;
}
public static int ReadSingDIValue(string DeviceName, short ioIndex)
{
byte Data = 0;
if (DIODeviceMap.ContainsKey(DeviceName))
{
short id = DIODeviceMap[DeviceName].DeviceId;
DateTime time = DateTime.Now;
cdio.InpBit(id, ioIndex, out Data);
TimeSpan span = DateTime.Now - time;
if (span.TotalMilliseconds > 10)
{
log.Error("执行cdio.InpBit【" + DeviceName + " " + ioIndex + "】,共耗费了【" + span.TotalMilliseconds + "】毫秒");
}
}
return Data;
}
/// <summary>
/// 读取这块板子上所有DI的值
/// </summary>
public static List<IO_VALUE> ReadAllDI(string DeviceName)
{
List<IO_VALUE> returnList = new List<IO_VALUE>();
try
{
if (DIODeviceMap.ContainsKey(DeviceName))
{
KTKDeviceInfo deviceInfo = DIODeviceMap[DeviceName];
short inPortNum = deviceInfo.InPortNum;
if (inPortNum <= 0)
{
return returnList;
}
short[] portArray = new short[inPortNum];
byte[] portValueArr = new byte[inPortNum];
for (int inPort = 0; inPort < inPortNum; inPort++)
{
portArray[inPort] = (short)inPort;
}
cdio.InpMultiByte(deviceInfo.DeviceId, portArray, inPortNum, portValueArr);
foreach (byte value in portValueArr)
{
string strValue = System.Convert.ToString(value, 2).PadLeft(portIONum, '0');
for (int index = portIONum - 1; index >= 0; index--)
{
string ioVStr = strValue.Substring(index, 1);
IO_VALUE iov = (IO_VALUE)Int32.Parse(ioVStr);
returnList.Add(iov);
}
}
}
}
catch (Exception ex)
{
LogUtil.error(log, ex.ToString());
}
return returnList;
}
/// <summary>
/// 读取单个DO
/// </summary>
public static IO_VALUE ReadSingDO(string DeviceName, short ioIndex)
{
return (IO_VALUE)ReadSingDOValue(DeviceName,ioIndex);
}
public static int ReadSingDOValue(string DeviceName, short ioIndex)
{
byte Data = 0;
if (DIODeviceMap.ContainsKey(DeviceName))
{
short id = DIODeviceMap[DeviceName].DeviceId;
DateTime time = DateTime.Now;
cdio.EchoBackBit(id, ioIndex, out Data);
TimeSpan span = DateTime.Now - time;
if (span.TotalMilliseconds > 10)
{
log.Error("执行cdio.EchoBackBit【" + DeviceName + " " + ioIndex + "】,共耗费了【" + span.TotalMilliseconds + "】毫秒");
}
}
return Data;
}
/// <summary>
/// 写入单个DO
/// </summary>
public static int WriteSingDO(string DeviceName, short ioIndex, IO_VALUE value)
{
if (DIODeviceMap.ContainsKey(DeviceName))
{
short id = DIODeviceMap[DeviceName].DeviceId;
byte Data = (byte)value;
return cdio.OutBit(id, ioIndex, Data);
}
return 0;
}
/// <summary>
/// 读取这块板子上所有DO的值
/// </summary>
public static List<IO_VALUE> ReadAllDO(string DeviceName)
{
List<IO_VALUE> returnList = new List<IO_VALUE>();
try
{
if (DIODeviceMap.ContainsKey(DeviceName))
{
KTKDeviceInfo deviceInfo = DIODeviceMap[DeviceName];
short outPortNum = deviceInfo.OutPortNum;
if (outPortNum <= 0)
{
return returnList;
}
short[] portArray = new short[outPortNum];
byte[] portValueArr = new byte[outPortNum];
for (int inPort = 0; inPort < outPortNum; inPort++)
{
portArray[inPort] = (short)inPort;
}
cdio.EchoBackMultiByte(deviceInfo.DeviceId, portArray, outPortNum, portValueArr);
foreach (byte value in portValueArr)
{
string strValue = System.Convert.ToString(value, 2).PadLeft(portIONum, '0');
for (int index = portIONum - 1; index >= 0; index--)
{
string ioVStr = strValue.Substring(index, 1);
IO_VALUE iov = (IO_VALUE)Int32.Parse(ioVStr);
returnList.Add(iov);
}
}
}
}
catch (Exception ex)
{
LogUtil.error(log, ex.ToString());
}
return returnList;
}
/// <summary>
/// 获取错误文字提示
/// </summary>
public static string DIOGetErrorString(int ErrorCode)
{
//获取错误信息
string errMsg = "";
if (ret != 0)
{
cdio.GetErrorString(ErrorCode, out errMsg);
}
return errMsg;
}
}
}