Example #1
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="serverPKID"></param>
        /// <param name="address"></param>
        /// <param name="deviceInterface">设备接口</param>
        /// <param name="deviceTagParams"></param>
        /// <param name="callback"></param>
        public void Initial(Int64 serverPKID, string address, DeviceCommInterface deviceInterface,
                            List <DeviceTagParam> deviceTagParams, DataChangeEventHandler callback)
        {
            pkid = serverPKID;

            #region Socket 通讯相关

            string ip   = (address == "") ? CBaseData.LocalIP : address;
            int    port = 2001;

            string[] addes = address.Split('|');  //分号隔开,前面是IP地址,后面是Port
            if (addes.Length > 1)
            {
                ip = addes[0];
                if ((ip.ToLower() == "local") || (ip.ToLower() == "localhost") || (ip.ToLower() == "."))
                {
                    ip = CBaseData.LocalIP;  //本机IP
                }
                int.TryParse(addes[1], out port);
            }

            Callback = callback;  //设置回调函数

            IPAddress remote;
            IPAddress.TryParse(ip, out remote);

            ServerIP   = remote;
            ServerPort = port;

            ThreadPool.QueueUserWorkItem(s => { CreateAndRefreshClient(); }); //不断重连

            #endregion

            #region 设置标签信息

            DeviceTags = deviceTagParams;  //设置标签

            #endregion

            #region 添加自定义协议设备

            CustomProtocol   = (addes.Length >= 3) ? addes[2] : ""; //自定义协议
            ProtocolVariable = (addes.Length >= 4) ? addes[3] : ""; //自定义协议

            SockeDevice = CustomSocket.GetDevice(deviceInterface, CustomProtocol, ProtocolVariable);

            #endregion
        }