Click here to Skip to main content
14,827,196 members
Articles / Web Development / HTML
Posted 16 Feb 2004


39 bookmarked

OPC XML-DA Sample Clients

Rate me:
Please Sign up or sign in to vote.
3.60/5 (9 votes)
25 Apr 20072 min read
The OPC XML-DA sample clients show how OPC XML-DA web services can be accessed from different kinds of desktop and web applications.

Sample Image - OPC_XML-DA_Clients.jpg


The OPC XML-DA standard and the currently widely used OPC DA V2 standard address the same tasks but the server interface is quite different, due to their different base, web services respectively DCOM. Developers of OPC client applications have to be aware of these differences. The sample client applications show how the Read and Subscribe/Poll functions are used from different kinds of applications. The VB.NET and C# samples use the .NET web services support, the HTML/ASP scripts use the MSSOAP toolkit.


Read the XMLDA.NET White Paper for a description of how web services differ from DCOM and how this influences the design of client applications. Evaluation versions of .NET based OPC XML DA servers a clients can be downloaded.

Using the Code

VB and C# applications typically use the .NET features to access web services. Desktop and ASP.NET applications can access OPC XML-DA servers in the same way. XML-DA has a stateless design that fits well into ASP.NET applications. The code snippet shown below uses the Read function to periodically read the value of two items from the server. The XML-DA Read function is self contained and not dependent on any previous calls.

' create the server object and set the call options
Dim Srv As localhost.OpcXmlDA = New localhost.OpcXmlDA
Srv.Url = ""
Dim ReqOptions As localhost.RequestOptions = New localhost.RequestOptions
ReqOptions.ClientRequestHandle = "test"
ReqOptions.ReturnErrorText = True
ReqOptions.ReturnItemName = True

' Data structures for an XML-DA Read call, initialized to read 2 items
Dim ReadReqList As localhost.ReadRequestItemList = New ReadRequestItemList
ReDim ReadReqList.Items(1)    ' Item request list for 2 items
ReadReqList.Items(0) = New localhost.ReadRequestItem       ' item 1
ReadReqList.Items(0).ItemName = "Test/Integer"
ReadReqList.Items(1) = New localhost.ReadRequestItem       ' item 2
ReadReqList.Items(1).ItemName = "Test/Float"

' Data structures for the read reply data
Dim reply As localhost.ReplyBase
Dim ReadReplyList As localhost.ReplyItemList
Dim Errors() As localhost.OPCError

