public void PLCRead() { try { // 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; } }
private void CheckGroupActive_CheckedChanged(object sender, EventArgs e) { if (theGroup != null) { if (CheckGroupActive.Checked) { theGroup.SetEnable(true); theGroup.Active = true; } else { theGroup.SetEnable(true); theGroup.Active = false; } } }
private void cmdAddGroup_Click(object sender, EventArgs e) { theGroup = theServer.AddGroup(txtGroup.Text, false, 900); if (CheckGroupActive.Checked) { theGroup.SetEnable(true); theGroup.Active = true; } else { theGroup.SetEnable(true); theGroup.Active = false; } theGroup.DataChanged += new DataChangeEventHandler(TheGrp_DataChanged); theGroup.ReadCompleted += new ReadCompleteEventHandler(TheGrp_ReadCompleted); theGroup.WriteCompleted += new WriteCompleteEventHandler(TheGrp_WriteCompleted); cmdAddGroup.Enabled = false; cmdDisconnect.Enabled = false; cmdRemGroup.Enabled = true; cmdAddItem.Enabled = true; }
public bool connectServer() { try { theSrv = new OpcServer(); theSrv.Connect(ServerProgID); TheGrp = theSrv.AddGroup("S7_200_01", false, 900); TheGrp.SetEnable(true); TheGrp.Active = true; return(true); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("连接服务器出错:" + e.ToString()); return(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; * } */ }
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(); }
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(); }
//[MethodImplAttribute(MethodImplOptions.Synchronized)]//Синхронизировать метод private bool Connect() { lock (lockConn) { DateTime dt1 = DateTime.Now; try { bool result; //создадим соединение с БД SERVERSTATUS status; _opcsrv.GetStatus(out status); if (_cntconn == 0 || status == null || status.eServerState != OPCSERVERSTATE.OPC_STATUS_RUNNING) { _opcsrv.Connect(_progid, _host); _opcsrv.GetStatus(out status); if (status != null && status.eServerState == OPCSERVERSTATE.OPC_STATUS_RUNNING) { try { _opcgrp = _opcsrv.AddGroup(_grpname, false, _interval); _opcgrp.HandleClient = 0; int _cnt = _maps.GetLength(0); _serverhandles = new int[_cnt]; _vals = new object[_cnt]; _errs = new object[_cnt]; OPCItemDef[] _opcitmsdef = new OPCItemDef[_cnt]; for (int i = 0; i < _cnt; i++) { _opcitmsdef[i] = new OPCItemDef(_maps[i, 1], true, i, VarEnum.VT_EMPTY); } _opcgrp.AddItems(_opcitmsdef, out _opcitms); for (int i = 0; i < _cnt; i++) { _serverhandles[i] = _opcitms[i].HandleServer; } _opcgrp.SetEnable(true); _opcgrp.Active = true; _server.LogWrite(LogType.DEBUG, "[" + this._name + "] : T(сек)=" + (DateTime.Now - dt1).Seconds + ", нап. груп. - " + _maps.GetLength(0)); _cntconn++; //Увеличим счётчик активных соединений _server.LogWrite(LogType.DEBUG, "[" + this._name + "] : Connect - Активных соединений: " + _cntconn); result = true; } catch (Exception e) { _server.LogWrite(LogType.ERROR, e.Message); result = false; } } else { result = false; } } else { _cntconn++; //Увеличим счётчик активных соединений result = true; } return(result); } finally { _server.LogWrite(LogType.DEBUG, "[" + this._name + "] : T(сек)=" + (DateTime.Now - dt1).Seconds + ", время на откытие соединения"); } } }
public void TestSetEnable01() { //Testing --- //Test Procedure Call group.SetEnable(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; } }