Read ADS status

Download: 'Read ADS status'

This program reads the status of the PLC. The variable of type ADSSTATE contains information such as, for example, whether the PLC is in the RUN or STOP state.

#include <iostream.h>
#include <windows.h>
#include <conio.h>

// ADS headers for TwinCAT 3
#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;

// Open communication port on the ADS router
nPort = AdsPortOpen();
nErr = AdsGetLocalAddress(pAddr);
if (nErr) cerr << "Error: AdsGetLocalAddress: " << nErr << '\n';

// TwinCAT3 PLC1 = 851
pAddr->port = 851;

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'); // continue on a carriage return, finish for any other key

// Close communication port
nErr = AdsPortClose();
if (nErr) cerr << "Error: AdsPortClose: " << nErr << '\n';
}