If you read our last post on sharing XML data, you may recall that our tech support engineers are regularly asked how to take data provided in an XML (eXtensible Markup Language) format (file or data stream) and share it with other systems such as OPC clients or servers, databases, devices or even the cloud.
In this Integrating XML Data sub-topic of our continuing Tech Support Corner blog series, this post covers how to easily integrate XML data with OPC systems when designing a custom developed application.
And it's common for web-based applications to exchange data using an XML payload format so that would be another use case. Regardless of what data and where it's coming from, the ultimate question is what you want to do with it once you have accessed it.
This post is concerned specifically with the need to take XML data and write it to an OPC server from a custom developed application.
Regardless of the core reasons, you have a development team working on some form of custom application. You discover that one of the functional requirements for this application is that it be able to consume XML data and write that data to an OPC server (it could be for any of the reasons we discussed above or for reasons wholly unique to your organization).
As a developer, you know that parsing XML data is relatively standard when using .NET (or most other development environments, for that matter). As such, the focus of this post is on the other requirement - acting as an OPC client to the OPC servers that need access to that XML data.
Building an OPC client interface from scratch is a non-trivial undertaking. And if you want to have your application support more than one OPC standard (such as OPC UA and OPC DA), this is even more true.
The most efficient and effective method is using an OPC client development toolkit. This makes it relatively easy for any developer to add an OPC client interface without having to learn, understand and implement the entire communication stack - the efficiency is increased when adding support for multiple OPC standards.
This approach frees up the bulk of a developer's time and energy to focus on the code for accessing and processing the XML data instead.
Utilizing the OPC Data Client toolkit, such a custom OPC client can be quickly created to consume your XML data and write it to an OPC UA, DA, AE or XML-DA server. OPC Data Client even installs with sample code that can be used as a good starting place. You can also download the sample code for the example solution referenced in this post below.
This first step in integrating XML data with a custom application is creating a Class with Fields matching the attributes and sub-elements from the format in your XML file or data stream. For example, the following XML file has a main Element called "MachineSetPoints" with a defined format of attributes and sub-elements for each machine. Any number of machines following the same standard format could be accessed using this method.
So crafting a Class based on that example file format looks like the following:
The next step is using the "Machine" class for processing and deserializing the model. Element values need to be assigned to each member of the class. To this end, the System.XML.Linq.XDocument.Load() method provides an easy way of handling this deserialization to the Machine model, among other methods that we won't cover in this post.
And, finally, you need to loop through the list of machines defined in the XML data and set points then issue your write command to the underlying OPC server (for this example, we're writing to an OPC UA server, but the method is similar for OPC DA or the other supported OPC standards supported by OPC Data Client).
In the "Using" statement referenced above, an OPC Data Client EasyUAClient object is instantiated, which controls the client connection to the OPC UA server. The OPC UA endpoint information itself is passed as part of the Write Statement itself – within the try-catch block.
So, to close, the OPC Data Client makes the OPC side of the operation straightforward so you can focus on the XML parsing and other coding required for your custom applications without having to dive into the low-level details of OPC communications. If you haven't already tried the OPC Data Client, the free trial version is available here.
And don't forget to subscribe to our blog to not only be notified of our other XML Integration posts as they are available but to find out about more relevant technical topics applying to industrial process automation and solving related challenges. Want to try integrating your own XML data using the OPC Data Client?