Pārlūkot izejas kodu

水设备的登录和心跳功能

张泳健 7 gadus atpakaļ
vecāks
revīzija
3b8e0bfb63

+ 2 - 0
DotNettyFrom/DotNettyFrom.csproj Parādīt failu

@@ -180,6 +180,8 @@
180 180
     <Compile Include="util\CheckUtil.cs" />
181 181
     <Compile Include="util\CmdUtil.cs" />
182 182
     <Compile Include="model\WvBean.cs" />
183
+    <Compile Include="util\HexUtil.cs" />
184
+    <Compile Include="util\Utils.cs" />
183 185
     <EmbeddedResource Include="dialog\HeartBeatTime.resx">
184 186
       <DependentUpon>HeartBeatTime.cs</DependentUpon>
185 187
     </EmbeddedResource>

+ 29 - 25
DotNettyFrom/MainForm.Designer.cs Parādīt failu

@@ -58,6 +58,8 @@
58 58
             this.RT_HI_CCID = new System.Windows.Forms.ToolStripMenuItem();
59 59
             this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
60 60
             this.RT_HI_SendData = new System.Windows.Forms.ToolStripMenuItem();
61
+            this.配置CToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
62
+            this.HalfPkgToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
61 63
             this.statusStrip1 = new System.Windows.Forms.StatusStrip();
62 64
             this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
63 65
             this.StatusItemHbTime = new System.Windows.Forms.ToolStripStatusLabel();
@@ -73,8 +75,6 @@
73 75
             this.TbDeviceId = new System.Windows.Forms.TextBox();
74 76
             this.label2 = new System.Windows.Forms.Label();
75 77
             this.label1 = new System.Windows.Forms.Label();
76
-            this.配置CToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
77
-            this.HalfPkgToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
78 78
             this.ListBoxLogContextMenu.SuspendLayout();
79 79
             this.menuStrip1.SuspendLayout();
80 80
             this.statusStrip1.SuspendLayout();
@@ -164,7 +164,7 @@
164 164
             this.TbHost.Name = "TbHost";
165 165
             this.TbHost.Size = new System.Drawing.Size(280, 22);
166 166
             this.TbHost.TabIndex = 1;
167
-            this.TbHost.Text = "127.0.0.1";
167
+            this.TbHost.Text = "i.device.yun.vcarefire.com";
168 168
             // 
169 169
             // TbPort
170 170
             // 
@@ -173,7 +173,7 @@
173 173
             this.TbPort.Name = "TbPort";
174 174
             this.TbPort.Size = new System.Drawing.Size(57, 22);
175 175
             this.TbPort.TabIndex = 1;
176
-            this.TbPort.Text = "15440";
176
+            this.TbPort.Text = "15443";
177 177
             this.TbPort.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TbPort_KeyPress);
178 178
             // 
179 179
             // BtnConnection
@@ -247,15 +247,19 @@
247 247
             // 
248 248
             // dtuToolStripMenuItem
249 249
             // 
250
+            this.dtuToolStripMenuItem.Checked = true;
251
+            this.dtuToolStripMenuItem.CheckOnClick = true;
252
+            this.dtuToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
250 253
             this.dtuToolStripMenuItem.Name = "dtuToolStripMenuItem";
251
-            this.dtuToolStripMenuItem.Size = new System.Drawing.Size(115, 22);
254
+            this.dtuToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
252 255
             this.dtuToolStripMenuItem.Text = "dtu";
253 256
             this.dtuToolStripMenuItem.Click += new System.EventHandler(this.dtuToolStripMenuItem_Click);
254 257
             // 
255 258
             // water2ToolStripMenuItem
256 259
             // 
260
+            this.water2ToolStripMenuItem.CheckOnClick = true;
257 261
             this.water2ToolStripMenuItem.Name = "water2ToolStripMenuItem";
258
-            this.water2ToolStripMenuItem.Size = new System.Drawing.Size(115, 22);
262
+            this.water2ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
259 263
             this.water2ToolStripMenuItem.Text = "water2";
260 264
             this.water2ToolStripMenuItem.Click += new System.EventHandler(this.water2ToolStripMenuItem_Click);
261 265
             // 
@@ -275,21 +279,21 @@
275 279
             this.RT_HB_Enable.CheckOnClick = true;
276 280
             this.RT_HB_Enable.CheckState = System.Windows.Forms.CheckState.Checked;
277 281
             this.RT_HB_Enable.Name = "RT_HB_Enable";
278
-            this.RT_HB_Enable.Size = new System.Drawing.Size(152, 22);
282
+            this.RT_HB_Enable.Size = new System.Drawing.Size(141, 22);
279 283
             this.RT_HB_Enable.Text = "启用心跳";
280 284
             this.RT_HB_Enable.Click += new System.EventHandler(this.RT_HB_Enable_Click);
281 285
             // 
282 286
             // RT_HB_Time
283 287
             // 
284 288
             this.RT_HB_Time.Name = "RT_HB_Time";
285
-            this.RT_HB_Time.Size = new System.Drawing.Size(152, 22);
289
+            this.RT_HB_Time.Size = new System.Drawing.Size(141, 22);
286 290
             this.RT_HB_Time.Text = "心跳超时(&H)";
287 291
             this.RT_HB_Time.Click += new System.EventHandler(this.RT_HB_Time_Click);
288 292
             // 
289 293
             // toolStripSeparator1
290 294
             // 
291 295
             this.toolStripSeparator1.Name = "toolStripSeparator1";
292
-            this.toolStripSeparator1.Size = new System.Drawing.Size(149, 6);
296
+            this.toolStripSeparator1.Size = new System.Drawing.Size(138, 6);
293 297
             // 
294 298
             // 历史HToolStripMenuItem
295 299
             // 
@@ -336,6 +340,21 @@
336 340
             this.RT_HI_SendData.Text = "发送数据";
337 341
             this.RT_HI_SendData.Click += new System.EventHandler(this.RT_HI_SendData_Click);
338 342
             // 
343
+            // 配置CToolStripMenuItem
344
+            // 
345
+            this.配置CToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
346
+            this.HalfPkgToolStripMenuItem});
347
+            this.配置CToolStripMenuItem.Name = "配置CToolStripMenuItem";
348
+            this.配置CToolStripMenuItem.Size = new System.Drawing.Size(60, 21);
349
+            this.配置CToolStripMenuItem.Text = "配置(&C)";
350
+            // 
351
+            // HalfPkgToolStripMenuItem
352
+            // 
353
+            this.HalfPkgToolStripMenuItem.Name = "HalfPkgToolStripMenuItem";
354
+            this.HalfPkgToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
355
+            this.HalfPkgToolStripMenuItem.Text = "半包-粘包";
356
+            this.HalfPkgToolStripMenuItem.Click += new System.EventHandler(this.HalfPkgToolStripMenuItem_Click);
357
+            // 
339 358
             // statusStrip1
340 359
             // 
341 360
             this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
@@ -458,7 +477,7 @@
458 477
             this.TbCCID.Name = "TbCCID";
459 478
             this.TbCCID.Size = new System.Drawing.Size(282, 22);
460 479
             this.TbCCID.TabIndex = 6;
461
-            this.TbCCID.Text = "3839383630324731313931353530333339393137";
480
+            this.TbCCID.Text = "3839383630324231313931353530343739313331";
462 481
             // 
463 482
             // label3
464 483
             // 
@@ -496,21 +515,6 @@
496 515
             this.label1.TabIndex = 2;
497 516
             this.label1.Text = "Host";
498 517
             // 
499
-            // 配置CToolStripMenuItem
500
-            // 
501
-            this.配置CToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
502
-            this.HalfPkgToolStripMenuItem});
503
-            this.配置CToolStripMenuItem.Name = "配置CToolStripMenuItem";
504
-            this.配置CToolStripMenuItem.Size = new System.Drawing.Size(60, 21);
505
-            this.配置CToolStripMenuItem.Text = "配置(&C)";
506
-            // 
507
-            // HalfPkgToolStripMenuItem
508
-            // 
509
-            this.HalfPkgToolStripMenuItem.Name = "HalfPkgToolStripMenuItem";
510
-            this.HalfPkgToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
511
-            this.HalfPkgToolStripMenuItem.Text = "半包-粘包";
512
-            this.HalfPkgToolStripMenuItem.Click += new System.EventHandler(this.HalfPkgToolStripMenuItem_Click);
513
-            // 
514 518
             // MainForm
515 519
             // 
516 520
             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 16F);

+ 33 - 12
DotNettyFrom/MainForm.cs Parādīt failu

@@ -4,6 +4,8 @@ using System.ComponentModel;
4 4
 using System.Data;
5 5
 using System.Drawing;
6 6
 using System.Linq;
