unit uMeasureDoubleDZ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uMeasureBase, dxExEdtr, SPComm, ADODB, DB, ExtCtrls, dxBar,
  dxBarExtItems, ImgList, StdCtrls, MPlayer, dxTL, dxCntner,uFunMesClient,ufuncom;

type
  TfrmMeasureDoubleDZ = class(TfrmMeasureBase)
    TimSenderOrder: TTimer;
    RadioOrder: TRadioGroup;
    procedure edtNumKeyPress(Sender: TObject; var Key: Char);
    procedure TimSenderOrderTimer(Sender: TObject);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    procedure FormCreate(Sender: TObject);
    procedure moCurRoutKeyPress(Sender: TObject; var Key: Char);
    procedure LbtnNewClick(Sender: TObject);
    procedure RadioOrderClick(Sender: TObject);
  private
    Canrec: Boolean;
    FirstID,SeID: String;
    function SendString(const str: string): Boolean;
    function  SaveMsgRoutBarcode(CurrootBar: String): Boolean; // 保存第二个电阻值
    procedure ShowMsg;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMeasureDoubleDZ: TfrmMeasureDoubleDZ;

implementation

uses PublicDBUnit;

{$R *.dfm}

procedure TfrmMeasureDoubleDZ.edtNumKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then
  begin
    meNum := StrToFloat(edtNum.Text) ;
    if (meNum > MyWork.MaxNum)or (meNum < MyWork.MinNum) then
    begin
      ImgRed.Visible := true   ;
      ImgGreen.Visible := false ;
      edtNum.text := '';
    end
    else
    begin
      ImgRed.Visible := false   ;
      ImgGreen.Visible := true ;
      moCurRout.SetFocus ;
    end;
  end;
end;

function TfrmMeasureDoubleDZ.SendString(const str: string): Boolean;
begin
  try
    Comm1.WriteCommData(Pchar(str),Length(str));
    Result := true;
  except
    Result := false;
  end;
end;

