BaseConnect.cs 37.4 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
///--------------------------------------------------------------------------
/// 文 件 名:HardwareManageARS.cs
/// 功能描述:基站连接类
///           基站连接
/// 创建标识:赵丽 2011-10-30
/// 修改标识:赵丽 2011-11-20
///--------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SunVote;
using System.Collections.Specialized;
using System.Collections;
using System.Threading;
using System.Windows.Forms;
using GeneralLib;

namespace SunVoteARSPPT
{

    #region 委托

    /// <summary>
    /// 基站连接委托
    /// </summary>
    /// <param name="BaseID"></param>
    /// <param name="baseConnectStatus"></param>
    public delegate void ConnectEvent(string statusStr);//基站连接委托,返回连接状态字符串
    /// <summary>
    /// 杨斌 2016-04-19
    /// </summary>
    public delegate void ReTryConnectEvt();
    /// <summary>
    /// 传基站连接状态
    /// </summary>
    /// <param name="BaseID"></param>
    public delegate void ConnectStatusEvent(int BaseID);
    /// <summary>
    /// 读基站信息委托
    /// </summary>
    /// <param name="BaseID"></param>
    public delegate void ReadBaseInfoEvent(int BaseID);//连接成功读基站信息
    /// <summary>
    /// 写配置文件
    /// 单基站需要改基站ID
    /// </summary>
    /// <param name="BaseID"></param>
    /// <param name="key"></param>
    /// <param name="val"></param>
    public delegate void SysConfigBaseListEvent(int BaseID, string key, object val);
    /// <summary>
    /// 是否为反馈状态
    /// </summary>
    public delegate void ResponseStateEventHander(bool beRespons);
    /// <summary>
    /// 激活窗体交给幻灯片的窗体,防止Windows7下播放幻灯片时,拔出基站时系统崩溃
    /// 创建标志 2012-05-09
    /// </summary>
    public delegate void ActiveSlideWindowEventHander();

    #endregion

    #region 基站信息类
    /// <summary>
    /// 基站信息类
    /// </summary>
    public class BasePara
    {

        /// <summary>
        /// 构造函数,初始化信息
        /// </summary>
        public BasePara()
        {
            BaseConnectStatus = ConnectStatus.csDisconnect;
        }

        /// <summary>
        /// 连接类型
        /// </summary>
        public enum ConnectStatus
        {

            /// <summary>
            /// 连接成功
            /// </summary>
            csSuccess = 1,
            /// <summary>
            /// 基站错误
            /// </summary>
            csBaseErr = 2,
            /// <summary>
            /// 不支持的连接方式
            /// </summary>
            csConnWayErr = 3,
            /// <summary>
            /// 串口错误
            /// </summary>
            csPortErr = 4,
            /// <summary>
            /// 手动断开连接
            /// </summary>
            csDisconnect = 5
        }

        #region 公有属性
        /// <summary>
        /// 基站编号
        /// </summary>
        private int baseID = 1;
        /// <summary>
        /// 基站ID
        /// </summary>
        public int BaseID
        {
            get { return baseID; }
            set
            {
                if (baseID != value)
                {
                    baseID = value;
                }
            }
        }
        /// <summary>
        /// 启用状态
        /// </summary>
        private bool enableStatus = true;
        /// <summary>
        /// 启用状态
        /// </summary>
        public bool EnableStatus
        {
            get { return enableStatus; }
            set
            {
                if (enableStatus != value)
                {
                    enableStatus = value;
                }
            }
        }
        /// <summary>
        /// 基站IP
        /// </summary>
        private string baseIPAddress = "";
        /// <summary>
        /// 基站IP地址
        /// </summary>
        public string BaseIPAddress
        {
            get { return baseIPAddress; }
            set
            {
                if (baseIPAddress != value)
                {
                    baseIPAddress = value;
                }
            }
        }

