Delphi 485 Modbus Rtu读写RFID示例源码

 读写器介绍:MODBUS RTU 读卡器IC-10MR PLC读卡器 M1/S50/S70/ 安卓Android-淘宝网 (taobao.com)https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.21.65135b43XOBlto&id=41901878033

procedure TForm1.FormShow(Sender: TObject);
var
  i:Integer;
  count:Integer;
begin
  dispstr:='';
  count := 0;

  for i := 1 to 20 do                                       //开发环境调试时,请关闭菜单栏  Tool/Debugger Options/integrated debugging 选项
  begin
      Comm1.CommName:='\\.\COM' + IntToStr(i);

      try
         Comm1.StartComm; //打开串口
         Comm1.StopComm; //打开串口
         ComboBox1.Items.Add('COM' + IntToStr(i));
         count := count + 1;
      except

      end;

    if(count>0) then
    begin
         ComboBox1.ItemIndex:=count-1;
    end;
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if(BitBtn1.Caption = '打开串口') then
  begin
    try
        Comm1.CommName:='\\.\'+ComboBox1.Text;
        //以下代码一定要复制过去
        Comm1.BaudRate:=strtoint(ComboBox2.text);        //19200;
        Comm1.ByteSize:=_8;
        Comm1.ParityCheck:=False;
        case combobox3.ItemIndex of
             0: Comm1.Parity:=None;
             1: Comm1.Parity:=Odd;
             2: Comm1.Parity:=Even;
             3: Comm1.Parity:=Mark;
        else
                Comm1.Parity:=Space;
        end;
        Comm1.StopBits:=_1;
        Comm1.XoffChar:='#';
        comm1.ReadIntervalTimeout:= StrToInt(RichEdit13.Lines[0]);

        Comm1.StartComm; //打开串口
        BitBtn1.Caption := '关闭串口';

        ComboBox1.Enabled:=False;
    except
        ShowMessage('打开端口失败!');
    end;
  end
  else
  begin
    try
        timer2.Enabled :=false;
        checkbox2.State := cbunchecked;

        timer1.Enabled :=false;
        checkbox1.State := cbunchecked;

        Comm1.StopComm; //关闭串口
        BitBtn1.Caption := '打开串口';
        ComboBox1.Enabled:=True;
    except

    end;
  end;

end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再进行读取在线站号的操作!', '警告', MB_OK+MB_ICONSTOP);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
    Exit;
  end;

    sendd[0] := $00;
    sendd[1] := $03;
    sendd[2] := $00;
    sendd[3] := $00;
    sendd[4] := $00;
    sendd[5] := $01;
    sendd[6] := $85;
    sendd[7] := $DB;

    FuncCode:=1;   //读取在线站号
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

 end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var

i,para:integer;
answ:integer;
GetdataStr:string;
SendDataStr:string;
cardhao:Longword;
strls1:string;
CRC16:longint;
sendd:array[0..7]of Byte;   //发送字节缓冲

