Reputation: 71
Following the advice to the question (TwinCAT 3.0 Automation Interface without Visual Studio?) I receive 1793 error (Service is not supported by server).
I try to write program to start/restart/config Beckhoff PLC using Twincat3 (this is the same functionality like the small Beckhoff GUI application). I'm trying to follow advices from solution presented above, but it seems that I cannot set the state. Reading values from the device works:
nPort = AdsPortOpen();
nErr = AdsGetLocalAddress(pAddr);
if (nErr)
cerr << "Error: AdsGetLocalAddress: " << nErr << '\n';
// TwinCAT 3 PLC1 = 851
pAddr->port = 10000;// 10000 as advised @ stackoverflow;
cout << "(R) -> PLC Run\n";
cout << "(S) -> PLC Stop\n";
cout.flush();
ch = _getch();
ch = toupper(ch);
while ((ch == 'R') || (ch == 'S'))
{
switch (ch)
{
case 'R':
nAdsState = ADSSTATE_RUN;
break;
case 'S':
nAdsState = ADSSTATE_CONFIG;
break;
}
nErr = AdsSyncReadStateReq(pAddr, &nAdsState, &nDeviceState);// , 0, pData);
if (nErr) cerr << "Error: AdsSyncReadStateReq: " << nErr << '\n';
cout << nAdsState << " " << nDeviceState << endl;
nErr = AdsSyncWriteControlReq(pAddr, nAdsState, nDeviceState, 0, pData);
if (nErr) cerr << "Error: AdsSyncWriteControlReq: " << nErr << '\n'; //1793
ch = _getch();
ch = toupper(ch);
}
Upvotes: 1
Views: 2831
Reputation: 71
Ok, I have found the solution, maybe someone will find it useful. There is no error in the code above, but required AdsState is wrong. It seems that ADSSTATE_RUN and ADSSTATE_CONFIG (and some others in this enum) are used only to return the state.
To actually change the state of device you should use ADSSTATE_RESET and ADSSTATE_RECONFIG (those two values reflect the functionality of start/restart in run mode and config mode). Also with ADSSTATE_STOP you can completely stop PLC, while ADSSTATE_SHUTDOWN allows for rebooting or turning off the PC (depending on DeviceState value 0/1).
Upvotes: 5