当前位置: 首页 > article >正文

ID读卡器UDP协议Delphi7小程序开发

 如下是小程序主页面:

代码如下:

function isrightint(textls:string):boolean;stdcall;
begin
  try
    if(strtoint(textls) =0) then
    begin
    end;
    result := True;

  except
    result := False;
    exit;
  end;
end;
procedure TForm1.Button9Click(Sender: TObject);
var
  sendbuf:array[0..280] of Byte;
  i:Integer;
begin
  Form3:=TForm3.Create(nil);
  Form3.ShowModal;
  if(Form3.ModalResult=mrOk) then
  begin
    //下传
    //启始机号2字节,结束机号2字节,目标电脑IP地址,
    sendbuf[0] := $e8;//表示修改目标电脑IP地址   1c

    //1d 1e 1f 20

    if Form3.isalljihao then
    begin
      sendbuf[1] := 0;
      sendbuf[2] := 0;

      sendbuf[3] := 255;
      sendbuf[4] := 255;
    end
    else
    begin
      i := StrToInt(Form3.startjihaostr);

      sendbuf[1] := i mod 256;
      sendbuf[2] := (i div 256) mod 256;

      i := StrToInt(Form3.endjihaostr);

      sendbuf[3] := i mod 256;
      sendbuf[4] := (i div 256) mod 256;

    end;

    //21 22 23 24 25 26 27 28
    sendbuf[5] := StrToInt(Form3.strremoteip1);
    sendbuf[6] := StrToInt(Form3.strremoteip2);
    sendbuf[7] := StrToInt(Form3.strremoteip3);
    sendbuf[8] := StrToInt(Form3.strremoteip4);

    sendbuf[9] := sendbuf[5];
    sendbuf[10] := sendbuf[6];
    sendbuf[11] := sendbuf[7];
    sendbuf[12] := sendbuf[8];

    //29 2a 2b 2c 2d 2e
    //对方的MAC   29 2a 2b 2c 2d 2e
    sendbuf[13] := StrToInt('$' + Form3.strremotemac1);
    sendbuf[14] := StrToInt('$' + Form3.strremotemac2);
    sendbuf[15] := StrToInt('$' + Form3.strremotemac3);
    sendbuf[16] := StrToInt('$' + Form3.strremotemac4);
    sendbuf[17] := StrToInt('$' + Form3.strremotemac5);
    sendbuf[18] := StrToInt('$' + Form3.strremotemac6);

    //2f
    if Form3.isautogetremotemac  then
    begin
      sendbuf[19] := 1;
    end
    else
    begin
      sendbuf[19] := 0;
    end;

    //30 31 32 33

    sendbuf[20] := $55;
    sendbuf[21] := $aa;
    sendbuf[22] := $66;
    sendbuf[23] := $99;

    IdUDPServer1.Binding.SendTo( '255.255.255.255',myport,sendbuf,24); //广播式发送

    Label6.Caption :='提示:已发送更改目标电脑IP地址信息至读卡器,请稍候搜寻所有读卡器以刷新最新参数!';
  end;

  Form3.Free;
end;


procedure TForm1.Button3Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
  if(Trim(Edit7.Text)  = '') then
  begin
   ShowMessage('时间长输入框不能为空');
   Edit7.Text:='';
   Edit7.SetFocus;
   Exit;
  end;
  if not isrightint(Edit7.Text) then
  begin
   ShowMessage('时间长输入必须为数字');
   Edit7.SelectAll;
   Edit7.SetFocus;
   Exit;

  end;

  if StrToInt(Edit7.Text)>65535 then
  begin
   ShowMessage('时间长输入不能大于65535,为65535是表示久永');
   Edit7.SelectAll;
   Edit7.SetFocus;
   Exit;

  end;

  if(Trim(Edit2.Text)  = '') then
  begin
   ShowMessage('机号不能为空');
   Edit2.Text:='';
   Edit2.SetFocus;
   Exit;
  end;
  if not isrightint(Edit2.Text) then
  begin
   ShowMessage('机号必须为数字');
   Edit2.SelectAll;
   Edit2.SetFocus;
   Exit;

  end;

  if StrToInt(Edit2.Text)>65535 then
  begin
   ShowMessage('机号不能大于65535');
   Edit2.SelectAll;
   Edit2.SetFocus;
   Exit;

  end;

  for i := 3 to 6 do
  begin
    pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
    strls := pedit.Text;
    if Trim(strls) = '' then
    begin
     ShowMessage('输入不能为空');
     pedit.Text:='';
     pedit.SetFocus;
     Exit;
    end;

     if not isrightint(strls) then
     begin
       ShowMessage('输入必须为数字');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

     if StrToInt(strls)>255 then
     begin
       ShowMessage('输入不能大于255');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

  end;

  //开启继电器

  sendbuf[0] := $78;//命令字:驱动继电器

  //机号低,机号高,命令字 时间低,时间高
  strls:=Edit2.Text;
  sendbuf[1] := StrToInt(strls) mod 256;
  sendbuf[2] := (StrToInt(strls) div 256) mod 256;

  case ComboBox3.ItemIndex of
    1: sendbuf[3] := $e1;
    2: sendbuf[3] := $e2;
    3: sendbuf[3] := $e3;
    4: sendbuf[3] := $e4;
    5: sendbuf[3] := $e5;
    6: sendbuf[3] := $e6;
    7: sendbuf[3] := $e7;
    8: sendbuf[3] := $e8;
  else sendbuf[3] := $e0;
  end;

  strls:=Edit7.Text;
  sendbuf[4] := StrToInt(strls) mod 256;
  sendbuf[5] := (StrToInt(strls) div 256) mod 256;

  if not CheckBox1.Checked then
  begin
    strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
  end
  else
  begin
    strls := '255.255.255.255';
  end;

  IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,6);


end;



procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;ABinding: TIdSocketHandle);
var
  len:Integer;
  strls:string;
  strls1:string;

  remortip:string;
  remortprot:integer;

  i:Integer;
  j:Integer;
  m:Integer;
  keystrlen:Integer;

  strip,Ancstr:string;

  listls:TListItem;
  divnostr:string;

  sendbuf:array[0..280] of Byte;

  MyUdpData:   array[1..280] of byte;

  cardhao:Longword;
  mccount:LongWord;

begin
  AData.Position:= 0;
  len:=AData.Size;
  if(len>269) then
  begin
    len := 269;
  end;

  if(len>0) then
  begin
    AData.Read(MyUdpData[1],len);

    remortip:=aBinding.PeerIP;
    remortprot:=aBindIng.PeerPort;

    divnostr:='From:'+copy(remortip+':'+inttostr(remortprot)+'                       ',1,25)+'Data: '   ;
    for i:=1 to len do
    begin
        divnostr := divnostr + IntToHex(MyUdpData[i],2)+' ';
    end;
    ListBox1.Items.Add(divnostr) ;
    listbox1.ItemIndex:=listbox1.Count-1;
    Label25.Caption := IntToStr(strtoint(Label25.Caption) + 1);

    if(MyUdpData[1] = $f1) then
    begin//有新读卡器重启
      //机器序列号
      divnostr:='';
      for i := 16 to 19 do
      begin
        divnostr := divnostr + RightStr('00'+IntToStr(MyUdpData[i]),3);
        if i <> 19 then
        begin
          divnostr:=divnostr+'-';
        end;
      end;

      for i := 0 to ListView1.Items.Count - 1  do
      begin
        if divnostr = ListView1.Items[i].SubItems.Strings[9] then
        begin
          ListView1.Items.Delete(i);
          Break;
        end;
      end;

      //len = 19
      //命令,IP地址            ,掩码             ,对方地址         , 机号,机器序列号
      //(241, 192, 168, 1, 218, 255, 255, 255, 0, 255, 255, 255, 255, 0, 0, 186, 0, 220, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
      // 1    2    3   4   5    6    7    8   9   10   11  12   13   14 15  16
      listls:=ListView1.Items.Add;

      //机号
      listls.Caption:=RightStr('0000'+IntToStr(MyUdpData[15]*256+MyUdpData[14]),5);

      //本机IP地址
      strip:='';
      for i := 2 to 5 do
      begin
        strip := strip + RightStr('00'+IntToStr(MyUdpData[i]),3);
        if i <> 5 then
        begin
          strip:=strip+'.';
        end;
      end;
      listls.SubItems.Add(strip);

      //子网掩码
      strls:='';
      for i := 6 to 9 do
      begin
        strls := strls + RightStr('00'+IntToStr(MyUdpData[i]),3);
        if i <> 9 then
        begin
          strls:=strls+'.';
        end;
      end;

      listls.SubItems.Add(strls);

      //固定端口号,读卡器内定的,不能再改
       listls.SubItems.Add('');

       listls.SubItems.Add('');//参数

       listls.SubItems.Add('');//网关IP

       listls.SubItems.Add('');//网关MAC

      //远程电脑IP地址
      strls:='';
      for i := 10 to 13 do
      begin
        strls := strls + RightStr('00'+IntToStr(MyUdpData[i]),3);
        if i <> 13 then
        begin
          strls:=strls+'.';
        end;
      end;

      listls.SubItems.Add(strls);

      listls.SubItems.Add('');//目标主机MAC

      listls.SubItems.Add('');//自动搜标志

      //机器序列号                         -
      listls.SubItems.Add(divnostr);

      //接收时间
      listls.SubItems.Add(FormatDateTime('hh:mm:ss',Now));
    end
    else if(MyUdpData[1] = $f2) then
    begin//有新读卡器重启
      //机器序列号
      divnostr:='';
      for i := 36 to 39 do
      begin
        divnostr := divnostr + RightStr('00'+IntToStr(MyUdpData[i]),3);
        if i <> 39 then
        begin
          divnostr:=divnostr+'-';
        end;
      end;

      //本机IP地址
      strip:='';
      for i := 2 to 5 do
      begin
        strip := strip + RightStr('00'+IntToStr(MyUdpData[i]),3);
        if i <> 5 then
        begin
          strip:=strip+'.';
        end;
      end;

      for i := 0 to ListView1.Items.Count - 1  do
      begin
        if (divnostr = ListView1.Items[i].SubItems.Strings[9]) and (strip = ListView1.Items[i].SubItems.Strings[0])  then
        begin
          ListView1.Items.Delete(i);
          Break;
        end;
      end;

      listls:=ListView1.Items.Add;

      //机号
      listls.Caption:=RightStr('0000'+IntToStr(MyUdpData[13]*256+MyUdpData[12]),5);


      listls.SubItems.Add(strip);

      //子网掩码
      strls:='';
      for i := 6 to 9 do
      begin
        strls := strls + RightStr('00'+IntToStr(MyUdpData[i]),3);
        if i <> 9 then
        begin
          strls:=strls+'.';

        end;
      end;

      listls.SubItems.Add(strls);

      //端口号
      strls := RightStr('0000'+IntToStr(MyUdpData[11]*256+MyUdpData[10]),5);

      listls.SubItems.Add(strls);

      //参数
      strls := IntToStr(MyUdpData[35]);

      listls.SubItems.Add(strls);


      //网关IP
      strls:='';
      for i := 14 to 17 do
      begin
        strls := strls + RightStr('00'+IntToStr(MyUdpData[i]),3);
        if i <> 17 then
        begin
          strls:=strls+'.';
        end;
      end;
      listls.SubItems.Add(strls);

      //网关MAC
      strls:='';
      for i := 18 to 23 do
      begin
        strls := strls + IntToHex(MyUdpData[i],2);
        if i <> 23 then
        begin
          strls:=strls+'-';
        end;
      end;
      listls.SubItems.Add(strls);

      //远程电脑IP地址
      strls:='';
      for i := 24 to 27 do
      begin
        strls := strls + RightStr('00'+IntToStr(MyUdpData[i]),3);
        if i <> 27 then
        begin
          strls:=strls+'.';
        end;
      end;
      listls.SubItems.Add(strls);

      //远程电脑MAC
      strls:='';
      for i := 28 to 33 do
      begin
        strls := strls + IntToHex(MyUdpData[i],2);
        if i <> 33 then
        begin
          strls:=strls+'-';
        end;
      end;
      listls.SubItems.Add(strls);

      //自动搜标志
      strls := IntToStr(MyUdpData[34]);
      listls.SubItems.Add(strls);


      //机器序列号
      listls.SubItems.Add(divnostr);

      //接收时间
      listls.SubItems.Add(FormatDateTime('hh:mm:ss',Now));
    end
    else if(MyUdpData[1] = $f7) then
    begin//WIFI热点和密码设定成功
      if (len = 8) and (MyUdpData[7] = 0) then
      begin
        ShowMessage('WIFI热点和密码设定成功!');
      end;


    end
    else if(MyUdpData[1] = $a4) then
    begin//WIFI热点和密码设定成功
      if (len >= 44) then
      begin
        strls := '';
        //wifi的ip
        strls := strls + 'WIFI的IP[' +  IntToStr(MyUdpData[8]) + '.' + IntToStr(MyUdpData[9]) + '.' + IntToStr(MyUdpData[10]) + '.' + IntToStr(MyUdpData[11]) + '],';

        //wifi的mask子网掩码
        strls := strls + '子网掩码[' +  IntToStr(MyUdpData[12]) + '.' + IntToStr(MyUdpData[13]) + '.' + IntToStr(MyUdpData[14]) + '.' + IntToStr(MyUdpData[15]) + '],';

        //MAC地址:
        strls := strls + '本地MAC地址[' + IntToHex(MyUdpData[16],2) + '-' + IntToHex(MyUdpData[17],2) + '-' + IntToHex(MyUdpData[18],2) + '-' + IntToHex(MyUdpData[19],2) + '-' + IntToHex(MyUdpData[20],2) + '-' + IntToHex(MyUdpData[21],2) + '],';

        //wifi的网关IP
        strls := strls + '网关IP[' +  IntToStr(MyUdpData[22]) + '.' + IntToStr(MyUdpData[23]) + '.' + IntToStr(MyUdpData[24]) + '.' + IntToStr(MyUdpData[25]) + '],';

        //wifi的网关MAC
        strls := strls + '网关MAC地址[' + IntToHex(MyUdpData[26],2) + '-' + IntToHex(MyUdpData[27],2) + '-' + IntToHex(MyUdpData[28],2) + '-' + IntToHex(MyUdpData[29],2) + '-' + IntToHex(MyUdpData[30],2) + '-' + IntToHex(MyUdpData[31],2) + '],';
        strls := strls + '状态[';

        //3:未初始化  2:已初始化  1连接失败  0已连接wifi
        if(MyUdpData[32] = 0) then
        begin
          strls := strls + '已连接wifi],';
        end
        else if(MyUdpData[32] = 1) then
        begin
          strls := strls + '连接失败],';
        end
        else if(MyUdpData[32] = 2) then
        begin
          strls := strls + '已初始化],';
        end
        else
        begin
          strls := strls + '未初始化],';
        end;

        strls1 := '                                     ';

        for i := 1 to MyUdpData[33] do
        begin
          strls1[i] := Char(MyUdpData[34+i]);
        end;

        strls := strls + '读卡器名称[' + Trim(strls1) + '],';

        strls1 := '                                                                                                                                                                                                ';

        for i := 1 to MyUdpData[34] do
        begin
          strls1[i] := char(MyUdpData[44+i]);
        end;
        Ancstr:=UTF8ToAnsi(strls1);

        strls := strls + '热点名称[' + Trim(Ancstr) + ']';

        Memo3.Text := strls;

      end;


    end
    else if (MyUdpData[1] = $c1) or (MyUdpData[1] = $d1) or (MyUdpData[1] = $d4) then  //$c1表示IC卡,$d1表示ID卡,$C4,$D4表示接收到韦根信号
    begin

      //读卡器IP地址
      strip := IntToStr(MyUdpData[2]) + '.' + IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]);



      strls := '接收:读卡器IP地址[';

      strls := strls + strip;

      strls := strls + '],机号[';

      //机号
      strls := strls + IntToStr(MyUdpData[6] + MyUdpData[7]*256);

      strls := strls + '],数据包序号[';

      //数据包序号,每个包都不一样,按递增1变化
      strls := strls + IntToStr(MyUdpData[8] + MyUdpData[9]*256);

      strls := strls + '],物理卡号[';

      //物理卡号
      //十六进制的
      //strls := strls + IntToHex(MyUdpData[10],2) + '-' + IntToHex(MyUdpData[11],2) + '-' + IntToHex(MyUdpData[12],2) + '-' + IntToHex(MyUdpData[13],2) + '-' + IntToHex(MyUdpData[14],2);

      //十进制的
      if (MyUdpData[1] = $c1) then
      begin//IC卡
        for i := 0 to 3 do
        begin
          cardnumberbuf[i] := MyUdpData[11+i]
        end;
      end
      else
      begin//ID卡
        for i := 0 to 3 do
        begin
          cardnumberbuf[i] := MyUdpData[10+i]
        end;
      end;

      if (MyUdpData[1] = $d4) then
      begin//韦根码
