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

📄 unit1.pas

📁 Delphi下串口通讯源代码(本人用的测试程序)
💻 PAS
📖 第 1 页 / 共 3 页
字号:
    strrec:string;
begin
    strMidHighFreq:='';  
    if(combobox3.ItemIndex =2) then
    begin
       if(strtoint(edit16.Text)<4063)then edit8.text:='01'
       else if(strtoint(edit16.Text)<10133)then edit8.text:='02'
       else if(strtoint(edit16.Text)<18143)then edit8.text:='04'
    end;

    if not timer1.Enabled then timer1.Enabled :=true;
    SendBuffer[0]:=$53;
    if edit2.Text<>'' then
        SendBuffer[1]:=strtoint('$'+edit2.Text)
    else
        SendBuffer[1]:=0;
    if edit3.Text<>'' then
        SendBuffer[2]:=strtoint('$'+edit3.Text)
    else
        SendBuffer[2]:=0;
    if edit4.Text<>'' then
        SendBuffer[3]:=strtoint('$'+edit4.Text)
    else
        SendBuffer[3]:=0;
    if edit5.Text<>'' then
        SendBuffer[4]:=strtoint('$'+edit5.Text)
    else
        SendBuffer[4]:=0;
    if edit6.Text<>'' then
        SendBuffer[5]:=strtoint('$'+edit6.Text)
    else
        SendBuffer[5]:=0;
    if edit7.Text<>'' then
        SendBuffer[6]:=strtoint('$'+edit7.Text)
    else
        SendBuffer[6]:=0;
    if edit8.Text<>'' then
        SendBuffer[7]:=strtoint('$'+edit8.Text)
    else
        SendBuffer[7]:=0;
    if edit9.Text<>'' then
        SendBuffer[8]:=strtoint('$'+edit9.Text)
    else
        SendBuffer[8]:=0;
    if edit10.Text<>'' then
        SendBuffer[9]:=strtoint('$'+edit10.Text)
    else
        SendBuffer[9]:=0;
    if edit11.Text<>'' then
        SendBuffer[10]:=strtoint('$'+edit11.Text)
    else
        SendBuffer[10]:=0;
    if edit12.Text<>'' then
        SendBuffer[11]:=strtoint('$'+edit12.Text)
    else
        SendBuffer[11]:=0;

    SendBuffer[12]:=SendBuffer[1];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[2];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[3];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[4];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[5];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[6];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[7];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[8];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[9];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[10];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[11];
    SendBuffer[12]:=$ff-SendBuffer[12];
    SendBuffer[12]:=SendBuffer[12]+1;
    SendBuffer[13]:=$45;

    edit13.Text :=format('%2.2x',[SendBuffer[12]]);



{
  ss:='$'+edit1.Text;
  if TryStrToInt(ss, ii) then
  begin
    SetBuffer(ii);
    BufferToMemo();
  end
  else
    showmessage('数据中有非数字字符(0~9,A~F,a~f),请修改后重试。');
 }

     vTmp:=VarArrayCreate([0,13],VarByte);
     for i:=0 to 13 do
     begin
          vTmp[i]:=SendBuffer[i];
     end;
     ovTmp:=vTmp;
     MSCOMM1.InputMode:=1;
     MSCOMM1.InBufferCount :=0;
     mscomm1.Output :=ovTmp;
     SLEEP(50);
     if(mscomm1.InBufferCount >=14)then
     BEGIN
        MSCOMM1.InputMode :=comInputModeBinary;
        iReceived:=MSCOMM1.InBufferCount;
        ovTmp:=MSCOMM1.Input ;
        vTmp:=VarArrayCreate([0,13],varByte);
        vTmp:=ovTmp;
        strrec:='';
        for i:=0 to 13 do
        begin
           bTmp:=vTmp[i];
           ByteArray[I]:=bTmp;
           strrec:=strrec+format('%2.2x',[bTmp])+' ';
        end;
        memo1.Lines.Add(strrec);
     END
     ELSE
     begin
        bflagsuccess:=false;
        showmessage('无返回数据,请检查线路和端口设置后重试。');
        Timer1.Enabled :=false;
        exit;
     end;
     if checkbox1.Checked =true then
     begin
         i:=strtoint('$'+edit5.Text)*256;
         i:=i+strtoint('$'+edit6.Text)+strtoint(edit15.Text );

         edit6.Text :=format('%2.2x',[lo(i)]);
         edit5.Text :=format('%2.2x',[Hi(i)]);
     end
     else if checkbox2.Checked then
     begin
         i:=strtoint('$'+edit5.Text)*256;
         i:=i+strtoint('$'+edit6.Text)-strtoint(edit15.Text );
         edit6.Text :=format('%2.2x',[lo(i)]);
         edit5.Text :=format('%2.2x',[Hi(i)]);
     end;

     if checkbox4.Checked then  button5.Click
     else
     begin
        label21.Caption :='Sm=NULL';
        label25.Caption :='AFC=NULL';
     end;
     if checkbox5.Checked then  button4.Click
     else
     begin
        label22.Caption :='Sh=NULL';
        label26.Caption :='A2..0=NULL';
     end;

