-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
5062 lines (4752 loc) · 192 KB
/
Form1.cs
File metadata and controls
5062 lines (4752 loc) · 192 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
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Resources;
using System.Reflection;
using System.Net;
using System.Threading;
using System.Diagnostics;
using UHF;
using System.IO;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace UHFReader288MPDemo
{
public partial class Form1 : Form
{
[DllImport("User32.dll", EntryPoint = "PostMessage")]
private static extern int PostMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
uint wParam, // first message parameter
uint lParam // second message parameter
);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public const int USER = 0x0400;
public const int WM_SENDTAG = USER + 101;
public const int WM_SENDTAGSTAT = USER + 102;
public const int WM_SENDSTATU = USER + 103;
public const int WM_SENDBUFF = USER + 104;
public const int WM_MIXTAG = USER + 105;
public const int WM_SHOWNUM = USER + 106;
public const int WM_FASTID = USER + 107;
private byte fComAdr = 0xff; //当前操作的ComAdr
private int ferrorcode;
private byte fBaud;
private double fdminfre;
private double fdmaxfre;
private int fCmdRet = 30; //所有执行指令的返回值
private bool fisinventoryscan_6B;
private byte[] fOperEPC = new byte[100];
private byte[] fPassWord = new byte[4];
private byte[] fOperID_6B = new byte[10];
ArrayList list = new ArrayList();
private List<string> epclist = new List<string>();
private List<string> tidlist = new List<string>();
private int CardNum1 =0;
private string fInventory_EPC_List; //存贮询查列表(如果读取的数据没有变化,则不进行刷新)
private int frmcomportindex;
private bool SeriaATflag = false;
private byte Target = 0;
private byte InAnt = 0;
private byte Scantime = 0;
private byte FastFlag = 0;
private byte Qvalue = 0;
private byte Session = 0;
private int total_turns = 0;//轮数
private int total_tagnum = 0;//标签数量
private int CardNum = 0;
private int total_time = 0;//总时间
private int targettimes = 0;
private byte TIDFlag = 0;
private byte tidLen = 0;
private byte tidAddr = 0;
public static byte antinfo = 0;
private int AA_times = 0;
private int CommunicationTime = 0;
public DeviceClass SelectedDevice;
private static List<DeviceClass> DevList;
private static SearchCallBack searchCallBack = new SearchCallBack(searchCB);
private string ReadTypes="";
/// <summary>
/// Device Search的回调函数;
/// </summary>
private static void searchCB(IntPtr dev, IntPtr data)
{
uint ipAddr = 0;
StringBuilder devname = new StringBuilder(100);
StringBuilder macAdd = new StringBuilder(100);
//获取搜索到的设备信息;
DevControl.tagErrorCode eCode = DevControl.DM_GetDeviceInfo(dev, ref ipAddr, macAdd, devname);
if (eCode == DevControl.tagErrorCode.DM_ERR_OK)
{
//将搜索到的设备加入设备列表;
DeviceClass device = new DeviceClass(dev, ipAddr, macAdd.ToString(), devname.ToString());
DevList.Add(device);
}
else
{
//异常处理;
string errMsg = ErrorHandling.GetErrorMsg(eCode);
Log.WriteError(errMsg);
}
}
private static IPAddress getIPAddress(uint interIP)
{
return new IPAddress((uint)IPAddress.HostToNetworkOrder((int)interIP));
}
RFIDCallBack elegateRFIDCallBack;
public Form1()
{
InitializeComponent();
//初始化设备列表;
DevList = new List<DeviceClass>();
//初始化设备控制模块;
DevControl.tagErrorCode eCode = DevControl.DM_Init(searchCallBack, IntPtr.Zero);
if (eCode != DevControl.tagErrorCode.DM_ERR_OK)
{
//如果初始化失败则关闭程序,并进行异常处理;
string errMsg = ErrorHandling.HandleError(eCode);
throw new Exception(errMsg);
}
elegateRFIDCallBack = new RFIDCallBack(GetUid);
}
string epcandtid = "";//标记整合数据
int lastnum = 0;
public void GetUid(IntPtr p, Int32 nEvt)
{
RFIDTag ce = (RFIDTag)Marshal.PtrToStructure(p, typeof(RFIDTag));
this.Invoke((EventHandler)delegate
{
IntPtr ptrWnd = IntPtr.Zero;
ptrWnd = FindWindow(null, "UHFReader288MP Demo V2.2");
if (ptrWnd != IntPtr.Zero) // 检查当前统计窗口是否打开
{
if (rb_mix.Checked)
{
int gnum = ce.PacketParam;
if (gnum < 0x80)//EPC号
{
lastnum = gnum;
epcandtid = ce.UID;
}
else//附带数据
{
if (((lastnum & 0x3F) == ((gnum & 0x3F) - 1)) || ((lastnum & 0x3F) == 127 && ((gnum & 0x3F) == 0)))//相邻的滚码
{
epcandtid = epcandtid + "-" + ce.UID;
if (ptrWnd != IntPtr.Zero) // 检查当前统计窗口是否打开
{
int Antnum = ce.ANT;
string str_ant = Convert.ToString(Antnum, 2).PadLeft(4, '0');
string para = str_ant + "," + epcandtid + "," + ce.RSSI.ToString();
SendMessage(ptrWnd, WM_MIXTAG, IntPtr.Zero, para);
}
}
else
{
epcandtid = "";
}
}
}
else if (rb_fastid.Checked)
{
int Antnum = ce.ANT;
string str_ant = Convert.ToString(Antnum, 2).PadLeft(4, '0');
string epclen = Convert.ToString(ce.LEN, 16);
if (epclen.Length == 1) epclen = "0" + epclen;
string para = str_ant + "," + epclen + ce.UID + "," + ce.RSSI.ToString() + " ";
SendMessage(ptrWnd, WM_FASTID, IntPtr.Zero, para);
}
else
{
int Antnum = ce.ANT;
string str_ant = Convert.ToString(Antnum, 2).PadLeft(4, '0');
string epclen = Convert.ToString(ce.LEN, 16);
if (epclen.Length == 1) epclen = "0" + epclen;
string para = str_ant + "," + epclen + ce.UID + "," + ce.RSSI.ToString() + " ";
SendMessage(ptrWnd, WM_SENDTAG, IntPtr.Zero, para);
}
}
total_tagnum++;
CardNum++;
});
}
protected override void DefWndProc(ref Message m)
{
if (m.Msg == WM_SENDTAG)
{
string tagInfo = Marshal.PtrToStringAnsi(m.LParam);
string sEPC;
string str_ant = tagInfo.Substring(0, 4);
tagInfo = tagInfo.Substring(5);
int index = tagInfo.IndexOf(',');
sEPC = tagInfo.Substring(0, index);
string str_epclen = sEPC.Substring(0, 2);
byte epclen = Convert.ToByte(str_epclen, 16);
sEPC = sEPC.Substring(2);
index++;
string RSSI = tagInfo.Substring(index);
DataTable dt = dataGridView1.DataSource as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("Column1", Type.GetType("System.String"));
dt.Columns.Add("Column2", Type.GetType("System.String"));
dt.Columns.Add("Column3", Type.GetType("System.String"));
dt.Columns.Add("Column4", Type.GetType("System.String"));
dt.Columns.Add("Column5", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr["Column1"] = (dt.Rows.Count + 1).ToString();
dr["Column2"] = sEPC;
dr["Column3"] = "1";
dr["Column4"] = RSSI;
dr["Column5"] = str_ant;
dt.Rows.Add(dr);
if (rb_fastid.Checked)
{
if ((epclen & 0x80) == 0)//只有EPC
{
if (epclist.IndexOf(sEPC) == -1)
{
epclist.Add(sEPC);
}
lxLedControl1.Text = epclist.Count.ToString();
}
else//同时有EPC和TID
{
int len = epclen & 0x7F;
string myepc = sEPC.Substring(0, (len - 12) * 2);
string mytid = sEPC.Substring((len - 12) * 2, 24);
if (epclist.IndexOf(myepc) == -1)
{
epclist.Add(myepc);
}
if (tidlist.IndexOf(mytid) == -1)
{
tidlist.Add(mytid);
}
lxLedControl1.Text = epclist.Count.ToString();
lxLedControl6.Text = tidlist.Count.ToString();
}
}
else if (rb_epc.Checked)
{
lxLedControl1.Text = dt.Rows.Count.ToString();
}
else if (rb_tid.Checked)
{
lxLedControl6.Text = dt.Rows.Count.ToString();
}
lxLedControl5.Text = dt.Rows.Count.ToString();
comboBox_EPC.Items.Add(sEPC);
}
else
{
DataRow[] dr;
dr = dt.Select("Column2='" + sEPC + "'");
if (dr.Length == 0) // epc号不存在
{
DataRow dr2 = dt.NewRow();
dr2["Column1"] = (dt.Rows.Count + 1).ToString();
dr2["Column2"] = sEPC;
dr2["Column3"] = "1";
dr2["Column4"] = RSSI;
dr2["Column5"] = str_ant;
dt.Rows.Add(dr2);
if (rb_fastid.Checked)
{
if ((epclen & 0x80) == 0)//只有EPC
{
if (epclist.IndexOf(sEPC) == -1)
{
epclist.Add(sEPC);
}
lxLedControl1.Text = epclist.Count.ToString();
}
else//同时有EPC和TID
{
int len = epclen & 0x7F;
string myepc = sEPC.Substring(0, (len - 12) * 2);
string mytid = sEPC.Substring((len - 12) * 2, 24);
if (epclist.IndexOf(myepc) == -1)
{
epclist.Add(myepc);
}
if (tidlist.IndexOf(mytid) == -1)
{
tidlist.Add(mytid);
}
lxLedControl1.Text = epclist.Count.ToString();
lxLedControl6.Text = tidlist.Count.ToString();
}
}
else if (rb_epc.Checked)
{
lxLedControl1.Text = dt.Rows.Count.ToString();
}
else if (rb_tid.Checked)
{
lxLedControl6.Text = dt.Rows.Count.ToString();
}
lxLedControl5.Text = (System.Environment.TickCount - total_time).ToString();
comboBox_EPC.Items.Add(sEPC);
}
else // epc号已存在
{
int cnt = int.Parse(dr[0]["Column3"].ToString());
cnt++;
dt.Rows[dt.Rows.IndexOf(dr[0])]["Column3"] = cnt.ToString();
dt.Rows[dt.Rows.IndexOf(dr[0])]["Column4"] = RSSI;
int ant1 = Convert.ToInt32(dr[0]["Column5"].ToString(), 2);
int ant2 = Convert.ToInt32(str_ant, 2);
dt.Rows[dt.Rows.IndexOf(dr[0])]["Column5"] = Convert.ToString((ant1 | ant2), 2).PadLeft(4, '0');
}
}
bool flagset = false;
flagset = (dataGridView1.DataSource == null) ? true : false;
dataGridView1.DataSource = dt;
if (flagset)
{
dataGridView1.Columns["Column1"].HeaderText = "NO.";
dataGridView1.Columns["Column1"].Width = 80;
dataGridView1.Columns["Column2"].HeaderText = "EPC";
dataGridView1.Columns["Column2"].Width = 300;
dataGridView1.Columns["Column3"].HeaderText = "Times";
dataGridView1.Columns["Column3"].Width = 80;
dataGridView1.Columns["Column4"].HeaderText = "RSSI";
dataGridView1.Columns["Column4"].Width = 80;
dataGridView1.Columns["Column5"].HeaderText = "Ant(4-1)";
dataGridView1.Columns["Column5"].Width = 100;
}
}
else if (m.Msg == WM_MIXTAG)
{
string tagInfo = Marshal.PtrToStringAnsi(m.LParam);
string sEPC, sData;
string str_ant = tagInfo.Substring(0, 4);
tagInfo = tagInfo.Substring(5);
int index = tagInfo.IndexOf(',');
sEPC = tagInfo.Substring(0, index);
int n = sEPC.IndexOf("-");
sData = sEPC.Substring(n + 1);
sEPC = sEPC.Substring(0, n);
index++;
string RSSI = tagInfo.Substring(index);
DataTable dt = dataGridView1.DataSource as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("Column1", Type.GetType("System.String"));
dt.Columns.Add("Column2", Type.GetType("System.String"));
dt.Columns.Add("Column3", Type.GetType("System.String"));
dt.Columns.Add("Column4", Type.GetType("System.String"));
dt.Columns.Add("Column5", Type.GetType("System.String"));
dt.Columns.Add("Column6", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr["Column1"] = (dt.Rows.Count + 1).ToString();
dr["Column2"] = sEPC;
dr["Column3"] = sData;
dr["Column4"] = "1";
dr["Column5"] = RSSI;
dr["Column6"] = str_ant;
dt.Rows.Add(dr);
lxLedControl1.Text = dt.Rows.Count.ToString();
lxLedControl5.Text = dt.Rows.Count.ToString();
comboBox_EPC.Items.Add(sEPC);
}
else
{
DataRow[] dr;
dr = dt.Select("Column2='" + sEPC + "'");
if (dr.Length == 0) // epc号不存在
{
DataRow dr2 = dt.NewRow();
dr2["Column1"] = (dt.Rows.Count + 1).ToString();
dr2["Column2"] = sEPC;
dr2["Column3"] = sData;
dr2["Column4"] = "1";
dr2["Column5"] = RSSI;
dr2["Column6"] = str_ant;
dt.Rows.Add(dr2);
lxLedControl1.Text = dt.Rows.Count.ToString();
lxLedControl5.Text = (System.Environment.TickCount - total_time).ToString();
comboBox_EPC.Items.Add(sEPC);
}
else // epc号已存在
{
int cnt = int.Parse(dr[0]["Column4"].ToString());
cnt++;
dt.Rows[dt.Rows.IndexOf(dr[0])]["Column4"] = cnt.ToString();
dt.Rows[dt.Rows.IndexOf(dr[0])]["Column5"] = RSSI;
int ant1 = Convert.ToInt32(dr[0]["Column6"].ToString(), 2);
int ant2 = Convert.ToInt32(str_ant, 2);
dt.Rows[dt.Rows.IndexOf(dr[0])]["Column6"] = Convert.ToString((ant1 | ant2), 2).PadLeft(4, '0');
}
}
bool flagset = false;
flagset = (dataGridView1.DataSource == null) ? true : false;
dataGridView1.DataSource = dt;
if (flagset)
{
dataGridView1.Columns["Column1"].HeaderText = "No.";
dataGridView1.Columns["Column1"].Width = 60;
dataGridView1.Columns["Column2"].HeaderText = "EPC";
dataGridView1.Columns["Column2"].Width = 280;
dataGridView1.Columns["Column3"].HeaderText = "Data";
dataGridView1.Columns["Column3"].Width = 150;
dataGridView1.Columns["Column4"].HeaderText = "Times";
dataGridView1.Columns["Column4"].Width = 60;
dataGridView1.Columns["Column5"].HeaderText = "RSSI";
dataGridView1.Columns["Column5"].Width = 60;
dataGridView1.Columns["Column6"].HeaderText = "Ant(4-1)";
dataGridView1.Columns["Column6"].Width = 60;
}
}
else if (m.Msg == WM_FASTID)
{
string tagInfo = Marshal.PtrToStringAnsi(m.LParam);
string sEPC = "";
string sTID = "";
string str_ant = tagInfo.Substring(0, 4);
tagInfo = tagInfo.Substring(5);
int index = tagInfo.IndexOf(',');
sEPC = tagInfo.Substring(0, index);
string str_epclen = sEPC.Substring(0, 2);
byte nlen = Convert.ToByte(str_epclen, 16);
if ((nlen & 0x80) == 0)
{
sEPC = sEPC.Substring(2);//只有EPC
if (epclist.IndexOf(sEPC) == -1)
{
epclist.Add(sEPC);
}
lxLedControl1.Text = epclist.Count.ToString();
}
else
{
int epclen = (nlen & 0x7F) - 12;
sTID = sEPC.Substring(2 + epclen * 2, 24);
sEPC = sEPC.Substring(2, epclen * 2);
if (epclist.IndexOf(sEPC) == -1)
{
epclist.Add(sEPC);
}
if (tidlist.IndexOf(sTID) == -1)
{
tidlist.Add(sTID);
}
lxLedControl1.Text = epclist.Count.ToString();
lxLedControl6.Text = tidlist.Count.ToString();
}
index++;
string RSSI = tagInfo.Substring(index);
DataTable dt = dataGridView1.DataSource as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("Column1", Type.GetType("System.String"));
dt.Columns.Add("Column2", Type.GetType("System.String"));
dt.Columns.Add("Column3", Type.GetType("System.String"));
dt.Columns.Add("Column4", Type.GetType("System.String"));
dt.Columns.Add("Column5", Type.GetType("System.String"));
dt.Columns.Add("Column6", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr["Column1"] = (dt.Rows.Count + 1).ToString();
dr["Column2"] = sEPC;
dr["Column3"] = sTID;
dr["Column4"] = "1";
dr["Column5"] = RSSI;
dr["Column6"] = str_ant;
dt.Rows.Add(dr);
//lxLedControl1.Text = dt.Rows.Count.ToString();
lxLedControl5.Text = dt.Rows.Count.ToString();
comboBox_EPC.Items.Add(sEPC);
}
else
{
DataRow[] dr;
dr = dt.Select("Column2='" + sEPC + "' and Column3='" + sTID + "'");
if (dr.Length == 0) // epc号不存在
{
DataRow dr2 = dt.NewRow();
dr2["Column1"] = (dt.Rows.Count + 1).ToString();
dr2["Column2"] = sEPC;
dr2["Column3"] = sTID;
dr2["Column4"] = "1";
dr2["Column5"] = RSSI;
dr2["Column6"] = str_ant;
dt.Rows.Add(dr2);
//lxLedControl1.Text = dt.Rows.Count.ToString();
lxLedControl5.Text = (System.Environment.TickCount - total_time).ToString();
comboBox_EPC.Items.Add(sEPC);
}
else // epc号已存在
{
int cnt = int.Parse(dr[0]["Column4"].ToString());
cnt++;
dt.Rows[dt.Rows.IndexOf(dr[0])]["Column4"] = cnt.ToString();
dt.Rows[dt.Rows.IndexOf(dr[0])]["Column5"] = RSSI;
int ant1 = Convert.ToInt32(dr[0]["Column6"].ToString(), 2);
int ant2 = Convert.ToInt32(str_ant, 2);
dt.Rows[dt.Rows.IndexOf(dr[0])]["Column6"] = Convert.ToString((ant1 | ant2), 2).PadLeft(4, '0');
}
}
bool flagset = false;
flagset = (dataGridView1.DataSource == null) ? true : false;
dataGridView1.DataSource = dt;
if (flagset)
{
dataGridView1.Columns["Column1"].HeaderText = "No.";
dataGridView1.Columns["Column1"].Width = 60;
dataGridView1.Columns["Column2"].HeaderText = "EPC";
dataGridView1.Columns["Column2"].Width = 280;
dataGridView1.Columns["Column3"].HeaderText = "Data";
dataGridView1.Columns["Column3"].Width = 150;
dataGridView1.Columns["Column4"].HeaderText = "Times";
dataGridView1.Columns["Column4"].Width = 60;
dataGridView1.Columns["Column5"].HeaderText = "RSSI";
dataGridView1.Columns["Column5"].Width = 60;
dataGridView1.Columns["Column6"].HeaderText = "Ant(4-1)";
dataGridView1.Columns["Column6"].Width = 60;
}
}
else if (m.Msg == WM_SENDTAGSTAT)
{
string tagInfo = Marshal.PtrToStringAnsi(m.LParam);
int index = tagInfo.IndexOf(',');
string tagRate = tagInfo.Substring(0, index);
index++;
string str = tagInfo.Substring(index);
index = str.IndexOf(',');
string tagNum = str.Substring(0, index);
index++;
string cmdTime = str.Substring(index);
lxLedControl2.Text = tagRate;
lxLedControl3.Text = cmdTime;
lxLedControl4.Text = tagNum;
}
else if (m.Msg == WM_SENDSTATU)
{
string Info = Marshal.PtrToStringAnsi(m.LParam);
fCmdRet = Convert.ToInt32(Info);
string strLog = "Inventory: " + GetReturnCodeDesc(fCmdRet);
WriteLog(lrtxtLog, strLog, 1);
}
else if (m.Msg == WM_SENDBUFF)
{
string tagInfo = Marshal.PtrToStringAnsi(m.LParam);
int index = tagInfo.IndexOf(',');
string tagNum = tagInfo.Substring(0, index);
index++;
string str = tagInfo.Substring(index);
index = str.IndexOf(',');
string cmdTime = str.Substring(0, index);
index++;
str = str.Substring(index);
index = str.IndexOf(',');
string tagRate = str.Substring(0, index);
index++;
str = str.Substring(index);
string total_tagnum = str;
lxLed_BNum.Text = tagNum;
lxLed_Bcmdsud.Text = tagRate;
lxLed_cmdTime.Text = cmdTime;
lxLed_Btoltag.Text = total_tagnum;
WriteLog(lrtxtLog, "Buffer-Inventiry:Operation success", 1);
}
else if (m.Msg == WM_SHOWNUM)
{
lxLedControl5.Text = (System.Environment.TickCount - total_time).ToString();
}
else
base.DefWndProc(ref m);
}
private delegate void WriteLogUnSafe(CustomControl.LogRichTextBox logRichTxt, string strLog, int nType);
private void WriteLog(CustomControl.LogRichTextBox logRichTxt, string strLog, int nType)
{
if (this.InvokeRequired)
{
WriteLogUnSafe InvokeWriteLog = new WriteLogUnSafe(WriteLog);
this.Invoke(InvokeWriteLog, new object[] { logRichTxt, strLog, nType });
}
else
{
if ((ckClearOperationRec.Checked)&&(lrtxtLog.Lines.Length > 20))
lrtxtLog.Clear();
if ((nType == 0) || (nType == 0x26) || (nType == 0x01) || (nType == 0x02) || (nType == 0xFB))
{
logRichTxt.AppendTextEx(strLog, Color.Indigo);
}
else
{
logRichTxt.AppendTextEx(strLog, Color.Red);
}
logRichTxt.Select(logRichTxt.TextLength, 0);
logRichTxt.ScrollToCaret();
}
}
/// <summary>
/// 16进制数组字符串转换
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
#region
public static byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
public static string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
return sb.ToString().ToUpper();
}
#endregion
/// <summary>
/// 错误代码
/// </summary>
/// <param name="cmdRet"></param>
/// <returns></returns>
#region
private string GetReturnCodeDesc(int cmdRet)
{
switch (cmdRet)
{
case 0x00:
case 0x26:
return "success";
case 0x01:
return "Return before Inventory finished";
case 0x02:
return "the Inventory-scan-time overflow";
case 0x03:
return "More Data";
case 0x04:
return "Reader module MCU is Full";
case 0x05:
return "Access Password Error";
case 0x09:
return "Destroy Password Error";
case 0x0a:
return "Destroy Password Error Cannot be Zero";
case 0x0b:
return "Tag Not Support the command";
case 0x0c:
return "Use the commmand,Access Password Cannot be Zero";
case 0x0d:
return "Tag is protected,cannot set it again";
case 0x0e:
return "Tag is unprotected,no need to reset it";
case 0x10:
return "There is some locked bytes,write fail";
case 0x11:
return "can not lock it";
case 0x12:
return "is locked,cannot lock it again";
case 0x13:
return "Parameter Save Fail,Can Use Before Power";
case 0x14:
return "Cannot adjust";
case 0x15:
return "Return before Inventory finished";
case 0x16:
return "Inventory-Scan-Time overflow";
case 0x17:
return "More Data";
case 0x18:
return "Reader module MCU is full";
case 0x19:
return "'Not Support Command Or AccessPassword Cannot be Zero";
case 0x1A:
return "Tag custom function error";
case 0xF8:
return "Check antenna error";
case 0xF9:
return "Command execute error";
case 0xFA:
return "Get Tag,Poor Communication,Inoperable";
case 0xFB:
return "No Tag Operable";
case 0xFC:
return "Tag Return ErrorCode";
case 0xFD:
return "Command length wrong";
case 0xFE:
return "Illegal command";
case 0xFF:
return "Parameter Error";
case 0x30:
return "Communication error";
case 0x31:
return "CRC checksummat error";
case 0x32:
return "Return data length error";
case 0x33:
return "Communication busy";
case 0x34:
return "Busy,command is being executed";
case 0x35:
return "ComPort Opened";
case 0x36:
return "ComPort Closed";
case 0x37:
return "Invalid Handle";
case 0x38:
return "Invalid Port";
case 0xEE:
return "Return Command Error";
default:
return "";
}
}
private string GetErrorCodeDesc(int cmdRet)
{
switch (cmdRet)
{
case 0x00:
return "Other error";
case 0x03:
return "Memory out or pc not support";
case 0x04:
return "Memory Locked and unwritable";
case 0x0b:
return "No Power,memory write operation cannot be executed";
case 0x0f:
return "Not Special Error,tag not support special errorcode";
default:
return "";
}
}
#endregion
private void DisabledForm()
{
////应答模式下
lxLedControl1.Text = "0";
lxLedControl2.Text = "0";
lxLedControl3.Text = "0";
lxLedControl4.Text = "0";
lxLedControl5.Text = "0";
dataGridView1.DataSource = null;
comboBox_EPC.Items.Clear();
text_RDVersion.Text = "";
text_Serial.Text = "";
timer_answer.Enabled = false;
btIventoryG2.Text = "Start";
panel1.Enabled = false;
panel4.Enabled = false;
panel5.Enabled = false;
panel8.Enabled =false;
panel9.Enabled = false;
panel10.Enabled = false;
gpb_address.Enabled = false;
gpb_antconfig.Enabled = false;
gpb_baud.Enabled = false;
gpb_GPIO.Enabled = false;
gpb_beep.Enabled = false;
gpb_RDVersion.Enabled = false;
gpb_checkant.Enabled = false;
gpb_DBM.Enabled = false;
gpb_Serial.Enabled = false;
gpb_Relay.Enabled = false;
gpb_OutputRep.Enabled = false;
gpb_Freq.Enabled = false;
gbp_buff.Enabled =false;
btDefault.Enabled = false;
btGetInformation.Enabled = false;
group_maxtime.Enabled = false;
gbp_wpower.Enabled = false;
gbp_Retry.Enabled = false;
gbp_DRM.Enabled = false;
gbCmdTemperature.Enabled = false;
gbReturnLoss.Enabled = false;
btDefault.Enabled = false;
}
private void EnabledForm()
{
panel1.Enabled = true;
panel4.Enabled = true;
panel5.Enabled = true;
panel8.Enabled =true;
panel9.Enabled = true;
panel10.Enabled = true;
gpb_address.Enabled = true;
gpb_antconfig.Enabled = true;
gpb_baud.Enabled = true;
gpb_GPIO.Enabled = true;
gpb_beep.Enabled = true;
gpb_RDVersion.Enabled = true;
gpb_checkant.Enabled = true;
gpb_DBM.Enabled = true;
gpb_Serial.Enabled = true;
gpb_Relay.Enabled = true;
gpb_OutputRep.Enabled = true;
gpb_Freq.Enabled = true;
gbp_buff.Enabled =true;
btGetInformation.Enabled = true;
group_maxtime.Enabled = true;
gbp_wpower.Enabled = true ;
gbp_Retry.Enabled = true;
gbp_DRM.Enabled = true;
gbCmdTemperature.Enabled = true;
gbReturnLoss.Enabled = true;
btDefault.Enabled = true;
}
private void rb_rs232_CheckedChanged(object sender, EventArgs e)
{
if (rb_rs232.Checked)
{
if ((frmcomportindex > 0) && (frmcomportindex < 256))
{
if (frmcomportindex > 0)
fCmdRet = RWDev.CloseNetPort(frmcomportindex);
if (fCmdRet == 0)
{
frmcomportindex = -1;
btConnectTcp.Enabled = true;
btDisConnectTcp.Enabled = false;
DisabledForm();
btConnectTcp.ForeColor = Color.Indigo;
btDisConnectTcp.ForeColor = Color.Black;
SetButtonBold(btConnectTcp);
SetButtonBold(btDisConnectTcp);
}
if (fCmdRet != 0)
{
string strLog = "TCP close failed: " + GetReturnCodeDesc(fCmdRet);
WriteLog(lrtxtLog, strLog, 1);
return;
}
else
{
string strLog = "TCP close success";
WriteLog(lrtxtLog, strLog, 0);
}
}
gpb_rs232.Enabled = true;
btDisConnect232.Enabled = false;
//设置按钮字体颜色
btConnect232.ForeColor = Color.Indigo;
SetButtonBold(btConnect232);
if (btConnectTcp.Font.Bold)
{
SetButtonBold(btConnectTcp);
}
gpb_tcp.Enabled = false;
}
}
private void rb_tcp_CheckedChanged(object sender, EventArgs e)
{
if (rb_tcp.Checked)
{
if ((frmcomportindex > 0) && (frmcomportindex<256))
{
if (frmcomportindex > 0)
fCmdRet = RWDev.CloseSpecComPort(frmcomportindex);
if (fCmdRet == 0)
{
frmcomportindex = -1;
DisabledForm();
btConnect232.Enabled = true;
btDisConnect232.Enabled = false;
btConnect232.ForeColor = Color.Indigo;
btDisConnect232.ForeColor = Color.Black;
SetButtonBold(btConnect232);
SetButtonBold(btDisConnect232);
}
if (fCmdRet != 0)
{
string strLog = "COM close failed: " + GetReturnCodeDesc(fCmdRet);
WriteLog(lrtxtLog, strLog, 1);
return;
}
else
{
string strLog = "COM close success";
WriteLog(lrtxtLog, strLog, 0);
}
}
gpb_tcp.Enabled = true;
btDisConnectTcp.Enabled = false;
//设置按钮字体颜色
btConnectTcp.ForeColor = Color.Indigo;
if (btConnect232.Font.Bold)
{
SetButtonBold(btConnect232);
}
SetButtonBold(btConnectTcp);
gpb_rs232.Enabled = false;
}
}
private void SetButtonBold(Button btnBold)
{
Font oldFont = btnBold.Font;
Font newFont = new Font(oldFont, oldFont.Style ^ FontStyle.Bold);
btnBold.Font = newFont;
}
private void SetRadioButtonBold(CheckBox ckBold)
{
Font oldFont = ckBold.Font;
Font newFont = new Font(oldFont, oldFont.Style ^ FontStyle.Bold);
ckBold.Font = newFont;
}
private void Form1_Load(object sender, EventArgs e)
{
gpb_rs232.Enabled = false;
gpb_tcp.Enabled = false;
rb_rs232.Checked = true;
ComboBox_COM.SelectedIndex = 0;
ComboBox_baud2.SelectedIndex = 3;
com_Q.SelectedIndex = 4;
com_Target.SelectedIndex = 0;
int i = 0;
for (i = 0x00; i <= 0xff; i++)
{
com_scantime.Items.Add(Convert.ToString(i) + "*100ms");
comboBox_maxtime.Items.Add(Convert.ToString(i) + "*100ms");
}
com_scantime.SelectedIndex = 20;
comboBox_maxtime.SelectedIndex = 0;
com_S.SelectedIndex = 4;
DisabledForm();
radioButton_band2.Checked = true;
ComboBox_baud.SelectedIndex = 3;
ComboBox_PowerDbm.SelectedIndex = 30;
for (i = 0; i < 256; i++)
{
com_MFliterTime.Items.Add(i.ToString()+"*1s");
}
for (i = 1; i < 256; i++)
{
ComboBox_RelayTime.Items.Add(Convert.ToString(i));
}
ComboBox_RelayTime.SelectedIndex = 0;
ComboBox_RelayTime.SelectedIndex = 0;
com_MFliterTime.SelectedIndex = 0;
COM_MRPTime.SelectedIndex = 0;
com_MQ.SelectedIndex = 4;
com_MS.SelectedIndex = 4;
com_Mmode.SelectedIndex = 0;
com_wpower.SelectedIndex = 30;