        private static Dictionary<string, string> DicBaseType = new Dictionary<string, string>();
        /// <summary>
        /// 取基站型号
        /// </summary>
        /// <param name="HModel"></param>
        /// <returns></returns>
        public static string GetBaseType(int HModel)
        {
            string res = HModel.ToString();
            string hex = HModel.ToString("X");

            if (DicBaseType.Count < 1)
            {
                DicBaseType.Add("B1", "EA1000");
                DicBaseType.Add("B2", "EA2000T");
                DicBaseType.Add("B3", "PVS-3000/2.4G");
                DicBaseType.Add("B4", "PVS-2010/2.4G");
                DicBaseType.Add("B5", "PVS-2010/433M");
                DicBaseType.Add("B6", "C1000");
                DicBaseType.Add("B7", "EA2100T");
                DicBaseType.Add("B8", "C2000T");
                DicBaseType.Add("B9", "EA4000T");
                DicBaseType.Add("BA", "EA4000R");
                DicBaseType.Add("BB", "C1200");//(建议取消)
                DicBaseType.Add("BC", "EA1200");
                DicBaseType.Add("BD", "EA4100T");
                DicBaseType.Add("BE", "C4000T");
                DicBaseType.Add("BF", "EA4200W");//WiFi
            }

            if (DicBaseType.ContainsKey(hex))
                res = DicBaseType[hex];

            return res;
        }

        /// <summary>
        /// 基站型号
        /// 修改:杨斌 2012-04-05
        /// </summary>
        private int baseModel = 0;
        public int BaseModel
        {
            get { return baseModel; }
            set
            {
                //if (baseModel != value)
                //{
                baseModel = value;
                BaseModelName = GetBaseType(baseModel);
                //switch (baseModel)
                //{
                //    case 177:
                //        BaseModelName = "EA1000";
                //        break;
                //    case 178:
                //        BaseModelName = "EA2000";
                //        break;
                //    case 182:
                //        BaseModelName = "C1000";
                //        break;
                //    case 185:
                //        BaseModelName = "EA4000T";
                //        break;
                //    case 188:
                //        BaseModelName = "EA1200";
                //        break;
                //    default:
                //        BaseModelName = baseModel.ToString();
                //        break;
                //}
                //}
            }
        }

        public string BaseModelName = "";
        /// <summary>
        /// 基站频点
        /// </summary>
        private int baseChannel = 0;
        public int BaseChannel
        {
            get { return baseChannel; }
            set
            {
                if (baseChannel != value)
                {
                    baseChannel = value;
                }
            }
        }

        /// <summary>
        /// 基站序列号
        /// </summary>
        public string BaseSerialNumber { get; set; }

        /// <summary>
        /// 基站配对模式
        /// 杨斌 2013-05-08
        /// </summary>
        public int BaseMatchMode { get; set; }

        /// <summary>
        /// 基站名称
        /// 杨斌 2015-01-06
        /// </summary>
        public string BaseName { get; set; }

        /// <summary>
        /// 在线键盘
        /// </summary>
        private int onLineKeyNum = 0;
        public int OnLineKeyNum
        {
            get { return onLineKeyNum; }
            set
            {
                if (onLineKeyNum != value)
                {
                    onLineKeyNum = value;

                }
            }
        }

        /// <summary>
        /// 在线键盘
        /// </summary>
        private int eLVKeyNum = 0;
        public int ELVKeyNum
        {
            get { return eLVKeyNum; }
            set
            {
                if (eLVKeyNum != value)
                {
                    eLVKeyNum = value;
                }
            }
        }
        /// <summary>
        /// 连接状态
        /// </summary>
        private ConnectStatus baseConnectStatus = ConnectStatus.csDisconnect;
        public ConnectStatus BaseConnectStatus
        {
            get { return baseConnectStatus; }
            set
            {
                if (baseConnectStatus != value)
                {
                    baseConnectStatus = value;
                }
            }
        }

        #endregion



    }
    #endregion

    #region 基站连接
    /// <summary>
    /// 基站连接类
    /// </summary>
    public class BaseConnect
    {
        public enum ConnectModel
        {
            cmConnAll = 1,
            cmConnOne = 2
        }

        /// <summary>
        /// 基站连接类
        /// </summary>
        public SunVote.BaseConnection baseConnection = null;

        /// <summary>
        /// 创建定时器
        /// </summary>
        //private System.Windows.Forms.Timer tmrConnectBase = null;
        System.Timers.Timer tmrConnectBase = null;
        //static System.Threading.Timer tmrConnectBase = null;