end;

procedure TForm1.FF1Click(Sender: TObject);
begin
  SetBuffer($FF);
  BufferToMemo();
end;

procedure TForm1.N5Click(Sender: TObject);
var
  ss:string;
  ii:integer;
begin
end;

procedure TForm1.N4Click(Sender: TObject);
var
  i,j:integer;
  strmemo:string;
begin
      for j:=0 to 31 do
      begin
          for i:=0 to 7 do
          begin
             buf_rec[j][i]:=8*j+i;
          end;
      end;
      BufferToMemo;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    memo1.Clear ;
end;

procedure TForm1.Edit9Change(Sender: TObject);

begin
    SendBuffer[0]:=$53;
    if edit2.Text<>'' then
        SendBuffer[1]:=strtoint('$'+edit2.Text)
    else
        SendBuffer[1]:=0;
    if edit3.Text<>'' then
        SendBuffer[2]:=strtoint('$'+edit3.Text)
    else
        SendBuffer[2]:=0;
    if edit4.Text<>'' then
        SendBuffer[3]:=strtoint('$'+edit4.Text)
    else
        SendBuffer[3]:=0;
    if edit5.Text<>'' then
        SendBuffer[4]:=strtoint('$'+edit5.Text)
    else
        SendBuffer[4]:=0;
    if edit6.Text<>'' then
        SendBuffer[5]:=strtoint('$'+edit6.Text)
    else
        SendBuffer[5]:=0;
    if edit7.Text<>'' then
        SendBuffer[6]:=strtoint('$'+edit7.Text)
    else
        SendBuffer[6]:=0;
    if edit8.Text<>'' then
        SendBuffer[7]:=strtoint('$'+edit8.Text)
    else
        SendBuffer[7]:=0;
    if edit9.Text<>'' then
        SendBuffer[8]:=strtoint('$'+edit9.Text)
    else
        SendBuffer[8]:=0;
    if edit10.Text<>'' then
        SendBuffer[9]:=strtoint('$'+edit10.Text)
    else
        SendBuffer[9]:=0;
    if edit11.Text<>'' then
        SendBuffer[10]:=strtoint('$'+edit11.Text)
    else
        SendBuffer[10]:=0;
    if edit12.Text<>'' then
        SendBuffer[11]:=strtoint('$'+edit12.Text)
    else
        SendBuffer[11]:=0;

    SendBuffer[12]:=SendBuffer[1];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[2];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[3];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[4];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[5];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[6];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[7];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[8];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[9];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[10];
    SendBuffer[12]:=SendBuffer[12]+SendBuffer[11];
    SendBuffer[12]:=$ff-SendBuffer[12];
    SendBuffer[12]:=SendBuffer[12]+1;
    SendBuffer[13]:=$45;

    edit13.Text :=format('%2.2x',[SendBuffer[12]]);
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  ByteArray:array[0..13] of byte;
  i,j:integer;
    ovTmp:OleVariant;
    vTmp:Variant;
    ADDR:BYTE;
    bTmp,recode,realcode,bTmp2:byte;
    bflagsuccess:boolean;
    strrec,VIFL,FMIFL:string;
    AFCWIN,PONR:integer;
