RFIDManager.cs
8.2 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
using Asa.RFID;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class RFIPManager
{
public static Dictionary<string, Reader> RFReaderMap = new Dictionary<string, Reader>();
private static List<string> RfIPList = new List<string>();
private static System.Timers.Timer conTimer = null;
internal static void ConnectRFIOList(List<string> rfioNameList)
{
if (conTimer == null)
{
conTimer = new System.Timers.Timer();
conTimer.AutoReset = true;
conTimer.Interval = 60000;
conTimer.Elapsed += ConTimer_Elapsed;
}
conTimer.Enabled = false;
RfIPList = new List<string>(rfioNameList);
foreach (string ip in rfioNameList)
{
ConnectionIP(ip);
}
if (RfIPList.Count > 0)
{
conTimer.Start();
}
}
private static bool isProcess = false;
private static DateTime lastTime = DateTime.Now;
private static void ConTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
TimeSpan span = DateTime.Now - lastTime;
if (span.TotalMinutes < RfIPList.Count && isProcess)
{
return;
}
isProcess = true;
lastTime = DateTime.Now;
try
{
List<string> list = new List<string>(RfIPList);
if (list.Count > 0)
{
LogUtil.info("开始重连RFIP模块 ------------");
foreach (string ip in list)
{
ConnectionIP(ip);
}
LogUtil.info("结束重连RFIP模块 ------------");
}
GC.Collect();
}
catch (Exception ex)
{
LogUtil.error("RFIP ConTimer_Elapsed 出错: " + ex.ToString());
}
isProcess = false;
}
internal static void ConnectionIP(string rfip)
{
Reader rfipReader = null;
if (RFReaderMap.ContainsKey(rfip))
{
rfipReader = RFReaderMap[rfip];
if (null != rfipReader)
{
rfipReader.Close();
rfipReader = null;
}
RFReaderMap.Remove(rfip);
}
string logName = "RFIP模块[" + rfip + "] ";
try
{
rfipReader = new Reader();
rfipReader.LocalIP = "192.168.100.101";
rfipReader.RemoteIP = rfip;
RFReaderMap.Add(rfip, rfipReader);
LogUtil.debug("开始连接" + logName + ",尝试重连3次");
for (int i = 1; i <= 3; i++)
{
bool result = rfipReader.Connect();
if (result)
{
LogUtil.info("第【" + i + "】次连接 " + logName + " 成功:");
Thread.Sleep(10);
if (RfIPList.Contains(rfip))
{
RfIPList.Remove(rfip);
}
break;
}
else
{
LogUtil.error("第【" + i + "】次连接 " + logName + " 失败:" + "");
}
Thread.Sleep(5);
GC.Collect();
}
}
catch (Exception error)
{
LogUtil.error("连接RFIP模块 " + logName + " 出错:" + error.ToString());
}
}
internal static void CloseAllConnection()
{
foreach (Reader reader in RFReaderMap.Values)
{
reader.Close();
}
}
public static bool WriteData(string rfIp, RFIPData obj)
{
byte[] sendData = obj.ToData();
return WriteData(rfIp, sendData, 3);
}
public static RFIPData ReadData(string rfIp)
{
byte[] reviceData = ReadData(rfIp, 3);
if (reviceData != null)
{
return new RFIPData(reviceData);
}
return null;
}
public static bool FindRFID(string rfIp)
{
try
{
Reader rfReader = getRfReader(rfIp);
if (rfReader != null)
{
return rfReader.FindRFID();
}
}
catch (Exception ex)
{
LogUtil.error("RFIP FindRFID [" + rfIp + "] 出错 :" + ex.ToString());
}
return false;
}
public static byte[] ReadData(string rfIp, int reReadCount = 1,int startIndex=0,int readLength=10)
{
try
{
Reader rfReader = getRfReader(rfIp);
if (rfReader == null)
{
return null;
}
for (int i = 1; i <= reReadCount; i++)
{
byte[] reviceData = rfReader.Read(startIndex, readLength);
if (reviceData != null)
{
return reviceData;
}
LogUtil.error("RFIP ReadData [" + rfIp + "] 第" + i + "次失败 ");
}
}
catch (Exception ex)
{
LogUtil.error("RFIP ReadData [" + rfIp + "] 出错 :" + ex.ToString());
}
return null;
}
public static bool WriteData(string rfIp, byte[] data, int reWriteCount = 1)
{
try
{
Reader rfReader = getRfReader(rfIp);
if (rfReader == null)
{
return false;
}
for (int i = 1; i <= reWriteCount; i++)
{
bool result = rfReader.Write(data);
if (result)
{
return result;
}
LogUtil.error("RFIP WriteData [" + rfIp + "] 第" + i + "次失败:");
}
}
catch (Exception ex)
{
LogUtil.error("RFIP WriteData [" + rfIp + "] 出错 :" + ex.ToString());
}
return false;
}
private static Reader getRfReader(string rfIp)
{
Reader rfReader = null;
if (RFReaderMap.ContainsKey(rfIp))
{
rfReader = RFReaderMap[rfIp];
}
else
{
LogUtil.error("getRfReader 没有连接RFIP模块:" + rfIp);
}
return rfReader;
}
}
public class RFIPData
{
/// <summary>
/// RFID类型,区分是料架还是托盘,托盘=L
/// </summary>
public int RFType= 1;
/// <summary>
/// 托盘编号,从1-32
/// </summary>
public int Num = 0;
public RFIPData (int num,int t=1)
{
this.RFType = t;
this.Num = num;
}
public RFIPData(byte[] data)
{
try
{
RFType = (int)data[0];
Num = Convert.ToInt32(data[1]);
}
catch (Exception ex)
{
LogUtil.error("RFIP 数据【" + data + "】 获取编码失败");
}
}
public byte[] ToData(int dataLength=10)
{
byte[] sendData = new byte[dataLength];
for (int i = 0; i < sendData.Length; i++)
{
sendData[i] = 0x00;
}
sendData[0] = (byte)RFType;
sendData[1] = (byte)Num;
return sendData;
}
public string ToStr()
{
return "["+RFType+"],Num=["+Num+"]";
}
}
}