Read flag synchronously from the PLC with Delphi

Console application. In this example program the value in flag double word 0 in the PLC is read and displayed on the screen.

Delphi Prism (Embarcadero Prism XE2, Oxygene for .NET) program

namespace Sample12;

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 'e' to exit or any key to read...");
  // Create a new instance of class TcAdsClient
  var tcClient := new TcAdsClient();
  try
    // Connect to local PLC - Runtime 1 - Port 801
    tcClient.Connect(801);
    // Read PLC variable
    while (Console.ReadKey().KeyChar <> 'e') do
    begin
      // Specify indexGroup, indexOffset and read PLCVar 
      var dwFlag := tcClient.ReadAny($4020, $0, typeof(UInt32));
      Console.WriteLine("PLCVar: " + dwFlag.ToString());
    end;
  except
    on err : Exception do
    begin
      Console.WriteLine(err.Message);
      Console.ReadKey();
    end;
  finally
    // Close connection
    tcClient.Dispose();
  end;
end;

end.

 

PLC program

PROGRAM MAIN
VAR
    PLCVar AT %MD0 : DWORD;
    timer : TON;
END_VAR
timer( IN := TRUE, PT := t#1s);
IF timer.Q THEN
    timer( IN := FALSE );
    timer( IN := TRUE );
    PLCVar := PLCVar + 1;
END_IF

 

Download

Language / IDE

Unpack the sample program

Delphi Prism (Embarcadero Prism XE2, Oxygene for .NET)

Sample12.exe