unit uDemoMain;

interface

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

type
  TfrmDemoMain = class(TForm)
    lbl1: TLabel;
    lbl2: TLabel;
    cbb2: TComboBox;
    cbb1: TComboBox;
    btn1: TButton;
    btn2: TButton;
    NeoNumLED: TLabel;
    weightStatus: TPanel;
    flayPan: TPanel;
    zeroPan: TPanel;
    btnClear: TButton;
    btndeTare: TButton;
    mmo1: TMemo;
    lbl3: TLabel;
    lbl4: TLabel;
    lbl6: TLabel;
    lbl5: TLabel;
    lbl8: TLabel;
    lbl7: TLabel;
    stat1: TStatusBar;
    Button1: TButton;
    procedure btn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btndeTareClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDemoMain: TfrmDemoMain;
  isCanTare : Boolean;
  WeithUnit : array[0..15] of string = ('公克','公斤','公吨','分(香港)','钱(香港)','两(香港)','斤(香港)',
                                       '担(香港)','分(台湾)','钱(台湾)','两(台湾)','斤(台湾)','担(台湾)','盎司','磅','千磅');


  procedure __Config(Retrycnt,TimeOut:Integer;Debug:Boolean;
         ErrorInfo,RecInfo,opboxInfo,leadinfo,IS1Info,PotInfo:Pointer);stdcall;external 'SensorDll.dll';
  function __Open(CommName:PChar;BaudRate:Integer):Boolean;stdcall;external 'SensorDll.dll';
  function __Close:Boolean;stdcall;external 'SensorDll.dll';
  function __GetWeight():PChar;stdcall;external 'SensorDll.dll';
  function __udeTare:Integer; stdcall;external 'SensorDll.dll';
  function __uCleart:Integer;stdcall;external 'SensorDll.dll';
  


implementation

{$R *.dfm}

procedure DisplayIS1Info(status,sign,Weight,fUnit:PChar;flag:Integer);stdcall;
var
  fstatus,fweight,oweight : string;
begin
  //
  fstatus := PChar(status);
  if fstatus = 'F' then frmDemoMain.weightStatus.Caption := '重量溢出或没有开机归零';
  if fstatus = 'S' then frmDemoMain.weightStatus.Caption := '重量稳定';
  if fstatus = 'U' then frmDemoMain.weightStatus.Caption := '重量不稳定';

  oweight := PChar(Weight);
  fweight := PChar(sign);
  fweight := fweight + PChar(Weight);
  fweight := fweight + PChar(fUnit);

  frmDemoMain.NeoNumLED.Caption := Trim(fweight);

  if fstatus = 'F' then
    frmDemoMain.NeoNumLED.Caption := '----------';

  if flag and $10 = $10 then frmDemoMain.zeroPan.Color := clGreen
  else frmDemoMain.zeroPan.Color := clBtnFace;

  if flag and $20 = $20 then
  begin
    frmDemoMain.flayPan.Color := clGreen;
    if (fstatus = 'U') and (StrToFloat(oweight)>0) then
      isCanTare := False
    else isCanTare := True;
  end
  else frmDemoMain.flayPan.Color := clBtnFace;
end;

procedure DisplayWeightEvent(NetWeight,Tare,Grossweight:Double;
                               Mode,DecimalDigit,UnitChar:Integer;
                             OldAd,FilterAd,ZeroAd,WeightAd,flag:Integer);stdcall;
var
  sfor,
  s:string;
  flagMode : Integer;
begin
  flagMode := 0;
  //
  s := WeithUnit[UnitChar];
  sfor := '%0.'+IntToStr(DecimalDigit)+'f'+s;
  frmDemoMain.lbl6.Caption := '皮重:';