        #region 属性
        /// <summary>
        /// 多基站
        /// </summary>
        public int MultiBase { get; set; }
        /// <summary>
        /// 最小键盘编号
        /// </summary>
        public int KeyIDMin { get; set; }
        /// <summary>
        /// 最大键盘编号
        /// </summary>
        public int KeyIDMax { get; set; }
        /// <summary>
        /// 射频功率等级
        /// </summary>
        public int RFPower { get; set; }
        /// <summary>
        /// 键盘报告状态模式
        /// </summary>
        public int KeyReportMode { get; set; }
        /// <summary>
        /// 键盘关机时间
        /// </summary>
        public int KeyOffTime { get; set; }
        /// <summary>
        /// 键盘提交模式
        /// </summary>
        public int CommitMode { get; set; }
        /// <summary>
        /// 背光模式
        /// </summary>
        public int BackLightMode { get; set; }
        /// <summary>
        /// 蜂鸣模式
        /// </summary>
        public int BuzzerMode { get; set; }
        /// <summary>
        /// 连接类型
        /// </summary>
        private int connectType = 1;

        /// <summary>
        /// 连接模式:1:USB,2:TCP/IP
        /// </summary>
        public int ConnectType
        {
            get
            {
                return connectType;
            }
            set
            {
                if (connectType != value)
                {
                    connectType = value;
                    DisConnect();
                    Connect();
                }//连接类型改变,重连基站
            }
        }

        /// <summary>
        /// 基站列表
        /// </summary>
        private OrderedDictionary baseList = null;
        /// <summary>
        /// 基站列表,存储各基站的信息
        /// </summary>
        public OrderedDictionary BaseList
        {
            get
            {
                return baseList;
            }
            set
            {
                if (baseList != value)
                {
                    baseList = value;
                }
            }
        }
        /// <summary>
        /// 单基站模式下上一次连接成功的IP
        /// </summary>
        private string singleBaseIP = "";
        public string SingleBaseIP
        {
            get
            {
                return singleBaseIP;
            }
            set
            {
                if (singleBaseIP != value)
                {
                    singleBaseIP = value;
                    scanBaseIPs = value;
                    for (int i = 0; i < cMaxBaseCount; i++)//杨斌 2012-10-11
                    {
                        if (i != 0)
                            scanBaseIPs += "," + value;
                    }
                }
            }
        }

        /// <summary>
        /// 上一次连接成功的基站编号
        /// </summary>
        private int singleBaseID = 1;
        public int SingleBaseID
        {
            get { return singleBaseID; }
            set
            {
                if (singleBaseID != value)
                {
                    singleBaseID = value;
                    scanBaseIDs = value.ToString();
                    for (int i = 1; i <= cMaxBaseCount; i++)//杨斌 2012-10-11
                    {
                        if (i != value)
                        {
                            scanBaseIDs += ",";
                            scanBaseIDs += i.ToString();
                        }
                    }
                }
            }
        }

        public const int cMaxBaseCount = 32;//杨斌 2013-03-29

