⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcontrol.~pas

📁 自己用API编写的端口通讯
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:
procedure TForm1.N2Click(Sender: TObject);
begin
     Self.ListView1.Clear;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
   tempIni:Tinifile;
begin
   tempIni:=TIniFile.Create( GetCurrentDir+ '\TestControl.ini');
   try
      self.FUnitMaxNumber:=StrToInt(tempIni.ReadString('config','单元总数','50'));
      self.FCabinetControler:=TCabinetControler.Create(tempIni.ReadString('config','端口号','COM1'));
//      Self.FCabinetControler:=TCabinetControler.Create('COM3');

//      showmessage(IntToStr(Self.FUnitMaxNumber));
   finally
      tempIni.free;
   end;
end;

procedure TForm1.ButtonQueryClick(Sender: TObject);
begin
     Self.QueryCabinet;
end;


//查询
procedure TForm1.QueryCabinet;
var
    tempControl:TCabinetControler;
    tempState:DiskBoxOpenState;
    i:integer;
    tempItem:TListItem;
begin
    self.ListView1.Clear;
    if ((self.ComboBoxCom.Text='') or (self.ComboBoxCabinet.Text='')) then
    begin
        ShowMessage('未选择串口、柜子');
        exit;
    end;
    for i:=0 to 15  do
    begin
        tempState[i]:=1;
    end;
//    {
    if  (self.FCabinetControler.GetIsDiskBoxOpen(Self.ComboBoxCabinet.ItemIndex,tempState)) then
        begin
            for i:=0 to 15 do
            begin
               tempItem:=Self.ListView1.Items.Add;
               tempItem.Caption:='抽屉'+IntToStr(i+1);
               if tempState[i]<>0 then
                  tempItem.SubItems.Add('开启')
               else
                  tempItem.SubItems.Add('关闭');
            end;
            ShowMessage(Self.ComboBoxCabinet.Text+'查询成功');
        end
        else
        begin
            ShowMessage(Self.ComboBoxCabinet.Text+'查询失败')
        end;
//        }
    {
    tempControl:=TCabinetControler.Create(self.ComboBoxCom.Text);
    try
        if  (tempControl.GetIsDiskBoxOpen(Self.ComboBoxCabinet.ItemIndex,tempState)) then
        begin
            for i:=0 to 15 do
            begin
               tempItem:=Self.ListView1.Items.Add;
               tempItem.Caption:='抽屉'+IntToStr(i+1);
               if tempState[i]<>0 then
                  tempItem.SubItems.Add('开启')
               else
                  tempItem.SubItems.Add('关闭');
            end;
            ShowMessage(Self.ComboBoxCabinet.Text+'查询成功');
        end
        else
        begin
            ShowMessage(Self.ComboBoxCabinet.Text+'查询失败')
        end;
     finally
        tempControl.Destroy;
     end;
     }
end;

procedure TForm1.ButtonClearClick(Sender: TObject);
begin
     Self.ListView1.Clear;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
     Self.FCabinetControler.Free;
end;

//遍历所有抽屉,并点亮所有指示灯。
procedure TForm1.ButtonLightAllUnitClick(Sender: TObject);
var
    ErrorMessage:String;
begin
    if   (Self.ComboBoxCabinet.Text='')  then
    begin
       ShowMessage('未选择柜子');
       exit;
    end;
    //判断线程是否运行,如运行则终止线程并重新运行。
    if (Self.FMyThread<>nil) then
    begin
         Self.FMyThread.Terminate;
         Self.FMyThread.WaitFor;
         Self.FMyThread.Destroy;
         Self.FMyThread:=nil;
    end;
    if (Self.FMyThread=nil) then
    begin
         Self.FMyThread:=LightAllBoxThread.Create(False);
         if (Self.FMyThread<>nil) then
         begin
             Self.ButtonQuery.Enabled:=False;
             Self.ButtonOpenOneBox.Enabled:=False;
             Self.ButtonLightOneUnit.Enabled:=False;
             Self.ButtonLightOneBox.Enabled:=False;
             Self.ButtonQuery.Enabled:=False;
             Self.ButtonLightAllUnit.Enabled:=False;
             Self.ButtonStopLightAllUnit.Enabled:=True;
             Self.ButtonStopLightOneBox.Enabled:=False;
             Self.ButtonOpenAllBox.Enabled:=False;
         end
         else
         begin
             ShowMessage('点亮所有单元失败');
             Self.FMyThread.Terminate;
             Self.FMyThread.WaitFor;
             Self.FMyThread.Destroy;
             Self.FMyThread:=nil;
         end;
    end;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