//        cardhao := cardnumberbuf[0] + cardnumberbuf[1]*256;
//        strls1 := RightStr('0000' + IntToStr(cardhao),5);

        cardhao := cardnumberbuf[0] + cardnumberbuf[1]*256 + cardnumberbuf[2]*256*256 + cardnumberbuf[3]*256*256*256;
        strls1 := RightStr('000000000' + IntToStr(cardhao),10);

      end
      else
      begin
        cardhao := cardnumberbuf[0] + cardnumberbuf[1]*256 + cardnumberbuf[2]*256*256 + cardnumberbuf[3]*256*256*256;
        strls1 := RightStr('000000000' + IntToStr(cardhao),10);
      end;


      strls := strls + strls1;


      strls := strls + ']';
      Memo2.Text := strls;




      strls := '';
      for i := 3 downto 0 do
      begin
        strls := strls + RightStr('00' + IntToHex(cardnumberbuf[i],2),2);

      end;
      Edit1.Text := strls;


      //接收成功要发送确认信息

      sendbuf[0] := $69;//命令字,表示确认收到信息

      //IP地址
      sendbuf[1] := MyUdpData[2];
      sendbuf[2] := MyUdpData[3];
      sendbuf[3] := MyUdpData[4];
      sendbuf[4] := MyUdpData[5];

      //机号
      sendbuf[5] := MyUdpData[6];
      sendbuf[6] := MyUdpData[7];

      //数据包序号
      sendbuf[7] := MyUdpData[8];
      sendbuf[8] := MyUdpData[9];

      IdUDPServer1.Binding.SendTo(abinding.PeerIP,abinding.PeerPort,sendbuf,9);

      sendbuf[0] := $5a;//命令字:显示发送至双行显示屏的读卡器
    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    if(CheckBox2.Checked) then
    begin//可以同时发出声响
      sendbuf[3] := ComboBox2.ItemIndex;
    end
    else
    begin
      sendbuf[3] := $ff;//不发出声响

    end;

    //背光可设为不亮(为兼容以前的客户,特改为以下算法)
//    if(sendbuf[3] = $ff) then
//    begin
//      sendbuf[3] := sendbuf[3] and $7f;//不发出声响时,bit7为0表示背光不亮
//    end
//    else
//    begin
//      sendbuf[3] := sendbuf[3] or $80;//发声响时,bit7为1表示背光不亮
//    end;

    sendbuf[4] := 1;//显示保留时间,单位为秒,为255时表示永久显示

    //显示文字的ASCII码
    //Edit8
    strls := Edit8.Text + '                                        ';
    for i := 1 to 34 do
    begin
      sendbuf[4+i] := Byte(strls[i]);

    end;

    IdUDPServer1.Binding.SendTo(abinding.PeerIP,abinding.PeerPort,sendbuf,4+1+34); //发送 ,全屏时字符数为34


    end

    else if (MyUdpData[1] = $c2) or (MyUdpData[1] = $d2) then  //$c2表示IC卡设备的按键信息,$d2表示ID卡设备的按键信息
    begin

      //读卡器IP地址
      strip := IntToStr(MyUdpData[2]) + '.' + IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]);



      strls := '接收:读卡器IP地址[';

      strls := strls + strip;

      strls := strls + '],机号[';

      //机号
      strls := strls + IntToStr(MyUdpData[6] + MyUdpData[7]*256);

      strls := strls + '],数据包序号[';

      //数据包序号,每个包都不一样,按递增1变化
      strls := strls + IntToStr(MyUdpData[8] + MyUdpData[9]*256);

      strls := strls + '],按键信息[';

       //MyUdpData[10]长度
      keystrlen := MyUdpData[10];
      for i := 1 to keystrlen do
      begin
        strls := strls + chr(MyUdpData[10 + i]);
      end;

      strls := strls + ']';
      Memo2.Text := strls;

      //接收成功要发送确认信息

      sendbuf[0] := $69;//命令字,表示确认收到信息

      //IP地址
      sendbuf[1] := MyUdpData[2];
      sendbuf[2] := MyUdpData[3];
      sendbuf[3] := MyUdpData[4];
      sendbuf[4] := MyUdpData[5];

      //机号
      sendbuf[5] := MyUdpData[6];
      sendbuf[6] := MyUdpData[7];

      //数据包序号
      sendbuf[7] := MyUdpData[8];
      sendbuf[8] := MyUdpData[9];

      IdUDPServer1.Binding.SendTo(strip,myport,sendbuf,9); //广播式发送

    end
    else if(MyUdpData[1] = $c3) then
    begin //读卡  10 11 12

      //读卡器IP地址
      strip := IntToStr(MyUdpData[2]) + '.' + IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]);



      strls := '接收:读卡器IP地址[';

      strls := strls + strip;

      strls := strls + '],机号[';

      //机号
      strls := strls + IntToStr(MyUdpData[6] + MyUdpData[7]*256);

      strls := strls + '],数据包序号[';

      //数据包序号,每个包都不一样,按递增1变化
      strls := strls + IntToStr(MyUdpData[8] + MyUdpData[9]*256);

      strls := strls + '],总接收扇区数[' + IntToStr(MyUdpData[10]) + ']';

      Memo2.Text := strls;

      j := MyUdpData[11] * 48;
      m := MyUdpData[12] * 48;

      for i := 0 to 3 do
      begin
        cardnumberbuf[i] := MyUdpData[13+i]
      end;

      for i := 0 to (m-1) do
      begin
        readcardbuf[j+i] := MyUdpData[17+i]
      end;

      if (MyUdpData[12] + MyUdpData[11]) >= MyUdpData[10] then
      begin//已完全收到所有包
        strls := '';
        for i := 3 downto 0 do
        begin
          strls := strls + RightStr('00' + IntToHex(cardnumberbuf[i],2),2);

        end;
        Edit1.Text := strls;

        m := MyUdpData[10] * 48;
        strls := '';
        for i := 0 to (m-1) do
        begin
          strls := strls + RightStr('00' + IntToHex(readcardbuf[i],2),2);

        end;


        Memo1.Text := strls;

      end;

    end

    else if(MyUdpData[1] = $c5) then//1  2  3  4 5  6  7  8  9  10 11 12 13 14 15
    begin //指定密码读卡的返回结果   3B C0 A8 01 DA 00 00 01 46 DF 44 C5 08 00 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 19 16

      //读卡器IP地址
      strip := IntToStr(MyUdpData[2]) + '.' + IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]);

       sendbuf[0] := $5a;//命令字:显示发送至双行显示屏的读卡器

      //机号
      strls:=Edit2.Text;
      sendbuf[1] := StrToInt(strls) mod 256;
      sendbuf[2] := (StrToInt(strls) div 256) mod 256;

      sendbuf[3] := 1;

      sendbuf[4] := 20;//显示保留时间,单位为秒,为255时表示永久显示

      if MyUdpData[14] = 0 then
      begin
        strls := '读卡成功!                                              ';
      end
      else if MyUdpData[14] = 12 then
      begin
        strls := '旧密码不正确!                                              ';

      end
      else
      begin
        strls := '错误代码:' + IntToStr(MyUdpData[14]) + '                                              ';
      end;

      for i := 1 to 34 do
      begin
        sendbuf[4+i] := Byte(strls[i]);

      end;

      IdUDPServer1.Binding.SendTo(strip,myport,sendbuf,4+1+34); //发送 ,全屏时字符数为34

      strls := '接收:读卡器IP地址[';

      strls := strls + strip;

      strls := strls + '],机号[';

      //机号
      strls := strls + IntToStr(MyUdpData[6] + MyUdpData[7]*256);

      strls := strls + '],共接收到卡号[';

      strls1 := IntToHex(MyUdpData[12],2) + IntToHex(MyUdpData[11],2) + IntToHex(MyUdpData[10],2) + IntToHex(MyUdpData[9],2);

      strls := strls + strls1 + ']的';

      Edit1.Text := strls1;

      //数据包序号,每个包都不一样,按递增1变化
      strls := strls + IntToStr(MyUdpData[8]);


      strls := strls + '个区返回,其中[' + IntToStr(MyUdpData[13]) + ']区返回代码为[';

      strls := strls + IntToStr(MyUdpData[14]) + ']';

      if MyUdpData[14] = 0 then
      begin//返回成功,下面提前区数据48个字节
        strls := strls + ',卡内数据如下';
        Memo2.Text := strls;

        strls := '';
        for i := 0 to 47 do
        begin
          strls := strls + RightStr('00' + IntToHex(MyUdpData[15+i],2),2);

        end;
        Memo1.Text := strls;

      end
      else
      begin
        Memo2.Text := strls;
        Memo1.Clear;

      end;

    end

    else if(MyUdpData[1] = $cd) then  //响应电脑改密码,写卡指令
    begin
      if(MyUdpData[2] = $3a) then
      begin        //1   2  3  4  5  6  7  8  9 10 11 12 13 14
        //修改卡密码 CD 3A C0 A8 01 DA 00 00 01 46 DF 44 C5 08 00

        //读卡器IP地址
        strip := IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]) + '.' + IntToStr(MyUdpData[6]);

        sendbuf[0] := $5a;//命令字:显示发送至双行显示屏的读卡器

        //机号
        strls:=Edit2.Text;
        sendbuf[1] := StrToInt(strls) mod 256;
        sendbuf[2] := (StrToInt(strls) div 256) mod 256;

        sendbuf[3] := 1;

        sendbuf[4] := 20;//显示保留时间,单位为秒,为255时表示永久显示

        if MyUdpData[15] = 0 then
        begin
          strls := '卡密码修改成功!                                              ';
        end
        else if MyUdpData[15] = 12 then
        begin
          strls := '旧密码不正确!                                              ';

        end
        else
        begin
          strls := '错误代码:' + IntToStr(MyUdpData[15]) + '                                              ';
        end;

        for i := 1 to 34 do
        begin
          sendbuf[4+i] := Byte(strls[i]);

        end;

        IdUDPServer1.Binding.SendTo(strip,myport,sendbuf,4+1+34); //发送 ,全屏时字符数为34

        strls := '更改卡第' + IntToStr(MyUdpData[14]) + '区密码:卡号[';
        //十六进制的
        strls := strls + IntToHex(MyUdpData[13],2) +IntToHex(MyUdpData[12],2) + IntToHex(MyUdpData[11],2) + IntToHex(MyUdpData[10],2) + '],提示:';

        if MyUdpData[15] = 0 then
        begin
          strls := strls + '成功!';
        end
        else if MyUdpData[15] = 12 then
        begin
          strls := strls + '旧密码不正确!';

        end
        else
        begin
          strls := strls + '错误代码:' + IntToStr(MyUdpData[15]);


        end;

        ShowMessage(strls);

      end
      else if(MyUdpData[2] = $3d) then
      begin
        //指定密码写卡 返回信息

        //读卡器IP地址
        strip := IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]) + '.' + IntToStr(MyUdpData[6]);

        sendbuf[0] := $5a;//命令字:显示发送至双行显示屏的读卡器

        //机号
        strls:=Edit2.Text;
        sendbuf[1] := StrToInt(strls) mod 256;
        sendbuf[2] := (StrToInt(strls) div 256) mod 256;

        sendbuf[3] := 1;

        sendbuf[4] := 20;//显示保留时间,单位为秒,为255时表示永久显示

        if MyUdpData[15] = 0 then
        begin
          strls := '卡密码修改成功!                                              ';
        end
        else if MyUdpData[15] = 12 then
        begin
          strls := '旧密码不正确!                                              ';

        end
        else
        begin
          strls := '错误代码:' + IntToStr(MyUdpData[15]) + '                                              ';
        end;

        for i := 1 to 34 do
        begin
          sendbuf[4+i] := Byte(strls[i]);

        end;

        IdUDPServer1.Binding.SendTo(strip,myport,sendbuf,4+1+34); //发送 ,全屏时字符数为34

        strls := '写数据到卡第' + IntToStr(MyUdpData[14]) + '区:卡号[';
        //十六进制的
        strls := strls + IntToHex(MyUdpData[13],2) + IntToHex(MyUdpData[12],2) + IntToHex(MyUdpData[11],2) + IntToHex(MyUdpData[10],2) + '],提示:';

        if MyUdpData[15] = 0 then
        begin
          strls := strls + '成功!';
        end
        else if MyUdpData[15] = 12 then
        begin
          strls := strls + '旧密码不正确!';

        end
        else
        begin
          strls := strls + '错误代码:' + IntToStr(MyUdpData[15]);


        end;

        ShowMessage(strls);

      end;

    end
    else if (MyUdpData[1] = $ce) or (MyUdpData[1] = $de) then  //响应电脑查询指令
    begin

      //读卡器IP地址
      strip := IntToStr(MyUdpData[2]) + '.' + IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]);



      strls := '响应电脑查询指令信息包:读卡器IP地址[';

      strls := strls + strip;

      strls := strls + '],机号[';

      //机号
      strls := strls + IntToStr(MyUdpData[6] + MyUdpData[7]*256);

      strls := strls + '],数据包序号[';

      //数据包序号,每个包都不一样,按递增1变化
      strls := strls + IntToStr(MyUdpData[8] + MyUdpData[9]*256);



      //MyUdpData[10]为返回的信息类型,1表示卡物理号

      if MyUdpData[10] = 0 then
      begin
        strls := strls + '],信息为空NULL';

        Memo2.Text := strls;

      end
      else
      begin
        strls := strls + '],物理卡号[';
        //十进制的
        if MyUdpData[11] > 10 then//物理卡号长度不能大于10个字节
        begin
          MyUdpData[11] := 10;
        end;

        for i := 1 to MyUdpData[11] do
        begin
          cardnumberbuf[i-1] := MyUdpData[11+i]
        end;

        cardhao := cardnumberbuf[0] + cardnumberbuf[1]*256 + cardnumberbuf[2]*256*256 + cardnumberbuf[3]*256*256*256;
        strls1 := RightStr('000000000' + IntToStr(cardhao),10);

        strls := strls + strls1;

        strls := strls + ']';
        Memo2.Text := strls;

        strls := '';
        for i := 3 downto 0 do
        begin
          strls := strls + RightStr('00' + IntToHex(cardnumberbuf[i],2),2);

        end;
        Edit1.Text := strls;

      end;

      

    end
    else if (MyUdpData[1] = $cf) or (MyUdpData[1] = $df) then  //表示卡离开,$cf表示IC卡,$df表示ID卡
    begin

      //读卡器IP地址
      strip := IntToStr(MyUdpData[2]) + '.' + IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]);



      strls := '卡离开信息包:读卡器IP地址[';

      strls := strls + strip;

      strls := strls + '],机号[';

      //机号
      strls := strls + IntToStr(MyUdpData[6] + MyUdpData[7]*256);

      strls := strls + '],数据包序号[';

      //数据包序号,每个包都不一样,按递增1变化
      strls := strls + IntToStr(MyUdpData[8] + MyUdpData[9]*256);

      strls := strls + '],物理卡号[';

      //MyUdpData[10]为返回的信息类型,1表示卡物理号

      //十进制的
      if MyUdpData[11] > 10 then//物理卡号长度不能大于10个字节
      begin
        MyUdpData[11] := 10;
      end;

      for i := 1 to MyUdpData[11] do
      begin
        cardnumberbuf[i-1] := MyUdpData[11+i]
      end;

      cardhao := cardnumberbuf[0] + cardnumberbuf[1]*256 + cardnumberbuf[2]*256*256 + cardnumberbuf[3]*256*256*256;
      strls1 := RightStr('000000000' + IntToStr(cardhao),10);

      strls := strls + strls1;

      strls := strls + ']';
      Memo2.Text := strls;

      strls := '';
      for i := 3 downto 0 do
      begin
        strls := strls + RightStr('00' + IntToHex(cardnumberbuf[i],2),2);

      end;
      Edit1.Text := strls;



      //接收成功要发送确认信息

      sendbuf[0] := $69;//命令字,表示确认收到信息

      //IP地址
      sendbuf[1] := MyUdpData[2];
      sendbuf[2] := MyUdpData[3];
      sendbuf[3] := MyUdpData[4];
      sendbuf[4] := MyUdpData[5];

      //机号
      sendbuf[5] := MyUdpData[6];
      sendbuf[6] := MyUdpData[7];

      //数据包序号
      sendbuf[7] := MyUdpData[8];
      sendbuf[8] := MyUdpData[9];

      IdUDPServer1.Binding.SendTo(strip,myport,sendbuf,9); //广播式发送


    end
    else if(MyUdpData[1] = $4b) then
    begin
      Label2.Caption := '设定成功!';


    end
    else if(MyUdpData[1] = $3c) then
    begin
      //继续发送写卡信息
      if((MyUdpData[8] + MyUdpData[9]) >= writecardarealen) then
      begin

        //读卡器IP地址
        strip := IntToStr(MyUdpData[2]) + '.' + IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]);

        Label2.Caption := '写卡成功!';

        sendbuf[0] := $5a;//命令字:显示发送至双行显示屏的读卡器

        //机号
        strls:=Edit2.Text;
        sendbuf[1] := StrToInt(strls) mod 256;
        sendbuf[2] := (StrToInt(strls) div 256) mod 256;

        sendbuf[3] := 1;

        sendbuf[4] := 20;//显示保留时间,单位为秒,为255时表示永久显示


        strls := '写卡成功!Write card successfully!                                              ';
        for i := 1 to 34 do
        begin
          sendbuf[4+i] := Byte(strls[i]);

        end;

        IdUDPServer1.Binding.SendTo(strip,myport,sendbuf,4+1+34); //发送 ,全屏时字符数为34

      end
      else
      begin

        sendbuf[0] := $3c;//命令字:写扇区肉容

        //机号
        strls:=Edit2.Text;
        sendbuf[1] := StrToInt(strls) mod 256;
        sendbuf[2] := (StrToInt(strls) div 256) mod 256;

        //本次传输的启始指针,以一区数据48个字节为一单位
        //总区数
        sendbuf[3] := writecardareapoint;

         //本次传输区数
        j := writecardarealen - writecardareapoint;
        if(j>4) then
        begin
          j := 4;
        end;
        sendbuf[4] := j;

        sendbuf[5] := cardnumberbuf[0];
        sendbuf[6] := cardnumberbuf[1];
        sendbuf[7] := cardnumberbuf[2];
        sendbuf[8] := cardnumberbuf[3];

        for i := 0 to (j*48 - 1) do
        begin
          sendbuf[9+i] := writecardbuf[writecardareapoint*48+i];
        end;


        if not CheckBox1.Checked then
        begin
          strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
        end
        else
        begin
          strls := '255.255.255.255';
        end;

        IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,9+j*48);

        Label2.Caption := '正在发送......';


        writecardareapoint := writecardareapoint + sendbuf[4];
      end;

    end


    else if (MyUdpData[1] = $F8) then  //设备返回时间信息
    begin
      //MyUdpData[10]为秒,[11]为分。。时日月周年
      //读卡器IP地址
      strip := IntToStr(MyUdpData[2]) + '.' + IntToStr(MyUdpData[3]) + '.' + IntToStr(MyUdpData[4]) + '.' + IntToStr(MyUdpData[5]);
      Edit19.Text := IntToStr(MyUdpData[16]);
      Edit20.Text := IntToStr(MyUdpData[14]);
      Edit21.Text := IntToStr(MyUdpData[13]);
      Edit23.Text := IntToStr(MyUdpData[12]);
      Edit24.Text := IntToStr(MyUdpData[11]);
      Edit25.Text := IntToStr(MyUdpData[10]);


      //接收成功要发送确认信息
      sendbuf[0] := $69;//命令字,表示确认收到信息

      //IP地址
      sendbuf[1] := MyUdpData[2];
      sendbuf[2] := MyUdpData[3];
      sendbuf[3] := MyUdpData[4];
      sendbuf[4] := MyUdpData[5];

      //机号
      sendbuf[5] := MyUdpData[6];
      sendbuf[6] := MyUdpData[7];

      //数据包序号
      sendbuf[7] := MyUdpData[8];
      sendbuf[8] := MyUdpData[9];

      IdUDPServer1.Binding.SendTo(strip,39169,sendbuf,9); //广播式发送




    end;






    
  end
  else
  begin
   // Adata.ReadBuffer(aUDP,Adata.Size);//这样收包

  end;