7
+using System.Net;
8
+using System.Net.Sockets;
7 9
 using System.Text;
8 10
 using System.Threading;
9 11
 using System.Threading.Tasks;
@@ -51,6 +53,18 @@ namespace DotNettyFrom
51 53
 
52 54
             UpdateUiComponent();
53 55
 
56
+            this.Text = "模拟器-DTU";
57
+            CmdGenetor.Genetor = new DtuCmdGenetor();
58
+
59
+            if ("".Equals(TbDeviceId.Text.Trim()))
60
+            {
61
+                CmdGenetor.Genetor.DeviceId = "000000000001";
62
+            }
63
+            else
64
+            {
65
+                CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
66
+            }
67
+
54 68
             var consumer = Task.Factory.StartNew(() =>
55 69
             {
56 70
                 foreach (UIInfoModel value in DataModel.ReceCollection.GetConsumingEnumerable())
@@ -120,9 +134,19 @@ namespace DotNettyFrom
120 134
 
121 135
         private void BeforeConnection()
122 136
         {
123
-            this.host = TbHost.Text.Trim();
124
-            this.port = int.Parse(TbPort.Text.Trim());
125
-            UpdateUiComponent();
137
+            try
138
+            {
139
+                IPAddress[] hostAddresses = Dns.GetHostAddresses(TbHost.Text.Trim());
140
+                IPAddress hostAddress = hostAddresses[0];
141
+                this.host = hostAddress.ToString();
142
+                this.port = int.Parse(TbPort.Text.Trim());
143
+
144
+                UpdateUiComponent();
145
+            }
146
+            catch (SocketException exception)
147
+            {
148
+                MessageBox.Show(exception.Message);
149
+            }
126 150
         }
127 151
 
128 152
         private void UpdateUiComponent()
@@ -130,15 +154,6 @@ namespace DotNettyFrom
130 154
             StatusItemHbTime.Text = "R:" + SystemConfig.ReaderIdleTimeSeconds + "/s,W:" +
131 155
                                     SystemConfig.WriterIdleTimeSeconds + "/s,A:" + SystemConfig.AllIdleTimeSeconds +
132 156
                                     "/s";
133
-            CmdGenetor.Genetor = new DtuCmdGenetor();
134
-            if ("".Equals(TbDeviceId.Text.Trim()))
135
-            {
136
-                CmdGenetor.Genetor.DeviceId = "000000000001";
137
-            }
138
-            else
139
-            {
140
-                CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
141
-            }
142 157
         }
143 158
 
144 159
         #region Component Event
@@ -215,11 +230,17 @@ namespace DotNettyFrom
215 230
 
216 231
         private void dtuToolStripMenuItem_Click(object sender, EventArgs e)
217 232
         {
233
+            this.Text = "模拟器-DTU";
234
+            water2ToolStripMenuItem.Checked = false;
235
+            CmdGenetor.Genetor = null;
218 236
             CmdGenetor.Genetor = new DtuCmdGenetor();
219 237
         }
220 238
 
221 239
         private void water2ToolStripMenuItem_Click(object sender, EventArgs e)
222 240
         {
241
+            this.Text = "模拟器-Water2";
242
+            dtuToolStripMenuItem.Checked = false;
243
+            CmdGenetor.Genetor = null;
223 244
             CmdGenetor.Genetor = new Water2CmdGenetor();
224 245
         }
225 246
 

+ 6 - 3
DotNettyFrom/generatecmd/Water2CmdGenetor.cs Parādīt failu

@@ -3,19 +3,22 @@ using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using System.Text;
5 5
 using System.Threading.Tasks;
6
+using DotNettyFrom.util;
6 7
 
7 8
 namespace DotNettyFrom.generatecmd
8 9
 {
9 10
     public class Water2CmdGenetor : CmdGenetor
10 11
     {
11
-        public override string GetLoginCmd( string ccid)
12
+        public override string GetLoginCmd(string ccid)
12 13
         {
13
-            throw new NotImplementedException();
14
+            string cmd = DeviceId + "0214" + ccid;
15
+            return "7e7e7e" + CmdUtil.CalcCrc16Code(cmd);
14 16
         }
15 17
 
16 18
         public override string GetHeartbeat()
17 19
         {
18
-            throw new NotImplementedException();
20
+            string cmd = DeviceId + "03020001";
21
+            return "7e7e7e" + CmdUtil.CalcCrc16Code(cmd);
19 22
         }
20 23
     }
21 24
 }

+ 70 - 0
DotNettyFrom/util/CmdUtil.cs Parādīt failu

@@ -96,5 +96,75 @@ namespace DotNettyFrom.util
96 96
             return str + FrontWithZero(Dec2Hex(result), 2);
97 97
         }
