Jump to content

Using AutoIt to read Data from OPC Server


Recommended Posts

I am trying to read data from an OPC Server so that I can paste the data into a web page. There are a couple of incomplete threads on accessing OPC data that have been helpful but I can't seem to make the last couple of steps to read the data.

Scavenged liberally from the previos examples, I get get all the way through adding items to a group but I can't figure out the correct syntax to read the items in the group individually, or ideally, as a synchronous read.

Appreciate any help or direction

Take Care

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc") ; Install a custom error handler 
$strServerName = ("Graybox.Simulator.1")

$oDA=ObjCreate("OPC.Automation")
$Output = "" 

Dim $arItems[4]
$arItems[0]="numeric.square.int8"
$arItems[1]="numeric.saw.float"
$arItems[2]="numeric.sin.float"
$arItems[3]="numeric.triangle.float"

;Begin Testing of the Server Object Methods
;GetOPCSERVERS
$OPCServers = $oDA.GetOPCServers
For $OPCServer IN $OPCServers
$Output = $Output & @CRLF & $OPCServer
NEXT
MsgBox(4096,"Test",$Output)

$testID=$oDA.Connect($strServerName,"localhost")
MsgBox(4096,"Test",$testID) 

;Begin Testing of the Server Object Properties
$Output = $oDA.ServerName
MsgBox(4096,"Test1",$Output)
$Output = $oDA.StartTime & @CRLF & $oDA.CurrentTime & @CRLF & $oDA.LastUpdateTime
MsgBox(4096,"Test2",$Output)
$Output = $oDA.MajorVersion & @CRLF & $oDA.MinorVersion & @CRLF & $oDA.BuildNumber
MsgBox(4096,"Test3",$Output)
$Output = $oDA.VendorInfo & @CRLF & $oDA.ServerState & @CRLF & $oDA.LocaleID
MsgBox(4096,"Test4",$Output)
$Output = $oDA.Bandwidth & @CRLF & $oDA.ServerNode & @CRLF & $oDA.ClientName
MsgBox(4096,"Test5",$Output)

$OPCGroups = $oDA.OPCGroups
$GroupsDebug=0
$OPCGroup1=$OPCGroups.Add("Test1")
If ($GroupsDebug=1) Then
MsgBox(4096,"Test Debug","Groups Parent...." & $OPCGroups.Parent)
MsgBox(4096,"Test Debug","Groups DefaultGroupIsActive...." & $OPCGroups.DefaultGroupIsActive)
MsgBox(4096,"Test Debug","Groups DefaultGroupUpdateRate...." & $OPCGroups.DefaultGroupUpdateRate)
MsgBox(4096,"Test Debug","Groups DefaultGroupDeadBand...." & $OPCGroups.DefaultGroupDeadBand)
MsgBox(4096,"Test Debug","Groups DefaultGroupLocaleID...." & $OPCGroups.DefaultGroupLocaleID)
MsgBox(4096,"Test Debug","Groups DefaultGroupTimeBias...." & $OPCGroups.DefaultGroupTimeBias)
MsgBox(4096,"Test Debug","Groups Count...." & $OPCGroups.Count)

$OPCGroup2=$OPCGroups.Add("Test2")
$OPCGroup3=$OPCGroups.Add("Test3")
$OPCGroup4=$OPCGroups.Add("Test4")
MsgBox(4096,"Test Debug","Groups Parent...." & $OPCGroups.Parent)
MsgBox(4096,"Test Debug","Groups DefaultGroupIsActive...." & $OPCGroups.DefaultGroupIsActive)
MsgBox(4096,"Test Debug","Groups DefaultGroupUpdateRate...." & $OPCGroups.DefaultGroupUpdateRate)
MsgBox(4096,"Test Debug","Groups DefaultGroupDeadBand...." & $OPCGroups.DefaultGroupDeadBand)
MsgBox(4096,"Test Debug","Groups DefaultGroupLocaleID...." & $OPCGroups.DefaultGroupLocaleID)
MsgBox(4096,"Test Debug","Groups DefaultGroupTimeBias...." & $OPCGroups.DefaultGroupTimeBias)
MsgBox(4096,"Test Debug","Groups Count...." & $OPCGroups.Count)
EndIf

