Click here to Skip to main content
12,751,690 members (33,222 online)
Click here to Skip to main content
Add your own
alternative version

Stats

145.6K views
3.9K downloads
38 bookmarked
Posted 16 Feb 2004

OPC XML-DA Sample Clients

, 25 Apr 2007
Rate this:
Please Sign up or sign in to vote.
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.

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

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()
   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 Java scripts 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 = "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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionXML DA regarding OPC Xi Pin
alex_dacosta8-Feb-10 22:42
memberalex_dacosta8-Feb-10 22:42 
AnswerRe: XML DA regarding OPC Xi Pin
Advosol Inc.10-Feb-10 14:53
memberAdvosol Inc.10-Feb-10 14:53 
GeneralRe: XML DA regarding OPC Xi Pin
xxxhw10-Mar-10 5:20
memberxxxhw10-Mar-10 5:20 
GeneralRe: XML DA regarding OPC Xi Pin
Advosol Inc.10-Mar-10 6:00
memberAdvosol Inc.10-Mar-10 6:00 
GeneralRe: XML DA regarding OPC Xi Pin
xxxhw11-Mar-10 5:23
memberxxxhw11-Mar-10 5:23 
GeneralRe: XML DA regarding OPC Xi Pin
Advosol Inc.14-Mar-10 17:41
memberAdvosol Inc.14-Mar-10 17:41 
GeneralRe: XML DA regarding OPC Xi Pin
xxxhw15-Mar-10 2:09
memberxxxhw15-Mar-10 2:09 
QuestionHow to start developing OPC Server in .Net Pin
Sudhir Chawla28-Nov-07 18:32
memberSudhir Chawla28-Nov-07 18:32 
AnswerRe: How to start developing OPC Server in .Net Pin
Advosol Inc.29-Nov-07 7:04
memberAdvosol Inc.29-Nov-07 7:04 
Generalfree OPC-server sourcen Pin
MessnerEW23-Feb-05 5:25
memberMessnerEW23-Feb-05 5:25 
GeneralRe: free OPC-server sourcen Pin
kh1723-Feb-05 6:09
susskh1723-Feb-05 6:09 
GeneralOPC DA Server Pin
kkmanna23-Mar-04 21:13
memberkkmanna23-Mar-04 21:13 
GeneralRe: OPC DA Server Pin
<font face=5-Apr-04 3:00
suss5-Apr-04 3:00 
GeneralRe: OPC DA Server Pin
NielsR6-May-04 22:37
memberNielsR6-May-04 22:37 
GeneralRe: OPC DA Server Pin
NielsR6-May-04 22:06
memberNielsR6-May-04 22:06 
GeneralRe: OPC DA Server Pin
<font face=18-Jan-05 1:30
suss18-Jan-05 1: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170215.1 | Last Updated 25 Apr 2007
Article Copyright 2004 by Advosol Inc.
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid