unit MainUnit;

interface

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

type
  TLc4hsForm = class(TForm)
    Status: TLabel;
    InitButton: TButton;
    FinishButton: TButton;
    ReadButton: TButton;
    IMSLabel: TLabel;
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure InitButtonClick(Sender: TObject);
    procedure FinishButtonClick(Sender: TObject);
    procedure ReadButtonClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    hCard: THandle;
  end;

var
  Lc4hsForm: TLc4hsForm;

implementation
uses
  Lc4hs;

{$R *.DFM}

procedure TLc4hsForm.FormCreate(Sender: TObject);
begin
  hCard:= Lc4hsOpen('4HS1');
  if hCard = INVALID_HANDLE_VALUE then
  begin
    MessageDlg('Cannot open driver', mtError, [mbOK], 0);
    Close
  end
end;

procedure TLc4hsForm.InitButtonClick(Sender: TObject);
begin
  InitButton.Caption:= 'working...';
  InitButton.Enabled:= false;
  InitButton.Update;
  try
    if Lc4hsInit(hCard) then
      MessageDlg('Initalised OK', mtInformation, [mbOK], 0)
    else
      MessageDlg('Could not initialise card', mtError, [mbOK], 0)
  finally
    InitButton.Enabled:= true;
    InitButton.Caption:= 'Init'
  end;
end;

procedure TLc4hsForm.FinishButtonClick(Sender: TObject);
begin
  if not Lc4hsFinish(hCard) then
    MessageDlg('Could not initialise card', mtError, [mbOK], 0)
end;

procedure TLc4hsForm.ReadButtonClick(Sender: TObject);
var
  Result: FLOAT4;
begin
  if Lc4hsGetCurrentWeight4(hCard, Result) then
    Status.Caption:= Format('%.3f %.3f %.3f %.3f',
      [Result[0], Result[1], Result[2], Result[3]])
  else
    MessageDlg('Could not read card', mtError, [mbOK], 0)
end;

procedure TLc4hsForm.FormDestroy(Sender: TObject);
begin
  CloseHandle(hCard)
end;

end.
