/// <summary> /// /// </summary> /// <returns></returns> public void Init(string serverIP, string opcServerName) { this.opcServerName = opcServerName; this.serverIP = serverIP; try { if (ConnectRemote()) { opcGroups = opcServer.OPCGroups; opcBrowser = opcServer.CreateBrowser(); opcAGroup = opcGroups.Add("Group"); opcAGroup.IsActive = true; opcAGroup.IsSubscribed = true; opcAGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(OnGroup_DataChange); } } catch (Exception) { } }
/////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// The OPCServer Object provides a method called 'Connect' that allows you /// to 'connect' with an OPC server. The 'Connect' method can take two arguments, /// a server name and a Node name. The Node name is optional and does not have to // be used to connect to a local server. When the 'Connect' method is called you /// should see the OPC Server application start if it is not aleady running. /// ///Special Note: When connect remotely to another PC running the KepserverEx make ///sure that you have properly configured DCOM on both PC's. You will find documentation ///explaining exactly how to do this on your installation CD or at the Kepware web site. ///The web site is www.kepware.com. /// </summary> /////////////////////////////////////////////////////////////////////////////////////////// public bool Connect() { if (!mInitialized) { return(false); } // 如果已经Connect了,就直接返回 if (Connected) { return(true); } try { mServer = new OPCServer(); mServer.Connect(mOPCServerName, mOPCServerIP); //mServer.Connect(mOPCServerName, ""); if (mServer.ServerState != (int)OPCServerState.OPCRunning) { return(false); } // 创建 OPCBrowser oPCBrowser = mServer.CreateBrowser(); GetDelimiter(); long DefaultGroupTimeBias = mServer.OPCGroups.DefaultGroupTimeBias; Console.WriteLine("DefaultGroupTimeBias is: " + DefaultGroupTimeBias); //mServer.OPCGroups.DefaultGroupTimeBias = (8 * 60); mServer.OPCGroups.DefaultGroupTimeBias = (int)-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes; DefaultGroupTimeBias = mServer.OPCGroups.DefaultGroupTimeBias; Console.WriteLine("DefaultGroupTimeBias is: " + DefaultGroupTimeBias); bool result = CreateGroup(DefaultGroupName); if (!result) { return(false); } defaultGroup = mGroups[DefaultGroupName]; if (defaultGroup == null) { return(false); } this.ServerInfo = new ServerInformation(); this.ServerInfo.MajorVersion = mServer.MajorVersion.ToString(); this.ServerInfo.MinorVersion = mServer.MinorVersion.ToString(); this.ServerInfo.BuildNumber = mServer.BuildNumber.ToString(); //LocaleID Console.WriteLine("LocaleID is: " + mServer.LocaleID); OPCGroups g = mServer.OPCGroups; foreach (OPCGroup x in g) { Console.WriteLine("group name is: " + x.Name); } //string ServerName = mServer.ServerName; //Console.WriteLine("ServerName is: " + ServerName ); //string ClientName = mServer.ClientName ; //Console.WriteLine("ClientName is: " + ClientName ); return(true); } catch (Exception ex) { mServer = null; return(false); } }