1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using OPC; using OPCDA; using OPCDA.NET; namespace OpcDemo { public class Demo { string ServerAccess() { OpcServer Srv = new OpcServer() ; int rtc = Srv.Connect( "Advosol.SimDAServer.1" ); if( HRESULTS.Failed( rtc ) return Srv.GetErrorString( rtc ); SyncIOGroup srwGroup = new SyncIOGroup( Srv ) ; OPCItemState Rslt ; int rtc = srwGroup.Read( OPCDATASOURCE.OPC_DS_CACHE, "SimulatedData.Step", out Rslt ); string retVal; if( HRESULTS.Failed(rtc) ) retVal = "Read error" ; else retVal = "data="+ Rslt.DataValue.ToString(); srwGroup.Dispose(); Srv.Disconnect(); return retVal ; } } |
Using the OpcGroup Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; using System.Runtime.InteropServices; using OPC; using OPCDA; using OPCDA.NET; namespace VCSSampleClient { public class DemoOpc { public string ServerAccessOpc() { OpcServer Srv = new OpcServer() ; int rtc = Srv.Connect( "Advosol.SimDAServer.1" ); if( HRESULTS.Failed( rtc ) return Srv.GetErrorString( rtc ); OpcGroup oGrp ; float deadBand = 0.0F; try { oGrp = Srv.AddGroup( "Grp1", true, 500, ref deadBand, 0, 0 ); catch( Exception ex ) { Srv.Disconnect(); return ex.Message ; } OPCItemDef[] items = new OPCItemDef[1]; items[0] = new OPCItemDef( "SimulatedData.Step", true, 0, VarEnum.VT_BSTR ); OPCItemResult[] addRslt = new OPCItemResult[1]; int rtc = oGrp.AddItems( items, out addRslt ) ; if( HRESULTS.Failed(rtc) ) { Srv.Disconnect(); return "Error at AddItem" ; } int[] iHnd = new Int32[1]; iHnd[0] = addRslt[0].HandleServer ; OPCItemState[] rslt ; rtc = oGrp.Read( OPCDATASOURCE.OPC_DS_CACHE, iHnd, out rslt ); string retVal; if( HRESULTS.Failed(rtc) ) retVal = "Read error" ; else if( HRESULTS.Failed(rslt[0].Error) ) retVal = "Item Read error" ; else retVal = "data="+ rslt[0].DataValue.ToString(); oGrp.Remove(true); Srv.Disconnect(); return retVal ; } } } |
from : http://advosol.us/manuals/opcdanet/topic6.html