end;

procedure TForm1.Button8Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
   if(Trim(Edit2.Text)  = '') then
   begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Exit;
   end;
   if not isrightint(Edit2.Text) then
   begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;



   if StrToInt(Edit2.Text)>65535 then
   begin
     ShowMessage('机号不能大于65535');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   for i := 3 to 6 do
   begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

   end;

    sendbuf[0] := $96;//命令字:驱动蜂鸣器响

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    sendbuf[3] := ComboBox1.ItemIndex;

    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;

    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,4); //发送






end;

procedure TForm1.FormShow(Sender: TObject);
begin

  Label10.Caption := IdIPWatch1.LocalIP;
  ListView1.Items.Clear;

  myport := 39169;
  IdUDPServer1.DefaultPort:=39169;//39169为读卡器专用端口,是固定的不可更改
  IdUDPServer1.BroadcastEnabled:=True;

  try
    IdUDPServer1.Active := True;
  except
    on EIdCouldNotBindSocket do
    begin

      showmessage('读卡器专用UDP协议端口[39169]已被其他程序占用,无法打开,程序将自行退出,请检查后重新打开软件!');
      Close();

    end;


  end;

  Memo1.Text := '123456789012345612345678901234561234567890123456123456789012345612345678901234561234567890123456';





end;

procedure TForm1.Button1Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
  if(Trim(Edit7.Text)  = '') then
  begin
   ShowMessage('时间长输入框不能为空');
   Edit7.Text:='';
   Edit7.SetFocus;
   Exit;
  end;
  if not isrightint(Edit7.Text) then
  begin
   ShowMessage('时间长输入必须为数字');
   Edit7.SelectAll;
   Edit7.SetFocus;
   Exit;

  end;

  if StrToInt(Edit7.Text)>65535 then
  begin
   ShowMessage('时间长输入不能大于65535,为65535是表示久永');
   Edit7.SelectAll;
   Edit7.SetFocus;
   Exit;

  end;

  if(Trim(Edit2.Text)  = '') then
  begin
   ShowMessage('机号不能为空');
   Edit2.Text:='';
   Edit2.SetFocus;
   Exit;
  end;
  if not isrightint(Edit2.Text) then
  begin
   ShowMessage('机号必须为数字');
   Edit2.SelectAll;
   Edit2.SetFocus;
   Exit;

  end;

  if StrToInt(Edit2.Text)>65535 then
  begin
   ShowMessage('机号不能大于65535');
   Edit2.SelectAll;
   Edit2.SetFocus;
   Exit;

  end;

  for i := 3 to 6 do
  begin
    pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
    strls := pedit.Text;
    if Trim(strls) = '' then
    begin
     ShowMessage('输入不能为空');
     pedit.Text:='';
     pedit.SetFocus;
     Exit;
    end;

     if not isrightint(strls) then
     begin
       ShowMessage('输入必须为数字');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

     if StrToInt(strls)>255 then
     begin
       ShowMessage('输入不能大于255');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

  end;

  //开启继电器

  sendbuf[0] := $78;//命令字:驱动继电器

  //机号低,机号高,命令字 时间低,时间高
  strls:=Edit2.Text;
  sendbuf[1] := StrToInt(strls) mod 256;
  sendbuf[2] := (StrToInt(strls) div 256) mod 256;

  case ComboBox3.ItemIndex of
    1: sendbuf[3] := $f1;
    2: sendbuf[3] := $f2;
    3: sendbuf[3] := $f3;
    4: sendbuf[3] := $f4;
    5: sendbuf[3] := $f5;
    6: sendbuf[3] := $f6;
    7: sendbuf[3] := $f7;
    8: sendbuf[3] := $f8;
  else sendbuf[3] := $f0;
  end;

  strls:=Edit7.Text;
  sendbuf[4] := StrToInt(strls) mod 256;
  sendbuf[5] := (StrToInt(strls) div 256) mod 256;

  if not CheckBox1.Checked then
  begin
    strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
  end
  else
  begin
    strls := '255.255.255.255';
  end;

  IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,6);


end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
sendbuf:array[0..280] of Byte;
begin
  sendbuf[0] := $96;//命令字:驱动蜂鸣器响

    //机号

    sendbuf[1] := 0;
    sendbuf[2] := 0;

    sendbuf[3] := 0;


    IdUDPServer1.Binding.SendTo('192.168.1.218',myport,sendbuf,4); //发送
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  sendbuf:array[0..280] of Byte;
begin
    sendbuf[0] := $a5;//搜寻所有连线的读卡器命令

    IdUDPServer1.Binding.SendTo('255.255.255.255',myport,sendbuf,1); //广播式发送

end;

procedure TForm1.Button5Click(Sender: TObject);
var
  listls:TListItem;
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;

begin
  if ListView1.SelCount>0 then
  begin

    listls:=ListView1.Selected;

    if(Trim(listls.SubItems.Strings[4]) <>'') then
    begin//跨网关型读卡器

      Form2:=TForm2.Create(nil);
      Form2.strjihao:=listls.Caption;
      strls:=listls.SubItems.Strings[0];
      Form2.strmyip1:=MidStr(strls,1,3);
      Form2.strmyip2:=MidStr(strls,5,3);
      Form2.strmyip3:=MidStr(strls,9,3);
      Form2.strmyip4:=MidStr(strls,13,3);

      strls:=listls.SubItems.Strings[1];
      Form2.strsubnetmask1:=MidStr(strls,1,3);
      Form2.strsubnetmask2:=MidStr(strls,5,3);
      Form2.strsubnetmask3:=MidStr(strls,9,3);
      Form2.strsubnetmask4:=MidStr(strls,13,3);

      //端口号
      if Trim(listls.SubItems.Strings[3]) = '' then
      begin
        Form2.strmyport := '39169';
      end
      else
      begin
        Form2.strmyport := listls.SubItems.Strings[2];
      end;


      //参数
      if Trim(listls.SubItems.Strings[3]) = '' then
      begin
        i := $01;

      end
      else
      begin
        i := StrToInt(listls.SubItems.Strings[3]);

      end;

      if (i mod 2) > 0 then   //b0
      begin
        Form2.isoncardbeep1 := True;
      end
      else
      begin
        Form2.isoncardbeep1 := False;
      end;

      i := i div 2;

      if (i mod 2) > 0 then   //b1
      begin
        Form2.isdhcp1 := True;
      end
      else
      begin
        Form2.isdhcp1 := False;
      end;

      i := i div 2;

      if (i mod 2) > 0 then   //b2
      begin
        Form2.isoutcardsend1 := True;
      end
      else
      begin
        Form2.isoutcardsend1 := False;
      end;

      Form2.devcardondelaytype := ((i div 2) mod 8); //b345,b67保留

       //网关
      strls:=listls.SubItems.Strings[4];
      Form2.strgateip1:=MidStr(strls,1,3);
      Form2.strgateip2:=MidStr(strls,5,3);
      Form2.strgateip3:=MidStr(strls,9,3);
      Form2.strgateip4:=MidStr(strls,13,3);

      //网关mac
      strls:=listls.SubItems.Strings[5];
      Form2.strgatemac1:=MidStr(strls,1,2);
      Form2.strgatemac2:=MidStr(strls,4,2);
      Form2.strgatemac3:=MidStr(strls,7,2);
      Form2.strgatemac4:=MidStr(strls,10,2);
      Form2.strgatemac5:=MidStr(strls,13,2);
      Form2.strgatemac6:=MidStr(strls,16,2);


      //目标主机IP地址
      strls:=listls.SubItems.Strings[6];
      Form2.strremoteip1:=MidStr(strls,1,3);
      Form2.strremoteip2:=MidStr(strls,5,3);
      Form2.strremoteip3:=MidStr(strls,9,3);
      Form2.strremoteip4:=MidStr(strls,13,3);

      //目标主机MAC
      strls:=listls.SubItems.Strings[7];
      Form2.strremotemac1:=MidStr(strls,1,2);
      Form2.strremotemac2:=MidStr(strls,4,2);
      Form2.strremotemac3:=MidStr(strls,7,2);
      Form2.strremotemac4:=MidStr(strls,10,2);
      Form2.strremotemac5:=MidStr(strls,13,2);
      Form2.strremotemac6:=MidStr(strls,16,2);

      //自动搜标志
      if Trim(listls.SubItems.Strings[8]) = '' then
      begin
        Form2.isautogetremotemac1 := True;
        Form2.isautogetgatemac1 := True;
      end
      else
      begin

        i := StrToInt(listls.SubItems.Strings[8]);

        if(i mod 2) = 1 then
        begin
          Form2.isautogetremotemac1 := True;

        end
        else
        begin
          Form2.isautogetremotemac1 := False;
        end;

        if((i div 2)  mod 2) = 1 then
        begin
          Form2.isautogetgatemac1 := True;

        end
        else
        begin
          Form2.isautogetgatemac1 := False;
        end;
      end;

      //出厂序号
      Form2.strmydevno:=listls.SubItems.Strings[9];

      Form2.ShowModal;
      if(Form2.ModalResult=mrOk) then
      begin

      //用新指令更改
        sendbuf[0] := $F9;

        //IP地址  1d 1e 1f 20
        sendbuf[1] := StrToInt(Form2.strmyip1);
        sendbuf[2] := StrToInt(Form2.strmyip2);
        sendbuf[3] := StrToInt(Form2.strmyip3);
        sendbuf[4] := StrToInt(Form2.strmyip4);

        //子网掩码       21 22 23 24
        sendbuf[5] := StrToInt(Form2.strsubnetmask1);
        sendbuf[6] := StrToInt(Form2.strsubnetmask2);
        sendbuf[7] := StrToInt(Form2.strsubnetmask3);
        sendbuf[8] := StrToInt(Form2.strsubnetmask4);

        //对方地址     25 26 27 28
        sendbuf[9] := StrToInt(Form2.strremoteip1);
        sendbuf[10] := StrToInt(Form2.strremoteip2);
        sendbuf[11] := StrToInt(Form2.strremoteip3);
        sendbuf[12] := StrToInt(Form2.strremoteip4);

        //对方的MAC   29 2a 2b 2c 2d 2e
        sendbuf[13] := StrToInt('$' + Form2.strremotemac1);
        sendbuf[14] := StrToInt('$' + Form2.strremotemac2);
        sendbuf[15] := StrToInt('$' + Form2.strremotemac3);
        sendbuf[16] := StrToInt('$' + Form2.strremotemac4);
        sendbuf[17] := StrToInt('$' + Form2.strremotemac5);
        sendbuf[18] := StrToInt('$' + Form2.strremotemac6);

        //网关地址    2f 30 31 32
        sendbuf[19] := StrToInt(Form2.strgateip1);
        sendbuf[20] := StrToInt(Form2.strgateip2);
        sendbuf[21] := StrToInt(Form2.strgateip3);
        sendbuf[22] := StrToInt(Form2.strgateip4);

        //网关MAC    33 34 35 36 37 38
        sendbuf[23] := StrToInt('$' + Form2.strgatemac1);
        sendbuf[24] := StrToInt('$' + Form2.strgatemac2);
        sendbuf[25] := StrToInt('$' + Form2.strgatemac3);
        sendbuf[26] := StrToInt('$' + Form2.strgatemac4);
        sendbuf[27] := StrToInt('$' + Form2.strgatemac5);
        sendbuf[28] := StrToInt('$' + Form2.strgatemac6);

        //机号      39 3a
        strls:=Form2.strjihao;
        sendbuf[29] := StrToInt(strls) mod 256;
        sendbuf[30] := (StrToInt(strls) div 256) mod 256;

        //端口  3b 3c
         strls:=Form2.strmyport;
        sendbuf[31] := StrToInt(strls) mod 256;
        sendbuf[32] := (StrToInt(strls) div 256) mod 256;

        //机器序列号   3d 3e 3f 40
        strls:=Form2.strmydevno;
        sendbuf[33] := StrToInt(MidStr(strls,1,3));
        sendbuf[34] := StrToInt(MidStr(strls,5,3));
        sendbuf[35] := StrToInt(MidStr(strls,9,3));
        sendbuf[36] := StrToInt(MidStr(strls,13,3));

        if Form2.isoncardbeep1 then
        begin
          sendbuf[37] := 1;
        end
        else
        begin
          sendbuf[37] := 0;
        end;


        if Form2.isautogetremotemac1  then
        begin//自动
          sendbuf[37] := sendbuf[37] + 2;
        end;

        if Form2.isautogetgatemac1  then
        begin
          sendbuf[37] := sendbuf[37] + 4;
        end;

        if Form2.isdhcp1  then
        begin
          sendbuf[37] := sendbuf[37] + 8;
        end;

        if Form2.isoutcardsend1 then
        begin
          sendbuf[37] := sendbuf[37] + 16;
        end;

        sendbuf[37] := sendbuf[37] + Form2.devcardondelaytype * 32;

        //输入编码,防止误操作 42 43 44 45

        sendbuf[38] := $55;
        sendbuf[39] := $aa;
        sendbuf[40] := $66;
        sendbuf[41] := $99;

        IdUDPServer1.Binding.SendTo( '255.255.255.255',myport,sendbuf,42); //广播式发送

        Label6.Caption :='提示:广播式发送更改信息至读卡器,由于广播式信息在网络中的延迟,读卡器将在0~5秒内响应,请稍候......';


      end;

      Form2.Free;


    end
    else
    begin
      Form5:=TForm5.Create(nil);
      Form5.strjihao:=listls.Caption;
      strls:=listls.SubItems.Strings[0];
      Form5.strmyip1:=MidStr(strls,1,3);
      Form5.strmyip2:=MidStr(strls,5,3);
      Form5.strmyip3:=MidStr(strls,9,3);
      Form5.strmyip4:=MidStr(strls,13,3);

      strls:=listls.SubItems.Strings[1];
      Form5.strsubnetmask1:=MidStr(strls,1,3);
      Form5.strsubnetmask2:=MidStr(strls,5,3);
      Form5.strsubnetmask3:=MidStr(strls,9,3);
      Form5.strsubnetmask4:=MidStr(strls,13,3);

      strls:=listls.SubItems.Strings[6];
      Form5.strremoteip1:=MidStr(strls,1,3);
      Form5.strremoteip2:=MidStr(strls,5,3);
      Form5.strremoteip3:=MidStr(strls,9,3);
      Form5.strremoteip4:=MidStr(strls,13,3);

      if(Trim(listls.SubItems.Strings[2]) <> '') then
      begin
        Form5.strmyport:=listls.SubItems.Strings[2];
      end
      else
      begin
        Form5.strmyport:='39169';
      end;

      Form5.strmydevno:=listls.SubItems.Strings[9];

      Form5.ShowModal;
      if(Form5.ModalResult=mrOk) then
      begin

        sendbuf[0] := $F0;//表示修改读卡器参数

        //IP地址
        sendbuf[1] := StrToInt(Form5.strmyip1);
        sendbuf[2] := StrToInt(Form5.strmyip2);
        sendbuf[3] := StrToInt(Form5.strmyip3);
        sendbuf[4] := StrToInt(Form5.strmyip4);

        //子网掩码
        sendbuf[5] := StrToInt(Form5.strsubnetmask1);
        sendbuf[6] := StrToInt(Form5.strsubnetmask2);
        sendbuf[7] := StrToInt(Form5.strsubnetmask3);
        sendbuf[8] := StrToInt(Form5.strsubnetmask4);

        //对方地址
        sendbuf[9] := StrToInt(Form5.strremoteip1);
        sendbuf[10] := StrToInt(Form5.strremoteip2);
        sendbuf[11] := StrToInt(Form5.strremoteip3);
        sendbuf[12] := StrToInt(Form5.strremoteip4);

        //机号
        strls:=Form5.strjihao;
        sendbuf[13] := StrToInt(strls) mod 256;
        sendbuf[14] := (StrToInt(strls) div 256) mod 256;

        //机器序列号
        strls:=Form5.strmydevno;
        sendbuf[15] := StrToInt(MidStr(strls,1,3));
        sendbuf[16] := StrToInt(MidStr(strls,5,3));
        sendbuf[17] := StrToInt(MidStr(strls,9,3));
        sendbuf[18] := StrToInt(MidStr(strls,13,3));

        if Form5.isoncardbeep1  then
        begin
          sendbuf[19] := 1;
        end
        else
        begin
          sendbuf[19] := 0;
        end;



        IdUDPServer1.Binding.SendTo( '255.255.255.255',myport,sendbuf,20); //广播式发送

        Label6.Caption :='提示:广播式发送更改信息至读卡器,由于广播式信息在网络中的延迟,读卡器将在0~5秒内响应,请稍候......';


      end;

      Form5.Free;


    end;



  end
  else
  begin
    ShowMessage('请先选择需要更改的行');
  end;