        /// <summary>
        /// 基站扫描基站ID号
        /// 2015-09-14
        /// </summary>
        //private string scanBaseIDs = "1,2,3,4,5,6,7,8";//杨斌 2013-03-29
        private string scanBaseIDs = "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";//杨斌 2013-03-29
        /// <summary>
        /// 基站扫描基站IP地址
        /// </summary>
        private string scanBaseIPs = "";
        /// <summary>
        /// 断开基站的标志
        /// </summary>
        public bool DisConnectFlag = false;
        /// <summary>
        /// 重连基站
        /// </summary>
        public bool ReConnect
        {
            set
            {
                if (value)
                {
                    ConnecOne = false;
                    tmrConnectBase.Enabled = true;
                    //tmrConnectBase.Change(100, 0);
                }
            }
        }
        /// <summary>
        /// 记录连接成功的基站个数
        /// </summary>
        public int BaseNum = 0;
        /// <summary>
        /// 处于反馈过程时,不读基站的信息
        /// </summary>
        public bool NeedReadBaseInfo = true;
        /// <summary>
        /// 是否处于反馈状态
        /// </summary>
        private bool isResponse = false;
        public bool IsResponse
        {
            get { return isResponse; }
            set
            {
                if (isResponse != value)
                {
                    isResponse = value;
                    if (ResponseStateEvent != null)
                        ResponseStateEvent(value);
                }
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public BaseConnect()
        {
            //tmrConnectBase = new System.Windows.Forms.Timer();
            //tmrConnectBase.Interval = 100;
            //tmrConnectBase.Tick += tmrConnectBase_Tick;

            tmrConnectBase = new System.Timers.Timer();
            tmrConnectBase.Interval = 100;
            tmrConnectBase.Elapsed += tmrConnectBase_Elapsed;

            //tmrConnectBase = new System.Threading.Timer(tmrConnectBase_CallBack);

            //杨斌 2012-05-07 调试问题
            //此行代码,在Win7的普通用户(Users组)下运行导致PowerPoint停止运行
            //创建其业务对象他如Choices也是如此

            /*屏蔽。杨斌 2017-04-19。这里只要new了,自动测试可能收不齐:400个可能只有392,399等,且速度稍慢
            baseConnection = new SunVote.BaseConnection();
            baseConnection.ProtocolType = 0;//ARS=0,CRS=1,EVS=2 杨斌 2013-04-01
            baseConnection.BaseOnLine += new SunVote.IBaseConnectionEvents_BaseOnLineEventHandler(baseConnection_baseOnLine);
            //*/
            connectType = 1;
            //初始化基站列表
            baseList = new OrderedDictionary(cMaxBaseCount);//杨斌 2012-10-11            

            //iniBaseParaList();

            FrmPvsServer.GetFrmPvs().OnBaseConnectEvent += myPVS_BaseConnectEvent;
            FrmPvsServer.GetFrmPvs().OnBaseEvent += myPVS_BaseEvent;
            FrmPvsServer.GetFrmPvs().OnCommErrEx += myPVS_CommErrEx;
            FrmPvsServer.GetFrmPvs().OnCommErrSys += myPVS_CommErrSys;

        }

        void myPVS_BaseConnectEvent(int ID, int iStatus)
        {
            try
            {
                BasePara.ConnectStatus baseConnectStatus = GetBaseConnectStatus(iStatus);

                if (ID == 0)
                {
                    for (int i = 0; i < baseList.Count; i++)
                    {
                        this[i].BaseConnectStatus = baseConnectStatus;
                    }//没有基站连接时重新扫描基站
                    if (ConnectStatusEvent != null) { ConnectStatusEvent(ID); }
                    if (ConnectEvent != null) { ConnectEvent(GetBaseIDs(false)); }

                }//没有连接基站
                else
                {
                    if (!baseList.Contains(ID.ToString()))
                    {
                        baseList.Add(ID.ToString(), new BasePara());
                    }
                    this[ID.ToString()].BaseID = ID;
                    this[ID.ToString()].BaseConnectStatus = baseConnectStatus;
                    if (ConnectEvent != null) { ConnectEvent(GetBaseIDs(false)); }
                    if (ConnectStatusEvent != null) { ConnectStatusEvent(ID); }
                    if (iStatus == 1)
                    {
                        if (ReadBaseInfoEvent != null) { ReadBaseInfoEvent(ID); }
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                SystemLog.WriterLog(ex, false);
            }
        }

        void myPVS_BaseEvent(int ID, int iMode, string sInfo)
        {

        }

        void myPVS_CommErrSys(string ID, int ErrNo, string ErrInfo)
        {

        }

        void myPVS_CommErrEx(string ID, int ErrNo, string ErrInfo)
        {

        }

        #endregion

        public void tmrConnectBaseRun()
        {
            if (Globals.SunVoteARSAddIn.IsExitApp)//杨斌 2015-11-10
                return;

            if (GlobalInfo.GetSdkType() == 1)
                return;

            //tmrConnectBase.Interval = 2147483647;
            tmrConnectBase.Enabled = false;

            //修改标志 2012-05-09
            //在连接前通知激活SlideShow窗体
            if (ActiveSlideWindowEvent != null)
                ActiveSlideWindowEvent();

            //MessageBox.Show("tmrConnectBase");
            //连接指定基站或者所有基站
            Connect();

            //MessageBox.Show("Connect");

            if (ReadBaseInfoEvent != null)
            {
                for (int i = 0; i < this.BaseList.Count; i++)
                {
                    if (this[i].BaseConnectStatus == BasePara.ConnectStatus.csSuccess)
                        ReadBaseInfoEvent(this[i].BaseID);
                }
            }

            //MessageBox.Show("ReadBaseInfoEvent");
        }

        System.Windows.Forms.Timer TmrConn = null;
        void TmrConn_Tick(object sender, EventArgs e)
        {
            TmrConn.Enabled = false;
            tmrConnectBaseRun();
        }

        private void tmrConnectBase_CallBack(object state)
        {
            //Action<object> action = (obj) => tmrConnectBaseRun();
            //action.BeginInvoke(this, ar => action.EndInvoke(ar), null);
            tmrConnectBaseRun();
        }

        void tmrConnectBase_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            tmrConnectBaseRun();

            //tmrConnectBase.Enabled = false;
            //baseConnection.Close();
            //if (TmrConn == null)
            //{
            //    TmrConn = new System.Windows.Forms.Timer();
            //    TmrConn.Tick += TmrConn_Tick;
            //    TmrConn.Interval = 10;
            //}
            //TmrConn.Enabled = true;
            //MessageBox.Show("tmrConnectBase_Elapsed");
        }

        void tmrConnectBase_Tick(object tmrConenctBase, EventArgs e)
        {
            tmrConnectBaseRun();
        }

        void baseConnection_baseOnLine(int BaseID, int BaseState)
        {
            Action<object> action = (obj) => BaseOnLine(BaseID, BaseState);
            action.BeginInvoke(this, ar => action.EndInvoke(ar), null);
        }


        /// <summary>
        /// 是否连接单个
        /// </summary>
        private bool ConnecOne = false;
        /// <summary>
        /// 连接次数:多基站连接时一轮检测完毕
        /// </summary>
        private int ConnectNum = 0;
        System.Windows.Forms.Timer TmrReTryConnect = null;
        #region SDK事件方法
        private int iNum = 0;
        /// <summary>
        /// 基站连接事件
        /// </summary>
        /// <param name="BaseID">基站ID</param>
        /// <param name="BaseState">连接状态</param>
        void BaseOnLine(int BaseID, int BaseState)
        {
            try
            {
                if (BaseState == -5)
                {
                    string BaseUsedByApp = GlobalInfo.SysLanguage.LPT.ReadString("Other", "BaseUsedByApp", "检测到与以下软件使用的基站冲突,请先关闭其他软件!");
                    if (MessageBox.Show(BaseUsedByApp + "\r\n" + baseConnection.BaseUsedByApp, "", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
                    {
                        if (TmrReTryConnect == null)
                        {
                            TmrReTryConnect = new System.Windows.Forms.Timer();
                            TmrReTryConnect.Tick += TmrReTryConnect_Tick;
                        }
                        TmrReTryConnect.Interval = 10;
                        TmrReTryConnect.Enabled = true;
                    }
                    return;
                }
                if (Globals.SunVoteARSAddIn.IsExitApp)//杨斌 2015-11-10
                    return;

                if (GlobalInfo.GetSdkType() == 1)
                    return;

                BasePara.ConnectStatus baseConnectStatus = GetBaseConnectStatus(BaseState);
                //if ((BaseID == 0) && (CheckConnectStatus()))
                if (BaseID == 0)
                {
                    for (int i = 0; i < baseList.Count; i++)
                    {
                        this[i].BaseConnectStatus = baseConnectStatus;
                    }//没有基站连接时重新扫描基站
                    if (ConnectStatusEvent != null) { ConnectStatusEvent(BaseID); }
                    if (ConnectEvent != null) { ConnectEvent(GetBaseIDs(false)); }
                    ConnecOne = false;

                    if (!tmrConnectBase.Enabled)
                    {
                        tmrConnectBase.Interval = 100;
                        tmrConnectBase.Enabled = true;
                    }
                    //tmrConnectBase.Change(100, 0);
                    return;

                }//没有连接基站
                else
                {
                    //tmrConnectBase.Stop();
                    //tmrConnectBase.Enabled = false;
                    if (MultiBase == 0)
                    {
                        //连接单个基站
                        if (BaseState == 1)
                        {
                            if (!baseList.Contains(BaseID.ToString()))
                            {
                                string singleIP = this[0].BaseIPAddress;
                                baseList.Clear();
                                baseList.Add(BaseID.ToString(), new BasePara());
                                this[BaseID.ToString()].BaseID = BaseID;
                                this[BaseID.ToString()].BaseIPAddress = singleIP;
                            }
                        }
                        if ((BaseState == 1) && (ConnecOne == false))
                        {

                            ConnecOne = true;
                            tmrConnectBase.Interval = 100;
                            tmrConnectBase.Enabled = true;
                            //tmrConnectBase.Change(100, 0);
                            //更改配置文件中的基站编号
                            if ((SysConfigBaseListEvent != null) && (BaseID != SingleBaseID))
                            {
                                SysConfigBaseListEvent(BaseID, "ID", BaseID);
                                SingleBaseID = BaseID;
                            }
                        }
                        if ((BaseState != 1) && (ConnecOne == true))
                        {
                            ConnecOne = false;
                            tmrConnectBase.Interval = 100;
                            tmrConnectBase.Enabled = true;
                            //tmrConnectBase.Change(100, 0);
                        }//单基站模式连接失败
                    }//单基站模式
                    else
                    {
                        ConnectNum += 1;
                        //检测基站状态,检测次数超过8次,连接指定基站,检测到已连接基站
                        if ((!ConnecOne) && (ConnectNum >= cMaxBaseCount) && (CheckConnectStatus()))//杨斌 2012-10-11
                        {
                            ConnectNum = 0;
                            ConnecOne = true;
                            tmrConnectBase.Interval = 100;
                            tmrConnectBase.Enabled = true;
                            //tmrConnectBase.Change(100, 0);
                        }
                    }//多基站模式
                    //检测完毕,如果为反馈状态不读取基站信息
                    if (ConnecOne && (NeedReadBaseInfo))
                    {
                        if (BaseState == 1)
                        {
                            //BaseNum += 1;
                            //if (ReadBaseInfoEvent != null) { ReadBaseInfoEvent(BaseID); }
                        }
                    }
                    if (!baseList.Contains(BaseID.ToString())) { return; }
                    //连接状态改变
                    if (this[BaseID.ToString()].BaseConnectStatus != baseConnectStatus)
                    {
                        this[BaseID.ToString()].BaseConnectStatus = baseConnectStatus;
                        if (ConnectEvent != null) { ConnectEvent(GetBaseIDs(false)); }
                        if (ConnectStatusEvent != null) { ConnectStatusEvent(BaseID); }
                        //连接成功触发事件读基站信息

                    }//更新列表中的值,状态发生变
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                SystemLog.WriterLog(ex, false);
            }
        }

        void TmrReTryConnect_Tick(object sender, EventArgs e)
        {
            TmrReTryConnect.Enabled = false;
            if (OnReTryConnectEvt != null)
                OnReTryConnectEvt();
        }



        #endregion
        #region 方法

        /// <summary>
        /// 连接基站
        /// </summary>
        public void Connect()
        {
            //硬件设置中断开基站,不允许自动连接
            if (DisConnectFlag == true) { return; }
            string baseIDs = "";
            string baseIPs = "";
            BaseNum = 0;
            ConnectNum = 0;

            if (GlobalInfo.GetSdkType() == 0)
            {
                if (ConnecOne)
                {
                    if (MultiBase == 1)
                    {
                        for (int i = 0; i < baseList.Count; i++)
                        {
                            if (this[i].BaseID != 0)
                            {
                                if (this[i].BaseConnectStatus == BasePara.ConnectStatus.csSuccess)
                                {
                                    if (this[i].BaseID == 0) { continue; }
                                    if (baseIDs != "")
                                    {
                                        baseIDs += ",";
                                        baseIPs += ",";
                                    }
                                    baseIDs += this[i].BaseID.ToString();
                                    if (connectType == 2)
                                    {
                                        baseIPs = baseIPs + this[i].BaseIPAddress.ToString();
                                    }

                                }//启用状态为true连接
                            }
                        }//获得连接基站编号\IP的字符串
                    }//多基站模式检测1-8号基站后重新连接检测到的基站
                    else
                    {
                        baseIPs = this[0].BaseIPAddress;
                        baseIDs = this[0].BaseID.ToString();
                    }
                }
                else
                {
                    if (MultiBase == 0)
                    {
                        baseIDs = scanBaseIDs;
                        baseIPs = scanBaseIPs;
                    }
                    else
                    {
                        baseIDs = scanBaseIDs;//杨斌 2012-10-11
                        baseIPs = this[0].BaseIPAddress;
                        for (int i = 0; i < baseList.Count; i++)
                            if (i > 0)
                                baseIPs += "," + this[i].BaseIPAddress;
                    }
                }
                //}//单基站模式
                baseConnection.BaseIP = baseIPs;
                //baseConnection.Open(connectType, baseIDs);//基站连接。屏蔽。杨斌 2016-10-26
                //杨斌 2016-04-19
                ConnectTypeReTry = connectType;
                ConnectIDsReTry = baseIDs;
            }
            else if (GlobalInfo.GetSdkType() == 1)
            {
                BaseConnectPvs();//杨斌 2015-03-18
            }
        }

        private int ConnectTypeReTry = 0;
        private string ConnectIDsReTry = "";

        /// <summary>
        /// 单基站模式先连上一个连接成功的基站
        /// </summary>
        public void ConnectOne()
        {
            if (MultiBase == 0)
            {
                string baseIPs = "";
                string baseIDs = "";
                ConnecOne = true;
                baseIPs = singleBaseIP;
                baseIDs = singleBaseID.ToString();

                if (GlobalInfo.GetSdkType() == 0)
                {
                    baseConnection.BaseIP = baseIPs;
                    //baseConnection.Open(connectType, baseIDs); //基站连接 。屏蔽。杨斌 2016-10-26
                    //杨斌 2016-04-19
                    ConnectTypeReTry = connectType;
                    ConnectIDsReTry = baseIDs;
                }
                else if (GlobalInfo.GetSdkType() == 1)
                {
                    BaseConnectPvs();//杨斌 2015-03-18
                }
            }
        }

        /// <summary>
        /// 检测冲突后,重试连接。
        /// </summary>
        public void ConnectReTry()
        {
            //baseConnection.Open(ConnectTypeReTry, ConnectIDsReTry);//基站连接。屏蔽。杨斌 2016-10-26
        }

        private void BaseConnectPvs()
        {
            //this.baseList.Clear();
            tmrConnectBase.Enabled = false;

            PVSServer.MyPVS pvs = FrmPvsServer.GetMyPvs();
            if (pvs != null)
            {
                FrmPvsServer.GetFrmPvs().VoteStop();
                pvs.IsOneBase = (MultiBase == 0);
                pvs.BaseConnectOpen();
            }
        }

        /// <summary>
        /// 断开基站
        /// </summary>
        public void DisConnect()
        {
            if (GlobalInfo.GetSdkType() == 0)
            {
                //baseConnection.Close();//杨斌 2017-04-19
            }
            else if (GlobalInfo.GetSdkType() == 1)
            {
                //杨斌 2015-03-18
                PVSServer.MyPVS pvs = FrmPvsServer.GetMyPvs();
                if (pvs != null)
                {
                    pvs.BaseConnectClose();
                }
            }
            for (int i = 0; i < baseList.Count; i++)
            {
                this[i].BaseConnectStatus = BasePara.ConnectStatus.csDisconnect;
            }
            //断开后重新连接
            ConnecOne = false;
            //记录检测基站的次数
            ConnectNum = 0;
        }

        /// <summary>
        /// 获取连接状态的基站字符串
        /// </summary>
        /// <param name="bStatus">基站连接状态</param>
        public string GetBaseIDs(bool bStatus)
        {
            string BaseIDs = "";
            for (int i = 0; i < baseList.Count; i++)
            {
                if (bStatus == (this[i].BaseConnectStatus == BasePara.ConnectStatus.csSuccess))
                {
                    if (i > 0)
                        BaseIDs += ",";

                    BaseIDs = BaseIDs + this[i].BaseID.ToString();
                    //if (bStatus == true)
                    //    AryBaseID = AryBaseID + ":已连接,";
                    //if (bStatus == false)
                    //    AryBaseID = AryBaseID + ":未连接,";
                }
            }
            return BaseIDs;
        }

        /// <summary>
        /// 转换连接状态为枚举
        /// </summary>
        /// <param name="baseStatus">连接状态</param>
        /// <returns>返回连接状态</returns>
        private BasePara.ConnectStatus GetBaseConnectStatus(int baseStatus)
        {
            BasePara.ConnectStatus connectStatus = BasePara.ConnectStatus.csDisconnect;
            switch (baseStatus)
            {
                case 0: //连接失败
                    connectStatus = BasePara.ConnectStatus.csDisconnect;
                    break;
                case 1://连接成功
                    connectStatus = BasePara.ConnectStatus.csSuccess;
                    break;
                case -1://不支持该连接方式
                    connectStatus = BasePara.ConnectStatus.csConnWayErr;
                    break;
                case -2://预连基站编号无效
                    connectStatus = BasePara.ConnectStatus.csBaseErr;
                    break;
                case -3://端口错误
                    connectStatus = BasePara.ConnectStatus.csPortErr;
                    break;

            }
            return connectStatus;
        }

        /// <summary>
        /// 检查基站是否连接成功
        /// </summary>
        /// <returns></returns>
        public bool CheckConnectStatus()
        {
            bool bValue = false;
            if (MultiBase == 0)
            {
                for (int i = 0; i < this.baseList.Count; i++)
                {
                    bValue = (this[i].BaseConnectStatus == BasePara.ConnectStatus.csSuccess) ? true : false;
                    if (bValue)
                        break;
                }
            }
            else
            {
                int ConnectNum = 0;
                for (int i = 0; i < BaseList.Count; i++)
                {
                    if (this[i].BaseConnectStatus == BasePara.ConnectStatus.csSuccess)
                    {
                        ConnectNum += 1;
                        break;
                    }
                }
                bValue = (ConnectNum > 0) ? true : false;
                //bValue = (ConnectNum >= ConnectIDsReTry.Split(',').Length);

            }//多基站模式
            return bValue;
        }


        #endregion

        #region 事件
        /// <summary>
        /// 基站连接事件,连接状态发生变化触发
        /// </summary>
        public event ConnectEvent ConnectEvent;
        public event ReTryConnectEvt OnReTryConnectEvt = null;

        public event ConnectStatusEvent ConnectStatusEvent;
        /// <summary>
        /// 读基站信息,基站连接成功触发
        /// </summary>
        public event ReadBaseInfoEvent ReadBaseInfoEvent;

        /// <summary>
        /// 写配置文件的事件
        /// </summary>
        public event SysConfigBaseListEvent SysConfigBaseListEvent;
        /// <summary>
        /// 是否为反馈状态
        /// </summary>
        public event ResponseStateEventHander ResponseStateEvent;
        /// <summary>
        /// 设置当前激活窗体为幻灯片的窗体
        /// 创建标志 2012-05-09
        /// </summary>
        public event ActiveSlideWindowEventHander ActiveSlideWindowEvent;
        #endregion


        # region 索引
        /// <summary>
        /// 从键获取基站信息
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public BasePara this[string key]
        {
            get
            {
                return ((BasePara)(baseList[key]));
            }

            set
            {
                this[key] = value;
            }
        }

        /// <summary>
        /// 从值获取基站信息
        /// </summary>
        /// <param name="index">索引值</param>
        /// <returns></returns>
        public BasePara this[int index]
        {
            get
            {
                return ((BasePara)(baseList[index]));
            }
            set
            {
                this[index] = value;
            }
        }
        #endregion

    }
    #endregion
}