//   Bit0: 0表示重量不稳定            1表示重量稳定
//   Bit1: 0表示重量没有溢出          1表示重量溢出
//   Bit2: 0表示有开机归零            1表示没有开机归零
//   Bit3: 0当前重量大于最小称量范围  1当前重量小于最小称量范围
//   Bit4: 1当前OP盒被软解锁         0当前OP盒没有软解锁
//   Bit5: 1当前OP盒硬件解锁         0当前OP盒硬件没解锁
//   Bit6: 1结果的最低位永远为零		  0结果的最低位可能非零
  if flag and $1 <> $1 then
    frmDemoMain.stat1.Panels[0].Text := '重量不稳定'
  else frmDemoMain.stat1.Panels[0].Text := '重量稳定';

  if flag and $2 = $2 then
  begin
    flagMode := $FF;  
    frmDemoMain.stat1.Panels[1].Text := '重量溢出'
  end
  else frmDemoMain.stat1.Panels[1].Text := '重量没有溢出';

  if flag and $4 = $4 then
    frmDemoMain.stat1.Panels[2].Text := '没有开机归零'
  else frmDemoMain.stat1.Panels[2].Text := '开机归零';

  if flag and $8 = $8 then
    frmDemoMain.stat1.Panels[3].Text := '当前重量小于最小称量范围'
  else frmDemoMain.stat1.Panels[3].Text := '当前重量大于最小称量范围';

  if flag and $10 = $10 then
    frmDemoMain.stat1.Panels[4].Text := '当前OP盒被软解锁'
  else frmDemoMain.stat1.Panels[4].Text := '当前OP盒没有软解锁';

  if flag and $20 = $20 then
    frmDemoMain.stat1.Panels[5].Text := '当前OP盒硬件解锁'
  else frmDemoMain.stat1.Panels[5].Text := '当前OP盒硬件没解锁';

  if flag and $40 = $40 then
    frmDemoMain.stat1.Panels[6].Text := '结果的最低位永远为零'
  else frmDemoMain.stat1.Panels[6].Text := '结果的最低位可能非零';

  case Mode of
    0:
     begin
        frmDemoMain.lbl3.Caption := Format(sfor,[NetWeight]);
        frmDemoMain.lbl5.Caption := Format(sfor,[0.00]);
        frmDemoMain.lbl7.Caption := Format(sfor,[0.00]);
     end;
    1,
    2:
     begin
        frmDemoMain.lbl3.Caption := Format(sfor,[NetWeight]);
        frmDemoMain.lbl5.Caption := Format(sfor,[Tare]);
        frmDemoMain.lbl7.Caption := Format(sfor,[Grossweight]);
        if Mode=2 then frmDemoMain.lbl6.Caption := '预制皮重:';
     end;
    $7F:
     begin
        s := format('原始AD:%d,'+'滤波后AD:%d,'+'零点AD:%d,'+'重量AD:%d',[OldAd,FilterAd,ZeroAd,WeightAd]);
        frmDemoMain.mmo1.Lines.Add(s);
     end;
  end;

  if flagMode = $FF then
  begin
    frmDemoMain.lbl3.Caption := '----';
    frmDemoMain.lbl5.Caption := '----';
    frmDemoMain.lbl7.Caption := '----';
  end;

end;


procedure TfrmDemoMain.btn1Click(Sender: TObject);
begin
  if __Open(PChar(cbb1.Text),StrToInt(cbb2.Text)) then
    mmo1.Lines.Add('串口打开成功')
  else
    mmo1.Lines.Add('串口打开失败');
end;

procedure TfrmDemoMain.FormCreate(Sender: TObject);
begin
//  ECRIDCaption:='123456';
  __Config(5,2000,True,nil,@DisplayWeightEvent,
              nil,nil,@DisplayIS1Info,nil);
end;

procedure TfrmDemoMain.btn2Click(Sender: TObject);
begin
  if __Close then
    mmo1.Lines.Add('串口关闭成功')
  else
    mmo1.Lines.Add('串口关闭失败');
end;

procedure TfrmDemoMain.btndeTareClick(Sender: TObject);
begin
//  if isCanTare then
  __udeTare;
end;

procedure TfrmDemoMain.btnClearClick(Sender: TObject);
begin
  __uCleart;
end;

procedure TfrmDemoMain.Button1Click(Sender: TObject);
begin
  ShowMessage(__GetWeight());
end;

end.