begin
    SendBuffer[0]:=$53;
    SendBuffer[1]:=$08;
    SendBuffer[2]:=$87;
    SendBuffer[3]:=$00;
    SendBuffer[4]:=$00;
    SendBuffer[5]:=$00;
    SendBuffer[6]:=$00;
    SendBuffer[7]:=$00;
    SendBuffer[8]:=$00;
    SendBuffer[9]:=$00;
    SendBuffer[10]:=$00;
    SendBuffer[11]:=$00;
    SendBuffer[12]:=$71;
    SendBuffer[13]:=$45;

     vTmp:=VarArrayCreate([0,13],VarByte);
     for i:=0 to 13 do
     begin
          vTmp[i]:=SendBuffer[i];
     end;
     ovTmp:=vTmp;
     MSCOMM1.InputMode:=1;
     MSCOMM1.InBufferCount :=0;
     mscomm1.Output :=ovTmp;
     SLEEP(50);
     if(mscomm1.InBufferCount >=14)then
     BEGIN
        MSCOMM1.InputMode :=comInputModeBinary;
        ovTmp:=MSCOMM1.Input ;
        vTmp:=VarArrayCreate([0,13],varByte);
        vTmp:=ovTmp;
        strrec:='';
        for i:=0 to 13 do
        begin
           bTmp:=vTmp[i];
           ByteArray[I]:=bTmp;
           strrec:=strrec+format('%2.2x',[bTmp])+' ';
           if (i=3)then
           begin
               if  (bTmp and $40)=$40 then
               begin
                     label19.Caption :='VIFL=1';
                     VIFL:='  1';
               end
               else
               begin
                     label19.Caption :='VIFL=0';
                     VIFL:='  0';
               end;

              label21.Caption :='Sm='+format('%2.2X',[bTmp])+'H';
              recode:=btmp shr 1;
              recode:=recode and $0F;
              label25.Caption :='AFC='+format('%2.2X',[recode])+'H';
              realcode:= recode;
              if(recode>7)then recode:=15-recode;
              if ((btmp and $40)=$40)then
              begin
                if(bcheckstart) and (recode<7) and ((btmp and $80)=$80) then    //bcheckstart=true表示没有检测到VIFL=1
                begin
                    TmpFreq:=edit16.Text;
                    TmpVIFL:=recode;
                    bcheckstart:=false;//已经检测到VIFL=1
                end
                else  //已经检测到VIFL=1,现检测VIFL的最小值
                begin
                    if recode<TmpVIFL then
                    begin
                        TmpFreq:=edit16.Text;
                        TmpVIFL:=recode;
                    end;
                end;
              end
              else if(not bcheckstart)then
              begin
                  inc(chlcount);
                  if checkbox4.Checked then   differ:=inttostr(strtoint(edit16.Text)-strtoint(TmpFreq))
                  else differ:='NONE';
                  memo2.Lines.Add(inttostr(chlcount)+'、'+TmpFreq+ '    '+differ);
                  bcheckstart:=true;
              end;
              if((btmp and $80)=$80) then AFCWIN:=1
              else AFCWIN:=0;
              if((btmp and $01)=$01) then PONR:=1
              else PONR:=0;
              strMidHighFreq:=edit16.Text+'  '+'  '+inttostr(recode)+'__'+inttostr(AFCWIN)+'  '+VIFL+'__'+inttostr(PONR);
//              strMidHighFreq:=edit16.Text+'  '+inttostr(btmp)+'  '+inttostr(realcode)+'  '+inttostr(recode)+VIFL;
              memo3.Lines.Add(strMidHighFreq);
              oldtmp:= btmp;
           end;

        end;
        memo1.Lines.Add(strrec);
     END
     ELSE
     begin
        bflagsuccess:=false;
        showmessage('无返回数据,请检查线路和端口设置后重试。');
        exit;
     end;

end;

procedure TForm1.ComboBox3Change(Sender: TObject);
begin
    case combobox3.ItemIndex of
      1:                 //制式设置
        begin
          edit1.Text :='53';
          edit2.Text :='0A';
          edit3.Text :='86';
          edit4.Text :='04';
          edit5.Text :='00';

⌨️ 快捷键说明

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