end;

procedure TForm1.Button10Click(Sender: TObject);
begin
  if ListView1.SelCount>0 then
  begin
    if(Application.MessageBox('是否删除有记录?', '警告', MB_YESNO +MB_ICONINFORMATION) = IDYES) then
    begin
      ListView1.DeleteSelected;

    end;

  end
  else
  begin
    ShowMessage('请先选择需要删除的行');
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
//  if(Application.MessageBox('是否删除所有记录?', '警告', MB_YESNO +MB_ICONINFORMATION) = IDYES) then
//  begin
//
//
//  end;

  ListView1.Items.Clear;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
   if(Trim(Edit2.Text)  = '') then
   begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Exit;
   end;
   if not isrightint(Edit2.Text) then
   begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   if StrToInt(Edit2.Text)>65535 then
   begin
     ShowMessage('机号不能大于65535');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   for i := 3 to 6 do
   begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

   end;



   sendbuf[0] := $78;//命令字:驱动继电器

    //机号低,机号高,命令字 时间低,时间高
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    case ComboBox3.ItemIndex of
      1: sendbuf[3] := $f1;
      2: sendbuf[3] := $f2;
      3: sendbuf[3] := $f3;
      4: sendbuf[3] := $f4;
      5: sendbuf[3] := $f5;
      6: sendbuf[3] := $f6;
      7: sendbuf[3] := $f7;
      8: sendbuf[3] := $f8;
    else sendbuf[3] := $f0;
    end;

    strls:=Edit7.Text;
    sendbuf[4] := StrToInt(strls) mod 256;
    sendbuf[5] := (StrToInt(strls) div 256) mod 256;

    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;

    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,6);
























    sendbuf[0] := $5a;//命令字:显示发送至双行显示屏的读卡器

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    if(CheckBox2.Checked) then
    begin//可以同时发出声响
      sendbuf[3] := ComboBox2.ItemIndex;
    end
    else
    begin
      sendbuf[3] := $ff;//不发出声响

    end;

    //背光可设为不亮(为兼容以前的客户,特改为以下算法)
//    if(sendbuf[3] = $ff) then
//    begin
//      sendbuf[3] := sendbuf[3] and $7f;//不发出声响时,bit7为0表示背光不亮
//    end
//    else
//    begin
//      sendbuf[3] := sendbuf[3] or $80;//发声响时,bit7为1表示背光不亮
//    end;

    sendbuf[4] := 20;//显示保留时间,单位为秒,为255时表示永久显示

    //显示文字的ASCII码
    //Edit8
    strls := Edit8.Text + '                                        ';
    for i := 1 to 34 do
    begin
      sendbuf[4+i] := Byte(strls[i]);

    end;

    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;

    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,4+1+34); //发送 ,全屏时字符数为34







end;

procedure TForm1.Button11Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
   if(Trim(Edit2.Text)  = '') then
   begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Exit;
   end;
   if not isrightint(Edit2.Text) then
   begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   if StrToInt(Edit2.Text)>65535 then
   begin
     ShowMessage('机号不能大于65535');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   for i := 3 to 6 do
   begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

   end;

    sendbuf[0] := $5a;//显示发送至四行显示屏的读卡器

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    if(CheckBox2.Checked) then
    begin//可以同时发出声响
      sendbuf[3] := ComboBox2.ItemIndex;
    end
    else
    begin
      sendbuf[3] := $ff;//不发出声响

    end;

    sendbuf[4] := 20;//显示保留时间,单位为秒,为255时表示永久显示

    //显示文字的ASCII码
    //Edit8
    strls := Edit8.Text + '                                                                                                                        ';
    for i := 1 to 72 do
    begin
      sendbuf[4+i] := Byte(strls[i]);

    end;

    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;

    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,4+1+72); //发送 ,全屏时字符数为34

end;

procedure TForm1.Button15Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  j:Integer;
  m:Integer;
  pedit:TEdit;
  pcheckbox:TCheckBox;
  pComboBox:TComboBox;

begin
  Form4:=TForm4.Create(nil);

  Form4.ShowModal;
  if(Form4.ModalResult=mrOk) then
  begin
    if(Trim(Edit2.Text)  = '') then
    begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Form4.Free;
     Exit;
    end;

    if not isrightint(Edit2.Text) then
    begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Form4.Free;
     Exit;

    end;

    if StrToInt(Edit2.Text)>65535 then
    begin
     ShowMessage('机号不能大于65535');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Form4.Free;
     Exit;

    end;

    for i := 3 to 6 do
    begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Form4.Free;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Form4.Free;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Form4.Free;
         Exit;

       end;

    end;

    sendbuf[0] := $4b;//命令字:设定读卡的区号及区密码

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    j := 0;
    i := 3;

    for m := 1 to 16 do
    begin
      case m of
        1:
          begin
            pedit := Form4.Edit1;
            pcheckbox := Form4.CheckBox1;
            pComboBox := Form4.ComboBox1;

          end;
        2:
          begin
            pedit := Form4.Edit2;
            pcheckbox := Form4.CheckBox2;
            pComboBox := Form4.ComboBox2;
          end;
        3:
          begin
            pedit := Form4.Edit3;
            pcheckbox := Form4.CheckBox3;
            pComboBox := Form4.ComboBox3;
          end;
        4:
          begin
            pedit := Form4.Edit4;
            pcheckbox := Form4.CheckBox4;
            pComboBox := Form4.ComboBox4;
          end;
        5:
          begin
            pedit := Form4.Edit5;
            pcheckbox := Form4.CheckBox5;
            pComboBox := Form4.ComboBox5;
          end;
        6:
          begin
            pedit := Form4.Edit6;
            pcheckbox := Form4.CheckBox6;
            pComboBox := Form4.ComboBox6;

          end;
        7:
          begin
            pedit := Form4.Edit7;
            pcheckbox := Form4.CheckBox7;
            pComboBox := Form4.ComboBox7;

          end;
        8:
          begin
            pedit := Form4.Edit8;
            pcheckbox := Form4.CheckBox8;
            pComboBox := Form4.ComboBox8;

          end;
        9:
          begin
            pedit := Form4.Edit9;
            pcheckbox := Form4.CheckBox9;
            pComboBox := Form4.ComboBox9;

          end;
        10:
          begin
            pedit := Form4.Edit10;
            pcheckbox := Form4.CheckBox10;
            pComboBox := Form4.ComboBox10;
          end;
        11:
          begin
            pedit := Form4.Edit11;
            pcheckbox := Form4.CheckBox11;
            pComboBox := Form4.ComboBox11;

          end;
        12:
          begin
            pedit := Form4.Edit12;
            pcheckbox := Form4.CheckBox12;
            pComboBox := Form4.ComboBox12;

          end;
        13:
          begin
            pedit := Form4.Edit13;
            pcheckbox := Form4.CheckBox13;
            pComboBox := Form4.ComboBox13;

          end;
        14:
          begin
            pedit := Form4.Edit14;
            pcheckbox := Form4.CheckBox14;
            pComboBox := Form4.ComboBox14;

          end;
        15:
          begin
            pedit := Form4.Edit15;
            pcheckbox := Form4.CheckBox15;
            pComboBox := Form4.ComboBox15;

          end;
        16:
          begin
            pedit := Form4.Edit16;
            pcheckbox := Form4.CheckBox16;
            pComboBox := Form4.ComboBox16;

          end;
      end;

      if(pcheckbox.Checked) then
      begin
        Inc(j);

        Inc(i);
        sendbuf[i] := m-1; //第0区   4
        if pComboBox.ItemIndex > 0 then
        begin
          sendbuf[i] := sendbuf[i] or $80;
        end;

        Inc(i);
        sendbuf[i] :=  StrToInt('$' + midstr(pedit.Text,1,2));

        Inc(i);
        sendbuf[i] :=  StrToInt('$' + midstr(pedit.Text,3,2));

        Inc(i);
        sendbuf[i] :=  StrToInt('$' + midstr(pedit.Text,5,2));

        Inc(i);
        sendbuf[i] :=  StrToInt('$' + midstr(pedit.Text,7,2));

        Inc(i);
        sendbuf[i] :=  StrToInt('$' + midstr(pedit.Text,9,2));

        Inc(i);
        sendbuf[i] :=  StrToInt('$' + midstr(pedit.Text,11,2));

      end;

    end;


    sendbuf[3] := j;//区数量

    sendbuf[4+j*7] := $55;
    sendbuf[5+j*7] := $aa;
    sendbuf[6+j*7] := $66;
    sendbuf[7+j*7] := $99;


    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;

    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,8+j*7); //发送 ,全屏时字符数为34

    Label2.Caption := '正在发送......';


  end;

  Form4.Free;



