private void OPC_Ini() { try { MyOPCServer = new OPCServer(); MyOPCServer.Connect("KEPware.KEPServerEx.V6", "193.100.101.221"); OpcIn = MyOPCServer.OPCGroups.Add("KtingByLight.MKtingByLight.001"); OpcIn = MyOPCServer.OPCGroups.Add("KtingByLight.MKtingByLight.002"); OpcIn = MyOPCServer.OPCGroups.Add("KtingByLight.MKtingByLight.003"); OpcIn = MyOPCServer.OPCGroups.Add("KtingByLight.MKtingByLight.004"); OpcIn = MyOPCServer.OPCGroups.Add("KittingLine1.KittingLine1-1"); BLL.GetPickData getPickData = new BLL.GetPickData(); configDt = getPickData.GetData(); int index = 1; foreach (DataRow item in configDt.Rows) { // MessageBox.Show(item["OPCSite"].ToString()); OpcIn.OPCItems.AddItem(item["OPCSite"].ToString().Trim(), index); index++; } OpcIn.OPCItems.AddItem("KtingByLight.MKtingByLight.004.001", index); index++; for (int i = 1; i < 6; i++) { OpcIn.OPCItems.AddItem("KittingLine1.KittingLine1-1." + i + "#放行", index); index++; OpcIn.OPCItems.AddItem("KittingLine1.KittingLine1-1." + i + "#阻挡", index); index++; OpcIn.OPCItems.AddItem("KittingLine1.KittingLine1-1." + i + "#到位", index); index++; } //蜂鸣器 OpcIn.OPCItems.AddItem("KittingLine1.KittingLine1-1.Lamp", index); OpcIn.UpdateRate = 50; OpcIn.IsActive = true; OpcIn.IsSubscribed = true; OpcIn.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(OpcInTri_DataChange); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void OPC_Ini() { try { MyOPCServer = new OPCServer(); MyOPCServer.Connect("KEPware.KEPServerEx.V6", "193.100.101.221"); //OpcIn = MyOPCServer.OPCGroups.Add("FR.FR"); XML.XmlConfig xmlConfig = new XML.XmlConfig(); xmlConfig.GetIPXML(); //string IP = XML.XmlConfig.GetIPXML(); Bll.T_MJAnswer t_MJAnswer = new T_MJAnswer(); //string workBay = t_MJAnswer.GetWorkBay(IP); //workBayTag = t_MJAnswer.GetWorkBayTag(IP); workBayTag = xmlConfig.staionName; T_OPCTag t_OPCTag = new T_OPCTag(); //string line = t_OPCTag.GetLine(workBay); string line = xmlConfig.line; OpcIn = MyOPCServer.OPCGroups.Add(line + "." + line); LineName = line; DataTable allOPCTag = t_OPCTag.GetAllOPCTag(workBayTag); int index = 1; foreach (DataRow item in allOPCTag.Rows) { OpcIn.OPCItems.AddItem(line + "." + line + "." + item["kepserverAllName"].ToString().Trim(), index); index++; } //OpcIn.OPCItems.AddItem("FR.FR.FR25_CODE", 1); //OpcIn.OPCItems.AddItem("FR.FR.FR25_IsCode", 2); OpcIn.UpdateRate = 50; OpcIn.IsActive = true; OpcIn.IsSubscribed = true; OpcIn.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(OpcInTri_DataChange); // blState = true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); // blState = false; // log.writeTxt(Application.StartupPath, ErrorLog.logType.ERRORLOG, "OPC连接异常:" + ex.Message); } }
/////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Disconnects the opc client (Removes all items and groups) /// </summary> /////////////////////////////////////////////////////////////////////////////////////////// public void Disconnect() { try { RemoveAllItems(); RemoveAllGroups(); if ((mServer != null)) { mServer.Disconnect(); } } catch (Exception ex) { //Exception Event Console.WriteLine("in Disconnect, error is: " + ex.Message); } finally { mServer = null; GC.Collect(); } }
public void Initialize(OpcConfiguration config) { Config = config; try { _OpcServer = new OPCAutomation.OPCServer(); _OpcServer.Connect(ServerName, ""); _OpcServer.OPCGroups.DefaultGroupIsActive = true; _OpcServer.OPCGroups.DefaultGroupDeadband = 0; _OpcGroup = _OpcServer.OPCGroups.Add("Group0"); _OpcGroup.UpdateRate = 250; _OpcGroup.IsSubscribed = true; _OpcGroup.OPCItems.DefaultIsActive = true; _OpcGroup.OPCItems.AddItems(Config.TagCount, Config.ClientTags.Select(x => x.Name).ToArray(), Config.ClientTags.Select(x => x.Handle).ToArray(), out var serverHandles, out var serverErrors); Config.ServerHandles = serverHandles; Config.ServerErrors = serverErrors; bool itemgood = false; for (int i = 1; i <= config.TagCount; i++) { int ab = (Int32)Config.ServerErrors.GetValue(i); itemgood = ab == 0; if (!itemgood) { break; } } if (itemgood) { _OpcGroup.DataChange += OPCGroup_DataChanged; } } catch (Exception ex) { _OpcServer = null; } }
//################ Sub procedure to initialize OPC Server ###### private void StopOPCClient() { // == // Description : 断开连接OPCServer,返回值False为断开失败,True为断开成功 // Created : 2009/01/19 // Author : zgh // Update : // Updater : // == //bool value = false; try { ObjOPCGroup.IsActive = false; //ObjOPCGroup.Remove(ObjOPCGroup.ServerHandle) ObjOPCGroups.RemoveAll(); ObjOPCItems = null; ObjOPCGroups = null; ObjOPCGroup = null; //ObjOPCGroup.AsyncReadComplete += ObjOPCGroup_AsyncReadComplete; ObjOPCServer.Disconnect(); ObjOPCServer = null; //colTagName = Nothing //value = true; //MessageLog("clsDeviceOPC.StopOPCClient(): Disconnected from OPC device \'" + DeviceName + "\'"); LOG.Info("断开OPC连接 \'" + SourceName + "\'"); } catch (Exception ex) { //value = false; //WarnLog(ex.StackTrace + "\r\n" + ex.Message); // MsgBox(ex.Message) LOG.Error(string.Format("OPCDataSource断开连接出错行:{0},", ex.Message)); } finally { System.GC.Collect(); //强制垃圾回收 } }
/////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Disconnects the opc client (Removes all items and groups) /// </summary> /////////////////////////////////////////////////////////////////////////////////////////// public void Disconnect() { try { RemoveAllItems(); RemoveAllGroups(); if ((mServer != null)) { mServer.Disconnect(); } } catch (Exception ex) { //Exception Event } finally { mServer = null; } }
/////////////////////////////////////////////////////////////////////////////////////////// /// <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; try { mServer = new OPCServer(); mServer.Connect(mOPCServerName, ""); return true; } catch (Exception ex) { mServer = null; return false; } }
public OPCClientWrapper() { opcServer = new OPCAutomation.OPCServer(); }
/////////////////////////////////////////////////////////////////////////////////////////// /// <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); } }
//################ Sub procedure to initialize OPC Server ###### private bool StartOPCClient() { int liCounter = 0; Array gpServerHandlers = null; Array gsErrors = null; string GroupName = SourceName; try { ObjOPCServer = new OPCServer(); //##### Initialize OPC Server ################################ ObjOPCServer.Connect(OPCServerName, ""); ObjOPCGroups = ObjOPCServer.OPCGroups; ObjOPCGroup = ObjOPCGroups.Add(GroupName); ObjOPCGroup.AsyncReadComplete += ObjOPCGroup_AsyncReadComplete; //Add OPCGroup ObjOPCGroup.UpdateRate = UpdateInterval; ObjOPCGroup.IsActive = false; ObjOPCGroup.IsSubscribed = ObjOPCGroup.IsActive; ObjOPCItems = ObjOPCGroup.OPCItems; //Create opc items Array_Address = Array.CreateInstance(typeof(string), NoOfItems + 1); Array_ClientHanlers = Array.CreateInstance(typeof(int), NoOfItems + 1); Array_ServerHandlers = Array.CreateInstance(typeof(int), NoOfItems + 1); //Build OPCItems Array for (liCounter = 1; liCounter <= NoOfItems; liCounter++) { Array_Address.SetValue(List_Address[liCounter - 1], liCounter); Array_ClientHanlers.SetValue(liCounter, liCounter); } ObjOPCItems.AddItems(NoOfItems, ref Array_Address, ref Array_ClientHanlers, out gpServerHandlers, out gsErrors); //Get the server handlers bool HasError = false; for (liCounter = 1; liCounter <= NoOfItems; liCounter++) { if ((int)gsErrors.GetValue(liCounter) == 0) { Array_ServerHandlers.SetValue((int)gpServerHandlers.GetValue(liCounter), liCounter); } else { LOG.Error("Tag \'" + List_Address[liCounter - 1] + "\' has problem连接失败错误"); HasError = true; } } if (!HasError) { LOG.Info(" 启动OPC组 \'" + SourceName + "\'"); } return(!HasError); } catch (Exception ex) { LOG.Error(string.Format("连接OPC出错:读取资源{0}{1}", GroupName, ex.Message)); return(false); } }