//     CanClose:=False;
     if  (Self.FMyThread<>nil) then
     begin
          Self.FMyThread.Terminate;
          Self.FMyThread.WaitFor;
          Self.FMyThread.Destroy;
          Self.FMyThread:=nil;
          CanClose:=True;
     end;
     if (Self.FLightOneBoxThread<>nil) then
     begin
          Self.FLightOneBoxThread.Terminate;
          Self.FLightOneBoxThread.WaitFor;
          Self.FLightOneBoxThread.Destroy;
          Self.FLightOneBoxThread:=nil;
          CanClose:=True;
     end;
     if ((Self.FMyThread=nil) and (Self.FLightOneBoxThread=nil)) then
        CanClose:=True
     else
        CanClose:=False;
end;

procedure TForm1.ButtonStopLightAllUnitClick(Sender: TObject);
begin
     if (Self.FMyThread<>nil) then
     begin
          Self.FMyThread.Terminate;
          Self.FMyThread.WaitFor;
          Self.FMyThread.Destroy;
          Self.FMyThread:=nil;
          Self.FLightOneBoxThread:=nil;
          Self.ButtonQuery.Enabled:=False;
          Self.ButtonOpenOneBox.Enabled:=True;
          Self.ButtonLightOneUnit.Enabled:=True;
          Self.ButtonLightOneBox.Enabled:=True;
          Self.ButtonQuery.Enabled:=True;
          Self.ButtonLightAllUnit.Enabled:=True;
          Self.ButtonStopLightAllUnit.Enabled:=False;
          Self.ButtonStopLightOneBox.Enabled:=False;
          Self.ButtonOpenAllBox.Enabled:=True;
     end;
end;

//开启所有抽屉,检测开门机构是否正常
procedure TForm1.ButtonOpenAllBoxClick(Sender: TObject);
var
     i:integer;
     ErrorMessage:string;
     bOK:Boolean;
begin
     if (Self.ComboBoxCabinet.Text='') then
     begin
          ShowMessage('没有选择柜子');
          exit;
     end;
     Self.ButtonQuery.Enabled:=False;
     Self.ButtonOpenOneBox.Enabled:=False;
     Self.ButtonLightOneUnit.Enabled:=False;
     Self.ButtonLightOneBox.Enabled:=False;
     Self.ButtonLightAllUnit.Enabled:=False;
     Self.ButtonStopLightAllUnit.Enabled:=False;
     Self.ButtonStopLightOneBox.Enabled:=False;
     Self.ButtonOpenAllBox.Enabled:=False;
     Self.ButtonClear.Enabled:=False;
     for i:=0 to 15 do
     begin
         if not (Self.FCabinetControler.OpenDiskBox(i,Self.ComboBoxCabinet.ItemIndex)) then
         begin
            ErrorMessage:='开启抽屉'+IntToStr(i)+'失败';
            Self.ButtonQuery.Enabled:=True;
            Self.ButtonOpenOneBox.Enabled:=True;
            Self.ButtonLightOneUnit.Enabled:=True;
            Self.ButtonLightOneBox.Enabled:=True;
            Self.ButtonQuery.Enabled:=True;
            Self.ButtonLightAllUnit.Enabled:=True;
            Self.ButtonStopLightAllUnit.Enabled:=False;
            Self.ButtonStopLightOneBox.Enabled:=False;
            Self.ButtonOpenAllBox.Enabled:=True;
            Self.ButtonClear.Enabled:=True;
            exit;
         end;
         Sleep(1000);
     end;
     ShowMessage('开启所有抽屉完成');
     Self.ButtonQuery.Enabled:=True;
     Self.ButtonOpenOneBox.Enabled:=True;
     Self.ButtonLightOneUnit.Enabled:=True;
     Self.ButtonLightOneBox.Enabled:=True;
     Self.ButtonLightAllUnit.Enabled:=True;
     Self.ButtonStopLightAllUnit.Enabled:=False;
     Self.ButtonStopLightOneBox.Enabled:=False;
     Self.ButtonOpenAllBox.Enabled:=True;
     Self.ButtonClear.Enabled:=True;
end;

procedure TForm1.ButtonStopLightOneBoxClick(Sender: TObject);
begin
     if (Self.FLightOneBoxThread<>nil) then
     begin
          Self.FLightOneBoxThread.Terminate;
          Self.FLightOneBoxThread.WaitFor;
          Self.FLightOneBoxThread.Destroy;
          Self.FLightOneBoxThread:=nil;
          Self.ButtonQuery.Enabled:=False;
          Self.ButtonOpenOneBox.Enabled:=True;
          Self.ButtonLightOneUnit.Enabled:=True;
          Self.ButtonLightOneBox.Enabled:=True;
          Self.ButtonQuery.Enabled:=True;
          Self.ButtonLightAllUnit.Enabled:=True;
          Self.ButtonStopLightAllUnit.Enabled:=False;
          Self.ButtonStopLightOneBox.Enabled:=False;
          Self.ButtonOpenAllBox.Enabled:=True;
     end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -