Start/stop PLC with Delphi
Console application. The following program starts or stops run-time system 1 in the PLC.
Delphi Prism (Embarcadero Prism XE2, Oxygene for .NET) program
namespace Sample14;
interface
uses System.IO, TwinCAT.Ads;
type
ConsoleApp = class
public
class method Main(args: array of string);
end;
implementation
class method ConsoleApp.Main(args: array of string);
begin
Console.WriteLine("Please enter 'r' to RUN or 's' to STOP the PLC or any key to exit...");
// Create a new instance of class TcAdsClient
var tcClient := new TcAdsClient();
var sInput : String;
try
// Connect to local PLC - Runtime 1 - Port 801
tcClient.Connect(801);
repeat
// Get current state
var oldState := tcClient.ReadState();
Console.WriteLine("State: " + oldState.AdsState.ToString());
// Read user input
sInput := Console.ReadKey().KeyChar;
if (sInput <> 'r') And (sInput <> 's') then
break;
// Set new state
var newState := new StateInfo(iif(sInput = 'r', AdsState.Run, AdsState.Stop), oldState.DeviceState);
tcClient.WriteControl(newState);
until (sInput <> 'r') And (sInput <> 's');
except
On err : Exception do
begin
Console.WriteLine(err.Message);
Console.ReadKey();
end;
finally
// Close connection
tcClient.Dispose();
end;
end;
end.
Download
Language / IDE | Unpack the sample program |
---|---|
Delphi Prism (Embarcadero Prism XE2, Oxygene for .NET) |