/// <summary>
        /// 初始化设备链接
        /// </summary>
        /// <returns></returns>
        private InheritMaster InitConnect(bool bInit = true)
        {
            try
            {
                if (bAllUnConnected == false)
                {
                    int       iNum  = 0;
                    PingReply reply = null;
                    do
                    {
                        if (iNum == 1 && bInit)
                        {
                            InternetStatus = false;
                            break;
                        }

                        if (iNum > 0)
                        {
                            System.Threading.Thread.Sleep(1000);
                        }
                        Ping pingSener = new Ping();
                        reply = pingSener.Send(this.host, this.port);
                        iNum++;
                    } while (reply.Status != IPStatus.Success && iNum <= 100);

                    if (reply.Status == IPStatus.Success)
                    {
                        if (_Master == null)
                        {
                            _Master = new InheritMaster(this.host, (ushort)this.port);
                        }
                        else
                        {
                            _Master.connect(this.host, (ushort)this.port);
                        }
                        //       if (cache == null)
                        cache          = new RedisCacheManager("CacheFileServer");
                        Connected      = true;
                        InternetStatus = true;
                        //_Master.OnResponseData += new InheritMaster.ResponseData(InMBmaster_OnResponseData);//启用数据监听事件
                        //_Master.OnException += InMBmaster_OnException;
                    }
                    else
                    {
                        bAllUnConnected = true;
                    }
                }
                // _Master.OnResponseData += new InheritMaster.ResponseData(InMBmaster_OnResponseData);//启用数据监听事件
                //     _Master.OnException += InMBmaster_OnException;
            }
            catch (Exception ex)
            {
            }
            return(_Master);
        }
Exemple #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            //--------------------------直接引用master测试--------------------------------------//
            // MBmaster = new Master(IPEdit.Text, (ushort)Convert.ToInt16(PortEdit.Text));  //master类通用连接功能,带IP跟端口参数
            //MBmaster.OnResponseData += new Master.ResponseData(MBmaster_OnResponseData);//启用数据监听事件
            //Status.Text = "已连接";
            //--------------------------直接引用master测试--------------------------------------//

            InMBmaster01 = new InheritMaster(IPEdit.Text, (ushort)Convert.ToInt16(PortEdit.Text));    //master类通用连接功能,带IP跟端口参数
            InMBmaster01.OnResponseData += new InheritMaster.ResponseData(InMBmaster_OnResponseData); //启用数据监听事件
            Status.Text = "已连接";
        }