Access by variable name with Delphi

Console application. The following program accesses a PLC variable that does not have an address. Access must therefore be made by the variable name. Once the PLC variable in the example program exceeds 10 it is reset to 0.

 

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

namespace Sample15;

interface

uses System.IO, TwinCAt.Ads, System.Data, System.Runtime.InteropServices;

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();
  var dwValue : UInt32 := 0;
  var iHandle : Int32 := 0;
  try
    // Connect to local PLC - Runtime 1 - Port 801
    tcClient.Connect(801);
    // Get the handle of the PLC variable "SPSVar"
    iHandle := tcClient.CreateVariableHandle("MAIN.PLCVar");

    while (Console.ReadKey().KeyChar <> 'e') do
    begin
      dwValue := UInt32(tcClient.ReadAny(iHandle, TypeOf(UInt32))); 
      Console.WriteLine("Value: " + dwValue.ToString());

      if dwValue > 10 then
    tcClient.WriteAny(iHandle, UInt32(0));
    end;

  except
    On err : Exception do
    begin
      Console.WriteLine(err.Message);
      Console.ReadKey();
    end;
  finally
    // Release variable handle
    tcClient.DeleteVariableHandle(iHandle);

    // Close connection
    tcClient.Dispose();
  end;

end;

end.

 

PLC program

PROGRAM MAIN
VAR
    PLCVar : 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)

Sample15.exe