end;

procedure TForm1.Button17Click(Sender: TObject);
begin
  Edit1.Text := '';
  Memo2.Text := '';
  Memo1.Text := '';
end;

procedure TForm1.Button16Click(Sender: TObject);
//写卡
var 
  strls:string;
  i:Integer;
  j:Integer;
  sendbuf:array[0..280] of Byte;
begin

  strls := Edit1.Text;

  for i := 0 to 3 do
  begin
    cardnumberbuf[3 - i] := StrToInt('$' + MidStr(strls,i*2+1,2));
  end;


  strls := Memo1.Text;

  j := Length(strls);

  j := j div 2;


  for i := 0 to (j-1) do
  begin
    writecardbuf[i] := StrToInt('$' + MidStr(strls,i*2+1,2));
  end;

  writecardarealen := j div 48;

  writecardareapoint := 0;

  if(writecardarealen > 0) then
  begin//至少一个扇区的内容
    
    sendbuf[0] := $3c;//命令字:写扇区肉容

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    //本次传输的启始指针,以一区数据48个字节为一单位
    //总区数
    sendbuf[3] := writecardareapoint;

     //本次传输区数
    j := writecardarealen - writecardareapoint;
    if(j>4) then
    begin
      j := 4;
    end;
    sendbuf[4] := j;

    sendbuf[5] := cardnumberbuf[0];
    sendbuf[6] := cardnumberbuf[1];
    sendbuf[7] := cardnumberbuf[2];
    sendbuf[8] := cardnumberbuf[3];

    for i := 0 to (j*48 - 1) do
    begin
      sendbuf[9+i] := writecardbuf[writecardareapoint*48+i];
    end;

    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;

    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,9+j*48);

    Label2.Caption := '正在发送......';


    writecardareapoint := writecardareapoint + sendbuf[4];



  end;


end;

procedure TForm1.Button18Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
   if(Trim(Edit2.Text)  = '') then
   begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Exit;
   end;
   if not isrightint(Edit2.Text) then
   begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   if StrToInt(Edit2.Text)>65535 then
   begin
     ShowMessage('机号不能大于65535');
     Edit1.SelectAll;
     Edit1.SetFocus;
     Exit;

   end;

   for i := 3 to 6 do
   begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

   end;

    sendbuf[0] := $1e;//命令字:设定开机及待机时的文字显示

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    sendbuf[3] := 0;//两行屏为0,四行屏为1

    //开机文字的ASCII码
    strls := Edit10.Text + '                                        ';
    for i := 1 to 34 do
    begin
      sendbuf[3+i] := Byte(strls[i]);

    end;

    //待机文字的ASCII码
    strls := Edit11.Text + '                                        ';
    for i := 1 to 34 do
    begin
      sendbuf[37+i] := Byte(strls[i]);//37+34+1=72

    end;

    //固定的后缀
    sendbuf[72] := $55;
    sendbuf[73] := $aa;
    sendbuf[74] := $66;
    sendbuf[75] := $99;


    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;
    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,76);

end;

procedure TForm1.Button12Click(Sender: TObject);
var
  sendbuf:array[0..280] of Byte;
begin
    sendbuf[0] := $a6;//返回连线读卡器的跨网关参数

    IdUDPServer1.Binding.SendTo('255.255.255.255',myport,sendbuf,1); //广播式发送

end;

procedure TForm1.Button13Click(Sender: TObject);
var
  i:Integer;
begin
  try

    i := StrToInt(Edit12.Text);
    if(i > 65535) then
    begin
      ShowMessage('端口输入错误,必须为不大于65535的数字!');
      Edit12.SelectAll;
      Edit12.SetFocus;
      Exit;
    end;

    IdUDPServer1.Free;

    IdUDPServer1:=TIdUDPServer.Create(self);
    IdUDPServer1.BroadcastEnabled:=True;

    IdUDPServer1.DefaultPort:=i;//39169为读卡器专用端口,是固定的不可更改

    IdUDPServer1.OnUDPRead := IdUDPServer1UDPRead;


    IdUDPServer1.Active := True;

    myport := i;

    ShowMessage('端口修改成功!');

  except

    on EIdCouldNotBindSocket do
    begin
      showmessage('读卡器专用UDP协议端口['+Edit12.Text+']已被其他程序占用,请尝试其他端口!');

      Edit12.SelectAll;
      Edit12.SetFocus;
      exit;
     end;
    on e:Exception do    //捕获异常
    begin
      ShowMessage('端口输入错误,必须为不大于65535的数字!');
      Edit12.SelectAll;
      Edit12.SetFocus;
    end;

  end;
end;

procedure TForm1.Button14Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
   if(Trim(Edit2.Text)  = '') then
   begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Exit;
   end;
   if not isrightint(Edit2.Text) then
   begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   if StrToInt(Edit2.Text)>65535 then
   begin
     ShowMessage('机号不能大于65535');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   for i := 3 to 6 do
   begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

   end;

    sendbuf[0] := $4b;//由于和设定区号密码指令相同,有冲突,仅用于2013前版本的四行只读读卡器

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    if(CheckBox2.Checked) then
    begin//可以同时发出声响
      sendbuf[3] := ComboBox2.ItemIndex;
    end
    else
    begin
      sendbuf[3] := $ff;//不发出声响

    end;

    sendbuf[4] := 20;//显示保留时间,单位为秒,为255时表示永久显示

    //显示文字的ASCII码
    //Edit8
    strls := Edit8.Text + '                                                                                                                        ';
    for i := 1 to 72 do
    begin
      sendbuf[4+i] := Byte(strls[i]);

    end;

    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;

    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,4+1+72); //发送 ,全屏时字符数为34

end;

procedure TForm1.Button19Click(Sender: TObject);
var
  sendbuf:array[0..280] of Byte;
  i:Integer;
begin
  Form6:=TForm6.Create(nil);
  Form6.ShowModal;
  if(Form6.ModalResult=mrOk) then
  begin
    //下传
    //启始机号2字节,结束机号2字节,目标电脑IP地址,
    sendbuf[0] := $e1;//表示修改目标电脑IP地址

    if Form6.isalljihao then
    begin
      sendbuf[1] := 0;
      sendbuf[2] := 0;

      sendbuf[3] := 255;
      sendbuf[4] := 255;
    end
    else
    begin
      i := StrToInt(Form6.startjihaostr);

      sendbuf[1] := i mod 256;
      sendbuf[2] := (i div 256) mod 256;

      i := StrToInt(Form6.endjihaostr);

      sendbuf[3] := i mod 256;
      sendbuf[4] := (i div 256) mod 256;

    end;


    sendbuf[5] := StrToInt(Form6.strremoteip1);
    sendbuf[6] := StrToInt(Form6.strremoteip2);
    sendbuf[7] := StrToInt(Form6.strremoteip3);
    sendbuf[8] := StrToInt(Form6.strremoteip4);

    sendbuf[9] := sendbuf[5];
    sendbuf[10] := sendbuf[6];
    sendbuf[11] := sendbuf[7];
    sendbuf[12] := sendbuf[8];

    IdUDPServer1.Binding.SendTo( '255.255.255.255',myport,sendbuf,13); //广播式发送

    Label6.Caption :='提示:已发送更改目标电脑IP地址信息至读卡器,请稍候搜寻所有读卡器以刷新最新参数!';
  end;

  Form6.Free;
end;

procedure TForm1.Button20Click(Sender: TObject);
var
  mydatetime:TDateTime;
begin
  mydatetime := Now;

  Edit19.Text := FormatDateTime('YY',mydatetime);
  Edit20.Text := FormatDateTime('MM',mydatetime);
  Edit21.Text := FormatDateTime('DD',mydatetime);
  Edit23.Text := FormatDateTime('hh',mydatetime);
  Edit24.Text := FormatDateTime('nn',mydatetime);
  Edit25.Text := FormatDateTime('ss',mydatetime);
end;

procedure TForm1.Button21Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin

  if(Trim(Edit2.Text)  = '') then
  begin
   ShowMessage('机号不能为空');
   Edit2.Text:='';
   Edit2.SetFocus;
   Exit;
  end;
  if not isrightint(Edit2.Text) then
  begin
   ShowMessage('机号必须为数字');
   Edit2.SelectAll;
   Edit2.SetFocus;
   Exit;

  end;

  if StrToInt(Edit2.Text)>65535 then
  begin
   ShowMessage('机号不能大于65535');
   Edit1.SelectAll;
   Edit1.SetFocus;
   Exit;

  end;

  for i := 3 to 6 do
  begin
    pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
    strls := pedit.Text;
    if Trim(strls) = '' then
    begin
     ShowMessage('输入不能为空');
     pedit.Text:='';
     pedit.SetFocus;
     Exit;
    end;

     if not isrightint(strls) then
     begin
       ShowMessage('输入必须为数字');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

     if StrToInt(strls)>255 then
     begin
       ShowMessage('输入不能大于255');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

  end;

  //读取设备时间
  sendbuf[0] := $d2;//读取设备时间

  //机号低,机号高,命令字 时间低,时间高
  strls:=Edit2.Text;
  sendbuf[1] := StrToInt(strls) mod 256;
  sendbuf[2] := (StrToInt(strls) div 256) mod 256;


  strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;

  IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,3);

  //设备返回结果在UDP返回消息中


end;

procedure TForm1.Button22Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
  if(Trim(Edit2.Text)  = '') then
  begin
   ShowMessage('机号不能为空');
   Edit2.Text:='';
   Edit2.SetFocus;
   Exit;
  end;
  if not isrightint(Edit2.Text) then
  begin
   ShowMessage('机号必须为数字');
   Edit2.SelectAll;
   Edit2.SetFocus;
   Exit;

  end;

  if StrToInt(Edit2.Text)>65535 then
  begin
   ShowMessage('机号不能大于65535');
   Edit1.SelectAll;
   Edit1.SetFocus;
   Exit;

  end;

  for i := 3 to 6 do
  begin
    pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
    strls := pedit.Text;
    if Trim(strls) = '' then
    begin
     ShowMessage('输入不能为空');
     pedit.Text:='';
     pedit.SetFocus;
     Exit;
    end;

     if not isrightint(strls) then
     begin
       ShowMessage('输入必须为数字');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

     if StrToInt(strls)>255 then
     begin
       ShowMessage('输入不能大于255');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

  end;


  sendbuf[0] := $c3;//设定设备时间

  //机号低,机号高,命令字 时间低,时间高
  strls:=Edit2.Text;
  sendbuf[1] := StrToInt(strls) mod 256;
  sendbuf[2] := (StrToInt(strls) div 256) mod 256;

  sendbuf[3] := StrToInt(Edit25.Text);
  sendbuf[4] := StrToInt(Edit24.Text);
  sendbuf[5] := StrToInt(Edit23.Text);
  sendbuf[6] := StrToInt(Edit21.Text);
  sendbuf[7] := StrToInt(Edit20.Text);
  sendbuf[8] := StrToInt(Edit19.Text);

  sendbuf[9] := 0;
  for i := 3 to 8 do
  begin
    sendbuf[9] := sendbuf[9] xor sendbuf[i];
  end;

  //固定的后缀 75+36+1
  sendbuf[10] := $55;
  sendbuf[11] := $aa;
  sendbuf[12] := $66;
  sendbuf[13] := $99;


  if not CheckBox1.Checked then
  begin
    strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
  end
  else
  begin
    strls := '255.255.255.255';
  end;

  IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,14);

