Click here to Skip to main content
15,886,137 members
Articles / Web Development / HTML

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 181.1K   4.5K   39   16
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

Introduction

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.

Background

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.

VB
' create the server object and set the call options
Dim Srv As localhost.OpcXmlDA = New localhost.OpcXmlDA
Srv.Url = "http://advosol.us/XMLDADemo/ts_sim/OpcDaGateway.asmx"
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
    Console.WriteLine()
    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 + " = " + _
        Convert.ToString(ReadReplyList.Items(i).Value))
      Else                               ' display error code
        Console.WriteLine(ReadReplyList.Items(i).ItemName + " = " + _
        ReadReplyList.Items(i).ResultID.Name)
      End If
    Next
  Catch ex As Exception
          Console.WriteLine("Error:  " + ex.Message)
  End Try

  Threading.Thread.Sleep(2000)
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.

VB
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.

VB
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()
   Try
      reply = srv.Read(options, readItemList, rslt, err)
      If rslt Is Nothing Then
         MessageBox.Show(err(0).Text, "Error at Read")
      Else
         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
            Else
               txt += " : Error: " + iv.ResultID.Name + Environment.NewLine
            End If
         Next
         tbItems.Text = txt
      End If
   Catch ex As Exception
      MessageBox.Show(ex.Message, "Exception in Read")
      Close()
   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.

JavaScript
function CreateXMLDAReadRequest(ServerURL)
{
    NSxmlda = "http://opcfoundation.org/webservices/XMLDA/1.0/";
    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" );
    DocXml.selectSingleNode("//Read").appendChild(oTempXml);
    // create ItemList node
    ItemListNode= DocXml.createNode(1, "ItemList", NSxmlda);
    DocXml.selectSingleNode("//Read").appendChild( ItemListNode );
    
    DocXml.setProperty("SelectionNamespaces", "xmlns:xda='" + NSxmlda + "'");
    return DocXml ;
}

License

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.


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionXML DA regarding OPC Xi Pin
alex_dacosta8-Feb-10 21:42
alex_dacosta8-Feb-10 21:42 
AnswerRe: XML DA regarding OPC Xi Pin
Advosol Inc.10-Feb-10 13:53
Advosol Inc.10-Feb-10 13:53 
GeneralRe: XML DA regarding OPC Xi Pin
xxxhw10-Mar-10 4:20
xxxhw10-Mar-10 4:20 
GeneralRe: XML DA regarding OPC Xi Pin
Advosol Inc.10-Mar-10 5:00
Advosol Inc.10-Mar-10 5:00 
GeneralRe: XML DA regarding OPC Xi Pin
xxxhw11-Mar-10 4:23
xxxhw11-Mar-10 4:23 
GeneralRe: XML DA regarding OPC Xi Pin
Advosol Inc.14-Mar-10 16:41
Advosol Inc.14-Mar-10 16:41 
GeneralRe: XML DA regarding OPC Xi Pin
xxxhw15-Mar-10 1:09
xxxhw15-Mar-10 1:09 
QuestionHow to start developing OPC Server in .Net Pin
Sudhir Chawla28-Nov-07 17:32
Sudhir Chawla28-Nov-07 17:32 
AnswerRe: How to start developing OPC Server in .Net Pin
Advosol Inc.29-Nov-07 6:04
Advosol Inc.29-Nov-07 6:04 
Generalfree OPC-server sourcen Pin
MGutmann23-Feb-05 4:25
MGutmann23-Feb-05 4:25 
GeneralRe: free OPC-server sourcen Pin
Advosol Inc.23-Feb-05 5:09
Advosol Inc.23-Feb-05 5:09 
GeneralOPC DA Server Pin
K K Manna23-Mar-04 20:13
K K Manna23-Mar-04 20:13 
GeneralRe: OPC DA Server Pin
.Suchit5-Apr-04 2:00
.Suchit5-Apr-04 2:00 
GeneralRe: OPC DA Server Pin
NielsR6-May-04 21:37
NielsR6-May-04 21:37 
GeneralRe: OPC DA Server Pin
NielsR6-May-04 21:06
NielsR6-May-04 21:06 
GeneralRe: OPC DA Server Pin
.Suchit18-Jan-05 0:30
.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.