private void cmdRemGroup_Click(object sender, EventArgs e) { if (theGroup != null) { int[] aE = new int[2]; try { theGroup.DataChanged -= new DataChangeEventHandler(TheGrp_DataChanged); theGroup.ReadCompleted -= new ReadCompleteEventHandler(TheGrp_ReadCompleted); theGroup.WriteCompleted -= new WriteCompleteEventHandler(TheGrp_WriteCompleted); theGroup.Remove(false); } catch (Exception ex) { MessageBox.Show(ex.Message); } theGroup = null; } }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { //theGrp.RemoveItems(handlesSrv, out aE); if (theGrp != null) { theGrp.Remove(false); theTargGrp.Remove(false); theSrv.Disconnect(); theGrp = null; theTargGrp = null; theSrv = null; } stopReceive = true; connect.Abort(); Process.GetCurrentProcess().Kill(); }
public void Disconnect() { List <int> l_ServerHandleList = _serverModel.GetServerHandleList(); if (l_ServerHandleList.Count > 0) { try { int[] l_RemoveRes; _group.RemoveItems(l_ServerHandleList.ToArray(), out l_RemoveRes); } catch (Exception) { } } if (_group != null) { _group.DataChanged -= new DataChangeEventHandler(Group_DataChange); try { _group.Remove(true); } catch (Exception) { } } if (_server != null) { _server.ShutdownRequested -= new ShutdownRequestEventHandler(Server_ShutdownRequest); try { _server.Disconnect(); } catch (Exception) { } } }
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (!opc_connected) { return; } if (theGrp != null) { theGrp.DataChanged -= new DataChangeEventHandler(this.theGrp_DataChange); theGrp.WriteCompleted -= new WriteCompleteEventHandler(this.theGrp_WriteComplete); RemoveItem(); theGrp.Remove(false); theGrp = null; } if (theSrv != null) { theSrv.Disconnect(); // should clean up theSrv = null; } opc_connected = false; }
public void Close() { if (!OpcConnected) { return; } if (_theGrp != null) { _theGrp.DataChanged -= new DataChangeEventHandler(this.theGrp_DataChange); _theGrp.WriteCompleted -= new WriteCompleteEventHandler(this.theGrp_WriteComplete); RemoveItem(); _theGrp.Remove(false); _theGrp = null; } if (_theSrv != null) { _theSrv.Disconnect(); // should clean up _theSrv = null; } OpcConnected = false; }
public void Work() { /* try // disabled for debugging * { */ theSrv = new OpcServer(); theSrv.Connect(serverProgID); Thread.Sleep(500); // we are faster then some servers! // add our only working group theGrp = theSrv.AddGroup("OPCCSharp-Group", false, 900); // add two items and save server handles itemDefs[0] = new OPCItemDef(itemA, true, 1234, VarEnum.VT_EMPTY); itemDefs[1] = new OPCItemDef(itemB, true, 5678, VarEnum.VT_EMPTY); OPCItemResult[] rItm; theGrp.AddItems(itemDefs, out rItm); if (rItm == null) { return; } if (HRESULTS.Failed(rItm[0].Error) || HRESULTS.Failed(rItm[1].Error)) { Console.WriteLine("OPC Tester: AddItems - some failed"); theGrp.Remove(true); theSrv.Disconnect(); return; } ; handlesSrv[0] = rItm[0].HandleServer; handlesSrv[1] = rItm[1].HandleServer; // asynch read our two items theGrp.SetEnable(true); theGrp.Active = true; theGrp.DataChanged += new DataChangeEventHandler(this.theGrp_DataChange); theGrp.ReadCompleted += new ReadCompleteEventHandler(this.theGrp_ReadComplete); int CancelID; int[] aE; theGrp.Read(handlesSrv, 55667788, out CancelID, out aE); // some delay for asynch read-complete callback (simplification) Thread.Sleep(500); // asynch write object[] itemValues = new object[2]; itemValues[0] = (int)1111111; itemValues[1] = (double)2222.2222; theGrp.WriteCompleted += new WriteCompleteEventHandler(this.theGrp_WriteComplete); theGrp.Write(handlesSrv, itemValues, 99887766, out CancelID, out aE); // some delay for asynch write-complete callback (simplification) Thread.Sleep(500); // disconnect and close Console.WriteLine("************************************** hit to close..."); Console.ReadLine(); theGrp.DataChanged -= new DataChangeEventHandler(this.theGrp_DataChange); theGrp.ReadCompleted -= new ReadCompleteEventHandler(this.theGrp_ReadComplete); theGrp.WriteCompleted -= new WriteCompleteEventHandler(this.theGrp_WriteComplete); theGrp.RemoveItems(handlesSrv, out aE); theGrp.Remove(false); theSrv.Disconnect(); theGrp = null; theSrv = null; /* } * catch( Exception e ) * { * Console.WriteLine( "EXCEPTION : OPC Tester " + e.ToString() ); * return; * } */ }
static void Main(string[] args) { /*create array of readable Tags*/ var Tags = new List <OPCClientItem>(); Tags.Add(new OPCClientItem() { Name = ".test", ClientHanle = 1 }); OpcServer server = new OpcServer(); try { int transactionID = new Random().Next(1024, 65535); int cancelID = 0; int updateRate = 1000; /*connect to the OPC Server and check it's state*/ server.Connect("Matrikon.OPC.Simulation.1"); var serverStatus = new SERVERSTATUS(); server.GetStatus(out serverStatus); if (serverStatus.eServerState == OPCSERVERSTATE.OPC_STATUS_RUNNING) { /*create group of items*/ OpcGroup group = server.AddGroup("Group1", true, updateRate); group.ReadCompleted += group_ReadCompleted; List <OPCItemDef> items = new List <OPCItemDef>(); Tags.ToList() .ForEach(x => items.Add(new OPCItemDef(x.Name, true, x.ClientHanle, VarEnum.VT_EMPTY))); /* add items and collect their attributes*/ OPCItemResult[] itemAddResults = null; group.AddItems(items.ToArray(), out itemAddResults); for (int i = 0; i < itemAddResults.Length; i++) { OPCItemResult itemResult = itemAddResults[i]; OPCClientItem tag = Tags[i]; tag.ServerHandle = itemResult.HandleServer; tag.AccessRight = (itemResult.AccessRights == OPCACCESSRIGHTS.OPC_READABLE) ? OPCClientItem.EAccessRight.ReadOnly : OPCClientItem.EAccessRight.ReadAndWrite; } ; /*Refresh items in group*/ // group.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE, transactionID, out cancelID); /*Async read data for the group items*/ int[] serverHandles = new int[Tags.Count]; for (int i = 0; i < Tags.Count; i++) { serverHandles[i] = Tags[i].ServerHandle; } ; OPCItemState[] itemsStateResult = null; /*sync read*/ group.Read(OPCDATASOURCE.OPC_DS_DEVICE, serverHandles, out itemsStateResult); Console.WriteLine("Sync read:"); for (int i = 0; i < itemsStateResult.Length; i++) { OPCItemState itemResult = itemsStateResult[i]; Console.WriteLine(" -> item:{0}; value:{1}; timestamp{2}; qualituy:{3}", Tags[i].Name, itemResult.DataValue.ToString(), itemResult.TimeStamp, itemResult.Quality); } ; /*sync write*/ object[] values = new object[Tags.Count]; int[] resultErrors = new int[Tags.Count]; values[0] = (object)256; group.Write(serverHandles, values, out resultErrors); /*async read*/ group.Read(serverHandles, transactionID, out cancelID, out resultErrors); /*wait for a while befor remove group to process async event*/ System.Threading.Thread.Sleep(3000); /*the group must be removed !!! */ group.Remove(true); } ; } finally { server.Disconnect(); server = null; GC.Collect(); }; Console.ReadKey(); }
public void Work() { /* try // disabled for debugging * { */ theSrv = new OpcServer(); theSrv.Connect(serverProgID); Thread.Sleep(500); // we are faster then some servers! // add our only working group theGrp = theSrv.AddGroup("OPCCSharp-Group", false, timeref); if (sendtags > tags.Length) { sendtags = tags.Length; } var itemDefs = new OPCItemDef[tags.Length]; for (var i = 0; i < tags.Length; i++) { itemDefs[i] = new OPCItemDef(tags[i], true, i, VarEnum.VT_EMPTY); } OPCItemResult[] rItm; theGrp.AddItems(itemDefs, out rItm); if (rItm == null) { return; } if (HRESULTS.Failed(rItm[0].Error) || HRESULTS.Failed(rItm[1].Error)) { Console.WriteLine("OPC Tester: AddItems - some failed"); theGrp.Remove(true); theSrv.Disconnect(); return; } ; var handlesSrv = new int[itemDefs.Length]; for (var i = 0; i < itemDefs.Length; i++) { handlesSrv[i] = rItm[i].HandleServer; } currentValues = new Single[itemDefs.Length]; // asynch read our two items theGrp.SetEnable(true); theGrp.Active = true; theGrp.DataChanged += new DataChangeEventHandler(this.theGrp_DataChange); theGrp.ReadCompleted += new ReadCompleteEventHandler(this.theGrp_ReadComplete); int CancelID; int[] aE; theGrp.Read(handlesSrv, 55667788, out CancelID, out aE); // some delay for asynch read-complete callback (simplification) Thread.Sleep(500); while (webSend == "yes") { HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; context.Response.AddHeader("Access-Control-Allow-Origin", "*"); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseStringG); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); // You must close the output stream. output.Close(); } // disconnect and close Console.WriteLine("************************************** hit <return> to close..."); Console.ReadLine(); theGrp.ReadCompleted -= new ReadCompleteEventHandler(this.theGrp_ReadComplete); theGrp.RemoveItems(handlesSrv, out aE); theGrp.Remove(false); theSrv.Disconnect(); theGrp = null; theSrv = null; /* } * catch( Exception e ) * { * Console.WriteLine( "EXCEPTION : OPC Tester " + e.ToString() ); * return; * } */ }
static void Main(string[] args) { string progID = args.Length > 0 ? args[0] : "Kepware.KEPServerEX.V5"; OpcServer opcServer = new OpcServer(); opcServer.Connect(progID); System.Threading.Thread.Sleep(500); // we are faster than some servers! OpcGroup opcGroup = opcServer.AddGroup("SampleGroup", false, 900); List <string> itemNames = new List <string>(); if (args.Length > 1) { for (int i = 1; i < args.Length; i++) { itemNames.Add(args[i]); } } else { itemNames.Add("Simulation Examples.Functions.Ramp1"); itemNames.Add("Simulation Examples.Functions.Random1"); } OpcItemDefinition[] opcItemDefs = new OpcItemDefinition[itemNames.Count]; for (int i = 0; i < opcItemDefs.Length; i++) { opcItemDefs[i] = new OpcItemDefinition(itemNames[i], true, i, VarEnum.VT_EMPTY); } opcGroup.AddItems(opcItemDefs, out OpcItemResult[] opcItemResult); if (opcItemResult == null) { Console.WriteLine("Error add items - null value returned"); return; } int[] serverHandles = new int[opcItemResult.Length]; for (int i = 0; i < opcItemResult.Length; i++) { if (HRESULTS.Failed(opcItemResult[i].Error)) { Console.WriteLine("AddItems - failed {0}", itemNames[i]); opcGroup.Remove(true); opcServer.Disconnect(); return; } else { serverHandles[i] = opcItemResult[i].HandleServer; } } opcGroup.DataChanged += OpcGroup_DataChanged; opcGroup.SetEnable(true); opcGroup.Active = true; Console.WriteLine("********** Press <Enter> to close **********"); Console.ReadLine(); opcGroup.DataChanged -= OpcGroup_DataChanged; opcGroup.Remove(true); opcServer.Disconnect(); }
/// <summary> /// initialize OPC server and group /// </summary> /// <param name="serverName">OPC Server Name</param> /// <param name="groupName">OPC Group name under the OPC server. /// Here groupName is trendViewer Process ID in string</param> /// public void InitializeServer(string serverName, string groupName) { string Function_Name = "InitializeServer"; LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Entered"); lock (m_opcDataDicObj) { try { m_OPCSrvName = serverName; m_OPCGroupName = groupName; //Fix - Remove opcGroup and Disconnect server to avoid GC Fianlizer thread call these methods try { if (m_OPCGroup != null) { m_OPCGroup.Remove(false); m_OPCGroup = null; } if (m_OPCServer != null) { m_OPCServer.Disconnect(); m_OPCServer = null; } } catch (Exception localException) { LogHelper.Error(CLASS_NAME, Function_Name, localException); } m_OPCServer = new OpcServer(); LogHelper.Trace(CLASS_NAME, Function_Name, "Before Connect"); m_OPCServer.Connect(serverName); LogHelper.Trace(CLASS_NAME, Function_Name, "After Connect"); Thread.Sleep(200); m_OPCServer.SetClientName("DirectOPC " + groupName + serverName); // set my client name (exe+process no) SERVERSTATUS sts; m_OPCServer.GetStatus(out sts); LogHelper.Info(CLASS_NAME, Function_Name, serverName + ": " + DateTime.FromFileTime(sts.ftStartTime).ToString()); LogHelper.Info(CLASS_NAME, Function_Name, serverName + ": " + sts.eServerState.ToString()); // add event handler for server shutdown m_OPCServer.ShutdownRequested += new ShutdownRequestEventHandler(this.OPCSrv_ServerShutDown); //Setup opc group m_OPCGroup = m_OPCServer.AddGroup("MFTOPC-Group-" + serverName, true, UPDATEGROUPRATE); m_OPCGroup.DataChanged += new DataChangeEventHandler(this.OPCGrp_DataChange); m_opcSrvConnectFlag = true; } catch (COMException localException) { LogHelper.Error(CLASS_NAME, Function_Name, localException); LogHelper.Info(CLASS_NAME, Function_Name, "Initialize of the OPC Server is not successful"); } catch (Exception localException) { LogHelper.Error(CLASS_NAME, Function_Name, localException); } } LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Exited"); }
static void Main(string[] args) { string progID = args.Length > 0 ? args[0] : "Kepware.KEPServerEX.V5"; OpcServer opcServer = new OpcServer(); opcServer.Connect(progID); System.Threading.Thread.Sleep(500); // we are faster than some servers! OpcGroup opcGroup = opcServer.AddGroup("SampleGroup", false, 900); List <string> itemNames = new List <string>(); if (args.Length > 1) { for (int i = 1; i < args.Length; i++) { itemNames.Add(args[i]); } } else { itemNames.Add("Simulation Examples.Functions.Ramp1"); itemNames.Add("Simulation Examples.Functions.Random1"); } OpcItemDefinition[] opcItemDefs = new OpcItemDefinition[itemNames.Count]; for (int i = 0; i < opcItemDefs.Length; i++) { opcItemDefs[i] = new OpcItemDefinition(itemNames[i], true, i, VarEnum.VT_EMPTY); } opcGroup.AddItems(opcItemDefs, out OpcItemResult[] opcItemResult); if (opcItemResult == null) { Console.WriteLine("Error add items - null value returned"); return; } int[] serverHandles = new int[opcItemResult.Length]; for (int i = 0; i < opcItemResult.Length; i++) { if (HRESULTS.Failed(opcItemResult[i].Error)) { Console.WriteLine("AddItems - failed {0}", itemNames[i]); opcGroup.Remove(true); opcServer.Disconnect(); return; } else { serverHandles[i] = opcItemResult[i].HandleServer; } } opcGroup.SetEnable(true); opcGroup.Active = true; bool result = opcGroup.Read(OPCDATASOURCE.OPC_DS_CACHE, serverHandles, out OpcItemState[] states); foreach (OpcItemState s in states) { if (HRESULTS.Succeeded(s.Error)) { Console.WriteLine(" {0}: {1} (Q:{2} T:{3})", s.HandleClient, s.DataValue, s.Quality, DateTime.FromFileTime(s.TimeStamp)); } else { Console.WriteLine(" {0}: ERROR = 0x{1:x} !", s.HandleClient, s.Error); } } opcGroup.Remove(true); opcServer.Disconnect(); }
public void TestErr_Remove01() { //Testing --- //Test Procedure Call group.Remove(true); }
public void Work() { theSrv = new OpcServer(); theSrv.Connect(serverProgID); Thread.Sleep(500); // we are faster then some servers! // add our only working group theGrp = theSrv.AddGroup("Group", false, 900); theTargGrp = theSrv.AddGroup("Target", false, 900); // add two items and save server handles itemDefs[0] = new OPCItemDef(itemA, true, 1, VarEnum.VT_EMPTY); itemDefs[1] = new OPCItemDef(itemB, true, 2, VarEnum.VT_EMPTY); // itemTarget[0] = new OPCItemDef(itemC, true, 1, VarEnum.VT_EMPTY); OPCItemResult[] rItm; theGrp.AddItems(itemDefs, out rItm); if (rItm == null) { return; } if (HRESULTS.Failed(rItm[0].Error) || HRESULTS.Failed(rItm[1].Error)) { AddTotextBox("OPC Tester: AddItems - some failed", textBox4); theGrp.Remove(true); theSrv.Disconnect(); return; } ; handlesSrv[0] = rItm[0].HandleServer; handlesSrv[1] = rItm[1].HandleServer; OPCItemResult[] rItmTarg; theTargGrp.AddItems(itemTarget, out rItmTarg); if (rItmTarg == null) { return; } if (HRESULTS.Failed(rItmTarg[0].Error)) { AddTotextBox("OPC Tester: AddItems - some failed", textBox4); theGrp.Remove(true); theSrv.Disconnect(); return; } ; handlesTargetSrv[0] = rItmTarg[0].HandleServer; //---------------------------------------------------------------------------------- // asynch read our two items theGrp.SetEnable(true); theGrp.Active = true; theGrp.DataChanged += new DataChangeEventHandler(this.theGrp_DataChange); theGrp.ReadCompleted += new ReadCompleteEventHandler(this.theGrp_ReadComplete); int CancelID; int[] aE; theGrp.Read(handlesSrv, 55667788, out CancelID, out aE); // some delay for asynch read-complete callback (simplification) Thread.Sleep(500); // asynch write theTargGrp.SetEnable(true); theTargGrp.Active = true; object[] itemValues = new object[1]; itemValues[0] = (int)450; theTargGrp.WriteCompleted += new WriteCompleteEventHandler(this.theGrp_WriteComplete); theTargGrp.Write(handlesTargetSrv, itemValues, 99887766, out CancelID, out aE); // some delay for asynch write-complete callback (simplification) Thread.Sleep(500); theGrp.DataChanged -= new DataChangeEventHandler(this.theGrp_DataChange); theGrp.ReadCompleted -= new ReadCompleteEventHandler(this.theGrp_ReadComplete); theGrp.WriteCompleted -= new WriteCompleteEventHandler(this.theGrp_WriteComplete); }
public void PLCWrite() { try { theSrv = new OpcServer(); theSrv.Connect(serverProgID); Thread.Sleep(500); // we are faster then some servers! //add our only working group theGrp = theSrv.AddGroup("Group", false, 900); //add two items and save server handles itemDefs[0] = new OPCItemDef(itemA, true, 1, VarEnum.VT_EMPTY); itemDefs[1] = new OPCItemDef(itemB, true, 2, VarEnum.VT_EMPTY); OPCItemResult[] rItm; theGrp.AddItems(itemDefs, out rItm); if (rItm == null) { return; } if (HRESULTS.Failed(rItm[0].Error) || HRESULTS.Failed(rItm[1].Error)) { AddTotextBox("OPC Tester: AddItems - some failed", textBox4); theGrp.Remove(true); theSrv.Disconnect(); return; } ; if (handlesSrv[0] == 0) { handlesSrv[0] = rItm[0].HandleServer; handlesSrv[1] = rItm[1].HandleServer; } // asynch read our two items theGrp.SetEnable(true); theGrp.Active = true; theGrp.DataChanged += new DataChangeEventHandler(this.theGrp_DataChange); theGrp.ReadCompleted += new ReadCompleteEventHandler(this.theGrp_ReadComplete); int CancelID; int[] aE; Random rnd = new Random(); int transactionID = rnd.Next(1, 55667788); theGrp.Read(handlesSrv, transactionID, out CancelID, out aE); // some delay for asynch read-complete callback (simplification) Thread.Sleep(500); theGrp.DataChanged -= new DataChangeEventHandler(this.theGrp_DataChange); theGrp.ReadCompleted -= new ReadCompleteEventHandler(this.theGrp_ReadComplete); //theGrp.WriteCompleted -= new WriteCompleteEventHandler(this.theGrp_WriteComplete); //theGrp.RemoveItems(handlesSrv, out aE); //теперь здесь //theGrp.Remove(false); } catch (Exception e) { SetText("Unexpected Error:" + e.Message); return; } }