Detect/alter state of the router and the PLC
Task
ADS-OCX provides methods for trapping state changes in the TwinCAT Router and the ADS devices. The events AdsRouterRemove(), AdsRouterShutdown(), AdsRouterStart(), AdsServerStateChanged() and AdsServerSymChanged() are available for this.
Description
If the AdsRouterShutdown() event is called when the TwinCAT Router is stopped, the affected program can react appropriately. When the TwinCAT Router is started, the AdsRouterStart() event is called, in which, for example, the AdsReadVarConnectEx() method can be used to re-establish the connections to the ADS variables. If the TwinCAT router is completely removed from the operating system in the Windows NT/2000/XP control panel, the AdsRouterRemove() event is called.
In addition to state changes of the TwinCAT router, state changes in ADS devices can also be intercepted. This is particularly significant to the PLC. The AdsServerStateChanged() event can be used to establish whether the PLC has been started or stopped. Changes of the symbol table are reported by the AdsServerSymChanged() event. This happens, for instance, when the PLC program is recompiled and then transferred to the PLC.
Just as the state of an ADS device can be queried, it can also be changed. The AdsSyncWriteControlReq() method makes this possible. The PLC can adopt the ADS states STOP and RUN. Using the check button in the sample program below, the user can switch between these two states.
Each change of state in the TwinCAT Router results in an appropriate entry in the listbox.
Attention: If a change in the symbol table is detected, it can be that a variable that was addressed by AdsReadVarConnectEx() has been deleted or renamed. When the AdsServerSymChanged() event occurs, all connects and handles should be deleted and then recreated.
Visual Basic 6 program
Option Explicit
'--- wird beim Starten aufgerufen ---
Private Sub Form_Load()
Call lstEvent.Clear
AdsOcx1.EnableErrorHandling = True
End Sub
'--- wird aufgerufen, wenn sich der Status des ADS-Gerätes ändert ---
Private Sub AdsOcx1_AdsServerStateChanged(ByVal nAdsState As ADSOCXLib.ADSSTATE, ByVal nDeviceState As Long)
Select Case nAdsState
Case ADSSTATE_INVALID: lstEvent.AddItem ("PLC invalid")
Case ADSSTATE_IDLE: lstEvent.AddItem ("PLC idle")
Case ADSSTATE_RESET: lstEvent.AddItem ("PLC reset")
Case ADSSTATE_INIT: lstEvent.AddItem ("PLC init")
Case ADSSTATE_START: lstEvent.AddItem ("PLC start")
Case ADSSTATE_RUN: lstEvent.AddItem ("PLC run")
chkRunStop.Value = 1
Case ADSSTATE_STOP: lstEvent.AddItem ("PLC stop")
chkRunStop.Value = 0
Case ADSSTATE_SAVECFG: lstEvent.AddItem ("PLC savecfg")
Case ADSSTATE_LOADCFG: lstEvent.AddItem ("PLC loadcfg")
Case ADSSTATE_POWERFAILURE: lstEvent.AddItem ("PLC powerfailure")
Case ADSSTATE_POWERGOOD: lstEvent.AddItem ("PLC powergood")
Case ADSSTATE_ERROR: lstEvent.AddItem ("PLC error")
End Select
End Sub
'--- wird bei Änderung der Symboltabelle aufgerufen ---
Private Sub AdsOcx1_AdsServerSymChanged()
lstEvent.AddItem ("PLC symbol changed")
End Sub
'--- wird beim Entfernen des TwinCAT-Routers aufgerufen ---
Private Sub AdsOcx1_AdsRouterRemove()
lstEvent.AddItem ("TwinCAT-Router remove")
End Sub
'--- wird beim Stoppen des TwinCAT-Routers aufgerufen ---
Private Sub AdsOcx1_AdsRouterShutdown()
lstEvent.AddItem ("TwinCAT-Router shutdown")
End Sub
'--- wird beim Starten des TwinCAT-Routers aufgerufen ---
Private Sub AdsOcx1_AdsRouterStart()
lstEvent.AddItem ("TwinCAT-Router start")
End Sub
'--- wird vom Bediener aufgerufen ---
Private Sub chkRunStop_Click()
Dim nState As ADSOCXLib.ADSSTATE
Dim nRet As Integer
nState = IIf(chkRunStop.Value = 0, ADSSTATE_STOP, ADSSTATE_RUN)
Call AdsOcx1.AdsSyncWriteControlReq(nState, 0&, 0&, nRet)
End Sub
Language / IDE | Unpack sample program |
---|---|
Visual Basic 6 |