98 98
 
99
+
100
+        public static string CalcCrc16Code2(string str)
101
+        {
102
+            int value = crc16(HexUtil.hexToBytes(str));
103
+
104
+            return FrontWithZero(Utils.tenToHex(value), 4);
105
+        }
106
+
107
+
108
+        public static string CalcCrc16Code(string str)
109
+        {
110
+            int value = crc16(HexUtil.hexToBytes(str));
111
+
112
+            return str + FrontWithZero(Utils.tenToHex(value), 4);
113
+        }
114
+
115
+
116
+        private static int crc16(byte[] bs)
117
+        {
118
+            return crc16(bs, 0, bs.Length, 0xFFFF);
119
+        }
120
+
121
+        private static int crc16(byte[] bs, int offset, int len, int initCRC)
122
+        {
123
+            byte temp;
124
+            int index = offset;
125
+            while (len-- > 0)
126
+            {
127
+                temp = (byte) (initCRC & 0xFF);
128
+                initCRC = (initCRC >> 8) ^ crc_t_4[(temp ^ bs[index++]) & 0xFF];
129
+            }
130
+            return initCRC;
131
+        }
132
+
133
+
134
+        public static readonly int[] crc_t_4 = new int[]
135
+        {
136
+            0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
137
+            0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
138
+            0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
139
+            0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
140
+            0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
141
+            0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
142
+            0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
143
+            0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
144
+            0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
145
+            0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
146
+            0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
147
+            0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
148
+            0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
149
+            0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
150
+            0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
151
+            0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
152
+            0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
153
+            0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
154
+            0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
155
+            0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
156
+            0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
157
+            0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
158
+            0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
159
+            0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
160
+            0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
161
+            0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
162
+            0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
163
+            0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
164
+            0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
165
+            0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
166
+            0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
167
+            0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
168
+        };
99 169
     }
100 170
 }

+ 54 - 0
DotNettyFrom/util/HexUtil.cs Parādīt failu

@@ -0,0 +1,54 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Text;
5
+using System.Threading.Tasks;
6
+
7
+namespace DotNettyFrom.util
8
+{
9
+    public class HexUtil
10
+    {
11
+        public static byte[] hexToBytes(string hex)
12
+        {
13
+            hex = hex.Replace(" ", "").Replace("\r", "").Replace("\t", "").Replace("\n", "");
14
+            int len = hex.Length / 2;
15
+            byte[] bs = new byte[len];
16
+            for (int i = 0; i < len; i++)
17
+            {
18
+                string sub = hex.Substring(2 * i, 2);
19
+                //                String sub = hex.substring(2 * i, 2 * i + 2);
20
+                int v = Utils.hexToTen(sub);
21
+                //                int v = Integer.valueOf(sub, 16);
22
+                bs[i] = (byte) v;
23
+            }
24
+
25
+            return bs;
26
+        }
27
+
28
+        public static String byteToHexStr(byte[] bs)
29
+        {
30
+            int len = bs.Length;
31
+            StringBuilder sb = new StringBuilder(len * 2);
32
+            for (int i = 0; i < len; i++)
33
+            {
34
+                byte b = bs[i];
35
+                string str = Convert.ToString(b, 16);
36
+                //                String str = Integer.toHexString(b);
37
+                if (str.Length == 1)
38
+                {
39
+                    sb.Append("0").Append(str);
40
+                }
41
+                else if (str.Length > 2)
42
+                {
43
+                    sb.Append(str.Substring(str.Length - 2));
44
+                }
45
+                else
46
+                {
47
+                    sb.Append(str);
48
+                }
49
+            }
50
+
51
+            return sb.ToString();
52
+        }
53
+    }
54
+}

+ 165 - 0
DotNettyFrom/util/Utils.cs Parādīt failu

