-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbpick.cs
672 lines (599 loc) · 22.1 KB
/
Abpick.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Apiabpick
{
class Abpick
{
public struct Diagnosis
{
public string[] Port;
public void Init(int count)
{
Port = new string[count];
}
}
Thread threadRcv;
bool threadRcvflg = true;
delegate void ShowMsgCallback(string text);
const int SUMCMD_CONFIRM_BUTTON = 6;
const int SUBCMD_SHORTAGE_BUTTON = 7;
const int SUBCMD_DIAGRESULT = 9;
const int SUBCMD_COMU_ERROR = 10;
const int SUBCMD_UNEXECUTED = 12;
const int SUBCMD_KEY_JAM = 13;
const int SUBCMD_NO_LIGHT_PUSH = 100;
const int SUBCMD_PRODUCT_FUNCTIONSETTING_INFO = 252;
bool bAPIOpen, bIsCountingTest;
byte iDigitPoint = 0, iTagMode, iCountingNum;
short iLEDInterval, iNodeAddr;
int GWCount, iNumData;
int[] GWID = new int[1000];
Diagnosis[] diagnosis = new Diagnosis[1000];
public Abpick()
{
FormMain_Load();
}
~Abpick()
{
bClose_Click();
}
private void FormMain_Load()
{
String currentpath;
currentpath = System.IO.Directory.GetCurrentDirectory();
ShowMsg(currentpath);
this.timerRcv = new System.Windows.Forms.Timer();
this.timerRcv.Tick += new System.EventHandler(this.timerRcv_Tick);
timerRcv.Interval = 100; timerRcv.Enabled = false;
}
public void bOpen_Click(bool mode)
{
Dap_Open();
ClearGWQuene();
TagClear();
GetGWStatus();
if (mode)
{
timerRcv.Enabled = true;
}
else
{
threadRcv = new Thread(RecFun);
threadRcv.IsBackground = true;
threadRcv.Start();
}
WriteLog("AT500 OPEN");
}
public void bClose_Click()
{
timerRcv.Enabled = false;
threadRcvflg = false; Thread.Sleep(500);
TagClear();
Dap_Close();
WriteLog("AT500 CLOSED");
threadRcvflg = true;
}
private System.Windows.Forms.Timer timerRcv;
private void timerRcv_Tick(object sender, EventArgs e)
{
RcvMsg();
}
private void RecFun()
{
while (threadRcvflg)
{
Thread.Sleep(100);
RcvMsg();
}
}
int intpasscount = 0;
int interrcount = 0;
int intAB_GW_Status = -100;
int intAB_Tag_RcvMsg = -100;
TextBox txMsg = new TextBox();
/// <summary>
/// 显示信息
/// </summary>
/// <param name="msg"></param>
private void ShowMsg(String msg)
{
if (txMsg.InvokeRequired)
{
ShowMsgCallback b = new ShowMsgCallback(ShowMsg);
txMsg.Invoke(b, new object[] { msg });
}
else
{
try
{
Dapapi.AB_AHA_ClrDsp(GWID[0], -86);
if (msg.Contains("SubCmd:6,Data:NOREAD"))
{
Dapapi.AB_Melody_On(GWID[0], -1, 1, 1);
Dapapi.AB_AHA_DspStr(GWID[0], -86, " NOREAD ", 1, 1); //1显示频率长亮
WriteLog(msg);
Thread.Sleep(500);
Dapapi.AB_Melody_On(GWID[0], -1, 1, 0);
Dapapi.AB_LB_DspNum(GWID[0], -19, ++interrcount, 0, 0);
Dapapi.AB_LED_Status(GWID[0], -19, 0, 1); //Red led
}
else
{
if (msg.Contains("TagNode:-84,SubCmd:6,Data:1S"))
{
string str = msg.Split(':')[5].Substring(2, 4) + msg.Split(':')[5].Substring(12, 8);
Dapapi.AB_AHA_DspStr(GWID[0], -86, str, 1, 1); //1显示频率长亮
Dapapi.AB_AHA_DspStr(GWID[0], -20, "PASS", 0, 0);
Dapapi.AB_LB_DspNum(GWID[0], -21, ++intpasscount, 0, 0);
Dapapi.AB_LED_Status(GWID[0], -21, 2, 1); //Amber led
}
else
{
Dapapi.AB_AHA_DspStr(GWID[0], -86, " PASS ", 1, 1); //1显示频率长亮
}
WriteLog(msg);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
/// <summary>
/// 接收标签信息
/// </summary>
private void RcvMsg()
{
int gwid, ret;
short tagNode, subCmd, msgType, dataCnt;
short gwPort, keyType, maxTag;
byte[] rcvData = new byte[200];
Dapapi.Tccb ccb_data;
string tmpStr, rcvStr;
gwid = 0; //all gateway
gwid = 1; //my gateway ID
tagNode = 0; //all tagnode
subCmd = -1; //all subcmd
msgType = 0;
dataCnt = 200;
ret = Dapapi.AB_GW_Status(gwid); //网关ID号
if (intAB_GW_Status != ret)
{
intAB_GW_Status = ret; WriteLog("AB_GW_Status(0)=" + ret);
}
ret = Dapapi.AB_Tag_RcvMsg(ref gwid, ref tagNode, ref subCmd, ref msgType, rcvData, ref dataCnt);
if (intAB_Tag_RcvMsg != ret)
{
intAB_Tag_RcvMsg = ret; WriteLog("AB_Tag_RcvMsg()=" + ret);
}
if (ret > 0)
{
rcvStr = System.Text.Encoding.Default.GetString(rcvData);
rcvStr = rcvStr.Replace("\0", "") + " ";
if (tagNode < 0)
gwPort = 1;
else
gwPort = 2;
tmpStr = "GW_ID:" + gwid + ",GW Port:" + gwPort + ",TagNode:" + tagNode + ",SubCmd:" + subCmd + ",Data:" + rcvStr;
switch (subCmd)
{
case SUMCMD_CONFIRM_BUTTON:
ShowMsg(tmpStr + @"Message:Confirm key is pressed.");
break;
case SUBCMD_SHORTAGE_BUTTON:
ShowMsg(tmpStr + @"Message:Shortage key is pressed.");
break;
case SUBCMD_COMU_ERROR:
ShowMsg(tmpStr + @"Message:Communication fail (Time out).");
break;
case SUBCMD_UNEXECUTED:
ShowMsg(tmpStr + @"Message:Un_executed command.");
break;
case SUBCMD_KEY_JAM:
Dapapi.AB_LB_DspStr(gwid, tagNode, "EEE", 0, 0);
ShowMsg(tmpStr + @"Message:Confirmation key fault(Key jam).");
break;
case SUBCMD_NO_LIGHT_PUSH:
switch (msgType)
{
case 0:
keyType = (byte)Dapapi.AB_GW_RcvButton(rcvData, ref dataCnt);
tmpStr = tmpStr + @"Message:Key code return. Key type = " + keyType + " (";
switch (keyType)
{
case 1:
tmpStr = tmpStr + @"Only CONFIRM button)";
break;
case 2:
tmpStr = tmpStr + @"Only UP button)";
break;
case 3:
tmpStr = tmpStr + @"Only DOWN button)";
break;
case 4:
tmpStr = tmpStr + @"First UP, then CONFIRM)";
break;
case 5:
tmpStr = tmpStr + @"First DOWN, then CONFIRM)";
break;
case 6:
tmpStr = tmpStr + "First UP and DOWN, then CONFIRM)";
break;
case 7:
tmpStr = tmpStr + "First DOWN, then UP)";
break;
case 8:
tmpStr = tmpStr + "First CONFIRM, then UP)";
break;
case 9:
tmpStr = tmpStr + "First DOWN and CONFIRM, then UP)";
break;
case 10:
tmpStr = tmpStr + "Fisrt UP, then DOWN)";
break;
case 11:
tmpStr = tmpStr + "First CONFIRM, then DOWN)";
break;
case 12:
tmpStr = tmpStr + "First UP and CONFIRM, then DOWN)";
break;
default:
tmpStr = tmpStr + "UnKnow)";
break;
}
break;
case 1:
ShowMsg(tmpStr + @"Message:Tag busy.");
break;
case 2:
ShowMsg(tmpStr + @"Message:First record disappear.");
break;
case 3:
ShowMsg(tmpStr + @"Message:506-3W-123 Last Record Confirmed.");
break;
case 7:
ShowMsg(tmpStr + @"Sensor CAPS auto warning return.");
break;
}
break;
case SUBCMD_DIAGRESULT:
string diagstr = "";
ShowMsg(tmpStr + @"DiagResult");
DiagResult(rcvData, ref diagstr);
ShowMsg("GWID:" + gwid + ",Port:" + gwPort + ",SubCmd:" + SUBCMD_DIAGRESULT + ",MaxTag:" + diagstr.Length + ",Data:" + diagstr + ",Set Best Polling Success.");
//ret = Dapapi.AB_GW_RcvMsg(gwid, cData);
//if (ret == 43)
//{
// for (int i = 0; i < 250; i++)
// {
// ccbData[i] = cData[i + 8];
// }
// gwPort = (byte)(ccb_data.msgtype - 96 + 1);
// maxTag = (short)DiagResult(ccbData, ref rcvStr);
// for (int j = 1; j < GWCount; j++)
// {
// if (gwPort == 1)
// {
// ret = Dapapi.AB_GW_SetPollRang(GWID[j], maxTag);
// }
// else
// {
// ret = Dapapi.AB_GW_SetPollRang(GWID[j], (short)(maxTag * -1));
// }
// }
// ShowMsg("GWID:" + gwid + ",Port:" + gwPort + ",SubCmd:" + cData[6] + ",MaxTag:" + maxTag + ",Data:" + rcvStr + ",Set Best Polling Success.");
//}
break;
case SUBCMD_PRODUCT_FUNCTIONSETTING_INFO:
tmpStr = "GWID:" + gwid + ",GW Port:" +
gwPort + ",Tag Node:" + tagNode + ",SubCmd:" + subCmd + ",Serial:" + rcvData[0] + ",Version:" + rcvData[1] + ",TagMode:" + rcvData[2];
ShowMsg(tmpStr);
break;
default:
ShowMsg(tmpStr);
break;
}
}
}
/// <summary>
/// 写日志
/// </summary>
/// <param name="msg"></param>
private void WriteLog(string msg)
{
Console.WriteLine(DateTime.Now.ToString() + " " + msg);
String logFile = "pick.log";
if (!File.Exists(logFile))
{
FileStream fs = new FileStream(logFile, FileMode.CreateNew);
fs.Close();
}
using (StreamWriter sw = File.AppendText(logFile))
{
sw.WriteLine(DateTime.Now.ToString() + "=>" + msg);
}
}
/// <summary>
/// 解析接收诊断值
/// </summary>
/// <param name="ccb_data"></param>
/// <param name="tagstr"></param>
/// <returns></returns>
private int DiagResult(byte[] ccb_data, ref string tagstr)
{
int k, tmp, maxid;
tagstr = "";
tmp = 0;
maxid = 0;
for (k = 1; k <= 250; k++)
{
if ((k - 1) % 8 == 0)
{
tmp = ccb_data[3 + (k - 1) / 8];
}
if (tmp % 2 != 1)
{
maxid = k;
tagstr = tagstr + "1";
}
else
{
tagstr = tagstr + "0";
}
tmp = tmp / 2;
}
tagstr = tagstr.Substring(0, maxid);
return maxid;
}
/// <summary>
/// API 函数初始化
/// </summary>
/// <returns></returns>
private int APIOpen()
{
if (!bAPIOpen)
{
int ret;
try
{
ret = Dapapi.AB_API_Open();
if (ret < 0)
return -1;
else
{
bAPIOpen = true;
return 1;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
return 0;
}
/// <summary>
/// API注销
/// </summary>
/// <returns></returns>
private int APIClose()
{
if (bAPIOpen)
{
Dapapi.AB_API_Close();
bAPIOpen = false;
return 1;
}
else
return 0;
}
/// <summary>
/// 控制器打开
/// </summary>
/// <returns></returns>
private int Dap_Open()
{
int posspace, postab, pos;
GWCount = 0;
if (!System.IO.File.Exists("IPINDEX"))
{
Console.WriteLine("文件IPINDEX不存在!");
return -1;
}
try
{
using (System.IO.StreamReader sr = new System.IO.StreamReader("IPINDEX"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
GWCount++;
posspace = line.IndexOf(" "); //find space
postab = line.IndexOf((char)9); //find tab
if (posspace <= 0) posspace = postab;
if (postab <= 0) postab = posspace;
pos = System.Math.Min(posspace, postab);
if (pos <= 0)
{
Console.WriteLine("IPINDEX格式错误!");
return -1;
}
//txAddrList.AppendText(line + "\r\n");
GWID[GWCount - 1] = int.Parse(line.Substring(0, pos));
diagnosis[GWCount - 1].Init(2);
}
}
}
catch (Exception e)
{
Console.WriteLine("读取文件IPINDEX失败!" + e.Message);
return -1;
}
if (APIOpen() < 0)
{
Console.WriteLine("初始化API失败!");
return -1;
}
ShowMsg("API Open Success!");
for (int i = 0; i < GWCount; i++)
{
if (Dapapi.AB_GW_Open(GWID[i]) < 0)
{
ShowMsg("控制器'" + GWID[i] + "'打开失败!");
}
}
return 1;
}
/// <summary>
/// 控制器关闭
/// </summary>
private void Dap_Close()
{
//TagClear
APIClose();
}
/// <summary>
/// 熄灭所有标签
/// </summary>
private void TagClear()
{
for (int i = 0; i < GWCount; i++)
{
if (Dapapi.AB_GW_Status(GWID[i]) == 7)
{
Dapapi.AB_LB_DspNum(GWID[i], -252, 0, 0, -3);
Dapapi.AB_LB_DspNum(GWID[i], 252, 0, 0, -3);
Dapapi.AB_LED_Dsp(GWID[i], -252, 0, 0);
Dapapi.AB_LED_Dsp(GWID[i], 252, 0, 0);
Dapapi.AB_BUZ_On(GWID[i], -252, 0);
Dapapi.AB_BUZ_On(GWID[i], 252, 0);
Dapapi.AB_LB_DspStr(GWID[i], -252, "", 0, -3);
Dapapi.AB_LB_DspStr(GWID[i], 252, "", 0, -3);
//12-digits Alphanumerical display
Dapapi.AB_AHA_ClrDsp(GWID[i], -252);
Dapapi.AB_AHA_ClrDsp(GWID[i], 252);
Dapapi.AB_AHA_BUZ_On(GWID[i], -252, 0);
Dapapi.AB_AHA_BUZ_On(GWID[i], 252, 0);
}
}
}
/// <summary>
/// 清空消息对列
/// </summary>
private void ClearGWQuene()
{
byte[] cData = new byte[200];
int gwid;
short tagNode, subCmd, msgType, dataCnt;
gwid = 0;
tagNode = 0;
subCmd = -1;
msgType = 0;
dataCnt = 0;
while (Dapapi.AB_Tag_RcvMsg(ref gwid, ref tagNode, ref subCmd, ref msgType, cData, ref dataCnt) > 0)
{
}
}
/// <summary>
/// 获取控制器状态
/// </summary>
private void GetGWStatus()
{
bool bGoOn;
int ret, timeStart;
for (int i = 0; i < GWCount; i++)
{
Dapapi.AB_GW_Open(GWID[i]);
ret = Dapapi.AB_GW_Status(GWID[i]);
if (ret != 7)
{
bGoOn = true;
timeStart = System.Environment.TickCount;
while (bGoOn)
{
ret = Dapapi.AB_GW_Status(GWID[i]);
if (ret == 7)
bGoOn = false;
else if (System.Environment.TickCount - timeStart > 3000)
{
bGoOn = false;
}
}
}
if (ret == 7)
{
ShowMsg("Gateway ID:" + GWID[i] + " success connected, status return :" + ret);
}
else
{
ShowMsg("Gateway ID:" + GWID[i] + " failed to connected, status return :" + ret);
}
}
}
/// <summary>
/// 获取连结错误代码
/// </summary>
/// <param name="retcode"></param>
/// <returns></returns>
private string GetConnectErr(int retcode)
{
switch (retcode)
{
case -3:
return "Parameter data is error!";
case -2:
return "TCP is not created yet!";
case -1:
return "DAP_IP out of range !";
case 0:
return "Closed";
case 1:
return "Open";
case 2:
return "Listening";
case 3:
return "Connection is Pending";
case 4:
return "Resolving the host name";
case 5:
return "Host is resolved";
case 6:
return "Waiting to Connect";
case 7:
return "Connect OK";
case 8:
return "Connection is closing";
case 9:
return "State error has occurred";
case 10:
return "Connection state is undetermined";
default:
return "Unkown error code";
}
}
/// <summary>
/// 检查字符是否是数字
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private bool IsNumber(String value)
{
for (int i = 0; i < value.Length; i++)
{
if (!Char.IsDigit(value, i))
{
return false;
}
}
return true;
}
}
}