While 1 = 1            ' periodically read the items
  Try                  ' access the server
    reply = Srv.Read(ReqOptions, ReadReqList, ReadReplyList, Errors)

    '  display the result
    Dim i As Int32
    For i = 0 To ReadReplyList.Items.Length - 1
      If ReadReplyList.Items(i).ResultID Is Nothing Then  ' display value
        Console.WriteLine(ReadReplyList.Items(i).ItemName + " = " + _
      Else                               ' display error code
        Console.WriteLine(ReadReplyList.Items(i).ItemName + " = " + _
      End If
  Catch ex As Exception
          Console.WriteLine("Error:  " + ex.Message)
  End Try

End While

Optional parameters are a source for common mistakes. The .NET web services interface has an 'enable' parameter with each optional value type parameter. If this parameter is not set to 'true', then setting the optional parameter has no effect.

subscription.EnableBuffering = True
subscription.EnableBufferingSpecified = True

The usage of the XML-DA functions is quite simple but initializing the request lists with the required definitions for hundreds of items need quite a bit of code. In applications with many items, it is better to define the request lists in an XML file and in the application only call a helper method that initializes the request list object from the XML file. Advosol's XML-DA Client Component offers such support. The code snippet below shows how a read call is done using the SDK helper classes. Download the XML-DA Client SDK evaluation version to explore this further.

Private Sub btnRead_Click_1(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) Handles btnRead.Click
   Dim srv As XmlServer = New XmlServer(cbURL.Text)   ' server object
   Dim readItemList As ReadRequestItemList
   readItemList = embeddedList.GetReadItemList("OCSTKSimulatedData")
   Dim reply As ReplyBase
   Dim options As RequestOptions = New RequestOptions
   options.ReturnErrorText = True
   options.ReturnItemName = True
   Dim rslt As ReplyItemList
   Dim err As OPCError()
      reply = srv.Read(options, readItemList, rslt, err)
      If rslt Is Nothing Then
         MessageBox.Show(err(0).Text, "Error at Read")
         Dim txt As String = ""
         Dim iv As ItemValue
         For Each iv In rslt.Items
            txt += iv.ItemName
            If iv.ResultID Is Nothing Then ' success
               txt += "   = " + iv.Value.ToString() + Environment.NewLine
               txt += " : Error: " + iv.ResultID.Name + Environment.NewLine
            End If
         tbItems.Text = txt
      End If
   Catch ex As Exception
      MessageBox.Show(ex.Message, "Exception in Read")
   End Try
End Sub

HTML and ASP applications need to make the OPC XML-DA server access from client-side respectively server-side scripts. The source code download contains samples with JavaScripts and VB scripts. They show how to use the Read and the more complex but also more efficient Subscribe/Poll functions. The XML-DA web service call is made by building the SOAP request message using the MSSOAP toolkit. The code snippet shows how a Read request message is created.

function CreateXMLDAReadRequest(ServerURL)
    NSxmlda = "";
    var mobjWS = new ActiveXObject("MSSOAP.SoapClient30");
    ErrText = "";
    if( ! mobjWS )
        ErrText = "SoapClient NOT created";

    mobjWS.ClientProperty("ServerHTTPRequest") = "true";
    mobjWS.MSSoapInit( ServerURL + "?wsdl", "OpcXmlDaSrv", "OpcXmlDaSrvSoap" );

    //-------------------- We are going to build the SOAP request message
    var oTempXml;
    var strXML;
    DocXml = new ActiveXObject("msxml2.domdocument.4.0");
    if( ! DocXml )
        ErrText = "DomDocument NOT created" ;

    strXML = "";
    DocXml.loadXML( strXML );
    oTempXml = DocXml.createElement("Read");
    oTempXml.setAttribute( "xmlns", NSxmlda );
    // create Options Node
    DocXml.documentElement.childNodes(0).appendChild( oTempXml );
    oTempXml = DocXml.createNode(1, "Options", NSxmlda );
    oTempXml.setAttribute( "ReturnErrorText", "true" );
    oTempXml.setAttribute( "ReturnItemTime", "true" );
    oTempXml.setAttribute( "ReturnItemName", "true" );
    oTempXml.setAttribute( "LocaleID", "en" );
    // create ItemList node
    ItemListNode= DocXml.createNode(1, "ItemList", NSxmlda);
    DocXml.selectSingleNode("//Read").appendChild( ItemListNode );
    DocXml.setProperty("SelectionNamespaces", "xmlns:xda='" + NSxmlda + "'");
    return DocXml ;


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


About the Author

Advosol Inc.
Web Developer
United States United States
No Biography provided

Comments and Discussions

QuestionXML DA regarding OPC Xi Pin
alex_dacosta8-Feb-10 21:42
Memberalex_dacosta8-Feb-10 21:42 
AnswerRe: XML DA regarding OPC Xi Pin
Advosol Inc.10-Feb-10 13:53
MemberAdvosol Inc.10-Feb-10 13:53 
GeneralRe: XML DA regarding OPC Xi Pin
xxxhw10-Mar-10 4:20
Memberxxxhw10-Mar-10 4:20 
GeneralRe: XML DA regarding OPC Xi Pin
Advosol Inc.10-Mar-10 5:00
MemberAdvosol Inc.10-Mar-10 5:00 
GeneralRe: XML DA regarding OPC Xi Pin
xxxhw11-Mar-10 4:23
Memberxxxhw11-Mar-10 4:23 
GeneralRe: XML DA regarding OPC Xi Pin
Advosol Inc.14-Mar-10 16:41
MemberAdvosol Inc.14-Mar-10 16:41 
GeneralRe: XML DA regarding OPC Xi Pin
xxxhw15-Mar-10 1:09
Memberxxxhw15-Mar-10 1:09 
QuestionHow to start developing OPC Server in .Net Pin
Sudhir Chawla28-Nov-07 17:32
MemberSudhir Chawla28-Nov-07 17:32 
AnswerRe: How to start developing OPC Server in .Net Pin
Advosol Inc.29-Nov-07 6:04
MemberAdvosol Inc.29-Nov-07 6:04 
Generalfree OPC-server sourcen Pin
ThGeyer23-Feb-05 4:25
MemberThGeyer23-Feb-05 4:25 
GeneralRe: free OPC-server sourcen Pin
Advosol Inc.23-Feb-05 5:09
MemberAdvosol Inc.23-Feb-05 5:09 
GeneralOPC DA Server Pin
K K Manna23-Mar-04 20:13
MemberK K Manna23-Mar-04 20:13 
GeneralRe: OPC DA Server Pin
.Suchit5-Apr-04 2:00
Member.Suchit5-Apr-04 2:00 
GeneralRe: OPC DA Server Pin
NielsR6-May-04 21:37
MemberNielsR6-May-04 21:37 
GeneralRe: OPC DA Server Pin
NielsR6-May-04 21:06
MemberNielsR6-May-04 21:06 
GeneralRe: OPC DA Server Pin
.Suchit18-Jan-05 0:30
Member.Suchit18-Jan-05 0:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.