QuickOPC.NET: How to log OPC Data Access item changes into an XML file

From OPC Labs Knowledge Base
Revision as of 14:32, 31 July 2016 by User (talk | contribs) (Created page with "The example below subscribes to one process value (OPC item), and logs its changes to an XML file. There are three factors that make the code very short: * You can use anonym...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The example below subscribes to one process value (OPC item), and logs its changes to an XML file.

There are three factors that make the code very short:

  • You can use anonymous methods (or lambdas) to specify the handler (callback) for item changes.
  • The event arguments that are passed to the event handler or callback are directly serializable to XML (same as with most QuickOPC objects).
  • There is no need for reconnection logic (more about this further down in this post).


The output file generated by the logger (OpcData.xml) is listed further below.

What makes this example quite nice is the fact it is is fairly complete - it is fully resistant against network problems, OPC server crashes etc. You do not need to write any additional code for that! To prove it, increase the logging time in the code to e.g. 10 minutes, start the logger, and then find and select the OPC Simulation Server process (opcrtkit.exe) in the Task Manager, and "kill it" by pressing the "End process" button. The logger will log an en exception record into the output XML. If you, however, wait for some minutes, you will see that the server process restarts itself (this is caused by the reconnection facility in the QuickOPC component), and valid values will be outputted to the output XML again. The reconnection delays can be influenced by setting various properties on EasyDAClient object.

This example project is included with the product. Please use the example from the product itself for the most up-to-date code of the example.

There is also an example that shows logging of OPC Alarms and Events into XML – in a separate application note.

OpcData.xml: