Write synchronously to the PLC
In this sample program, the entered value is written to the flag double word 0.
Download: sample02-c-ads-dll-writeflag.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()
{
long nErr, nPort;
AmsAddr Addr;
PAmsAddr pAddr = &Addr;
DWORD dwData;
// Kommunikationsport auf dem ADS Router öffnen
nPort = AdsPortOpen();
nErr = AdsGetLocalAddress(pAddr);
if (nErr) cerr << "Error: AdsGetLocalAddress: " << nErr << '\n';
// TwinCAT2 RTS1 Port = 801
pAddr->port = 801;
// Wert vom Benutzer einlesen, welcher zur SPS geschrieben werden sollen
cout << "Value: ";
cin >> dwData;
// Wert in MD0 schreiben
nErr = AdsSyncWriteReq( pAddr, 0x4020, 0x0, 0x4, &dwData );
if (nErr) cerr << "Error: AdsSyncWriteReq: " << nErr << '\n';
_getch();
// Kommunikationsport schließen
nErr = AdsPortClose();
if (nErr) cerr << "Error: AdsPortClose: " << nErr << '\n';
}