@@ -0,0 +1,165 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Globalization;
4
+using System.Linq;
5
+using System.Text;
6
+using System.Threading.Tasks;
7
+
8
+namespace DotNettyFrom.util
9
+{
10
+    public class Utils
11
+    {
12
+        public static string Bytes2Hex(byte[] bs)
13
+        {
14
+            return BitConverter.ToString(bs, 0, bs.Length).Replace("-", "").ToUpper();
15
+        }
16
+
17
+
18
+        public static byte[] Hex2Bytes(string hex)
19
+        {
20
+            hex = hex.Replace(" ", "");
21
+            if (hex.Length % 2 != 0)
22
+                hex += " ";
23
+            byte[] returnBytes = new byte[hex.Length / 2];
24
+            for (int i = 0; i < returnBytes.Length; i++)
25
+                returnBytes[i] = Convert.ToByte(hex.Substring(i * 2, 2).Trim(), 16);
26
+            return returnBytes;
27
+        }
28
+
29
+        public static int hexToTen(string hex)
30
+        {
31
+            return int.Parse(hex, NumberStyles.HexNumber);
32
+        }
33
+
34
+
35
+        public static string hexToBin(string hex)
36
+        {
37
+            int res = hexToTen(hex);
38
+            return stringWith0(Convert.ToString(res, 2), 8);
39
+        }
40
+
41
+        public static string tenToHex(string ten)
42
+        {
43
+            return tenToHex(Convert.ToInt32(ten));
44
+        }
45
+
46
+
47
+        public static string tenToHex(int ten)
48
+        {
49
+            return Convert.ToString(ten, 16);
50
+        }
51
+
52
+
53
+        public static string tenToBin(string ten)
54
+        {
55
+            return tenToBin(Convert.ToInt32(ten));
56
+        }
57
+
58
+
59
+        public static string tenToBin(int ten)
60
+        {
61
+            return Convert.ToString(ten, 2);
62
+        }
63
+
64
+
65
+        public static string binToTen(string bin)
66
+        {
67
+            return binToTenInt(bin) + "";
68
+        }
69
+
70
+        public static int binToTenInt(string bin)
71
+        {
72
+            return Convert.ToInt32(bin, 2);
73
+        }
74
+
75
+
76
+        public static string binToHex(string bin)
77
+        {
78
+            int i = binToTenInt(bin);
79
+            return stringWith0(Convert.ToString(i, 16), 2);
80
+        }
81
+
82
+
83
+        public static string stringWith0(string str, int len)
84
+        {
85
+            int delta = len - str.Length;
86
+            string sb = "";
87
+            for (int i = 0; i < delta; i++)
88
+            {
89
+                sb += "0";
90
+            }
91
+            return sb + str;
92
+        }
93
+
94
+        public static string[] REG_STRING =
95
+        {
96
+            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "a", "b", "c", "d", "e", "f"
97
+        };
98
+
99
+        public static string validInput(string reg)
100
+        {
101
+            string trim = reg.Trim();
102
+            if (trim.Length > 0)
103
+            {
104
+                string t = trim.Substring(trim.Length - 1, 1);
105
+                if (REG_STRING.Contains(t))
106
+                {
107
+                    return trim;
108
+                }
109
+                else
110
+                {
111
+                    return trim.Substring(0, trim.Length - 1);
112
+                }
113
+            }
114
+            return trim;
115
+        }
116
+
117
+        public static string calcCrcCode(string str)
118
+        {
119
+            List<string> list = new List<string>();
120
+            for (int i = 0; i < str.Length; i += 2)
121
+            {
122
+                list.Add(str.Substring(i, 2));
123
+            }
124
+
125
+            int result = 0;
126
+            for (int i = 3; i < list.Count(); i++)
127
+            {
128
+                result ^= hexToTen(list[i]);
129
+            }
130
+            return str + stringWith0(tenToHex(result), 2);
131
+        }
132
+
133
+
134
+        public static string calcCrcCode2(string str)
135
+        {
136
+            List<string> list = new List<string>();
137
+            for (int i = 0; i < str.Length; i += 2)
138
+            {
139
+                list.Add(str.Substring(i, 2));
140
+            }
141
+
142
+            int result = 0;
143
+            for (int i = 3; i < list.Count(); i++)
144
+            {
145
+                result ^= hexToTen(list[i]);
146
+            }
147
+            return stringWith0(tenToHex(result), 2);
148
+        }
149
+
150
+        public static string transateCCID(string source)
151
+        {
152
+            string res = "";
153
+            for (int i = 0; i < source.Length; i++)
154
+            {
155
+                int hex = Int32.Parse(source[i].ToString(), NumberStyles.HexNumber);
156
+                int ten = hexToTen(hex + "") + 30;
157
+
158
+                res += ten + "";
159
+                // Console.WriteLine(ten);
160
+            }
161
+            return res;
162
+        }
163
+
164
+    }
165
+}