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 COM (OLE Automation) objects with easy interfaces to perform all kinds of OPC tasks
- You instantiate one of the main QuickOPC objects.
- You call methods that perform operations such as OPC reading or writing.
Useful links: Examples / Knowledge Base
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:
import win32com.client # Instantiate the client object client = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.EasyDAClient') # Perform the operation value = client.ReadItemValue('', '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:
import win32com.client # Instantiate the client object client = win32com.client.Dispatch('OpcLabs.EasyOpc.UA.EasyUAClient') # Perform the operation value = client.ReadValue('opc.tcp://opcua.demo-this.com:51210/UA/SampleServer',
'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:
import time import win32com.client # Instantiate the client object client = win32com.client.Dispatch('OpcLabs.EasyOpc.UA.EasyUAClient') client.PullDataChangeNotificationQueueCapacity = 1000 print('Subscribing...') client.SubscribeDataChange('opc.tcp://opcua.demo-this.com:51210/UA/SampleServer',
'nsu=http://test.org/UA/Data/;i=10853', 1000) print('Processing data change events for 1 minute...') endTime = time.time() + 60 while time.time() < endTime: eventArgs = client.PullDataChangeNotification(2*1000) if eventArgs is not None: # Handle the notification event print(eventArgs)
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.
# Define the PubSub connection we will work with.
subscribeDataSetArguments = win32com.client.Dispatch('OpcLabs.EasyOpc.UA.PubSub.OperationModel.EasyUASubscribeDataSetArguments')
connectionDescriptor = subscribeDataSetArguments.DataSetSubscriptionDescriptor.ConnectionDescriptor
connectionDescriptor.ResourceAddress.ResourceDescriptor.UrlString = 'opc.udp://239.0.0.1'
# Instantiate the subscriber object.
subscriber = win32com.client.Dispatch('OpcLabs.EasyOpc.UA.PubSub.EasyUASubscriber')
subscriber.PullDataSetMessageQueueCapacity = 1000
subscriber.SubscribeDataSet(subscribeDataSetArguments)
print('Processing dataset message events for 20 seconds...')
endTime = time.time() + 20
while time.time() < endTime:
eventArgs = subscriber.PullDataSetMessage(2*1000)
if eventArgs is not None:
print(eventArgs)
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. Installing QuickOPC or its Options does not change system settings.