OPCDA.NET Reference Manual
C# sample code that reads an item from an OPC server
Send comments on this topic.
OPCDA.NET Client Component > Getting Started > C# sample code that reads an item from an OPC server

Glossary Item Box

Using the QuickUse Classes Using the OpcGroup Class

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 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 ; } } }

 

See the Controls chapter for sample application code based on the DAServer/DAGroup Visual Studio controls.


A number of sample client applications including the Visual Studio project are in the OPCDA.NET installation sub-folder C# Sample Clients.

If you don't find this folder then probably the sample installation option was not selected when OPCDA.NET was installed.

 

The Visual Studio project wizard creates a Windows Form application with sample code according the selections in the wizard dialog. It may be quicker to create a new project with the required features than search for a matching sample application.

If you don't find the OPCDA.NET client template in the Visual Studio Add New Project template selection then probably the C# wizard installation option was not selected when OPCDA.NET was installed.

Copyright © 2002-2018 Advosol Inc. All Rights Reserved.