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

📄 tempreader.pas

📁 温度恒温控制,与PLC,工控板通讯,实现炉字过程控制.
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit tempreader;

interface
  uses Windows,Dialogs,Classes,sysutils,strutils,CPort,
       graphics,stdctrls,extctrls,forms,SyncObjs,MSCommLib_TLB;
type
  Ttempreader=class(TThread)
  private
     fcomm:Tmscomm;
     fcomport:Tcomport;
     seq:integer;
     procedure endteminate(sender:Tobject);
     function makesignal(node:integer):string;
    procedure comm_ReceiveData;
    procedure comportreceive;
    function PIDCalc(HeatPort:integer;temp:string):double;
  protected
     procedure execute;override;
  public
     function initcomm(scom:string):boolean;
     constructor create();

end;
implementation
uses test, dsVariable, Driver;
constructor Ttempreader.create();
begin
{  fcomm:=Tmscomm.Create(nil);
  fcomm.Settings:='9600,n,8,1';
  fcomm.RThreshold:=1;   }
  self.OnTerminate:=self.endteminate;
  fcomport:=Tcomport.Create(nil);
  fcomport.BaudRate:=br9600;
  self.FreeOnTerminate:=false;
  self.seq:=1;
  inherited create(true);
end;

procedure Ttempreader.execute;
var AA:integer;
    sout:string;
begin
  while not terminated do
  begin
      AA:=GETTICKCOUNT;
      synchronize(comportreceive);
//      frmglass.edit1.Text:=inttostr(GetTickCount-aa); //debug}
      self.Suspend;
  end;
end;

function Ttempreader.initcomm(scom:string):boolean;
begin
    result:=true;
  //  fcomm.CommPort:=strtoint(scom);
   fcomport.Port:=scom;
    try
//      fcomm.PortOpen:=true;
      fcomport.Connected:=true;
    except
       begin
          result:=false;
       end;
    end;
end;

procedure Ttempreader.comm_ReceiveData;
var sout,stemp:string;
    i,k,m:integer;
    inputstring:string;
begin
end;
function Ttempreader.makesignal(node:integer):string;
var sign:string;
    checksum:integer;
    i:integer;
begin
  checksum:=0;
  sign:='#01'+inttostr(node);   //读RemoDAQ 8018B 的温度生成checksum;
  for i:=1 to length(sign) do
    checksum:=checksum+ord(sign[i]);
  sign:=sign+rightstr(inttohex(checksum,4),2)+chr(13);
  result:=sign;
end;


procedure Ttempreader.endteminate(sender: Tobject);
begin
  if self.fcomport.Connected then
  begin
    self.fcomport.Connected:=false;
    self.fcomport.Free;
  end;
end;

procedure Ttempreader.comportreceive();
var sout:string;
    iheartport:integer;
    inputstring:string;
    rpidout:double;
    obj:Pasync;
    dwstart:dword;
    stemp:array[0..8] of string;
    i:integer;
    Fedit:Tedit;
    Ftime:TTimer;
    Fpanel:Tpanel;
    icontrolseq:integer;
    itemp:integer;
begin
  try
      itemp:=0;
      dwstart:=GetTickCount;
      sout:='#0'+inttostr(tempseq)+chr(13);
//      sout:=makesignal(tempseq);取8个温度时不能checksum;
{      self.fcomm.InBufferCount:=0;
      self.fcomm.OutBufferCount:=0;
      self.fcomm.Output:=sout; }
      self.fcomport.ClearBuffer(true,true);
      self.fcomport.WriteStr(sout);
      sleep(100);   //发送命令后,最少需要延时80ms后,才能到达58个字节;
//      frmglass.edit1.Text:=inttostr(GetTickCount-dwstart); //debug}
      inputstring:='';
      self.fcomport.ReadStr(inputstring,58);

