Get OPC data into Python
With QuickOPC, you can integrate OPC functionality into your Python programs, or create dedicated OPC Python solutions.
How does QuickOPC allow integration of OPC data into Python?
- QuickOPC provides .NET objects with easy interfaces to perform all kinds of OPC tasks.
- Using Python.NET package, we expose the QuickOPC objects to your Python code.
- In Python, you instantiate one of the main QuickOPC objects.
- You call Python methods that perform operations such as OPC reading or writing.
The Python support in QuickOPC gives you access to most of the QuickOPC features. You can use OPC DA, OPC A&E, OPC XML-DA, or OPC UA (Client-Server or PubSub), including OPC UA Alarms & Conditions. Specialized client objects, such as that for OPC UA File Transfer, or for OPC UA Certificate Management, and OPC UA Complex Data support, are available. In Windows desktop applications, you can also invoke OPC browsing dialogs.
QuickOPC supports Python development of OPC clients and subscribers for Windows and Linux.
Useful links: Getting Started with OPC in Python / Python examples in the Documentation / Python examples GitHub repository / Python in Knowledge Base
The Python packages (opclabs_quickopc etc.) are available on the Python Package Index (pypi.org).
OPC Python Example
If you want see it for yourself, we have an example that shows how to read a value of an OPC item in Python and print it out. It is just a few lines of code, and with an OPC Data Access server it looks like this:
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.OperationModel import * # Instantiate the client object client = EasyDAClient() # Perform the operation value = IEasyDAClientExtension.ReadItemValue(client, '', 'OPCLabs.KitServer.2', 'Demo.Single') # Display results print('value:', value)
The example connects to Simulation OPC Server, but you can quickly modify it to connect to your OPC server instead. Simply download the product and then open the project.
You can also connect to OPC Unified Architecture (OPC UA) servers, and the code is just as simple:
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.EasyOpc.UA import * from OpcLabs.EasyOpc.UA.OperationModel import * endpointDescriptor = UAEndpointDescriptor('opc.tcp://opcua.demo-this.com:51210/UA/SampleServer') # Instantiate the client object. client = EasyUAClient() # Perform the operation. value = IEasyUAClientExtension.ReadValue(client, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853')) # Display results. print('value:', value)
Besides "one-shot" operations such as reads and writes, you can also set up subscriptions, and receive data change and even event (Alarms & Conditions) notifications:
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from OpcLabs.EasyOpc.UA import * from OpcLabs.EasyOpc.UA.OperationModel import * def dataChangeNotification(sender, e): print(e) endpointDescriptor = UAEndpointDescriptor('opc.tcp://opcua.demo-this.com:51210/UA/SampleServer') # Instantiate the client object and hook events. client = EasyUAClient() client.DataChangeNotification += dataChangeNotification print('Subscribing...') IEasyUAClientExtension.SubscribeDataChange(client, endpointDescriptor, UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'), 1000) print('Processing data change events for 20 seconds...') time.sleep(20)
OPC UA PubSub
The PubSub variety of OPC UA (as opposed to client-server) uses message-oriented middleware to deliver the data. QuickOPC supports it as well, as shown in the example below.
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc import time # Import .NET namespaces. from OpcLabs.EasyOpc.UA.PubSub import * from OpcLabs.EasyOpc.UA.PubSub.OperationModel import * def dataSetMessage(sender, e): print(e) # Define the PubSub connection we will work with. pubSubConnectionDescriptor = UAPubSubConnectionDescriptor.op_Implicit('opc.udp://239.0.0.1') # Instantiate the subscriber object and hook events. subscriber = EasyUASubscriber() subscriber.DataSetMessage += dataSetMessage print('Subscribing...') IEasyUASubscriberExtension.SubscribeDataSet(subscriber, pubSubConnectionDescriptor) print('Processing dataset message events for 20 seconds...') time.sleep(20)
Community Use
We have discovered that QuickOPC is being used by an Open Source project smap-data (smap -- Simple Measurement and Actuation Profile). Nice work!
Footnote & required disclosure: QuickOPC (including its Options) is a software development kit (SDK) for development of OPC clients and subscribers. Installing QuickOPC or its Options does not change system settings.