unit Demo;

interface

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

Var
   hScale: THandleStream;
   grossbuf: array [0..8] of char;
   tarebuf: array [0..8] of char;
   netbuf: array [0..8] of char;
   unitbuf: array [0..2] of char;

type
  TForm1 = class(TForm)
    PBruto: TPanel;
    PTara: TPanel;
    PLiquido: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    ButtonTeste: TButton;
    Timer1: TTimer;
    LabelError: TLabel;
    Label4: TLabel;
    PUnit: TPanel;
    procedure ButtonTesteClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
function AttachScale(hScale : THandleStream;Com,BaudRate,ByteSize,StopBits,Parity: Integer): Integer; stdcall;
external 'PcScale.dll';

function ReadScale(hScale : THandleStream; Indicator: Integer; gross,tare,net,unitd: Pchar): Integer; stdcall;
external 'PcScale.dll';

function DettachScale(hScale : THandleStream): Integer; stdcall;
external 'PcScale.dll';

procedure TForm1.ButtonTesteClick(Sender: TObject);
var
   Com,BaudRate,ByteSize,StopBits,Parity,status : integer;
begin
     if( ButtonTeste.Caption = 'Teste' ) then
     begin
          Com := 1;           // comunication port
          BaudRate := 9600;   // BaudRate
          ByteSize := 8;      // number of bits/byte, 4-8
          StopBits := 0;      // 0,1,2 = 1, 1.5, 2
          Parity := 0;        // 0-4=no,odd,even,mark,space

          status := AttachScale(@hScale,Com,BaudRate,ByteSize,StopBits,Parity);
          if( status < 0 ) then
          begin
               if(status = -2) then LabelError.Caption := 'Erro abrindo com'
	       else if(status = -3) then LabelError.Caption := 'Com inválida'
	       else if(status = -4) then LabelError.Caption := 'Muitos arquivos abertos'
               else if(status = -5) then LabelError.Caption := 'Acesso a Com não permitido'
               else if(status = -6) then LabelError.Caption := 'Erro setando parâmetros de comunicação'
               else if(status = -7) then LabelError.Caption := 'Erro setando timeout de comunicação'
               else if(status = -100) then LabelError.Caption := 'Arquivo vazio'
               else if(status = -101) then LabelError.Caption := 'Disco cheio'
    	       else if(status = -106) then LabelError.Caption := 'Input inválido'
               else LabelError.Caption := 'Erro Genérico';
          end
          else
          begin
               Timer1.Enabled := true;
               ButtonTeste.Caption := 'Parar';
          end
     end
     else
     begin
          Timer1.Enabled := false;
          DettachScale(@hScale);
          ButtonTeste.Caption := 'Teste';
     end
end;

procedure TForm1.Timer1Timer(Sender: TObject);

Var
status : Integer;
const
ScaleError :Integer = 0;
ScaleInMotion :Integer = 1;
ScaleStable :Integer = 2;
ScaleOutOfRange :Integer = 3;
ProteqNotFound :Integer = 4;

BIDS :Integer = 0;
BIQP810 :Integer = 1;
B9091 :Integer = 2;
B8132 :Integer = 3;
BID10000 :Integer = 4;
BBP :Integer = 5;

begin

     status := ReadScale(@hScale,BID10000,@grossbuf,@tarebuf,@netbuf,@unitbuf);

     if( status = ScaleError      ) then LabelError.Caption := 'Erro de leitura da balança';
     if( status = ScaleOutOfRange ) then LabelError.Caption := 'Sobrecarga';
     if( status = ProteqNotFound  ) then LabelError.Caption := 'Chave de licença não encontrada';
     if( status = ScaleInMotion   ) then LabelError.Caption := 'Peso oscilando';
     if( status = ScaleStable     ) then LabelError.Caption := 'Peso estável';
     PBruto.Caption := grossbuf;
     PTara.Caption := tarebuf;
     PLiquido.Caption := netbuf;
     PUnit.Caption := unitbuf;
end;



end.