end;

procedure TForm1.Button23Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
   if(Trim(Edit2.Text)  = '') then
   begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Exit;
   end;
   if not isrightint(Edit2.Text) then
   begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   if StrToInt(Edit2.Text)>65535 then
   begin
     ShowMessage('机号不能大于65535');
     Edit1.SelectAll;
     Edit1.SetFocus;
     Exit;

   end;

   for i := 3 to 6 do
   begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

   end;

    sendbuf[0] := $1e;//命令字:设定开机及待机时的文字显示

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    sendbuf[3] := 1;//两行屏为0,四行屏为1

    //开机文字的ASCII码
    strls := Edit10.Text + '                                                                                                                        ';
    for i := 1 to 72 do
    begin
      sendbuf[3+i] := Byte(strls[i]);

    end;

    //待机文字的ASCII码
    strls := Edit9.Text + '                                                                                ';
    for i := 1 to 36 do
    begin
      sendbuf[75+i] := Byte(strls[i]); //3+72

    end;

    //固定的后缀 75+36+1
    sendbuf[112] := $55;
    sendbuf[113] := $aa;
    sendbuf[114] := $66;
    sendbuf[115] := $99;


    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;
    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,116);    //4+34+34+4=76,4+72+36+4=116

end;

procedure TForm1.Button24Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
  if(Trim(Edit2.Text)  = '') then
  begin
   ShowMessage('机号不能为空');
   Edit2.Text:='';
   Edit2.SetFocus;
   Exit;
  end;
  if not isrightint(Edit2.Text) then
  begin
   ShowMessage('机号必须为数字');
   Edit2.SelectAll;
   Edit2.SetFocus;
   Exit;

  end;

  if StrToInt(Edit2.Text)>65535 then
  begin
   ShowMessage('机号不能大于65535');
   Edit1.SelectAll;
   Edit1.SetFocus;
   Exit;
  end;

  for i := 3 to 6 do
  begin
    pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
    strls := pedit.Text;
    if Trim(strls) = '' then
    begin
     ShowMessage('输入不能为空');
     pedit.Text:='';
     pedit.SetFocus;
     Exit;
    end;

     if not isrightint(strls) then
     begin
       ShowMessage('输入必须为数字');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

     if StrToInt(strls)>255 then
     begin
       ShowMessage('输入不能大于255');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

  end;

  if(Length(Edit13.Text) < 6) then
  begin
    ShowMessage('密码长度不足6位');
    Edit13.SetFocus;
    Exit;
  end;

  sendbuf[0] := $0f;//设定设备密码

  //机号低,机号高,命令字 密码
  strls:=Edit2.Text;
  sendbuf[1] := StrToInt(strls) mod 256;
  sendbuf[2] := (StrToInt(strls) div 256) mod 256;

  strls := Edit13.Text;

  sendbuf[3] := StrToInt('$'+midstr(strls,1,2));
  sendbuf[4] := StrToInt('$'+midstr(strls,3,2));
  sendbuf[5] := StrToInt('$'+midstr(strls,5,2));

  sendbuf[6] := 0;
  for i := 3 to 5 do
  begin
    sendbuf[6] := sendbuf[6] xor sendbuf[i];
  end;

  //固定的后缀 75+36+1
  sendbuf[7] := $55;
  sendbuf[8] := $aa;
  sendbuf[9] := $66;
  sendbuf[10] := $99;


  if not CheckBox1.Checked then
  begin
    strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
  end
  else
  begin
    strls := '255.255.255.255';
  end;

  IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,11);

end;
procedure TForm1.Button25Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
   if(Trim(Edit2.Text)  = '') then
   begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Exit;
   end;
   if not isrightint(Edit2.Text) then
   begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;



   if StrToInt(Edit2.Text)>65535 then
   begin
     ShowMessage('机号不能大于65535');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   for i := 3 to 6 do
   begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

   end;

    sendbuf[0] := $A7;//命令字:重新获取刷卡信息

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;

    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,3); //发送
end;

procedure TForm1.Button26Click(Sender: TObject);
//修改卡密码
var
  strls:string;
  i:Integer;
  sendbuf:array[0..280] of Byte;
begin

  //卡号提取
  strls := Trim(Edit1.Text);

  if (Length(strls) < 1) then
  begin
    cardnumberbuf[0] := 0;
    cardnumberbuf[1] := 0;
    cardnumberbuf[2] := 0;
    cardnumberbuf[3] := 0;

  end
  else
  begin
    for i := 0 to 3 do
    begin
      cardnumberbuf[3 - i] := StrToInt('$' + MidStr(strls,i*2+1,2));
    end;
  end;

  //新A密码

  if(Length(Edit15.Text) < 12) then
  begin
    ShowMessage('新A密码长度不足');
    Edit15.SetFocus;
    Exit;
  end;

  if(Length(Edit17.Text) < 12) then
  begin
    ShowMessage('新B密码长度不足');
    Edit17.SetFocus;
    Exit;
  end;

  //3a 机号L 机号H 区数量 卡号 区号 密码验证方式及修改项标志 旧密码 区第三块数据  55 AA 66 99
  sendbuf[0] := $3a;//修改卡密码

  //机号
  strls:=Edit2.Text;
  sendbuf[1] := StrToInt(strls) mod 256;
  sendbuf[2] := (StrToInt(strls) div 256) mod 256;

  //本次操作的区数
  sendbuf[3] := 1;//仅操作一个区

  //卡号 不为零表示操作指定卡号的卡
  sendbuf[4] := cardnumberbuf[0];
  sendbuf[5] := cardnumberbuf[1];
  sendbuf[6] := cardnumberbuf[2];
  sendbuf[7] := cardnumberbuf[3];

  //区参数----------------------------------------------------------------------------------
  //区号


  sendbuf[8] := ComboBox4.ItemIndex;


  //密码修改项标志,任何情况下都会更改A密码
  sendbuf[9] := 0;

  if ComboBox5.ItemIndex > 0 then
  begin
    sendbuf[9] := sendbuf[9] + 1;//为1以B密码来验证旧密码,为0以A密码来验证旧密码
  end;

  if CheckBox3.Checked then
  begin
    sendbuf[9] := sendbuf[9] + 2;//表示更改该区的访问位
  end;

  if CheckBox4.Checked then
  begin
    sendbuf[9] := sendbuf[9] + 4;//表示更改B密码,不加2表示不更改B密码
  end;

  //旧密码
  strls := Edit14.Text;

  for i := 0 to 5 do
  begin
    sendbuf[10 + i] := StrToInt('$' + MidStr(strls,i*2+1,2));
  end;

  //区第三块数据
  //新A密码
  strls := Edit15.Text;
  for i := 0 to 5 do
  begin
    sendbuf[16 + i] := StrToInt('$' + MidStr(strls,i*2+1,2));

  end;

  //访问位
  strls := Edit16.Text;
  for i := 0 to 3 do
  begin
    sendbuf[22 + i] := StrToInt('$' + MidStr(strls,i*2+1,2));

  end;

  //新B密码
  strls := Edit17.Text;
  for i := 0 to 5 do
  begin
    sendbuf[26 + i] := StrToInt('$' + MidStr(strls,i*2+1,2));

  end;

  //-----------------------------------------------------------------------------------

  //抗干扰
  sendbuf[32] := $55;
  sendbuf[33] := $AA;
  sendbuf[34] := $66;
  sendbuf[35] := $99;

  if not CheckBox1.Checked then
  begin
    strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
  end
  else
  begin
    strls := '255.255.255.255';
  end;

  IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,8+1*24+4);


end;


procedure TForm1.Button28Click(Sender: TObject);
//指定密码写卡
var
  strls:string;
  i:Integer;
  j:Integer;
  sendbuf:array[0..280] of Byte;
begin

  //卡号提取
  strls := Trim(Edit1.Text);

  if (Length(strls) < 1) then
  begin
    cardnumberbuf[0] := 0;
    cardnumberbuf[1] := 0;
    cardnumberbuf[2] := 0;
    cardnumberbuf[3] := 0;

  end
  else
  begin
    for i := 0 to 3 do
    begin
      cardnumberbuf[3 - i] := StrToInt('$' + MidStr(strls,i*2+1,2));
    end;
  end;

  //写卡数据准备
  strls := Memo1.Text;
  j := Length(strls);
  j := j div 2;
  for i := 0 to (j-1) do
  begin
    writecardbuf[i] := StrToInt('$' + MidStr(strls,i*2+1,2));
  end;

  //3b 机号L 机号H 区数量 卡号 区号 密码验证方式 旧密码
  sendbuf[0] := $3d;//指定密码写卡

  //机号
  strls:=Edit2.Text;
  sendbuf[1] := StrToInt(strls) mod 256;
  sendbuf[2] := (StrToInt(strls) div 256) mod 256;

  //本次操作的区数
  sendbuf[3] := 1;//仅操作一个区

  //卡号 不为零表示操作指定卡号的卡
  sendbuf[4] := cardnumberbuf[0];
  sendbuf[5] := cardnumberbuf[1];
  sendbuf[6] := cardnumberbuf[2];
  sendbuf[7] := cardnumberbuf[3];

  //区参数----------------------------------------------------------------------------------
  //区号


  sendbuf[8] := ComboBox4.ItemIndex;


  //密码修改项标志,任何情况下都会更改A密码
  sendbuf[9] := 0;

  if ComboBox5.ItemIndex > 0 then
  begin
    sendbuf[9] := sendbuf[9] + 1;//为1以B密码来验证旧密码,为0以A密码来验证旧密码
  end;


  //旧密码
  strls := Edit14.Text;

  for i := 0 to 5 do
  begin
    sendbuf[10 + i] := StrToInt('$' + MidStr(strls,i*2+1,2));
  end;

  //卡数据48个字节
  for i := 0 to 47 do
  begin
    sendbuf[16 + i] := writecardbuf[i];
  end;


  //-----------------------------------------------------------------------------------

  //抗干扰
  sendbuf[64] := $55;
  sendbuf[65] := $AA;
  sendbuf[66] := $66;
  sendbuf[67] := $99;


  if not CheckBox1.Checked then
  begin
    strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
  end
  else
  begin
    strls := '255.255.255.255';
  end;

  IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,8+1*56+4);

end;

procedure TForm1.Button27Click(Sender: TObject);
//指定密码读卡
var
  strls:string;
  i:Integer;
  sendbuf:array[0..280] of Byte;
begin

  //卡号提取
  strls := Trim(Edit1.Text);

  if (Length(strls) < 1) then
  begin
    cardnumberbuf[0] := 0;
    cardnumberbuf[1] := 0;
    cardnumberbuf[2] := 0;
    cardnumberbuf[3] := 0;

  end
  else
  begin
    for i := 0 to 3 do
    begin
      cardnumberbuf[3 - i] := StrToInt('$' + MidStr(strls,i*2+1,2));
    end;
  end;


  //3b 机号L 机号H 区数量 卡号 区号 密码验证方式 旧密码
  sendbuf[0] := $3b;//指定密码读卡

  //机号
  strls:=Edit2.Text;
  sendbuf[1] := StrToInt(strls) mod 256;
  sendbuf[2] := (StrToInt(strls) div 256) mod 256;

  //本次操作的区数
  sendbuf[3] := 1;//仅操作一个区

  //卡号 不为零表示操作指定卡号的卡
  sendbuf[4] := cardnumberbuf[0];
  sendbuf[5] := cardnumberbuf[1];
  sendbuf[6] := cardnumberbuf[2];
  sendbuf[7] := cardnumberbuf[3];

  //区参数----------------------------------------------------------------------------------
  //区号


  sendbuf[8] := ComboBox4.ItemIndex;


  //密码修改项标志,任何情况下都会更改A密码
  sendbuf[9] := 0;

  if ComboBox5.ItemIndex > 0 then
  begin
    sendbuf[9] := sendbuf[9] + 1;//为1以B密码来验证旧密码,为0以A密码来验证旧密码
  end;


  //旧密码
  strls := Edit14.Text;

  for i := 0 to 5 do
  begin
    sendbuf[10 + i] := StrToInt('$' + MidStr(strls,i*2+1,2));
  end;

  //-----------------------------------------------------------------------------------


  if not CheckBox1.Checked then
  begin
    strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
  end
  else
  begin
    strls := '255.255.255.255';
  end;

  IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,8+1*8);