$GroupDebug=0
If ($GroupDebug=1) Then
FOR $OPCGroup IN $OPCGroups ; Here it starts.. 
MsgBox(4096,"Test Debug","Group Active...." & $OPCGroup.IsActive)
MsgBox(4096,"Test Debug","Group Name...." & $OPCGroup.Name)
MsgBox(4096,"Test Debug","Group Parent...." & $OPCGroup.Parent)
;MsgBox(4096,"Test Debug","Group IsPublic...." & $OPCGroup.IsPublic)
MsgBox(4096,"Test Debug","Group IsSubscribed...." & $OPCGroup.IsSubscribed)
MsgBox(4096,"Test Debug","Group ClientHandle...." & $OPCGroup.ClientHandle)
MsgBox(4096,"Test Debug","Group ServerHandle...." & $OPCGroup.ServerHandle)
MsgBox(4096,"Test Debug","Group LocaleID...." & $OPCGroup.LocaleID)
MsgBox(4096,"Test Debug","Group TimeBias...." & $OPCGroup.TimeBias)
MsgBox(4096,"Test Debug","Group DeadBand...." & $OPCGroup.DeadBand)
MsgBox(4096,"Test Debug","Group UpdateRate...." & $OPCGroup.UpdateRate)
NEXT
EndIF

$OPCGroup1ID=$OPCGroup1.ClientHandle

$OPCItems=$OPCGroup1.OPCItems

Dim $arError[4]
$ItemsDebug=0
IF ($ItemsDebug=1) Then
MsgBox(4096,"Test Debug","Items Parent...." & $OPCItems.Parent)
MsgBox(4096,"Test Debug","Items DefaultRequestedDataType...." & $OPCItems.DefaultRequestedDataType)
MsgBox(4096,"Test Debug","Items DefaultAccessPath...." & $OPCItems.DefaultAccessPath)
MsgBox(4096,"Test Debug","Items DefaultIsActive...." & $OPCItems.DefaultIsActive)
$OPCitems.DefaultIsActive=False
MsgBox(4096,"Test Debug","Items DefaultIsActive...." & $OPCItems.DefaultIsActive)
MsgBox(4096,"Test Debug","Items Count...." & $OPCItems.Count)
;$arError=$OPCItems.Validate(4,$arItems)
EndIF


$OPCItems.AddItem($arItems[0],$OPCGroup1ID)
$OPCItems.AddItem($arItems[1],$OPCGroup1ID)
$OPCItems.AddItem($arItems[2],$OPCGroup1ID)
$OPCItems.AddItem($arItems[3],$OPCGroup1ID)

MsgBox(4096,"Test Debug","Items Count...." & $OPCItems.Count)

FOR $OPCItem IN $OPCItems ; Here it starts.. 
;   $value1 = $OPCItem.Read

;MsgBox(0, "Value = ", $Value1)

Next
Link to comment
Share on other sites

Ok. Partially answered my own question. This snippet added to the end of the previous code successfully reads data from a text OPC server.

Rather than polling the data for each item, one at a time, I would like to do a syncronous read of all of the values simultaneously.

Ideally I would like to subscribe to the items and onyl get data when the data changes on the server.

For $i = 1 to 50
FOR $OPCItem IN $OPCItems 

; Here it starts.. 
;   $value1 = $OPCItem.Read
;   MsgBox(4096,"Test Debug","Item ItemID...." & $OPCItem.ItemID)
    $OPCItem.Read(2)
;   MsgBox(4096,"Value","Item Value...." & $OPCItem.Value)

;   MsgBox(4096,"Test Debug","Item Quality...." & $OPCItem.Quality)
    ConsoleWrite($OPCItem.Value & @tab)
Next
ConsoleWrite(@CRLF)
sleep(1000)
next
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...