Read ADS information
Download: 'Read ADS information'
Each ADS device contains a version number and an identification. The example program reads this information from the PLC and displays it on the screen.
#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()
{
LONG nErr, nPort;
AdsVersion Version;
AdsVersion *pVersion = &Version;
char pDevName[50];
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;
nErr = AdsSyncReadDeviceInfoReq(pAddr, pDevName, pVersion);
if (nErr)
cerr << "Error: AdsSyncReadDeviceInfoReq: " << nErr << '\n';
else
{
cout << "Name: " << pDevName << '\n';
cout << "Version: " << (int)pVersion->version << '\n';
cout << "Revision: " << (int)pVersion->revision << '\n';
cout << "Build: " << pVersion->build << '\n';
}
cout.flush();
getch();
// Close communication port
nErr = AdsPortClose();
if (nErr) cerr << "Error: AdsPortClose: " << nErr << '\n';
}