procedure TfrmMeasureDoubleDZ.TimSenderOrderTimer(Sender: TObject);
begin  
  SendString('?'+#10);
end;

procedure TfrmMeasureDoubleDZ.Comm1ReceiveData(Sender: TObject;
  Buffer: Pointer; BufferLength: Word);
var
  C_num : real;
  str :string;
begin
  ImageOff.Visible  := false ;
  ImageOn.Visible   := true ;
  if edtCode.Text = '' then Exit;
  SetLength(Str,BufferLength);
  move(buffer^,pchar(@Str[1])^,bufferlength);
  C_num := Getdianzhu(str) ;
  if meNum <> C_num then
  begin
    ImgYel.Visible := not ImgYel.Visible ;
    meNum := C_num ;
    Exit;
  end;
  if (meNum > MyWork.MaxNum)or (meNum < MyWork.MinNum) then
  begin
    ImgRed.Visible := true   ;
    ImgGreen.Visible := false ;
  end
  else
  begin
    ImgRed.Visible := false   ;
    ImgGreen.Visible := true ;
  end;
  edtNum.Text := FloatToStr(meNum)+ 'Ω' ;

end;

procedure TfrmMeasureDoubleDZ.FormCreate(Sender: TObject);
begin
  inherited;
  Canrec := false ;
  FirstID := IntToStr(MyWork.dWorkID);
  SeID := IntToStr(MyWork.nextWorkID);
  if SeID = '-1' then
  begin
    dmPublic.ShowMessageBox('请选择单电阻测量程序!');
    Application.Terminate ;
  end;
end;

procedure TfrmMeasureDoubleDZ.moCurRoutKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key <> #13 then Exit;
  if RadioOrder.ItemIndex = 1 then
  begin
    if SaveMsgRoutBarcode(moCurRout.Text) then
    begin
      RadioOrder.ItemIndex := 0 ;
      dmPublic.RefeshWorkMsg(FirstID) ;
      ShowMsg ;
      LbtnNew.Click ;
    end
    else  moCurRout.SelectAll ;
  end else
  begin
    if MeasurebyRoutBarcode(moCurRout.Text) then
    begin
      RadioOrder.ItemIndex := 1;
      dmPublic.RefeshWorkMsg(SeID) ;
      ShowMsg ;
      moCurRout.Text := '';
      moCurRout.SetFocus ;
    end
    else moCurRout.SelectAll ;

 end;

end;

procedure TfrmMeasureDoubleDZ.LbtnNewClick(Sender: TObject);
begin
  inherited;
  RadioOrder.ItemIndex := 0;
end;

function TfrmMeasureDoubleDZ.SaveMsgRoutBarcode(CurrootBar: String): Boolean;
var
  isOK: Boolean;
  iState: integer;
  Selsql,S: String;
  toWorkID,Towork,Remark: String;
const
  SetEndSQL = 'Update tabCodeBring Set iType =1 Where ID =%s';
begin
  Result := false ;
  if (meNum > MyWork.MaxNum) or (Menum < MyWork.MinNum)then
  begin
    ToWarning;
    if trim(CurrootBar) = '' then Exit;
    if trim(CurrootBar) = OKNo then Exit;
  end;
  CurrootBar  :=  trim(CurrootBar) ;
  if CurrootBar = '' then  Exit;

  CurBringID :=  GetBringID(MyDuct.Barcode);
  with dmPublic do
  begin
    if CurBringID = '' then
      CurBringID :=  SaveBringID(MyDuct,IntToStr(curLineID));

    if CurrootBar = OkNo then
    begin
      iState := 1 ;
      toWorkID := intToStr(MyWork.nextWorkID);
      Towork := MyWork.nextWorkName;
      Remark := MyWork.WorkName + ' 测量值:'+ FloatToStr(meNum) + MyWork.mUnit;
    end  else
    begin
      iState := 0;
      S := 'SELECT ID, toWorkID, ToWork, iState,Remark FROM tabRouting WHERE RoutNo ='''+ CurrootBar +''' ';
      with dmpublic do
      begin
        ExcuteSQLCommand(S,1);
        if qryCom1.IsEmpty then
        begin
          MessageBox(handle,pchar('工艺条码不正确！'),'系统提示',MB_OK + MB_ICONWARNING);
          Exit;
        end
        else begin
          toWorkID := qryCom1.Fields[1].AsString ;
          ToWork := qryCom1.Fields[2].AsString ;
          Remark := qryCom1.Fields[4].AsString ;
        end;
      end;
    end;
     //isOK :=  SaveAutoWork(ManworkList);  //保存排班
    SaveCodeBringSub(CurBringID,CurrootBar ,toWorkID,ToWork,Remark,MyWork,meNum,iState);

      // SaveMductIDbyList(MDuctAddNumList,inttoStr(CurLineID),MyDuct.Barcode); // 保存自动安装料
      addToRecList(MyDuct.DuctID,MyDuct.DuctNo,MyDuct.DuctName,MyDuct.Barcode);
      ///AddtoTask(MyDuct.DuctID,mywork.WorkNo,CurLineID);
    if MyWork.isEnd then
    begin
      ExcuteSQLCommand(Format(SetEndSQL, [CurBringID]), 0);
      AddToTask(MyDuct.DuctID,'DoNumber',CurLineID);
      S := ' update tabCurTask set finish = 1 where finish <> 1 and  Donumber >= sNumber  ';
      dmPublic.ExcuteSQLCommand(S,0);
    end;
  end;
  Result := true  ;
end;



procedure TfrmMeasureDoubleDZ.RadioOrderClick(Sender: TObject);
begin
  inherited;
  if RadioOrder.ItemIndex = 1 then
    dmPublic.RefeshWorkMsg(SeID)
  else   dmPublic.RefeshWorkMsg(FirstID)   ;

  ShowMsg;
  edtCode.SetFocus ;
end;

procedure TfrmMeasureDoubleDZ.ShowMsg;
begin
  edtMax.Text :=  FloatToStr(MyWork.MaxNum )+ MyWork.mUnit ;
  edtMin.Text := FloatToStr(MyWork.MinNum )+ MyWork.mUnit ;
  plCurRout.Caption := '    本次工序为：' + MyWork.WorkName ;
  self.Caption := ModuleName +'--' + MyWork.WorkName ;
  edtNum.Text := '0 Ω' ;
  meNum := 0 ;
end;

end.
