Asynchronous block scanning

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
BlockScanButton: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BlockScanButtonClick(Sender: TObject);
private
procedure WMUser(var Msg: TMessage); message WM_USER;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Dll: HMODULE;
hRadio: Integer;
curFreq: DWORD;
Freqs: array[0..1000] of DWORD;   // buffer for scan data

OpenRadioDevice: function(iDeviceNum: Integer): Integer; stdcall;
CloseRadioDevice: function(hRadio: Integer): Bool; stdcall;
SetAtten: function(hRadio: Integer; fAtten: Bool): Bool; stdcall;
SetPower: function(hRadio: Integer; fPower: Bool): Bool; stdcall;
SetAGC: function(hRadio: Integer; iAGC: Integer): Bool; stdcall;
G3BlockScan: function(hRadio: Integer; Freqs: PDWORD; Count: Integer; StopSquelchRaw: Integer; FeedbackTime: DWORD; WinHandle: HWND; Msg: DWORD): Bool; stdcall;
G3StopBlockScan: function(hRadio: Integer): Bool; stdcall;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
Dll:=LoadLibrary('wrg3130api.dll');
hRadio:=0;

if (Dll=0) then ShowMessage('WRG3130API.DLL not found !')
else begin
OpenRadioDevice:=GetProcAddress(dll,'OpenRadioDevice');
CloseRadioDevice:=GetProcAddress(dll,'CloseRadioDevice');
SetAtten:=GetProcAddress(dll,'SetAtten');
SetPower:=GetProcAddress(dll,'SetPower');
SetAGC:=GetProcAddress(dll,'SetAGC');
G3BlockScan:=GetProcAddress(dll,'G3BlockScan');
G3StopBlockScan:=GetProcAddress(dll,'G3StopBlockScan');

hRadio:=OpenRadioDevice(0);
end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if (hRadio <> 0) then begin
G3StopBlockScan(hRadio);    // just to be sure
CloseRadioDevice(hRadio);
end;
if (Dll <> 0) then FreeLibrary(Dll);
end;

procedure TForm1.BlockScanButtonClick(Sender: TObject);
var i: Integer;
begin
if (hRadio <> 0) then begin    // if a radio is opened
// set the properties
SetPower(hRadio,TRUE);
SetAtten(hRadio,FALSE);
SetAGC(hRadio,3);      // FAST AGC

for i:=0 to 1000 do Freqs[i]:=600000+200*i;   // frequencies from 600 to 800 kHz
curFreq:=600000;    // variable used to display scanned data
if (not G3BlockScan(hRadio,@Freqs,1001,256,500,Handle,WM_USER)) then
ShowMessage('Block scan failed to start');
end;
end;

procedure TForm1.WMUser(var Msg: TMessage);
var i: Integer;
pi: PDWORD;
begin
if (Msg.WParam=0) then ListBox1.Items.Add('Block scan ended')
else begin
pi:=PDWORD(Msg.WParam);
for i:=1 to Msg.LParam do begin
ListBox1.Items.Add('Freq: '+IntToStr(curFreq)+' RAW: '+IntToStr(pi^));
inc(pi); inc(curFreq,200);   // next frequency to display
end;
end;
end;

end.