program ProxyLib_Demo;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Unit1 in 'Unit1.pas' {Form1};

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

  //RFDevice
function RFDevice_Initialize(): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RFDevice_Initialize';
procedure RFDevice_Cleanup(); cdecl;
  external 'RFDevice_proxy.dll' name 'RFDevice_Cleanup';
  //RF620Device
function CreateRF603Device(): THandle; cdecl;
  external 'RFDevice_proxy.dll' name 'CreateRF603Device';
function RF603Device_OpenPort(rf620DeviceHandle: THandle; PortName:string; dwSpeed: integer): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF603Device_OpenPort';
function RF603Device_ClosePort(rf620DeviceHandle: THandle): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF603Device_ClosePort';
function RF603Device_StartStream(rf620DeviceHandle: THandle; address: Byte): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF603Device_StartStream';
function RF603Device_StopStream(rf620DeviceHandle: THandle;  address: Byte): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF603Device_StopStream';
function RF603Device_GetStreamMeasure(rf620DeviceHandle: THandle; lpusValu:PInteger): BOOLEAN; cdecl;
  external 'RFDevice_proxy.dll' name 'RF603Device_GetStreamMeasure';
procedure DisposeRF603Device(rf620DeviceHandle: THandle); cdecl;
  external 'RFDevice_proxy.dll' name 'DisposeRF620Device';
  //RFLanDetector
{function CreateRFLanDetector(): THandle; cdecl;
  external 'RFDevice_proxy.dll' name 'CreateRFLanDetector';
function RFLanDetector_Search(rfDev: THandle; devType: Word; timeout: integer): integer; cdecl;
  external 'RFDevice_proxy.dll' name 'RFLanDetector_Search';
function RFLanDetector_GetDevBlock(rfDev: THandle; index: integer): THandle; cdecl;
  external 'RFDevice_proxy.dll' name 'RFLanDetector_GetDevBlock';
procedure DisposeRFLanDetector(rfLanDetectorHandle: THandle); cdecl;
  external 'RFDevice_proxy.dll' name 'DisposeRFLanDetector'; }

var
  _connected: boolean;
  _res: boolean;
  _res_1: boolean;
  name: string;
  devCount: integer;
  devType: Word;
  _rfLanDetectorHandle: THandle;
  _rf620DeviceHandle: THandle;
  _devBlockHandle: THandle;
  _arr1: DataArray;
  _arr,_arr2,_arr3,_arr4: integer;
  _result:real;
  i: integer;

begin
  try
    _connected := RFDevice_Initialize();
    if _connected <> TRUE then
    begin
      WriteLn('init library');
      ReadLn(name);

    end;
       _rf620DeviceHandle := CreateRF603Device();
    _connected := RF603Device_OpenPort(_rf620DeviceHandle,'COM6',921600);
    if _connected <> TRUE then
    begin
      WriteLn('ERROR open port');
      ReadLn(name);
         Exit;
    end;
    _res:=false ;
    _res:= RF603Device_StartStream(_rf620DeviceHandle,1);
    if _res = TRUE then
      begin
        WriteLn('Stream');
      end;
        for i:= 0 to 10 - 1 do
    begin
     // _arr := @_arr1;
      _res_1:= RF603Device_GetStreamMeasure(_rf620DeviceHandle, @_arr);
      if _res_1 <> TRUE then
      begin
        WriteLn('Getting data error');
      end;
      if _res_1 = TRUE then
      begin
           _result:=((_arr*50) / 16384);
           Writeln(_result:2:3, ' mm  ');

      end;
    end;
         _res := RF603Device_StopStream(_rf620DeviceHandle,1);
    if _res = TRUE then
      begin
        WriteLn('Stop');
      end;

       DisposeRF603Device(_rf620DeviceHandle);
       RF603Device_ClosePort(_rf620DeviceHandle);
       RFDevice_Cleanup();
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;

 readln(name);
end.