end;

procedure TForm1.Button32Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;

  urf8str: UTF8String;
  urf8str1: UTF8String;

begin

   if Length(Trim(Edit18.Text)) < 1 then
   begin
     ShowMessage('wifi热点名称不能为空!');
     Edit18.Text:='';
     Edit18.SetFocus;
     Exit;

   end;

   if CheckBox5.Checked then
   begin
     if Length(Trim(Edit22.Text)) < 1 then
     begin
       ShowMessage('如果密码打了勾,密码输入框不能为空!');
       Edit22.Text:='';
       Edit22.SetFocus;
       Exit;

     end;
   end;
   if(Trim(Edit2.Text)  = '') then
   begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Exit;
   end;
   if not isrightint(Edit2.Text) then
   begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   if StrToInt(Edit2.Text)>65535 then
   begin
     ShowMessage('机号不能大于65535');
     Edit1.SelectAll;
     Edit1.SetFocus;
     Exit;

   end;

   for i := 3 to 6 do
   begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

   end;

   if RadioButton2.Checked then
   begin//指定WIFI的IP地址

     for i := 26 to 37 do
     begin
        pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
        strls := pedit.Text;
        if Trim(strls) = '' then
        begin
         ShowMessage('输入不能为空');
         pedit.Text:='';
         pedit.SetFocus;
         Exit;
        end;

         if not isrightint(strls) then
         begin
           ShowMessage('输入必须为数字');
           pedit.SelectAll;
           pedit.SetFocus;
           Exit;

         end;

         if StrToInt(strls)>255 then
         begin
           ShowMessage('输入不能大于255');
           pedit.SelectAll;
           pedit.SetFocus;
           Exit;

         end;

     end;





   end;

    sendbuf[0] := $F7;//命令字:设定WIFI连接的热点名称和密码

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    //wifi热点名称
    Edit18.Text := Trim(Edit18.Text);
    Edit22.Text := Trim(Edit22.Text);

    sendbuf[3] := Length(Edit18.Text);//热点名称字符的长度,最大60个字节
    if CheckBox5.Checked then
    begin
      sendbuf[4] := Length(Edit22.Text);//密码的长度,最大64个字节,必有密码时为0
    end
    else
    begin
      sendbuf[4] := 0;//密码的长度,最大64个字节,必有密码时为0
    end;


    strls := Edit18.Text;
    for i := 1 to sendbuf[3] do
    begin
      sendbuf[4+i] := Byte(strls[i]);

    end;

    for i := sendbuf[3] + 1 to 60 do
    begin
      sendbuf[4+i] := 0;

    end;
    //密码
    strls := Edit22.Text;
    for i := 1 to sendbuf[4] do
    begin
      sendbuf[64+i] := Byte(strls[i]);//37+34+1=72

    end;

     for i := sendbuf[4] + 1 to 64 do
    begin
      sendbuf[64+i] := 0;//37+34+1=72

    end;

    if RadioButton2.Checked then
    begin
      sendbuf[129] := 1;//0为DHCP自动分配IP地址,1=指令IP地址

      sendbuf[130] := StrToInt(Edit26.Text);
      sendbuf[131] := StrToInt(Edit27.Text);
      sendbuf[132] := StrToInt(Edit28.Text);
      sendbuf[133] := StrToInt(Edit29.Text);

      sendbuf[134] := StrToInt(Edit30.Text);
      sendbuf[135] := StrToInt(Edit31.Text);
      sendbuf[136] := StrToInt(Edit32.Text);
      sendbuf[137] := StrToInt(Edit33.Text);

      sendbuf[138] := StrToInt(Edit34.Text);
      sendbuf[139] := StrToInt(Edit35.Text);
      sendbuf[140] := StrToInt(Edit36.Text);
      sendbuf[141] := StrToInt(Edit37.Text);



    end
    else
    begin
      sendbuf[129] := 0;//0为DHCP自动分配IP地址,1=指令IP地址

      sendbuf[130] := 0;
      sendbuf[131] := 0;
      sendbuf[132] := 0;
      sendbuf[133] := 0;

      sendbuf[134] := 0;
      sendbuf[135] := 0;
      sendbuf[136] := 0;
      sendbuf[137] := 0;

      sendbuf[138] := 0;
      sendbuf[139] := 0;
      sendbuf[140] := 0;
      sendbuf[141] := 0;
    end;

    sendbuf[142] := ComboBox6.ItemIndex;

    //固定的后缀
    sendbuf[143] := $55;
    sendbuf[144] := $aa;
    sendbuf[145] := $66;
    sendbuf[146] := $99;

    if not CheckBox1.Checked then
    begin
      strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
    end
    else
    begin
      strls := '255.255.255.255';
    end;
    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,147);

end;

procedure TForm1.Button33Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin

   if CheckBox1.Checked then
   begin
      ShowMessage('广播式不能查询WIFI联接情况!');
     Exit;

   end;


   if(Trim(Edit2.Text)  = '') then
   begin
     ShowMessage('机号不能为空');
     Edit2.Text:='';
     Edit2.SetFocus;
     Exit;
   end;
   if not isrightint(Edit2.Text) then
   begin
     ShowMessage('机号必须为数字');
     Edit2.SelectAll;
     Edit2.SetFocus;
     Exit;

   end;

   if StrToInt(Edit2.Text)>65535 then
   begin
     ShowMessage('机号不能大于65535');
     Edit1.SelectAll;
     Edit1.SetFocus;
     Exit;

   end;

   for i := 3 to 6 do
   begin
      pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
      strls := pedit.Text;
      if Trim(strls) = '' then
      begin
       ShowMessage('输入不能为空');
       pedit.Text:='';
       pedit.SetFocus;
       Exit;
      end;

       if not isrightint(strls) then
       begin
         ShowMessage('输入必须为数字');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

       if StrToInt(strls)>255 then
       begin
         ShowMessage('输入不能大于255');
         pedit.SelectAll;
         pedit.SetFocus;
         Exit;

       end;

   end;


   Memo3.Text := '已发送搜索指令,正在等待返回信息......';

    sendbuf[0] := $A4;//命令字:查询WIFI联接情况

    //机号
    strls:=Edit2.Text;
    sendbuf[1] := StrToInt(strls) mod 256;
    sendbuf[2] := (StrToInt(strls) div 256) mod 256;

    strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;

    IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,3);

end;
procedure TForm1.FormCreate(Sender: TObject);
var
  aa:TList;
  i :Integer;

begin
  try
    begin
      aa :=GetAdapterInfo;
      for i := 0 to aa.Count - 1 do
      begin
               ComboBox7.Items.Add(TAdapterInfo(aa.Items[i]).IPAddress);
      end;
      ComboBox7.ItemIndex:= aa.Count - 1;
    end;
    except
     begin
        showmessage('本计算机网卡异常,读卡器专用UDP协议端口[39169]已被其他程序占用,无法与设备正常连接,请检查后重启软件!');
        Close ;
     end;
    end;
 end;


procedure TForm1.ComboBox7Change(Sender: TObject);
begin
  try
    begin
      IdUDPServer1.Active :=False ;
      IdUDPServer1.BroadcastEnabled:=False;
      IdUDPServer1.Bindings.Clear;
      IdUDPServer1.Bindings.Add;
      IdUDPServer1.Bindings[0].IP := ComboBox7.Text ; //本地IP
      IdUDPServer1.Bindings[0].Port := 39169; //本地IP
      IdUDPServer1.BroadcastEnabled:=true;
      IdUDPServer1.Active := true; //激活IdUDPServer 控件
     end;
   except

  end;
end;

procedure TForm1.Button31Click(Sender: TObject);
var
  strls:string;
  sendbuf:array[0..280] of Byte;
  i:Integer;
  pedit:TEdit;
begin
  if(Trim(Edit2.Text)  = '') then
  begin
   ShowMessage('机号不能为空');
   Edit2.Text:='';
   Edit2.SetFocus;
   Exit;
  end;
  if not isrightint(Edit2.Text) then
  begin
   ShowMessage('机号必须为数字');
   Edit2.SelectAll;
   Edit2.SetFocus;
   Exit;

  end;

  if StrToInt(Edit2.Text)>65535 then
  begin
   ShowMessage('机号不能大于65535');
   Edit1.SelectAll;
   Edit1.SetFocus;
   Exit;
  end;

  for i := 3 to 6 do
  begin
    pedit := TEdit(FindComponent('Edit' + IntToStr(i)));
    strls := pedit.Text;
    if Trim(strls) = '' then
    begin
     ShowMessage('输入不能为空');
     pedit.Text:='';
     pedit.SetFocus;
     Exit;
    end;

     if not isrightint(strls) then
     begin
       ShowMessage('输入必须为数字');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

     if StrToInt(strls)>255 then
     begin
       ShowMessage('输入不能大于255');
       pedit.SelectAll;
       pedit.SetFocus;
       Exit;

     end;

  end;

  if(Length(Edit13.Text) < 6) then
  begin
    ShowMessage('密码长度不足6位');
    Edit13.SetFocus;
    Exit;
  end;

  sendbuf[0] := $1d;//设定设备参数

  //机号低,机号高,命令字 密码
  strls:=Edit2.Text;
  sendbuf[1] := StrToInt(strls) mod 256;
  sendbuf[2] := (StrToInt(strls) div 256) mod 256;

  sendbuf[3] := 2;
  sendbuf[4] := 2;//表示设定心跳包延时

  if(CheckBox6.Checked) then
  begin//可以同时发出声响
    sendbuf[5] := StrToInt(Trim(Edit38.Text));
  end
  else
  begin
    sendbuf[5] := $ff;

  end;


  //固定的后缀 75+36+1
  sendbuf[6] := $55;
  sendbuf[7] := $aa;
  sendbuf[8] := $66;
  sendbuf[9] := $99;


  if not CheckBox1.Checked then
  begin
    strls := Edit3.Text + '.' + Edit4.Text + '.' + Edit5.Text + '.' + Edit6.Text;
  end
  else
  begin
    strls := '255.255.255.255';
  end;

  IdUDPServer1.Binding.SendTo(strls,myport,sendbuf,10);

end;

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

end.

 


http://www.kler.cn/a/457603.html

相关文章:

  • Flutter-插件 scroll-to-index 实现 listView 滚动到指定索引位置
  • 原子类及原理和ABA问题解决
  • 动态规划<八> 完全背包问题及其余背包问题
  • 如何使用Python自动化发送消息:用pynput库批量输入并发送文本
  • 重学 Android 自定义 View 系列(十):带指针的渐变环形进度条
  • 国产编辑器EverEdit - 常用资源汇总
  • Java编程题_面向对象和常用API01_B级
  • 电动汽车用永磁同步电机控制系统研究与设计
  • Streamlining QA with Automated Testing for 3D Models
  • Vue计算属性、侦听器
  • 基于AI IDE 打造快速化的游戏LUA脚本的生成系统
  • 自学记录鸿蒙API 13:实现多目标识别Object Detection
  • 学习测试day1
  • ClickHouse 日常操作记录
  • 跟着问题学18——大模型基础transformer详解(2)多头自注意力机制
  • Linux 使用入门指南
  • 如何在 Spring Boot 中配置数据库?
  • 【bluedroid】A2dp Source播放流程源码分析(3)
  • C#控件开发2—流动管道
  • 小猫咪抽奖系统源码1.11(有卡密功能)
  • 【潜意识Java】探寻Java子类构造器的神秘面纱与独特魅力,深度学习子类构造器特点
  • 4. 指针和动态内存
  • Pytorch | 利用PC-I-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击
  • 【13】Selenium+Python UI自动化测试 集成日志(某积载系统实例-07)
  • 【学习笔记】ChatGPT原理与应用开发——基础科普
  • No.29 笔记 | CTF 学习干货