- Posts: 6
- Thank you received: 1
Online Forums
Technical support is provided through Support Forums below. Anybody can view them; you need to Register/Login to our site (see links in upper right corner) in order to Post questions. You do not have to be a licensed user of our product.
Please read Rules for forum posts before reporting your issue or asking a question. OPC Labs team is actively monitoring the forums, and replies as soon as possible. Various technical information can also be found in our Knowledge Base. For your convenience, we have also assembled a Frequently Asked Questions page.
Do not use the Contact page for technical issues.
Using Python with Quick OPC
Please Log in or Create an account to join the conversation.
This is doable, but we do not have ready-made example for that in Python. The number of examples theoretically needed is huge (because there are many tools an languages that can use QuickOPC), so we do not have them all. But the list keeps growing.
OPC XML-DA is handled by the same object as OPC DA "Classic" in QuickOPC; the difference is just in the "server descriptor" which for COM/DCOM normally contains the computer name and ProgID, but for OPC XML-DA it contains the URL of the server endpoint.
So the code needs to be made by combining knowledge from multiple related examples (and not all of them are in Python but they show the idea). I will to to send them to you or point you to them. Hopefully that will help. If not, let me know, and I will put it together myself.
Here is the basic event pull in Python for OPC DA:
# This example shows how to subscribe to item changes and obtain the events by pulling them.
import time
import win32com.client
# Instantiate the client object
client = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.EasyDAClient')
# In order to use event pull, you must set a non-zero queue capacity upfront.
client.PullItemChangedQueueCapacity = 1000
print('Subscribing item changes...')
client.SubscribeItem('', 'OPCLabs.KitServer.2', 'Simulation.Random', 1000)
print('Processing item changes for 1 minute...')
endTime = time.time() + 60
while time.time() < endTime:
eventArgs = client.PullItemChanged(2*1000)
if eventArgs is not None:
# Handle the notification event
print(eventArgs)
print('Unsubscribing item changes...')
client.UnsubscribeAllItems()
print('Finished.')
This may be the starting code snippet. The problem with it is that is for OPC DA and not OPC XML DA. And, the EasyDAClient.SubscribeItem does not support OPC XML-DA; you need to use SubscribeMultipleItems instead. An example for it - in VBScript - is:
Rem This example shows how to subscribe to changes of multiple items and display the value of the item with each change.
Option Explicit
Dim ItemSubscriptionArguments1: Set ItemSubscriptionArguments1 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments")
ItemSubscriptionArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2"
ItemSubscriptionArguments1.ItemDescriptor.ItemID = "Simulation.Random"
ItemSubscriptionArguments1.GroupParameters.RequestedUpdateRate = 1000
Dim ItemSubscriptionArguments2: Set ItemSubscriptionArguments2 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments")
ItemSubscriptionArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2"
ItemSubscriptionArguments2.ItemDescriptor.ItemID = "Trends.Ramp (1 min)"
ItemSubscriptionArguments2.GroupParameters.RequestedUpdateRate = 1000
Dim ItemSubscriptionArguments3: Set ItemSubscriptionArguments3 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments")
ItemSubscriptionArguments3.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2"
ItemSubscriptionArguments3.ItemDescriptor.ItemID = "Trends.Sine (1 min)"
ItemSubscriptionArguments3.GroupParameters.RequestedUpdateRate = 1000
Dim ItemSubscriptionArguments4: Set ItemSubscriptionArguments4 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.EasyDAItemSubscriptionArguments")
ItemSubscriptionArguments4.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2"
ItemSubscriptionArguments4.ItemDescriptor.ItemID = "Simulation.Register_I4"
ItemSubscriptionArguments4.GroupParameters.RequestedUpdateRate = 1000
Dim arguments(3)
Set arguments(0) = ItemSubscriptionArguments1
Set arguments(1) = ItemSubscriptionArguments2
Set arguments(2) = ItemSubscriptionArguments3
Set arguments(3) = ItemSubscriptionArguments4
Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient")
WScript.ConnectObject Client, "Client_"
Client.SubscribeMultipleItems arguments
WScript.Echo "Processing item changed events for 1 minute..."
WScript.Sleep 60*1000
Sub Client_ItemChanged(Sender, e)
If Not (e.Succeeded) Then
WScript.Echo "*** Failure: " & e.ErrorMessageBrief
Exit Sub
End If
WScript.Echo e.Arguments.ItemDescriptor.ItemId & ": " & e.Vtq
End Sub
What matters is that an array of objects needs to be prepared upfront and passed to SubscribeMultipleItems; the rest of the Python code can remain the same.
The last difference is in how - in the above code - the ServerDescriptor is configure for XML-DA. What you need to do is instead of assigning to the ProgID ServerDescriptor.ServerClass property, you take the server URL (something like "opcxml.demo-this.com/XmlDaSampleServer/Service.asmx") and assign it to the ServerDescriptor.UrlString property.
Best regards
Please Log in or Create an account to join the conversation.
The PLC has an OPC DA-XML sever. When I went to the documentation (opclabs.doc-that.com/files/onlinedocs/QuickOpc/Latest/User's.../Event%20Pull%20Mechanism.html) in the OPC XML-DA section there is no example for python. Is this because it is not possible in python (many of the other sections have python example code) or is the example code listed on another page?
Please Log in or Create an account to join the conversation.