Delete a handle of a PLC variable with Delphi

Console application. This Sample shows how to delete a handle of a PLC variable.

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

namespace Sample11;

interface
uses TwinCAT.Ads;

type
  ConsoleApp = class
  public
    class method Main(args: array of string);
  end;

implementation

class method ConsoleApp.Main(args: array of string);
begin
  // Create a new instance of class TcAdsClient
  var tcClient := new TcAdsClient();
  var iHandle : Int32;

  try
    // Connect to local PLC - Runtime 1 - Port 801
    tcClient.Connect(801);

    // Get the handle of the PLC variable "MAIN.PLCVar"
    iHandle := tcClient.CreateVariableHandle("MAIN.PLCVar");

    // Use the handle to read from PLC variable
    var sOldValue := tcClient.ReadAny(iHandle, TypeOf(UInt32)).ToString();
    Console.WriteLine("PLCVar: " + sOldValue);

    // Use the handle to write to PLC variable, e.g. reset PLCVar
    var newValue := UInt32(0);
    tcClient.WriteAny(iHandle, newValue);
  except
    on err : Exception do
      Console.WriteLine(err.Message);
  finally
    // Release the variable handle
    tcClient.DeleteVariableHandle(iHandle);

    // Close connection to the PLC
    tcClient.Dispose();
  end;

  // Wait for user input
  Console.ReadKey();
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)

Sample11.exe