{      inputstring:=self.fcomm.Input; }
 //     inputstring:='>+0030.4+0060.5+0014.4+0015.4+0016.4+0017.4+0020.4+0050.4 ';
 //     frmglass.Panel15.Caption:=inputstring;
      if length(inputstring)<>58 then
      begin
         readtempcount:=readtempcount+1;
//         if readtempcount>5 then
         frmglass.memlog.Lines.Add(datetimetostr(now)+'读取8018温度出错  8018编号:'+inttostr(tempseq));
         exit;
      end
      else
        readtempcount:=0;
      stemp[0]:=copy(inputstring,3,6);
      if stemp[0]='8888.8' then
         stemp[0]:='0'
      else
      try
        stemp[0]:=inttostr(round(strtofloat( stemp[0])));
      except
        stemp[0]:='0';
      end;
          stemp[1]:=copy(inputstring,10,6);
      if stemp[1]='8888.8' then
         stemp[1]:='0'
      else
      try
        stemp[1]:=inttostr(round(strtofloat( stemp[1])));
      except
        stemp[1]:='0';
      end;
      stemp[2]:=copy(inputstring,17,6);
      if stemp[2]='8888.8' then
       stemp[2]:='0'
      else
      try
        stemp[2]:=inttostr(round(strtofloat( stemp[2])));
      except
        stemp[2]:='0';
      end;
      stemp[3]:=copy(inputstring,24,6);
      if stemp[3]='8888.8' then
       stemp[3]:='0'
      else
      try
        stemp[3]:=inttostr(round(strtofloat( stemp[3])));
      except
        stemp[3]:='0';
      end;
      stemp[4]:=copy(inputstring,31,6);
      if stemp[4]='8888.8' then
        stemp[4]:='0'
      else
      try
        stemp[4]:=inttostr(round(strtofloat( stemp[4])));
      except
        stemp[4]:='0';
      end;
      stemp[5]:=copy(inputstring,38,6);
      if stemp[5]='8888.8' then
        stemp[5]:='0'
      else
      try
        stemp[5]:=inttostr(round(strtofloat( stemp[5])));
      except
        stemp[5]:='0';
      end;
      stemp[6]:=copy(inputstring,45,6);
      if  stemp[6]='8888.8' then
        stemp[6]:='0'
      else
      try
        stemp[6]:=inttostr(round(strtofloat( stemp[6])));
      except
        stemp[6]:='0';
      end;
      stemp[7]:=copy(inputstring,52,6);
      if stemp[7]='8888.8' then
        stemp[7]:='0'
      else
      try
        stemp[7]:=inttostr(round(strtofloat( stemp[7])));
      except
        stemp[7]:='0';
      end;

  case tempseq of
   0:
   begin
    for i:=1 to 8 do
    begin
      iheartport:=2;
      lpDioWriteBit.bit:=7-i;
      if i=8 then
      begin
        iheartport:=1;
        lpDioWriteBit.bit:=6;
        //每一组port只用7个位,第8位留一位控制全关用,所以转到下一个port的第一位
      end;
      lpDioWriteBit.port:=iheartport;    //iheartport根据端口要变;
      if strtoint(stemp[i-1])>ipidtemp then
        rpidout:=PIDcalc(i,stemp[i-1])/8000
      else
        rpidout:=ipower/100;
{       if i=1 then
       begin
         starttime:=gettickcount;
//         frmglass.Edit6.Text:=floattostr(rpidout*8000);
      end; }
      if rpidout>=1 then
      begin
        rpidout:=1;
        pidout[i].state:=true;
      end
      else
        pidout[i].state:=false;
{         if i=8 then
            frmglass.Edit12.Text:=floattostr(rpidout); //debug }
      if (rpidout>0.001) and (rpidout<1)   then
      begin
{         if i=1 then
           frmglass.Edit7.Text:='开始';
         if i=8 then
           frmglass.Edit8.Text:='开始';  //debug }
        Ftime:=TTimer(frmglass.FindComponent('tmtemp'+inttostr(i)));
        Ftime.Interval:=trunc(pid_t*rpidout*1000);
        Ftime.Enabled:=true;
        lpDioWriteBit.state:=1;
        DRV_DioWriteBit(DeviceHandle,lpDioWriteBit); //打开PCL-722输出数据,输出占空比时间;
      end
      else
      begin
        if  (rpidout>=1) then
        begin
          lpDioWriteBit.state:=1;
{        if i=1 then
          frmglass.Edit10.Text:=inttostr(lpDioWriteBit.bit)
        else
          frmglass.Edit7.Text:=inttostr(lpDioWriteBit.bit);    }
          DRV_DioWriteBit(DeviceHandle,lpDioWriteBit);
        end
        else
        begin
          lpDioWriteBit.state:=0;
{        if i=1 then
          frmglass.Edit10.Text:=inttostr(lpDioWriteBit.bit)
        else
          frmglass.Edit7.Text:=inttostr(lpDioWriteBit.bit);    }
          DRV_DioWriteBit(DeviceHandle,lpDioWriteBit); //关闭PCL-722输出数据;
        end;
      end;
       Fpanel:=Tpanel(frmglass.FindComponent('pntem'+inttostr(i)));
       Fpanel.Caption:=inttostr(strtoint(stemp[i-1]));
       Fedit:=Tedit(frmglass.FindComponent('edtemp'+inttostr(i)));
       fedit.Text:=inttostr(strtoint(stemp[i-1]));
//       if (strtoint(stemp[i-1])>pidout[i].setpoint) then    改为设置报警温度
      itemp:=strtoint(stemp[i-1]);
      if (itemp>iwarntemp) then
       begin
         bwarn[i]:=true;
         fedit.color:=clred;
         fedit.Font.Color:=clwhite;
       end
       else
       begin
           bwarn[i]:=false;;
           fedit.Font.Color:=$006C6C00;
         if (itemp<100) then
           fedit.color:=$00FFFFE8;
         if (itemp<200) and (itemp>=100) then
           fedit.color:=$00A2FFAB;
         if (itemp<300) and (itemp>=200) then
           fedit.color:=$0080FFFF;
         if (itemp<400) and (itemp>=300) then
           fedit.color:=$00FFA4FF;
         if (itemp<500) and (itemp>=400) then
           fedit.color:=$00977AFC;
         if (itemp<600) and (itemp>=500) then
           fedit.color:=$00547AFC;
         if (itemp<700) and (itemp>=600) then
           fedit.color:=clLime;
       end;
    end;
   end;
   1:
   begin
    for i:=1 to 7 do
    begin
      iheartport:=1;
      lpDioWriteBit.bit:=6-i;
      if i=7 then
      begin
        iheartport:=0;
        lpDioWriteBit.bit:=6;
        //每一组port只用7个位,第8位留一位控制全关用,所以转到下一个port的第一位
      end;
      lpDioWriteBit.port:=iheartport;    //iheartport根据端口要变;
      if strtoint(stemp[i-1])>ipidtemp then
        rpidout:=PIDcalc(tempseq*8+i,stemp[i-1])/8000
      else
        rpidout:=ipower/100;
      if rpidout>1 then
      begin
        rpidout:=1;
        pidout[tempseq*8+i].state:=true;
      end
      else
        pidout[tempseq*8+i].state:=false;
      if (rpidout>0.001) and (rpidout<1)  then    //100%时不输出占空比;
      begin
        Ftime:=TTimer(frmglass.FindComponent('tmtemp'+inttostr(tempseq*8+i)));
        Ftime.Interval:=trunc(pid_t*rpidout*1000);
        Ftime.Enabled:=true;
        lpDioWriteBit.state:=1;
        DRV_DioWriteBit(DeviceHandle,lpDioWriteBit); //打开PCL-722输出数据,输出占空比时间;
      end
      else
      begin
        if  (rpidout>=1) then
        begin
          lpDioWriteBit.state:=1;
          DRV_DioWriteBit(DeviceHandle,lpDioWriteBit);
        end
        else
        begin
          lpDioWriteBit.state:=0;
          DRV_DioWriteBit(DeviceHandle,lpDioWriteBit); //关闭PCL-722输出数据;
        end;
      end;
       Fpanel:=Tpanel(frmglass.FindComponent('pntem'+inttostr(tempseq*8+i)));
       Fpanel.Caption:=inttostr(strtoint(stemp[i-1]));
       Fedit:=Tedit(frmglass.FindComponent('edtemp'+inttostr(tempseq*8+i)));
       fedit.Text:=inttostr(strtoint(stemp[i-1]));
 //      if (strtoint(stemp[i-1])>pidout[tempseq*8+i].setpoint) then
      itemp:=strtoint(stemp[i-1]);
       if (itemp>iwarntemp) then
       begin
         bwarn[tempseq*8+i]:=true;
         fedit.color:=clred;
         fedit.Font.Color:=clwhite;
       end
       else
       begin
          bwarn[tempseq*8+i]:=false;;
          fedit.Font.Color:=$006C6C00;
         if (itemp<100) then
           fedit.color:=$00FFFFE8;
         if (itemp<200) and (itemp>=100) then
           fedit.color:=$00A2FFAB;
         if (itemp<300) and (itemp>=200) then
           fedit.color:=$0080FFFF;
         if (itemp<400) and (itemp>=300) then
           fedit.color:=$00FFA4FF;
         if (itemp<500) and (itemp>=400) then
           fedit.color:=$00977AFC;
         if (itemp<600) and (itemp>=500) then
           fedit.color:=$00547AFC;
         if (itemp<700) and (itemp>=600) then
           fedit.color:=clLime;
       end;
    end;
   end;
  2:
   begin
    for i:=1 to 7 do
    begin
      iheartport:=0;
      lpDioWriteBit.bit:=6-i;
      if i=7 then
      begin
        iheartport:=5;
        lpDioWriteBit.bit:=6;
      end;
      lpDioWriteBit.port:=iheartport;    //iheartport根据端口要变;
      if strtoint(stemp[i-1])>ipidtemp then
        rpidout:=PIDcalc(tempseq*8+i-1,stemp[i-1])/8000
      else
        rpidout:=ipower/100;
      if rpidout>1 then
      begin
        rpidout:=1;
        pidout[tempseq*8+i-1].state:=true;
      end
      else
        pidout[tempseq*8+i-1].state:=false;
      if (rpidout>0.001) and (rpidout<1)  then
      begin
        Ftime:=TTimer(frmglass.FindComponent('tmtemp'+inttostr(tempseq*8+i-1)));
        Ftime.Interval:=trunc(pid_t*rpidout*1000);
        Ftime.Enabled:=true;
        lpDioWriteBit.state:=1;
        DRV_DioWriteBit(DeviceHandle,lpDioWriteBit); //打开PCL-722输出数据,输出占空比时间;
      end
      else
      begin
        if  (rpidout>=1) then
        begin
          lpDioWriteBit.state:=1;
          DRV_DioWriteBit(DeviceHandle,lpDioWriteBit);
        end
        else
        begin
          lpDioWriteBit.state:=0;
          DRV_DioWriteBit(DeviceHandle,lpDioWriteBit); //关闭PCL-722输出数据;
        end;
      end;
       Fpanel:=Tpanel(frmglass.FindComponent('pntem'+inttostr(tempseq*8+i-1)));
       Fpanel.Caption:=inttostr(strtoint(stemp[i-1]));
       Fedit:=Tedit(frmglass.FindComponent('edtemp'+inttostr(tempseq*8+i-1)));
       fedit.Text:=inttostr(strtoint(stemp[i-1]));
//       if (strtoint(stemp[i-1])>pidout[tempseq*8+i-1].setpoint) then
      itemp:=strtoint(stemp[i-1]);
       if (itemp>iwarntemp) then
       begin
         bwarn[tempseq*8+i-1]:=true;
         fedit.color:=clred;

⌨️ 快捷键说明

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