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.
As we discussed in the last post on integrating XML data, there are many reasons that you might need to access and share data in an XML format (whether it is in an actual file or simply a data stream formatted as XML). XML is commonly used to quickly initialize data points in a PLC or other device. Other applications might include reading and integrating stored configuration information for devices and software or simply accessing process data that some devices provide in an XML format.
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.
Integrating XML data with OPC servers in a custom application
For many organizations, it is common practice to develop custom utilities or applications with a specific set of functionality required for a project or for use by the entire organization. This is typically due to an off-the-shelf solution either not being available or not being flexible enough to meet the needs of the project (or possibly being outside the budget for the project).
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.
How to use OPC Data Client for XML data integration
1. Defining a Class to match your XML file data format
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:
This "Machine" class then becomes the model used to deserialize the XML data (file or stream). Each member of this class corresponds to an XML tag in the XML data example above.
2. Processing and Deserializing XML data with your code
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.
In this example, a machine is initialized, the member assignment is done, and then the resulting machine is added to the MachineSetPoint list of machines.
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 this particular example, you may notice that the writes are being issued one at a time for each attribute but it is perfectly feasible to aggregate all of the values first and then issue a single write request using the "WriteMultipleValues" method, if that meets the needs of your project more effectively.
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?