begin
  GetdataStr:='串口接收数据:';
  if issendwaitrev1 then
  begin//在有发送指令等待返回
    issendwaitrev1 := False;
    sendwaitrevsize := BufferLength;
    move(Buffer^,pchar(@(sendwaitData[0]))^,BufferLength);

    for i:=0 to  BufferLength-1  do
    begin
        GetdataStr:=GetdataStr+inttohex(sendwaitData[i],2)+' ';
    end;
    ListBox1.Items.Add(GetdataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

    CRC16 := CRC_16(@sendwaitData,BufferLength);
    if crc16=0 then
    begin
        case FuncCode of
             1:
               begin
                       RichEdit1.Text :=inttostr(sendwaitData[4]);
               end;
             2:
               begin
                       answ:=Application.MessageBox('在线站点号设置成功!是否要读出在线设备的站号?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                       if answ=1 then
                       begin
                           BitBtn2.Click;
                       end;
               end;
             6:
               begin
                       BitBtn9.Click;

                       {answ:=Application.MessageBox('读卡操作已成功执行,是否要读出设备内寄存器内数据?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                       if answ=1 then
                       begin
                           BitBtn9.Click;
                       end;}
               end;
             7:
               begin
                    strls1:='';
                    for i:=3 to  3+sendwaitData[2]-1  do
                    begin
                        strls1:=strls1+inttohex(sendwaitData[i],2)+' ';
                    end;
                    RichEdit3.Text :=strls1;

                    if (StrToInt(RichEdit5.Lines[0])=7) and  (StrToInt(RichEdit6.Lines[0])>2) then
                    begin
                        if sendwaitData[4]>0 then
                        begin
                             cardhao := sendwaitData[8] + sendwaitData[7]*256 + sendwaitData[6]*256*256 + sendwaitData[5]*256*256*256;
                             strls1:='状态位:'+inttohex(sendwaitData[4],2)+' 物理卡号:'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2)+'-'+inttohex(sendwaitData[7],2)+'-'+inttohex(sendwaitData[8],2);
                             strls1:=strls1+' 换成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                             strls1:=strls1+ ' 扇区内数据:' ;
                             if cardno10<>RightStr('000000000' + IntToStr(cardhao),10) then
                             begin
                                  if CheckBox1.Checked =true then cardno10:=RightStr('000000000' + IntToStr(cardhao),10);
                                  if MidStr(DectoBin(sendwaitData[4]),7,1)='1' then
                                  begin
                                      for i:=9 to BufferLength-3 do
                                       begin
                                           strls1:=strls1+inttohex(sendwaitData[i],2);
                                       end;
                                  end;
                                  edit1.Text :=strls1;
                                  if checkbox3.checked=true then
                                  begin
                                     BitBtn5.Click;
                                  end;
                             end;
                        end
                        else
                        begin
                             edit1.Text :='';
                        end;
                    end
                    else
                    begin
                          edit1.Text :='';
                    end;
               end;
             8:
               begin

               end;
             9:
               begin
                    sendd[0] := StrToInt(RichEdit1.Lines[0]);
                    sendd[1] := $06;
                    sendd[2] := $00;
                    sendd[3] := $07;
                    sendd[4] := $00;
                    sendd[5] := $14;

                    CRC16 := CRC_16(@sendd,6);

                    sendd[6] := CRC16 mod 256;;
                    sendd[7] := (CRC16 div 256) mod 256;

                    FuncCode:=10;   //驱动在线站号完成写卡
                    issendwaitrev1 := True;
                    sendwaitrevsize := 0;

                    Comm1.WriteCommData(@sendd[0], 8);

                    SendDataStr:='串口发送数据:';
                    for i:=0 to 7 do
                    begin
                        SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
                    end;

                    ListBox1.Items.Add(SendDataStr);
                    listbox1.ItemIndex:=listbox1.Count-1;
               end;
             10:
               begin
                    sendd[0] := StrToInt(RichEdit1.Lines[0]);
                    sendd[1] := $03;
                    sendd[2] := $00;
                    sendd[3] := $07;
                    sendd[4] := $00;
                    sendd[5] := $01;

                    CRC16 := CRC_16(@sendd,6);

                    sendd[6] := CRC16 mod 256;;
                    sendd[7] := (CRC16 div 256) mod 256;

                    FuncCode:=11;   //写卡后读出写卡状态
                    issendwaitrev1 := True;
                    sendwaitrevsize := 0;

                    Comm1.WriteCommData(@sendd[0], 8);

                    SendDataStr:='串口发送数据:';
                    for i:=0 to 7 do
                    begin
                        SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
                    end;

                    ListBox1.Items.Add(SendDataStr);
                    listbox1.ItemIndex:=listbox1.Count-1;
               end;
             11:
               begin
                   if RightStr(DectoBin(sendwaitData[4]),1)='1' then
                   begin
                      BitBtn5.Click;
                      Application.MessageBox('写卡操作成功!', '提示', MB_OK+MB_ICONINFORMATION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                   end;
               end;
             21:
               begin
                       RichEdit4.Text :=inttostr(sendwaitData[4]);
               end;
             22:
               begin
                       answ:=Application.MessageBox('在线站点号设置成功!是否要读出在线设备的站号?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                       if answ=1 then
                       begin
                           BitBtn11.Click;
                       end;
               end;
             25:
               begin
                   if (sendwaitData[1]=3) and (sendwaitData[3]+sendwaitData[4]+sendwaitData[5]+sendwaitData[6]>0) then
                   begin
                        cardhao := sendwaitData[4] + sendwaitData[3]*256 + sendwaitData[6]*256*256 + sendwaitData[5]*256*256*256;
                        strls1:='物理卡号:'+inttohex(sendwaitData[3],2)+'-'+inttohex(sendwaitData[4],2)+'-'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2);
                        strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                        edit3.Text :=strls1;

                        if checkbox2.Checked =false then
                        begin
                             answ:=Application.MessageBox('    已成功读取设备寄存器内卡号,是否要清除寄存器数据使读卡器能继续读卡?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                             if answ=1 then
                             begin
                                  BitBtn16.Click;
                             end;
                        end
                        else
                        begin
                             BitBtn16.Click;
                        end;
                   end
                   else
                   begin
                       // edit3.Text :='';
                   end;
               end;
             30:
               begin
                  if sendwaitrevsize>4 then
                  begin
                      ComboBox14.ItemIndex := StrToint(LeftStr(inttohex(sendwaitData[4],2),1));
                      para:= StrToint(RightStr(inttohex(sendwaitData[4],2),1));
                      ComboBox8.ItemIndex := para mod 2;
                      ComboBox13.ItemIndex := para div 2;
                  end;
               end;
             31:
               begin
                       RichEdit1.Text :=inttostr(sendwaitData[4]);
                       timer3.Enabled :=false;
                       revn:=true;
                       BitBtn20.Caption :='检测在线读写器的波特率及校验位';
                       Application.MessageBox('已检测到在线设备的波特率及校验码,可以开始测试各项功能!', '提示', MB_OK+MB_ICONINFORMATION);
               end;
             32:
               begin
                       RichEdit4.Text :=inttostr(sendwaitData[4]);
                       timer4.Enabled :=false;
                       revn:=true;
                       BitBtn21.Caption :='检测在线读卡器的波特率及校验位';
                       Application.MessageBox('已检测到在线设备的波特率及校验码,可以开始测试各项功能!', '提示', MB_OK+MB_ICONINFORMATION);
               end;
        end;
    end;
  end;
end;
    {case FuncCode of
         1:
           begin
              if  sendwaitData[5]=(sendwaitData[3] xor sendwaitData[4])  then
              begin
                  RichEdit1.Text :=inttostr(sendwaitData[4]*256+sendwaitData[3]) ;
              end;
              FuncCode:=0;
           end;
         2:
           begin
               FuncCode:=0;
               answ:=Application.MessageBox('在线站点号设置成功!是否要读出在线设备的站号?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
               if answ=1 then
               begin
                   BitBtn2.Click;
               end;
           end;
         3:
           begin
               If (sendwaitData[1] = $69) And (sendwaitData[2] = $D2) And ((sendwaitData[3] Xor sendwaitData[4] Xor sendwaitData[5] Xor sendwaitData[6] Xor sendwaitData[7]) = sendwaitData[8]) Then
               begin
                    cardhao := sendwaitData[7] + sendwaitData[6]*256 + sendwaitData[5]*256*256 + sendwaitData[4]*256*256*256;
                    strls1:='物理卡号:'+inttohex(sendwaitData[4],2)+'-'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2)+'-'+inttohex(sendwaitData[7],2);
                    strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                    edit1.Text :=strls1;
               end
               else
               begin
                    edit1.Text :='';
               end;
               FuncCode:=0;
           end;
         4:
           begin
                If (sendwaitData[1] = $69) And (sendwaitData[2] = $2A) and (sendwaitrevsize>2) then
                begin
                     strls1:='设备按键:';
                     for i:=2 to  sendwaitrevsize  do
                     begin
                          if sendwaitData[i]=$0D then break ;
                          strls1:=strls1+chr(sendwaitData[i]);
                     end;
                     edit1.Text :=strls1;
                end
                else
                begin
                     edit1.Text :='';
                end;
               FuncCode:=0;
           end;
         5:
           begin
               If (sendwaitData[1] = $69) And (sendwaitData[2] = $69)  then
               begin
                   If (sendwaitData[3] = $01) And ((sendwaitData[4] Xor sendwaitData[5] Xor sendwaitData[6] Xor sendwaitData[7] Xor sendwaitData[8]) = sendwaitData[9])  then     //只有卡号
                   begin
                        cardhao := sendwaitData[8] + sendwaitData[7]*256 + sendwaitData[6]*256*256 + sendwaitData[5]*256*256*256;
                        strls1:='物理卡号:'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2)+'-'+inttohex(sendwaitData[7],2)+'-'+inttohex(sendwaitData[8],2);
                        strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                        edit2.Text :=strls1;
                        dispstr:='卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                   end
                   else
                   begin
                        if (sendwaitData[3] = $02) And (sendwaitData[4] = $2A) And (sendwaitrevsize > 8)  then   //只有按键
                        begin
                             strls1:='设备按键:';
                             dispstr:='';
                             for i:=4 to  sendwaitrevsize  do
                             begin
                                  if sendwaitData[i]=$0D then break ;
                                  strls1:=strls1+chr(sendwaitData[i]);
                                  dispstr:=dispstr+chr(sendwaitData[i]);
                             end;
                             edit2.Text :=strls1;
                             dispstr:='按键:'+midstr(dispstr+'               ',1,10);
                        end
                        else
                        begin
                             if  (sendwaitData[3] = $03) And ((sendwaitData[4] Xor sendwaitData[5] Xor sendwaitData[6] Xor sendwaitData[7] Xor sendwaitData[8]) = sendwaitData[9]) then //卡号和按键
                             begin
                                  cardhao := sendwaitData[8] + sendwaitData[7]*256 + sendwaitData[6]*256*256 + sendwaitData[5]*256*256*256;
                                  strls1:='物理卡号:'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2)+'-'+inttohex(sendwaitData[7],2)+'-'+inttohex(sendwaitData[8],2);
                                  strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);

                                  strls1:=strls1+',设备按键:';
                                  keystr:='';
                                  for i:=10 to  sendwaitrevsize  do
                                  begin
                                       if sendwaitData[i]=$0D then break ;
                                       strls1:=strls1+chr(sendwaitData[i]);
                                       keystr:=keystr+chr(sendwaitData[i]);
                                  end;
                                  edit2.Text :=strls1;

                                  dispstr:='卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                                  dispstr:=dispstr+'按键:'+midstr(keystr+'               ',1,10);
                             end
                             else
                             begin
                                 edit2.Text :='';
                                 dispstr:='';
                             end;
                        end;
                   end;
                   if checkbox2.State = cbchecked then BitBtn12.Click;
               end
               else
               begin
                      edit2.Text :='';
                      dispstr:='';
               end;
               FuncCode:=0;
           end;
    else
         begin

         end;
    end;
  end
  else
  begin

    revsize := BufferLength;
    if (revsize>50) then
    begin
      revsize := 50;
    end;
    move(Buffer^,pchar(@(MyReceiveData[MyReceivePoint].MyData[1]))^,revsize);
    Inc(MyReceivePoint);
  end;

end;        }


end.

​

procedure TForm1.FormShow(Sender: TObject);
var
  i:Integer;
  count:Integer;
begin
  dispstr:='';
  count := 0;

  for i := 1 to 20 do                                       //开发环境调试时,请关闭菜单栏  Tool/Debugger Options/integrated debugging 选项
  begin
      Comm1.CommName:='\\.\COM' + IntToStr(i);

      try
         Comm1.StartComm; //打开串口
         Comm1.StopComm; //打开串口
         ComboBox1.Items.Add('COM' + IntToStr(i));
         count := count + 1;
      except

      end;

    if(count>0) then
    begin
         ComboBox1.ItemIndex:=count-1;
    end;
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if(BitBtn1.Caption = '打开串口') then
  begin
    try
        Comm1.CommName:='\\.\'+ComboBox1.Text;
        //以下代码一定要复制过去
        Comm1.BaudRate:=strtoint(ComboBox2.text);        //19200;
        Comm1.ByteSize:=_8;
        Comm1.ParityCheck:=False;
        case combobox3.ItemIndex of
             0: Comm1.Parity:=None;
             1: Comm1.Parity:=Odd;
             2: Comm1.Parity:=Even;
             3: Comm1.Parity:=Mark;
        else
                Comm1.Parity:=Space;
        end;
        Comm1.StopBits:=_1;
        Comm1.XoffChar:='#';
        comm1.ReadIntervalTimeout:= StrToInt(RichEdit13.Lines[0]);

        Comm1.StartComm; //打开串口
        BitBtn1.Caption := '关闭串口';

        ComboBox1.Enabled:=False;
    except
        ShowMessage('打开端口失败!');
    end;
  end
  else
  begin
    try
        timer2.Enabled :=false;
        checkbox2.State := cbunchecked;

        timer1.Enabled :=false;
        checkbox1.State := cbunchecked;

        Comm1.StopComm; //关闭串口
        BitBtn1.Caption := '打开串口';
        ComboBox1.Enabled:=True;
    except

    end;
  end;

end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再进行读取在线站号的操作!', '警告', MB_OK+MB_ICONSTOP);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
    Exit;
  end;

    sendd[0] := $00;
    sendd[1] := $03;
    sendd[2] := $00;
    sendd[3] := $00;
    sendd[4] := $00;
    sendd[5] := $01;
    sendd[6] := $85;
    sendd[7] := $DB;

    FuncCode:=1;   //读取在线站号
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

 end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var

i,para:integer;
answ:integer;
GetdataStr:string;
SendDataStr:string;
cardhao:Longword;
strls1:string;
CRC16:longint;
sendd:array[0..7]of Byte;   //发送字节缓冲

begin
  GetdataStr:='串口接收数据:';
  if issendwaitrev1 then
  begin//在有发送指令等待返回
    issendwaitrev1 := False;
    sendwaitrevsize := BufferLength;
    move(Buffer^,pchar(@(sendwaitData[0]))^,BufferLength);

    for i:=0 to  BufferLength-1  do
    begin
        GetdataStr:=GetdataStr+inttohex(sendwaitData[i],2)+' ';
    end;
    ListBox1.Items.Add(GetdataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

    CRC16 := CRC_16(@sendwaitData,BufferLength);
    if crc16=0 then
    begin
        case FuncCode of
             1:
               begin
                       RichEdit1.Text :=inttostr(sendwaitData[4]);
               end;
             2:
               begin
                       answ:=Application.MessageBox('在线站点号设置成功!是否要读出在线设备的站号?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                       if answ=1 then
                       begin
                           BitBtn2.Click;
                       end;
               end;
             6:
               begin
                       BitBtn9.Click;

                       {answ:=Application.MessageBox('读卡操作已成功执行,是否要读出设备内寄存器内数据?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                       if answ=1 then
                       begin
                           BitBtn9.Click;
                       end;}
               end;
             7:
               begin
                    strls1:='';
                    for i:=3 to  3+sendwaitData[2]-1  do
                    begin
                        strls1:=strls1+inttohex(sendwaitData[i],2)+' ';
                    end;
                    RichEdit3.Text :=strls1;

                    if (StrToInt(RichEdit5.Lines[0])=7) and  (StrToInt(RichEdit6.Lines[0])>2) then
                    begin
                        if sendwaitData[4]>0 then
                        begin
                             cardhao := sendwaitData[8] + sendwaitData[7]*256 + sendwaitData[6]*256*256 + sendwaitData[5]*256*256*256;
                             strls1:='状态位:'+inttohex(sendwaitData[4],2)+' 物理卡号:'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2)+'-'+inttohex(sendwaitData[7],2)+'-'+inttohex(sendwaitData[8],2);
                             strls1:=strls1+' 换成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                             strls1:=strls1+ ' 扇区内数据:' ;
                             if cardno10<>RightStr('000000000' + IntToStr(cardhao),10) then
                             begin
                                  if CheckBox1.Checked =true then cardno10:=RightStr('000000000' + IntToStr(cardhao),10);
                                  if MidStr(DectoBin(sendwaitData[4]),7,1)='1' then
                                  begin
                                      for i:=9 to BufferLength-3 do
                                       begin
                                           strls1:=strls1+inttohex(sendwaitData[i],2);
                                       end;
                                  end;
                                  edit1.Text :=strls1;
                                  if checkbox3.checked=true then
                                  begin
                                     BitBtn5.Click;
                                  end;
                             end;
                        end
                        else
                        begin
                             edit1.Text :='';
                        end;
                    end
                    else
                    begin
                          edit1.Text :='';
                    end;
               end;
             8:
               begin

               end;
             9:
               begin
                    sendd[0] := StrToInt(RichEdit1.Lines[0]);
                    sendd[1] := $06;
                    sendd[2] := $00;
                    sendd[3] := $07;
                    sendd[4] := $00;
                    sendd[5] := $14;

                    CRC16 := CRC_16(@sendd,6);

                    sendd[6] := CRC16 mod 256;;
                    sendd[7] := (CRC16 div 256) mod 256;

                    FuncCode:=10;   //驱动在线站号完成写卡
                    issendwaitrev1 := True;
                    sendwaitrevsize := 0;

                    Comm1.WriteCommData(@sendd[0], 8);

                    SendDataStr:='串口发送数据:';
                    for i:=0 to 7 do
                    begin
                        SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
                    end;

                    ListBox1.Items.Add(SendDataStr);
                    listbox1.ItemIndex:=listbox1.Count-1;
               end;
             10:
               begin
                    sendd[0] := StrToInt(RichEdit1.Lines[0]);
                    sendd[1] := $03;
                    sendd[2] := $00;
                    sendd[3] := $07;
                    sendd[4] := $00;
                    sendd[5] := $01;

                    CRC16 := CRC_16(@sendd,6);

                    sendd[6] := CRC16 mod 256;;
                    sendd[7] := (CRC16 div 256) mod 256;

                    FuncCode:=11;   //写卡后读出写卡状态
                    issendwaitrev1 := True;
                    sendwaitrevsize := 0;

                    Comm1.WriteCommData(@sendd[0], 8);

                    SendDataStr:='串口发送数据:';
                    for i:=0 to 7 do
                    begin
                        SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
                    end;

                    ListBox1.Items.Add(SendDataStr);
                    listbox1.ItemIndex:=listbox1.Count-1;
               end;
             11:
               begin
                   if RightStr(DectoBin(sendwaitData[4]),1)='1' then
                   begin
                      BitBtn5.Click;
                      Application.MessageBox('写卡操作成功!', '提示', MB_OK+MB_ICONINFORMATION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                   end;
               end;
             21:
               begin
                       RichEdit4.Text :=inttostr(sendwaitData[4]);
               end;
             22:
               begin
                       answ:=Application.MessageBox('在线站点号设置成功!是否要读出在线设备的站号?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                       if answ=1 then
                       begin
                           BitBtn11.Click;
                       end;
               end;
             25:
               begin
                   if (sendwaitData[1]=3) and (sendwaitData[3]+sendwaitData[4]+sendwaitData[5]+sendwaitData[6]>0) then
                   begin
                        cardhao := sendwaitData[4] + sendwaitData[3]*256 + sendwaitData[6]*256*256 + sendwaitData[5]*256*256*256;
                        strls1:='物理卡号:'+inttohex(sendwaitData[3],2)+'-'+inttohex(sendwaitData[4],2)+'-'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2);
                        strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                        edit3.Text :=strls1;

                        if checkbox2.Checked =false then
                        begin
                             answ:=Application.MessageBox('    已成功读取设备寄存器内卡号,是否要清除寄存器数据使读卡器能继续读卡?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
                             if answ=1 then
                             begin
                                  BitBtn16.Click;
                             end;
                        end
                        else
                        begin
                             BitBtn16.Click;
                        end;
                   end
                   else
                   begin
                       // edit3.Text :='';
                   end;
               end;
             30:
               begin
                  if sendwaitrevsize>4 then
                  begin
                      ComboBox14.ItemIndex := StrToint(LeftStr(inttohex(sendwaitData[4],2),1));
                      para:= StrToint(RightStr(inttohex(sendwaitData[4],2),1));
                      ComboBox8.ItemIndex := para mod 2;
                      ComboBox13.ItemIndex := para div 2;
                  end;
               end;
             31:
               begin
                       RichEdit1.Text :=inttostr(sendwaitData[4]);
                       timer3.Enabled :=false;
                       revn:=true;
                       BitBtn20.Caption :='检测在线读写器的波特率及校验位';
                       Application.MessageBox('已检测到在线设备的波特率及校验码,可以开始测试各项功能!', '提示', MB_OK+MB_ICONINFORMATION);
               end;
             32:
               begin
                       RichEdit4.Text :=inttostr(sendwaitData[4]);
                       timer4.Enabled :=false;
                       revn:=true;
                       BitBtn21.Caption :='检测在线读卡器的波特率及校验位';
                       Application.MessageBox('已检测到在线设备的波特率及校验码,可以开始测试各项功能!', '提示', MB_OK+MB_ICONINFORMATION);
               end;
        end;
    end;
  end;
end;
    {case FuncCode of
         1:
           begin
              if  sendwaitData[5]=(sendwaitData[3] xor sendwaitData[4])  then
              begin
                  RichEdit1.Text :=inttostr(sendwaitData[4]*256+sendwaitData[3]) ;
              end;
              FuncCode:=0;
           end;
         2:
           begin
               FuncCode:=0;
               answ:=Application.MessageBox('在线站点号设置成功!是否要读出在线设备的站号?', '提示', MB_OKcancel+MB_ICONQUESTION);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
               if answ=1 then
               begin
                   BitBtn2.Click;
               end;
           end;
         3:
           begin
               If (sendwaitData[1] = $69) And (sendwaitData[2] = $D2) And ((sendwaitData[3] Xor sendwaitData[4] Xor sendwaitData[5] Xor sendwaitData[6] Xor sendwaitData[7]) = sendwaitData[8]) Then
               begin
                    cardhao := sendwaitData[7] + sendwaitData[6]*256 + sendwaitData[5]*256*256 + sendwaitData[4]*256*256*256;
                    strls1:='物理卡号:'+inttohex(sendwaitData[4],2)+'-'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2)+'-'+inttohex(sendwaitData[7],2);
                    strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                    edit1.Text :=strls1;
               end
               else
               begin
                    edit1.Text :='';
               end;
               FuncCode:=0;
           end;
         4:
           begin
                If (sendwaitData[1] = $69) And (sendwaitData[2] = $2A) and (sendwaitrevsize>2) then
                begin
                     strls1:='设备按键:';
                     for i:=2 to  sendwaitrevsize  do
                     begin
                          if sendwaitData[i]=$0D then break ;
                          strls1:=strls1+chr(sendwaitData[i]);
                     end;
                     edit1.Text :=strls1;
                end
                else
                begin
                     edit1.Text :='';
                end;
               FuncCode:=0;
           end;
         5:
           begin
               If (sendwaitData[1] = $69) And (sendwaitData[2] = $69)  then
               begin
                   If (sendwaitData[3] = $01) And ((sendwaitData[4] Xor sendwaitData[5] Xor sendwaitData[6] Xor sendwaitData[7] Xor sendwaitData[8]) = sendwaitData[9])  then     //只有卡号
                   begin
                        cardhao := sendwaitData[8] + sendwaitData[7]*256 + sendwaitData[6]*256*256 + sendwaitData[5]*256*256*256;
                        strls1:='物理卡号:'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2)+'-'+inttohex(sendwaitData[7],2)+'-'+inttohex(sendwaitData[8],2);
                        strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                        edit2.Text :=strls1;
                        dispstr:='卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                   end
                   else
                   begin
                        if (sendwaitData[3] = $02) And (sendwaitData[4] = $2A) And (sendwaitrevsize > 8)  then   //只有按键
                        begin
                             strls1:='设备按键:';
                             dispstr:='';
                             for i:=4 to  sendwaitrevsize  do
                             begin
                                  if sendwaitData[i]=$0D then break ;
                                  strls1:=strls1+chr(sendwaitData[i]);
                                  dispstr:=dispstr+chr(sendwaitData[i]);
                             end;
                             edit2.Text :=strls1;
                             dispstr:='按键:'+midstr(dispstr+'               ',1,10);
                        end
                        else
                        begin
                             if  (sendwaitData[3] = $03) And ((sendwaitData[4] Xor sendwaitData[5] Xor sendwaitData[6] Xor sendwaitData[7] Xor sendwaitData[8]) = sendwaitData[9]) then //卡号和按键
                             begin
                                  cardhao := sendwaitData[8] + sendwaitData[7]*256 + sendwaitData[6]*256*256 + sendwaitData[5]*256*256*256;
                                  strls1:='物理卡号:'+inttohex(sendwaitData[5],2)+'-'+inttohex(sendwaitData[6],2)+'-'+inttohex(sendwaitData[7],2)+'-'+inttohex(sendwaitData[8],2);
                                  strls1:=strls1+' 换算成十位卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);

                                  strls1:=strls1+',设备按键:';
                                  keystr:='';
                                  for i:=10 to  sendwaitrevsize  do
                                  begin
                                       if sendwaitData[i]=$0D then break ;
                                       strls1:=strls1+chr(sendwaitData[i]);
                                       keystr:=keystr+chr(sendwaitData[i]);
                                  end;
                                  edit2.Text :=strls1;

                                  dispstr:='卡号:'+ RightStr('000000000' + IntToStr(cardhao),10);
                                  dispstr:=dispstr+'按键:'+midstr(keystr+'               ',1,10);
                             end
                             else
                             begin
                                 edit2.Text :='';
                                 dispstr:='';
                             end;
                        end;
                   end;
                   if checkbox2.State = cbchecked then BitBtn12.Click;
               end
               else
               begin
                      edit2.Text :='';
                      dispstr:='';
               end;
               FuncCode:=0;
           end;
    else
         begin

         end;
    end;
  end
  else
  begin

    revsize := BufferLength;
    if (revsize>50) then
    begin
      revsize := 50;
    end;
    move(Buffer^,pchar(@(MyReceiveData[MyReceivePoint].MyData[1]))^,revsize);
    Inc(MyReceivePoint);
  end;

end;        }

procedure TForm1.BitBtn3Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   newjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

  if(not isrightint(RichEdit2.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit2.SetFocus();
            RichEdit2.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);
    newjh:=StrToInt(RichEdit2.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $00;
    sendd[4] := $00;
    sendd[5] := newjh;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=2;   //更改在线站号
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;

procedure TForm1.BitBtn4Click(Sender: TObject);
begin
  listbox1.Clear;
end;

procedure TForm1.BitBtn7Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再进行读取在线站号的操作!', '警告', MB_OK+MB_ICONSTOP);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
    Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $07;
    sendd[4] := $00;
    sendd[5] := $04;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;

    FuncCode:=9;   //驱动在线站号开始写卡
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.BitBtn8Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再进行读取在线站号的操作!', '警告', MB_OK+MB_ICONSTOP);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
    Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;
    RichEdit5.Text :='7';

    oldjh:=StrToInt(RichEdit1.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $07;
    sendd[4] := $00;
    sendd[5] := $08;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;

    FuncCode:=6;   //驱动在线站号读卡
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;


procedure TForm1.CheckBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再进行读取在线站号的操作!', '警告', MB_OK+MB_ICONSTOP);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
    Exit;
  end;

  if checkbox2.checked=true then
  begin
     checkbox2.checked:=false;
     timer2.Enabled :=false;
  end;

  if (strtoint(RichEdit1.Lines[0])=0) then
  begin
            Application.MessageBox('轮询的站点号不可以为0,请先指定轮询站号!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;
  if(not isrightint(RichEdit8.Lines[0])) then
  begin
            Application.MessageBox('间隔时间输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit8.SetFocus();
            RichEdit8.SelectAll();
            exit;
  end;

  if checkbox1.State = cbUnchecked then
  begin
      timer1.Interval := StrToInt(RichEdit8.Lines[0]);
      timer1.Enabled :=true;
  end
  else
  begin
      timer1.Enabled :=false;
      timer1.Enabled :=false;
      timer1.Enabled :=false;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
    if checkbox1.State = cbchecked then
    begin
         timer1.Enabled :=false;
         BitBtn8.Click;
         sleep(StrToInt(RichEdit8.Lines[0]));
         timer1.Enabled :=true;
    end;
end;

procedure TForm1.BitBtn12Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit4.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $04;
    sendd[4] := $00;
    sendd[5] := Combobox7.ItemIndex+1 ;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=23;   //驱动在线站号发声
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;

procedure TForm1.BitBtn14Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   newjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit4.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;

  if(not isrightint(RichEdit7.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit7.SetFocus();
            RichEdit7.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);
    newjh:=StrToInt(RichEdit7.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $02;
    sendd[4] := $00;
    sendd[5] := newjh;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=22;   //更改在线站号
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.BitBtn15Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit4.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $03;
    sendd[2] := $00;
    sendd[3] := $00;
    sendd[4] := $00;
    sendd[5] := $02;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=25;   //驱动在线站号发声
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    if listbox1.Count>10 then listbox1.Clear ;
    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.RichEdit8Chang(Sender: TObject);
begin
   if  StrToInt(RichEdit8.Lines[0])<50 then
   begin
        RichEdit8.Text :='50';
        Application.MessageBox('    轮询一台机的间隔时间不要小于50MS!如果同时轮询多台机可缩短间隔时间。', '警告', MB_OK+MB_ICONSTOP);
   end;
   if  StrToInt(RichEdit13.Lines[0])>=StrToInt(RichEdit8.Lines[0]) then
   begin
          if StrToInt(RichEdit13.Lines[0])+10>50 then
          begin
              RichEdit8.Text :=inttostr(StrToInt(RichEdit13.Lines[0])+10);
          end
          else
          begin
              RichEdit8.Text :='50';
          end;
          Application.MessageBox('    轮询间隔时间不能小于通讯超时时间。', '警告', MB_OK+MB_ICONSTOP);
   end;
end;

procedure TForm1.RichEdit13Change(Sender: TObject);
begin
   if  StrToInt(RichEdit13.Lines[0])<10 then
   begin
        RichEdit13.Text :='10';
        Application.MessageBox('    通讯超时时间不要小于10毫秒。', '警告', MB_OK+MB_ICONSTOP);
   end;
   if  StrToInt(RichEdit13.Lines[0])>=StrToInt(RichEdit8.Lines[0]) then
   begin
          if StrToInt(RichEdit13.Lines[0])+10>50 then
          begin
              RichEdit8.Text :=inttostr(StrToInt(RichEdit13.Lines[0])+10);
          end
          else
          begin
              RichEdit8.Text :='50';
          end;
          Application.MessageBox('    轮询间隔时间不能小于通讯超时时间。', '警告', MB_OK+MB_ICONSTOP);
   end;
   comm1.ReadIntervalTimeout:= StrToInt(RichEdit13.Lines[0]);
end;

procedure TForm1.BitBtn5Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $02;
    sendd[4] := $00;
    sendd[5] := Combobox4.ItemIndex+1 ;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=3;   //驱动在线站号发声
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;

procedure TForm1.BitBtn6Click(Sender: TObject);
var
   sendd:array[0..12]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   BTL:longint;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);
    BTL:=StrToInt(Combobox5.Text);

    sendd[0] := oldjh;
    sendd[1] := $10;
    sendd[2] := $00;
    sendd[3] := $3C;
    sendd[4] := $00;
    sendd[5] := $02;
    sendd[6] := $04;
    sendd[7] := (BTL div 256) mod 256;
    sendd[8] := BTL mod 256;
    sendd[9] := $55;
    sendd[10] := $AA;

    CRC16 := CRC_16(@sendd,11);

    sendd[11] := CRC16 mod 256;;
    sendd[12] := (CRC16 div 256) mod 256;


    FuncCode:=4;   //更改在线站号波特率
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 13);

    SendDataStr:='串口发送数据:';
    for i:=0 to 12 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.BitBtn17Click(Sender: TObject);
var
   sendd:array[0..12]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $10;
    sendd[2] := $00;
    sendd[3] := $3E;
    sendd[4] := $00;
    sendd[5] := $02;
    sendd[6] := $04;
    sendd[7] := $00;
    sendd[8] := Combobox6.ItemIndex ;
    sendd[9] := $55;
    sendd[10] := $AA;

    CRC16 := CRC_16(@sendd,11);

    sendd[11] := CRC16 mod 256;;
    sendd[12] := (CRC16 div 256) mod 256;


    FuncCode:=5;   //更改在线站号校验位
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 13);

    SendDataStr:='串口发送数据:';
    for i:=0 to 12 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;

procedure TForm1.BitBtn9Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   BegEE:integer;
   EENum:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再进行读取在线站号的操作!', '警告', MB_OK+MB_ICONSTOP);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
    Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);
    BegEE:=StrToInt(RichEdit5.Lines[0]);
    EENum:=StrToInt(RichEdit6.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $03;
    sendd[2] := $00;
    sendd[3] := BegEE;
    sendd[4] := $00;
    sendd[5] := EENum;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;

    FuncCode:=7;   //驱动在线站号读卡
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    if listbox1.Count>10 then listbox1.Clear ;
    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;

procedure TForm1.BitBtn10Click(Sender: TObject);
var
   sendd:array[0..100]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   EENum:integer;
   CRC16:longint;
   wstr:string;
   rwstr:string;
   wlen:integer;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再进行读取在线站号的操作!', '警告', MB_OK+MB_ICONSTOP);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
    Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);
    EENum:=StrToInt(RichEdit6.Lines[0]);
    wlen:=EENum*2;      //每个寄存器为2个字节

    wstr:=trim(RichEdit3.Lines[0]);
    wstr:=wstr+' '+trim(RichEdit3.Lines[1]);
    if (length(wstr)>0) then wstr:=wstr+' '    ;
    for i:=1 to  wlen do
    begin
        wstr:=wstr+'00 ';
    end;


    sendd[0] := oldjh;
    sendd[1] := $10;
    sendd[2] := $00;
    sendd[3] := $0A;      //写卡信息的起始寄存器
    sendd[4] := $00;
    sendd[5] := EENum;    //操作寄存器的个数
    sendd[6] := wlen;     //每个寄存器为2个字节

    rwstr:='';
    for i:=1 to wlen do
    begin
       sendd[6+i]:=strtoint('$'+midstr(wstr,i*3-2,2));
       rwstr:=rwstr+midstr(wstr,i*3-2,2)+' ';
    end;
    RichEdit3.Text := rwstr;


    CRC16 := CRC_16(@sendd,wlen+7);

    sendd[wlen+7] := CRC16 mod 256;;
    sendd[wlen+8] := (CRC16 div 256) mod 256;

    FuncCode:=8;   //更新在线站卡的卡数据寄存器
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], wlen+9);

    SendDataStr:='串口发送数据:';
    for i:=0 to wlen+8 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.BitBtn11Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再进行读取在线站号的操作!', '警告', MB_OK+MB_ICONSTOP);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
    Exit;
  end;

    sendd[0] := $00;
    sendd[1] := $03;
    sendd[2] := $00;
    sendd[3] := $02;
    sendd[4] := $00;
    sendd[5] := $01;
    sendd[6] := $24;
    sendd[7] := $1B;

    FuncCode:=21;   //读取在线站号
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.BitBtn13Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   part:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
   hexpart:string;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit4.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $03;
    sendd[4] := $80;

    part:= Combobox8.ItemIndex ;
    part:=part+2*Combobox13.ItemIndex ;

    hexpart:=inttohex(part,1);
    hexpart:='$'+inttohex(Combobox14.ItemIndex,1)+hexpart;

    sendd[5] := strtoint(hexpart) ;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=26;   //驱动在线站号发声
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;

procedure TForm1.BitBtn16Click(Sender: TObject);
var
   sendd:array[0..12]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit4.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $10;
    sendd[2] := $00;
    sendd[3] := $00;
    sendd[4] := $00;
    sendd[5] := $02;
    sendd[6] := $04;
    sendd[7] := $00;
    sendd[8] := $00;
    sendd[9] := $00;
    sendd[10] := $00;

    CRC16 := CRC_16(@sendd,11);

    sendd[11] := CRC16 mod 256;;
    sendd[12] := (CRC16 div 256) mod 256;


    FuncCode:=24;   //驱动在线站号发声
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 13);

    SendDataStr:='串口发送数据:';
    for i:=0 to 12 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.CheckBox2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再进行读取在线站号的操作!', '警告', MB_OK+MB_ICONSTOP);        //MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
    Exit;
  end;

  if (strtoint(RichEdit4.Lines[0])=0) then
  begin
            Application.MessageBox('轮询的站点号不可以为0,请先指定轮询站号!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;

  if(not isrightint(RichEdit4.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;
  if(not isrightint(RichEdit9.Lines[0])) then
  begin
            Application.MessageBox('间隔时间输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit9.SetFocus();
            RichEdit9.SelectAll();
            exit;
  end;

  if checkbox2.State = cbUnchecked then
  begin
      timer2.Interval := StrToInt(RichEdit9.Lines[0]);
      timer2.Enabled :=true;
  end
  else
  begin
      timer2.Enabled :=false;
      timer2.Enabled :=false;
      timer2.Enabled :=false;
  end;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
    if checkbox2.State = cbchecked then
    begin
         timer2.Enabled :=false;
         BitBtn15.Click;
         timer2.Enabled :=true;
    end;
end;

procedure TForm1.BitBtn18Click(Sender: TObject);
var
   sendd:array[0..12]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   BTL:longint;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit4.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);
    BTL:=StrToInt(Combobox9.Text);

    sendd[0] := oldjh;
    sendd[1] := $10;
    sendd[2] := $00;
    sendd[3] := $3C;
    sendd[4] := $00;
    sendd[5] := $02;
    sendd[6] := $04;
    sendd[7] := (BTL div 256) mod 256;
    sendd[8] := BTL mod 256;
    sendd[9] := $55;
    sendd[10] := $AA;

    CRC16 := CRC_16(@sendd,11);

    sendd[11] := CRC16 mod 256;;
    sendd[12] := (CRC16 div 256) mod 256;


    FuncCode:=4;   //更改在线站号波特率
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 13);

    SendDataStr:='串口发送数据:';
    for i:=0 to 12 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.BitBtn19Click(Sender: TObject);
var
   sendd:array[0..12]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit4.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $10;
    sendd[2] := $00;
    sendd[3] := $3E;
    sendd[4] := $00;
    sendd[5] := $02;
    sendd[6] := $04;
    sendd[7] := $00;
    sendd[8] := Combobox10.ItemIndex ;
    sendd[9] := $55;
    sendd[10] := $AA;

    CRC16 := CRC_16(@sendd,11);

    sendd[11] := CRC16 mod 256;;
    sendd[12] := (CRC16 div 256) mod 256;


    FuncCode:=5;   //更改在线站号校验位
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 13);

    SendDataStr:='串口发送数据:';
    for i:=0 to 12 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;

procedure TForm1.BitBtn20Click(Sender: TObject);
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再继续操作!', '警告', MB_OK+MB_ICONSTOP);
    Exit;
  end;
  BitBtn20.Caption :='正在测试串口波特率及校验位';
  btlp:=0;
  jlmp:=-1;
  revn:=false;
  timer3.Enabled :=true;
end;

procedure TForm1.Timer3Timer(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   btlstr:string;
begin
  if (revn=true) or (btlp>5)  then
  begin
      Timer3.Enabled :=false;
      BitBtn20.Caption :='检测在线读写器的波特率及校验位';
      Application.MessageBox('  未能检测到在线设备,请查询线路是否正常连接及设备型号是否正确!', '提示', MB_OK+MB_ICONSTOP);
      Exit;
  end;

  Comm1.StopComm; //关闭串口
  Comm1.CommName:='\\.\'+ComboBox1.Text;
  combobox2.ItemIndex:=btlp;
  Comm1.BaudRate:=strtoint(ComboBox2.text);
  btlstr:='   串口波特率:'+ComboBox2.text;
  Comm1.ByteSize:=_8;
  Comm1.ParityCheck:=False;
  Comm1.StopBits:=_1;
  Comm1.XoffChar:='#';
  jlmp:=jlmp+1;
  if jlmp<5 then
  begin
    combobox3.ItemIndex:=jlmp;
    case combobox3.ItemIndex of
                  0:
                    begin
                         Comm1.Parity:=None;
                         btlstr:=btlstr+',校验位:None'
                    end;
                  1:
                    begin
                         Comm1.Parity:=Odd;
                         btlstr:=btlstr+',校验位:Odd '
                    end;
                  2:
                    begin
                         Comm1.Parity:=Even;
                         btlstr:=btlstr+',校验位:Even'
                    end;
                  3:
                    begin
                         Comm1.Parity:=Mark;
                         btlstr:=btlstr+',校验位:Mark'
                    end;
                  4:
                    begin
                         Comm1.Parity:=Space;
                         btlstr:=btlstr+',校验位:Space'
                    end;
    end;
    Comm1.StartComm; //打开串口
    sleep(2);

    sendd[0] := $00;
    sendd[1] := $03;
    sendd[2] := $00;
    sendd[3] := $00;
    sendd[4] := $00;
    sendd[5] := $01;
    sendd[6] := $85;
    sendd[7] := $DB;

    FuncCode:=31;   //读取在线站号
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    SendDataStr:=SendDataStr+btlstr ;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

  end
  else
  begin
       jlmp:=-1;
       btlp:=btlp+1;
  end;
end;

procedure TForm1.BitBtn21Click(Sender: TObject);
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
    Application.MessageBox('请先打开串口再继续操作!', '警告', MB_OK+MB_ICONSTOP);
    Exit;
  end;
  BitBtn21.Caption :='正在测试串口波特率及校验位';
  btlp:=0;
  jlmp:=-1;
  revn:=false;
  timer4.Enabled :=true;
end;
{
procedure TForm1.Timer3Timer(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   btlstr:string;
begin
  if (revn=true) or (btlp>5)  then
  begin
      Timer3.Enabled :=false;
      BitBtn20.Caption :='检测在线读写器的波特率及校验位';
      Application.MessageBox('  未能检测到在线设备,请查询线路是否正常连接及设备型号是否正确!', '提示', MB_OK+MB_ICONSTOP);
      Exit;
  end;

  Comm1.StopComm; //关闭串口
  Comm1.CommName:='\\.\'+ComboBox1.Text;
  combobox2.ItemIndex:=btlp;
  Comm1.BaudRate:=strtoint(ComboBox2.text);
  btlstr:='   串口波特率:'+ComboBox2.text;
  Comm1.ByteSize:=_8;
  Comm1.ParityCheck:=False;
  Comm1.StopBits:=_1;
  Comm1.XoffChar:='#';
  jlmp:=jlmp+1;
  if jlmp<5 then
  begin
    combobox3.ItemIndex:=jlmp;
    case combobox3.ItemIndex of
                  0:
                    begin
                         Comm1.Parity:=None;
                         btlstr:=btlstr+',校验位:None'
                    end;
                  1:
                    begin
                         Comm1.Parity:=Odd;
                         btlstr:=btlstr+',校验位:Odd '
                    end;
                  2:
                    begin
                         Comm1.Parity:=Even;
                         btlstr:=btlstr+',校验位:Even'
                    end;
                  3:
                    begin
                         Comm1.Parity:=Mark;
                         btlstr:=btlstr+',校验位:Mark'
                    end;
                  4:
                    begin
                         Comm1.Parity:=Space;
                         btlstr:=btlstr+',校验位:Space'
                    end;
    end;
    Comm1.StartComm; //打开串口
    sleep(2);

    sendd[0] := $00;
    sendd[1] := $03;
    sendd[2] := $00;
    sendd[3] := $00;
    sendd[4] := $00;
    sendd[5] := $01;
    sendd[6] := $85;
    sendd[7] := $DB;

    FuncCode:=31;   //读取在线站号
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    SendDataStr:=SendDataStr+btlstr ;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

  end
  else
  begin
       jlmp:=-1;
       btlp:=btlp+1;
  end;
end;  }

procedure TForm1.Timer4Timer(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   btlstr:string;
begin
  if (revn=true) or (btlp>5)  then
  begin
      Timer4.Enabled :=false;
      BitBtn21.Caption :='检测在线读卡器的波特率及校验位';
      Application.MessageBox('  未能检测到在线设备,请查询线路是否正常连接及设备型号是否正确!', '提示', MB_OK+MB_ICONSTOP);
      Exit;
  end;

  Comm1.StopComm; //关闭串口
  Comm1.CommName:='\\.\'+ComboBox1.Text;
  combobox2.ItemIndex:=btlp;
  Comm1.BaudRate:=strtoint(ComboBox2.text);
  btlstr:='   串口波特率:'+ComboBox2.text;
  Comm1.ByteSize:=_8;
  Comm1.ParityCheck:=False;
  Comm1.StopBits:=_1;
  Comm1.XoffChar:='#';
  jlmp:=jlmp+1;
  if jlmp<5 then
  begin
    combobox3.ItemIndex:=jlmp;
    case combobox3.ItemIndex of
                  0:
                    begin
                         Comm1.Parity:=None;
                         btlstr:=btlstr+',校验位:None'
                    end;
                  1:
                    begin
                         Comm1.Parity:=Odd;
                         btlstr:=btlstr+',校验位:Odd '
                    end;
                  2:
                    begin
                         Comm1.Parity:=Even;
                         btlstr:=btlstr+',校验位:Even'
                    end;
                  3:
                    begin
                         Comm1.Parity:=Mark;
                         btlstr:=btlstr+',校验位:Mark'
                    end;
                  4:
                    begin
                         Comm1.Parity:=Space;
                         btlstr:=btlstr+',校验位:Space'
                    end;
    end;
    Comm1.StartComm; //打开串口
    sleep(2);

    sendd[0] := $00;
    sendd[1] := $03;
    sendd[2] := $00;
    sendd[3] := $02;
    sendd[4] := $00;
    sendd[5] := $01;
    sendd[6] := $24;
    sendd[7] := $1B;

    FuncCode:=32;   //读取在线站号
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    SendDataStr:=SendDataStr+btlstr ;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

  end
  else
  begin
       jlmp:=-1;
       btlp:=btlp+1;
  end;
end;

procedure TForm1.BitBtn22Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
   delaytime:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);
    delaytime:=StrToInt(RichEdit10.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $46;
    sendd[4] :=  (delaytime div 256) mod 256;
    sendd[5] := delaytime mod 256;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=3;   //驱动在线站号发声
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.BitBtn23Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
   delaytime:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);
    delaytime:=StrToInt(RichEdit10.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $46;
    sendd[4] := $00;
    sendd[5] := $00;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=3;   //驱动在线站号发声
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.BitBtn24Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit1.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $01;
    if checkbox4.checked=true then
    begin
        sendd[4] := $80;
        sendd[5] := Combobox11.ItemIndex+2*Combobox12.ItemIndex;
    end
    else
    begin
        sendd[4] := $00;
        sendd[5] := Combobox11.ItemIndex+2*Combobox12.ItemIndex;   //$00;
    end;


    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=3;   //驱动在线站号发声
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.BitBtn25Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
   delaytime:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);
    delaytime:=StrToInt(RichEdit11.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $46+Combobox15.ItemIndex;

    sendd[4] :=  (delaytime div 256) mod 256;
    sendd[5] := delaytime mod 256;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=46;   
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;


procedure TForm1.BitBtn26Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
   delaytime:longint;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit1.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit1.SetFocus();
            RichEdit1.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);
    delaytime:=StrToInt(RichEdit11.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $06;
    sendd[2] := $00;
    sendd[3] := $46;

    sendd[4] := $00;
    sendd[5] := $00;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=46;   
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
   sendd:array[0..7]of Byte;   //发送字节缓冲
   i:integer;
   part:integer;
   SendDataStr:string;
   oldjh:integer;
   CRC16:longint;
   hexpart:string;
begin
  if(BitBtn1.Caption <> '关闭串口') then
  begin
       Application.MessageBox('请先打开串口再进行修改在线设备站号的操作!', '警告', MB_OK+MB_ICONSTOP);
       Exit;
  end;
  if(not isrightint(RichEdit4.Lines[0])) then
  begin
            Application.MessageBox('设备号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
            RichEdit4.SetFocus();
            RichEdit4.SelectAll();
            exit;
  end;

    oldjh:=StrToInt(RichEdit4.Lines[0]);

    sendd[0] := oldjh;
    sendd[1] := $03;
    sendd[2] := $00;
    sendd[3] := $03;
    sendd[4] := $00;
    sendd[5] := $01;

    CRC16 := CRC_16(@sendd,6);

    sendd[6] := CRC16 mod 256;;
    sendd[7] := (CRC16 div 256) mod 256;


    FuncCode:=30;   //读设备参数
    issendwaitrev1 := True;
    sendwaitrevsize := 0;

    Comm1.WriteCommData(@sendd[0], 8);

    SendDataStr:='串口发送数据:';
    for i:=0 to 7 do
    begin
         SendDataStr:=SendDataStr+inttohex(sendd[i],2)+' ';
    end;

    ListBox1.Items.Add(SendDataStr);
    listbox1.ItemIndex:=listbox1.Count-1;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

end.

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Delphi Modbus RTU串口助手是一款基于Delphi开发的用于与Modbus RTU设备通信的串口助手软件。Modbus RTU是一种串口通信协议,常用于工业领域的设备通信。该串口助手软件提供了源码,可以帮助用户快速了解和学习Modbus RTU通信协议,并针对具体需求进行二次开发。 Delphi Modbus RTU串口助手源码的主要功能包括:串口通信的建立与关闭、Modbus协议的封装与解析、Modbus命令的发送与接收、数据的读取与写入、错误处理等。用户可以根据具体的需求,进行修改和调整源码,以满足自己的实际应用场景。 该串口助手源码使用Delphi开发,具有跨平台性和易于使用的特点。用户可以根据自己的需求,将源码集成到自己的项目中,实现与Modbus RTU设备的通信。同时,Delphi提供了丰富的GUI界面开发工具,用户可以根据自己的需求,对串口助手进行美化和界面优化。 总之,Delphi Modbus RTU串口助手源码为用户提供了一个快速开发和实现与Modbus RTU设备通信的平台。通过学习和使用该源码,用户可以轻松地实现数据的读写、命令的发送和接收等功能,提高工作效率并简化通信流程。 ### 回答2: Delphi Modbus RTU串口助手是一个用于Modbus RTU通信协议的串口助手应用程序。Modbus是一种通信协议,用于在不同设备之间进行数据通信。RTU(Remote Terminal Unit)是Modbus协议的一种传输模式,它使用二进制编码进行数据传输。 Delphi是一个编程语言,可以使用它来开发Windows应用程序。Delphi Modbus RTU串口助手是用Delphi开发的一个应用程序,可以通过串口与Modbus RTU设备进行通信。 该串口助手源码是指Delphi Modbus RTU串口助手的源代码。源代码是指程序员编写的用于实现特定功能的代码。由于我无法提供具体的源码,但可以为您提供一些源码设计的思路。 首先,您需要确定串口通信的参数,包括波特率、数据位、校验位和停止位等。然后,您需要使用Delphi提供的串口通信库来打开串口、设置串口参数,并进行数据的发送和接收。 在Modbus RTU通信协议中,有几个重要的功能码,例如读取线圈、读取输入寄存器、写入单个寄存器等。您需要在源码中实现这些功能码,并进行相应的数据解析和处理。 在处理串口数据时,您需要注意数据的格式,Modbus RTU使用16位的CRC校验来检测数据的完整性,您需要计算并验证CRC校验码。 除了基本的Modbus RTU功能外,您还可以考虑添加一些额外的功能,例如日志记录、数据显示和保存等。 这只是一个大致的源码设计思路,具体的实现和细节取决于您的需求和编程能力。希望这些信息对您有所帮助。 ### 回答3: Delphi Modbus RTU串口助手是一个用于Modbus通信协议的串口助手程序,旨在帮助开发人员简化Modbus通信的开发过程。源码提供了基于Delphi编程语言的示例代码,开发人员可以根据自己的需求进行修改和定制。 Modbus是一种常用的工业通信协议,用于实现不同设备之间的通信。RTUModbus的一种通信模式,通过串口进行数据传输。Delphi Modbus RTU串口助手源码基于这种模式,实现了Modbus通信过程的封装和处理。 通过这个源码,开发人员可以学习和理解Modbus通信协议及其在Delphi中的实现。源码提供了与Modbus相关的常用功能,如读取和写入寄存器,读取和写入线圈等。开发人员可以根据自己的需要进行功能的添加和修改。 在源码中,串口的相关设置、Modbus通信的建立和维护以及数据的解析和处理等都被封装在相应的函数和过程中,方便开发人员调用和使用。此外,源码还提供了一些常用的工具和方法,用于帮助开发人员进行调试和测试。 总之,Delphi Modbus RTU串口助手源码是一个有用的工具,可以帮助开发人员快速开发和实现基于Modbus通信协议的应用程序。通过学习和理解源码,开发人员可以更好地掌握Modbus通信协议及其在Delphi中的实现,从而提高开发效率和质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值