Read ADS state
This program reads the state of the PLC. The variable of type ADSSTATE contains information such as, for example, whether the PLC is in the RUN or STOP state.
Download: sample04-c-ads-dll-readadsstate.zip
#include <iostream.h>
#include <windows.h>
#include <conio.h>
// ADS headers
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsDef.h"
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsApi.h"
void main()
{
ADSSTATE nAdsState;
USHORT nDeviceState;
long nErr, nPort;
AmsAddr Addr;
PAmsAddr pAddr = &Addr;
// Kommunikationsport auf dem ADS Router öffnen
nPort = AdsPortOpen();
nErr = AdsGetLocalAddress(pAddr);
if (nErr) nErr << "Error: AdsGetLocalAddress: " << nErr << '\n';
// TwinCAT2 RTS1 Port = 801
pAddr->port = 801;
do
{
nErr = AdsSyncReadStateReq(pAddr, &nAdsState, &nDeviceState);
if (nErr)
cerr << "Error: AdsSyncReadStateReq: " << nErr << '\n';
else
{
cout << "AdsState: " << nAdsState << '\n';
cout << "DeviceState: " << nDeviceState << '\n';
}
cout.flush();
}
while ( getch() == '\r'); // Carriage return weiter, sonst Ende
// Kommunikationsport schließen
nErr = AdsPortClose();
if (nErr) cerr << "Error: AdsPortClose: " << nErr << '\n';
}