Exemplo n.º 1
0
        public void ConnectMasterOPCServer()
        {
            if (IsConnect)
            {
                EventLogger.Log("OPC服务器已连接");
                //NLogHelper.Error("生产线OPC服务器已连接");
            }
            else
            {
                string errorMsg = string.Empty;
                this.MasterServer = OPCConnector.ConnectOPCServer(0, "localhost", "RSLinx OPC Server", ref errorMsg);

                if (!string.IsNullOrEmpty(errorMsg))
                {
                    EventLogger.Log(errorMsg);
                    //NLogHelper.Error(errorMsg);
                    return;
                }

                IsConnect = this.MasterServer != null;
                if (!IsConnect)
                {
                    EventLogger.Log("OPC服务器通讯失败");
                    //NLogHelper.Error("OPC服务器通讯失败");
                }
                EventLogger.Log("OPC服务器通讯成功。");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="serverPKID"></param>
        /// <param name="address"></param>
        /// <param name="updateRate"></param>
        /// <param name="deviceTagParams"></param>
        /// <param name="callback"></param>
        private void Initial(Int64 serverPKID, string address, int updateRate,
                             List <DeviceTagParam> deviceTagParams, DataChangeEventHandler callback)
        {
            pkid = serverPKID;

            #region 初始化参数

            string ip         = (address == "") ? CBaseData.LocalIP : address;
            string serverName = address;

            string[] addes = address.Split('|', ';');  //分号隔开,前面是IP地址,后面是OPCServer名称
            if (addes.Length > 1)
            {
                ip = addes[0];
                if ((ip.ToLower() == "local") || (ip.ToLower() == "localhost") || (ip.ToLower() == "."))
                {
                    ip = CBaseData.LocalIP; //本机IP
                }
                serverName = addes[1];
            }
            else  //默认为本机
            {
                ip = CBaseData.LocalIP;
            }

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

            ServerIP      = remote;
            OPCServerName = serverName;
            Callback      = callback; //设置回调函数

            #endregion

            DeviceTags = deviceTagParams;  //标签

            #region 自定义协议

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

            #endregion

            #region OPC 设定

            string error = String.Empty;

            _opcServer = OpcServers.FirstOrDefault(c => c.PKID == serverPKID);

            if ((_opcServer == null) || (!_opcServer.IsConnected))
            {
                _opcServer = OPCConnector.ConnectOPCServer(serverPKID, ip, serverName, ref error);
            }
            else
            {
                EventLogger.Log($"【{serverPKID}】OPC服务器已连接");
            }

            if (_opcServer != null && _opcServer.IsConnected)
            {
                List <Guid>   subHandels = new List <Guid>();                                           //Handel
                List <string> subItemIds = new List <string>();                                         //标签地址

                List <Guid>   normalHandels = new List <Guid>();                                        //Handel
                List <string> normalItemIds = new List <string>();                                      //标签地址

                if (subHandels.Count > 0)                                                               //添加订阅
                {
                    OpcGroup subGroup = _opcServer.AddGroup("GP" + serverPKID + "S", updateRate, true); //订阅的Group

                    subGroup.AddItems(subItemIds.ToArray(), subHandels.ToArray());

                    subGroup.DataChanged -= OPCDataChanged;
                    subGroup.DataChanged += OPCDataChanged;
                }

                if (normalHandels.Count > 0)                                                                //添加正常
                {
                    OpcGroup normalGroup = _opcServer.AddGroup("GP" + serverPKID + "N", updateRate, false); //正常的Group

                    normalGroup.AddItems(normalItemIds.ToArray(), normalHandels.ToArray());
                }

                EventLogger.Log($"【{serverPKID}】OPC服务器连接成功。");
            }
            else
            {
                error = $"【{serverPKID}】OPC服务器连接失败,具体为:" + error;
                EventLogger.Log(error);
            }

            OpcServers.Add(_opcServer);  //将OPCServer添加到系统中

            #endregion
        }