Get OPC data into Python
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.
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('http://opcua.demo-this.com:51211/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('http://opcua.demo-this.com:51211/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)
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.