Difference between revisions of "How to subscribe to OPC data changes in PowerShell"

From OPC Labs Knowledge Base
Jump to navigation Jump to search
 
Line 1: Line 1:
 
[[Category:.NET]] [[Category:Console application]] [[Category:Example]] [[Category:PowerShell]] [[Category:How to]] [[Category:OPC DA]]
 
[[Category:.NET]] [[Category:Console application]] [[Category:Example]] [[Category:PowerShell]] [[Category:How to]] [[Category:OPC DA]]
 +
{{Note|Beginning with version 2021.3, QuickOPC product installation now contains a large set of PowerShell example scripts, and they are included in the conceptual and reference documentation. OPC "Classic", OPC UA Client-Server, and OPC UA PubSub are covered. The User's Guide also provides more information to QuickOPC usage from PowerShell.}}
 
The following PowerShell code subscribes to an OPC item and displays the incoming data changes:
 
The following PowerShell code subscribes to an OPC item and displays the incoming data changes:
  

Latest revision as of 12:58, 23 September 2021

Note2-icon.png

Note: Beginning with version 2021.3, QuickOPC product installation now contains a large set of PowerShell example scripts, and they are included in the conceptual and reference documentation. OPC "Classic", OPC UA Client-Server, and OPC UA PubSub are covered. The User's Guide also provides more information to QuickOPC usage from PowerShell.

The following PowerShell code subscribes to an OPC item and displays the incoming data changes:

Add-Type -Path OpcLabs.EasyOpcClassic.dll

# Create EasyOPC-DA component 
$client = New-Object OpcLabs.EasyOpc.DataAccess.EasyDAClient

# Hook events
Register-ObjectEvent -InputObject $client -EventName ItemChanged -Action { Write-Host $EventArgs }

# Subscribe
$client.SubscribeItem("", "OPCLabs.KitServer.2", "Demo.Single", 1000)

We have tested this with QuickOPC 5.40 and PowerShell 3.0.

Note2-icon.png

Note: For some issues and limitations, see How to read an OPC item value in PowerShell.

Note2-icon.png

Note: A memory leak related to event handling has been observed under some circumstances in PowerShell (and QuickOPC 2017.2). Workaround is to use the "event pull" mechanism in QuickOPC, instead of hooking event handlers.


Links related to/used when writing this article: