Class1.cs 24.8 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Asa.RFID
{
    /// <summary>
    /// RFID读卡器
    /// </summary>
    public class HFReader
    {
        private Log log;
        private byte[] _buff;   //8字节缓存
        private byte[] _uid;    //卡片ID
        private bool loop;
        private bool empty;
        
        private int port;
        private byte addr;
        private int portIndex;

        private System.Threading.Thread tScan;
        //private System.Threading.Thread tReceive;
        //private System.Collections.Concurrent.ConcurrentQueue<byte> _receive;

        /// <summary>
        /// 接收事件
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="buff"></param>
        public delegate void Received_Event(string ip, byte[] buff);
        /// <summary>
        /// 接收数据
        /// </summary>
        public event Received_Event Received;


        /// <summary>
        /// RFID读卡器
        /// </summary>
        public HFReader(string ip)
        {
            IP = ip;
            _buff = new byte[8];
            _uid = new byte[8];
            //_receive = new System.Collections.Concurrent.ConcurrentQueue<byte>();
         
        }

        /// <summary>
        /// 是否连接
        /// </summary>
        public bool IsConn { private set; get; } = false;

        /// <summary>
        /// 是否自动扫描
        /// </summary>
        public bool IsAutoScan { private set; get; } = false;

        /// <summary>
        /// 是否存在ID卡
        /// </summary>
        public bool IsExist { private set; get; } = false;

        /// <summary>
        /// 错误代码
        /// </summary>
        public int ErrCode { private set; get; } = 0;

        /// <summary>
        /// IP地址
        /// </summary>
        public string IP { set; get; }

        /// <summary>
        /// 日志目录
        /// </summary>
        public string LogPath { set; get; } = "";



        /// <summary>
        /// ID号码
        /// </summary>
        public string ID
        {
            get
            {
                string s = "";
                for (int i = 0; i < _uid.Length; i++)
                    s += _uid[i].ToString("X2");
                return s;
            }
        }

        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="autoScan">自动扫描</param>
        public void Open(bool autoScan)
        {
            log = new Log(LogPath, "RFID_" + IP);
            Connect();
            System.Threading.Thread.Sleep(100);
            OpenCloseRF(false);
            System.Threading.Thread.Sleep(100);
            OpenCloseRF(true);
            AutoScanMode(false);

            if (autoScan)
                AutoScanRead();
            else
                FindIDMode();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        public void Close()
        {
            loop = false;
            log.OutString("Call Close");
            try
            {
                System.Threading.Thread.Sleep(100);
                IsConn = false;
                ErrCode = ReaderA.StaticClassReaderA.CloseNetPort(portIndex);
                log.OutString("CloseNetPort[" + ErrCode + "] " + (Error)ErrCode);
            }
            catch (Exception ex)
            {
                log.OutError(ex.Message);
            }
            log.Exit();
        }

        /// <summary>
        /// 查找电子标签,扫描模式不能使用
        /// </summary>
        /// <returns></returns>
        public bool FindRFID()
        {
            //0 不带AFI
            //1 带AFI
            //2 不带AFI,继续查询
            //3 带AFI,继续查询
            //6 不带AFI,新的查询
            //7 带AFI,新的查询
            byte state = 0;

            //Select模式需要AFI
            byte AFI = 0;

            //输出,1字节DSFID,8字节UID
            byte[] DSFIDAndUID = new byte[9];

            //输出,标签数量
            byte cardNumber = 0;

            ErrCode = ReaderA.StaticClassReaderA.Inventory(ref addr, ref state, ref AFI, DSFIDAndUID, ref cardNumber, portIndex);
            if (ErrCode == 0)
            {
                //查询时间
                //fCmdRet = StaticClassReaderA.GetInventoryTime(ref n_time, portIndex);
                Array.Copy(DSFIDAndUID, 1, _uid, 0, 8);
                IsExist = true;
                return true;
            }
            else
            {
                for (int i = 0; i < _uid.Length; i++)
                    _uid[i] = 0;
                IsExist = false;
                return false;
            }


        }

        /// <summary>
        /// 读取电子标签
        /// </summary>
        /// <returns></returns>
        public byte[] Read()
        {
            string s = "";
            log.OutString("Call Read");

            try
            {
                if (IsAutoScan)
                {
                    byte[] buff = new byte[_buff.Length];
                    Array.Copy(_buff, buff, _buff.Length);

                    for (int i = 0; i < buff.Length; i++)
                        s += buff[i].ToString("X2") + " ";
                    log.OutString(s);
                    return buff;
                }
                else if (IsExist)
                {
                    ReadRFID();

                    for (int i = 0; i < _buff.Length; i++)
                        s += _buff[i].ToString("X2") + " ";
                    log.OutString(s);
                    return _buff;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                log.OutError(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 写入数据到电子标签,扫描模式不能使用
        /// </summary>
        /// <param name="buff">数据,必须小于等于112字节</param>
        /// <returns></returns>
        public bool Write(byte[] buff)
        {
            if (IsExist)
            {
                if (buff.Length == 8)
                    return WriteRFID(buff);
                else
                    return false;
            }
            else
                return false;


            //if (IsExist)
            //{
            //    Array.Copy(buff, 0, _buff, 0, buff.Length);
            //    int num = 0;
            //    int count = (buff.Length - 1) / 4 - num + 1;
            //    bool rtn;

            //    for (int i = 0; i < count; i++)
            //    {
            //        rtn = WriteRFID(num + i);
            //        if (!rtn) continue;
            //    }

            //    return true;
            //}
            //else
            //    return false;
        }

        /// <summary>
        /// 自动扫描模式
        /// </summary>
        /// <param name="open"></param>
        /// <param name="open2"></param>
        /// <returns></returns>
        public bool AutoScanMode(bool open)
        {
            log.OutString("Call AutoScanMode");

            byte[] scanModeData = new byte[11];

            //bit0: 0停止扫描,1开始扫描
            //bit1: 0正常数据扫描,1EAS扫描
            //bit2: 0正常灵敏度,1高灵敏度
            //bit3: 0禁止输入同步,1允许输入同步
            //bit4: 0禁止指令同步,1允许指令同步
            //bit5: 0禁止扫描提示,1允许扫描提示
            //bit6: 0禁止EAS探测提示,1允许EAS探测提示
            //bit7: 0禁止EAS消息输出提示,1允许EAS消息输出提示
            if (open)
                scanModeData[0] = 1;
            else
                scanModeData[0] = 0;

            //bit0: 0不发送comAddr,1发送comAddr
            //bit1: 0不带UID,1带UID
            //bit2: 0不包含指定块,1包含指定块
            //bit3: 0不带安全状态字,1带安全状态字
            //bit4: 0 ASCII,1 HEX
            //bit5: 0不进行开关射频场,1进行开关射频场
            //bit6: 0无操作,1停止扫描关闭射频场
            //bit7: 0无,1鸣叫闪烁
            scanModeData[1] = 255;

            scanModeData[2] = 0;  //起始块号
            scanModeData[3] = 2;  //块总数          之前是28
            scanModeData[4] = 1;  //块的起始字节
            scanModeData[5] = 4;  //块的字节数

            //1   CR+LF      0x0D 0x0A
            //2   CR         0x0D
            //4   LF         0x0A
            //8   BEL        0x07
            //16  :(分号)    0x3B
            //32  ,(逗号)    0x2C
            //64  " "(空格)  0x20
            //128 自定义
            scanModeData[6] = 8;  //间隔符
            scanModeData[7] = 0;  //自定义间隔符,[6] = 128

            //1   CR+LF      0x0D 0x0A
            //2   CR         0x0D
            //4   LF         0x0A
            //8   BEL        0x07
            //16  :(分号)    0x3B
            //32  ,(逗号)    0x2C
            //64  " "(空格)  0x20
            //128 自定义
            scanModeData[8] = 8;  //结束符
            scanModeData[9] = 0;  //自定义间隔符,[8] = 128

            scanModeData[10] = 0x00;

            try
            {
                ErrCode = ReaderA.StaticClassReaderA.SetScanMode(ref addr, scanModeData, portIndex);
                log.OutString("SetScanMode[" + ErrCode + "] Mode=" + open.ToString() + " " + (Error)ErrCode);
                if (ErrCode == 0)
                {
                    IsAutoScan = open;
                    //if (open2) AutoScanRead();
                }
                else
                {
                    IsAutoScan = false;
                }
            }
            catch (Exception ex)
            {
                IsAutoScan = false;
                log.OutError(ex.Message);
            }
            return IsAutoScan;
        }




        private void FindIDMode()
        {
            loop = true;
            tScan = new System.Threading.Thread(new System.Threading.ThreadStart(ScanFind));
            tScan.Start();
        }

        private void AutoScanRead()
        {
            loop = true;
            tScan = new System.Threading.Thread(new System.Threading.ThreadStart(Scan));
            tScan.Start();
        }

        private void Connect()
        {
            IsConn = false;
            log.OutString("Call Connect");

            //IP合法
            string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
            bool rtn = System.Text.RegularExpressions.Regex.IsMatch(IP, pattern);
            if (!rtn)
            {
                log.OutString("IP不合法," + IP);
                return;
            }

            //Ping服务端
            System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
            System.Net.NetworkInformation.PingReply result = ping.Send(IP, 1000);
            ping.Dispose();
            if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
            {
                log.OutString("Ping " + IP + " 请求没有响应");
                return;
            }

            string[] arr = IP.Split('.');
            addr = Convert.ToByte(arr[3]);
            port = 6000 + Convert.ToInt32(arr[3]);
            portIndex = 0;
            log.OutString("Connect ip=" + IP + ", port=" + port + ", addr=" + addr);

            try
            {
                ErrCode = ReaderA.StaticClassReaderA.OpenNetPort(port, IP, ref addr, ref portIndex);
                IsConn = ErrCode == 0;
                //AutoScanMode(false);
                log.OutString("Connect[" + ErrCode + "] IsConn=" + IsConn + " " + (Error)ErrCode);
            }
            catch (Exception ex)
            {
                IsConn = false;
                log.OutError(ex.Message);
            }

        }

        public void OpenCloseRF(bool rtn)
        {
            if (rtn)
            {
                ErrCode = ReaderA.StaticClassReaderA.OpenRf(ref addr, portIndex);
                log.OutInfo("OpenRf[" + ErrCode + "] " + (Error)ErrCode);
            }
            else
            {
                ErrCode = ReaderA.StaticClassReaderA.CloseRf(ref addr, portIndex);
                log.OutInfo("CloseRf[" + ErrCode + "] " + (Error)ErrCode);
            }
          
        }

        private void ReadRFID()
        {
            //0 不带AFI
            //1 带AFI
            //2 不带AFI,继续查询
            //3 带AFI,继续查询
            //6 不带AFI,新的查询
            //7 带AFI,新的查询
            byte state = 0;

            //起始块号
            byte blockNum = 0;

            //块数量
            byte blockCount = 2;

            //输出,块内安全信息,长度为BlockCount个字节
            byte[] blockSecStatus = new byte[blockCount];

            //输出,块内数据信息,长度为块的大小(4或8字节)乘以BlockCount个字节
            byte[] data = new byte[4 * blockCount];

            //错误代码
            byte errorCode = 0;

            ErrCode = ReaderA.StaticClassReaderA.ReadMultipleBlock(ref addr, ref state, _uid, blockNum, blockCount, blockSecStatus, data, ref errorCode, portIndex);
            log.OutInfo("ReadRFID[" + ErrCode + "] " + (Error)ErrCode);

            if (ErrCode == 0)
            {
                Array.Copy(data, 0, _buff, 0, data.Length);
            }
            else
            {
                for (int i = 0; i < _buff.Length; i++)
                    _buff[i] = 0;
            }
        }

        //private void ReadAllRFID()
        //{
        //    //0 不带AFI
        //    //1 带AFI
        //    //2 不带AFI,继续查询
        //    //3 带AFI,继续查询
        //    //6 不带AFI,新的查询
        //    //7 带AFI,新的查询
        //    byte state = 0;

        //    //起始块号
        //    byte blockNum = 0;

        //    //块数量
        //    byte blockCount = 28;

        //    //输出,块内安全信息,长度为BlockCount个字节
        //    byte[] blockSecStatus = new byte[blockCount];

        //    //输出,块内数据信息,长度为块的大小(4或8字节)乘以BlockCount个字节
        //    byte[] data = new byte[4 * blockCount];

        //    //错误代码
        //    byte errorCode = 0;

        //    ErrCode = ReaderA.StaticClassReaderA.ReadMultipleBlock(ref addr, ref state, _uid, blockNum, blockCount, blockSecStatus, data, ref errorCode, portIndex);
        //    if (ErrCode == 0)
        //    {
        //        Array.Copy(data, 0, _buff, 0, data.Length);
        //    }
        //}

        private bool WriteRFID(byte[] buff)
        {
            //0x00 块4字节 A类标签 Address模式
            //0x01 块4字节 A类标签 Select模式
            //0x04 块8字节 A类标签 Address模式
            //0x05 块8字节 A类标签 Select模式
            //0x08 块4字节 B类标签 Address模式
            //0x09 块4字节 B类标签 Select模式
            //0x0C 块8字节 B类标签 Address模式
            //0x0D 块8字节 B类标签 Select模式
            byte state = 0;

            //错误代码
            byte errorCode = 0;


            byte[] data = new byte[4];
            Array.Copy(buff, data, 4);
            ErrCode = ReaderA.StaticClassReaderA.WriteSingleBlock(ref addr, ref state, _uid, 0, data, ref errorCode, portIndex);
            System.Threading.Thread.Sleep(50);

            state = 0;
            errorCode = 0;
            Array.Copy(buff, 4, data, 0, 4);
            ErrCode = ReaderA.StaticClassReaderA.WriteSingleBlock(ref addr, ref state, _uid, 1, data, ref errorCode, portIndex);

            data = new byte[4];
            for (byte i = 2; i < 28; i++)
            {
                state = 0;
                errorCode = 0;
                ErrCode = ReaderA.StaticClassReaderA.WriteSingleBlock(ref addr, ref state, _uid, i, data, ref errorCode, portIndex);
            }


            //ErrCode = ReaderA.StaticClassReaderA.WriteMultipleBlock(ref addr, ref state, _uid, 1, 2, data, ref errorCode, portIndex);
            if (ErrCode == 0)
                return true;
            else
                return false;

        }

        private void Scan()
        {
            bool start = false;
            bool end = false;
            byte[] data = new byte[4096];
            byte[] bb = new byte[10];
            byte[] temp = new byte[0];
            int dataLen;
            int idx = 0;
            empty = false;

            while (loop)
            {
                System.Threading.Thread.Sleep(1000);
                if (!IsAutoScan) continue;
                try
                {
                    dataLen = 0;
                    ErrCode = ReaderA.StaticClassReaderA.ReadScanModeData(data, ref dataLen, portIndex);
                    log.OutString("ReadScanModeData[" + ErrCode + "] Len=" + dataLen + " " + (Error)ErrCode);

                    if (ErrCode == 0)
                    {
                        temp = new byte[dataLen];
                        Array.Copy(data, 0, temp, 0, dataLen);
                        empty = false;
                        //for (int i = 0; i < temp.Length; i++)
                        //    _receive.Enqueue(temp[i]);
                    }
                    else if (ErrCode == 48)
                    {
                        if (empty)
                        {
                            continue;
                        }
                        else
                        {
                            temp = new byte[7] { 0x5A, 0, 0, 0, 0, 0, 0x4A };

                            //_receive.Enqueue(0x5A);
                            //_receive.Enqueue(0);
                            //_receive.Enqueue(0);
                            //_receive.Enqueue(0);
                            //_receive.Enqueue(0);  //RFID的格式,每4个字节有一个0
                            //_receive.Enqueue(0);
                            //_receive.Enqueue(0x4A);
                            empty = true;
                        }
                    }
                    else if (IsConn && (ErrCode == 14 || ErrCode == 55))  //55无效句柄
                    {
                        //AutoScanMode(false);
                        IsConn = false;
                        ErrCode = ReaderA.StaticClassReaderA.CloseNetPort(portIndex);
                        log.OutString("CloseNetPort[" + ErrCode + "] " + (Error)ErrCode);
                        System.Threading.Thread.Sleep(100);
                        Connect();

                        if (IsConn)
                        {
                            AutoScanMode(false);
                            System.Threading.Thread.Sleep(100);
                            AutoScanMode(true);
                        }
                        continue;
                    }

                    string s = "AutoScan ";
                    for (int i = 0; i < temp.Length; i++)
                        s += temp[i].ToString("X2") + " ";
                    log.OutString(s);

                    for (int i = 0; i < temp.Length; i++)
                    {
                        if (temp[i] == 0x5A)
                        {
                            start = true;
                            end = false;
                            idx = 0;
                            log.OutString("Start 5A");
                        }
                        else if (temp[i] == 0x4A && start)
                        {
                            start = false;
                            end = true;
                            bb[idx++] = temp[i];
                            log.OutString("End 4A");
                        }

                        if (start)
                        {
                            if (idx + 1 == bb.Length)
                            {
                                start = false;
                                end = false;
                                log.OutString("idx+1=" + (idx + 1).ToString());
                            }
                            else
                            {
                                bb[idx++] = temp[i];
                                log.OutString("bb[" + (idx - 1).ToString() + "]=" + temp[i].ToString("X2"));
                            }
                        }

                        if (end)
                        {
                            log.OutString("idx=" + idx);
                            if (idx == 7)  //中间有个00
                            {
                                _buff[0] = bb[0];
                                _buff[1] = bb[1];
                                _buff[2] = bb[2];
                                _buff[3] = bb[3];
                                _buff[4] = bb[5];
                                _buff[5] = bb[6];
                                _buff[6] = 0;
                                _buff[7] = 0;

                                string ss = "Receive ";
                                for (int j = 0; j < _buff.Length; j++)
                                    ss += _buff[j].ToString("X2") + " ";
                                log.OutString(ss);
                                Received?.Invoke(IP, _buff);
                            }
                            end = false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    IsConn = false;
                    loop = false;
                    log.OutError(ex.Message);
                }

            }
        }

        private void ScanFind()
        {
            while (loop)
            {
                System.Threading.Thread.Sleep(1000);
                bool rtn = FindRFID();
                if (rtn)
                {
                    if (!loop) break;
                    ReadRFID();
                    Received?.Invoke(IP, _buff);
                }
            }
        }

        //private void Receive()
        //{
        //    bool start = false;
        //    bool end = false;
        //    byte[] bb = new byte[10];
        //    int idx = 0;

        //    while(loop)
        //    {
        //        if (_receive.TryDequeue(out byte result))
        //        {
        //            log.OutString("result=" + result.ToString("X2"));
        //            try
        //            {
        //                if (result == 0x5A)
        //                {
        //                    start = true;
        //                    end = false;
        //                    idx = 0;
        //                    log.OutString("Start 5A");
        //                }
        //                else if (result == 0x4A && start)
        //                {
        //                    start = false;
        //                    end = true;
        //                    bb[idx++] = result;
        //                    log.OutString("End 4A");
        //                }

        //                if (start)
        //                {
        //                    if (idx + 1 == bb.Length)
        //                    {
        //                        start = false;
        //                        end = false;
        //                        log.OutString("idx+1=" + (idx + 1).ToString());
        //                    }
        //                    else
        //                    {
        //                        bb[idx++] = result;
        //                        log.OutString("bb[" + (idx - 1).ToString() + "]=" + result.ToString("X2"));
        //                    }
        //                }

        //                if (end)
        //                {
        //                    log.OutString("idx=" + idx);
        //                    if (idx == 7)  //中间有个00
        //                    {
        //                        _buff[0] = bb[0];
        //                        _buff[1] = bb[1];
        //                        _buff[2] = bb[2];
        //                        _buff[3] = bb[3];
        //                        _buff[4] = bb[5];
        //                        _buff[5] = bb[6];
        //                        _buff[6] = 0;
        //                        _buff[7] = 0;

        //                        string s = "Receive ";
        //                        for (int i = 0; i < _buff.Length; i++)
        //                            s += _buff[i].ToString("X2") + " ";
        //                        log.OutString(s);
        //                        Received?.Invoke(IP, _buff);
        //                    }
        //                    end = false;
        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                loop = false;
        //                log.OutError(ex.Message);
        //            }
        //        }
        //        System.Threading.Thread.Sleep(20);
        //    }
        //}



    }
}