program ProxyLib_Demo;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  PByte = ^Byte;
  PDataArray = ^DataArray;
  DataArray = array [0..327680] of Byte;

  //RFDevice
function RFDevice_Initialize(): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RFDevice_Initialize';
procedure RFDevice_Cleanup(); cdecl;
  external 'RFDevice_proxy.dll' name 'RFDevice_Cleanup';
  //RF625DDevice
function CreateRF625DDevice_1(devBlockHandle: THandle): THandle; cdecl;
  external 'RFDevice_proxy.dll' name 'CreateRF625DDevice_1';
function RF625DDevice_Connect(RF625DDeviceHandle: THandle): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF625DDevice_Connect';
function RF625DDevice_GetResult(RF625DDeviceHandle: THandle; arrayPointer: PByte): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF625DDevice_GetResult';
function RF625DDevice_GetImage(RF625DDeviceHandle: THandle; arrayPointer: PByte): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF625DDevice_GetImage';
function RF625DDevice_ReadParams(RF625DDeviceHandle: THandle): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF625DDevice_ReadParams';
function RF625DDevice_Disconnect(RF625DDeviceHandle: THandle): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF625DDevice_Disconnect';
procedure DisposeRF625DDevice(RF625DDeviceHandle: THandle); cdecl;
  external 'RFDevice_proxy.dll' name 'DisposeRF625DDevice';
  //RFLanDetector
function CreateRFEthernetDetector(): THandle; cdecl;
  external 'RFDevice_proxy.dll' name 'CreateRFEthernetDetector';
function RFEthernetDetector_Search(rfDev: THandle; devType: Word; timeout: integer): integer; cdecl;
  external 'RFDevice_proxy.dll' name 'RFEthernetDetector_Search';
function RFEthernetDetector_GetDevBlock(rfDev: THandle; index: integer): THandle; cdecl;
  external 'RFDevice_proxy.dll' name 'RFEthernetDetector_GetDevBlock';
{procedure DisposeRFEthernetDetector(rfLanDetectorHandle: THandle); cdecl;
  external 'RFDevice_proxy.dll' name 'DisposeRFEthernetDetector';  }

var
  _connected: boolean;
  _res: boolean;
  name: string;
  devCount: integer;
  devType: Word;
  _rfLanDetectorHandle: THandle;
  _RF625DDeviceHandle: THandle;
  _devBlockHandle: THandle;
  _arr1: DataArray;
  _arr: PByte;
  i: integer;

begin
  try
    _connected := RFDevice_Initialize();
    if _connected <> TRUE then
    begin
      WriteLn('Cant init library');
      ReadLn(name);
      Exit;
    end;
    _rfLanDetectorHandle := CreateRFEthernetDetector();
    devType := 625;
    devCount := RFEthernetDetector_Search(_rfLanDetectorHandle, devType, 2);
    if devCount < 1 then
    begin
      WriteLn('Devices not found');
      ReadLn(name);
      Exit;
    end;
    _devBlockHandle := RFEthernetDetector_GetDevBlock(_rfLanDetectorHandle, 0);
    _RF625DDeviceHandle := CreateRF625DDevice_1(_devBlockHandle);
    _connected := RF625DDevice_Connect(_RF625DDeviceHandle);
    if _connected <> TRUE then
    begin
      WriteLn('Unable to connect');
      ReadLn(name);
      Exit;
    end;
    _res := RF625DDevice_ReadParams(_RF625DDeviceHandle);
    if _res = TRUE then
      begin
        WriteLn('Params read');
      end;
      _arr := @_arr1;
       _res := RF625DDevice_GetImage(_RF625DDeviceHandle, _arr);
    for i := 0 to 5000 - 1 do
    begin


      if _res <> TRUE then
      begin
        WriteLn('Getting data error');
      end;
      if _res = TRUE then
      begin
        WriteLn('Got data',_arr1[i]);
      end;
    end;
    RF625DDevice_Disconnect(_RF625DDeviceHandle);
    DisposeRF625DDevice(_RF625DDeviceHandle);
   // DisposeRFEthernetDetector(_rfLanDetectorHandle);
    RFDevice_Cleanup();
    WriteLn('Done');
    ReadLn(name);
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.
