Delphi 写UID卡0区0块

本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.11.7154789eKODONP&id=615391857885icon-default.png?t=N7T8https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.11.7154789eKODONP&id=615391857885

unit declaredll;

interface
     //读卡函数声明
     function piccreadex(ctrlword:byte;pserial:pbyte;area:byte;keyA1B0:byte;picckey:pbyte;piccdata0_2:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';

     //写卡函数声明
     function piccwriteex(ctrlword:byte;pserial:pbyte;area:byte;keyA1B0:byte;picckey:pbyte;piccdata0_2:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';

     //驱动蜂鸣器函数声明
     function pcdbeep(xms:integer):byte;stdcall;external 'OUR_MIFARE.dll';

     //修改单区密码函数声明
     function piccchangesinglekey(ctrlword:byte;pserial:pbyte;area:byte;keyA1B0:byte;piccoldkey:pbyte;piccnewkey:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';

     //修改单区密码改单区AB密码或访问控制位函数声明
     function piccchangesinglekeyex(ctrlword:byte;serial:pbyte;area:byte;keyA1B0:byte;piccoldkey:pbyte;piccdata:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';

     //读出设备编号函数声明
     function pcdgetdevicenumber(pdevicenumber:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';

     //只读取卡号函数
     function piccrequest(pserial:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';

     //发送显示
      function lcddispfull(lcdstr:PChar):byte;stdcall;external 'OUR_MIFARE.dll';

     //改写0区0块UID
      function piccwriteserial(ctrlword:byte;pserial:pbyte;keyA1B0:byte;picckey:pbyte;piccdata:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';



const
   //以下控制字的含义请查看本公司网站提供的动态库说明
    BLOCK0_EN = $01;  //读块0
    BLOCK1_EN = $02;  //读块1
    BLOCK2_EN = $04;   //读块2
    NEEDSERIAL = $08;  //是否需要只对指定系列号的卡操作
    EXTERNKEY = $10;   //是否使用外部密码
    NEEDHALT = $20;   //是否休眠本卡

implementation



procedure TForm1.Button11Click(Sender: TObject);
//用UID卡复制M1卡,写入物理卡号到UID卡的0扇区0块

var
    i:integer;
    x:LongWord ;
    status:byte;//存放返回值
    edc:byte;//校验码
    authmode:byte;//密码类型,用A密码或B密码
    myctrlword:byte;//控制字
	  mypicckey:array[0..5] of byte;//密码
    mypiccserial:array[0..3] of byte;//卡序列号
    mypiccdata:array[0..15] of byte;//卡数据缓冲
    strs:string;

begin
    for i:=0 to 16 do
    begin
        if i<4 then  mypiccserial[i]:=0;
        if i<6 then  mypicckey[i]:=0;
        mypiccdata[i]:=0;
    end;

    if RichEdit2.Lines[0]>'4294967295' then
    begin
       Application.MessageBox('卡号最大取值:4294967295', '提示', MB_OK+MB_ICONSTOP) ;
       exit;
    end;

    x:= StrToInt64(RichEdit2.Lines[0]) ;
    if x>4294967295 then
    begin
       Showmessage('卡号最大取值:4294967295') ;
       exit;
    end;

    edc:=0;
    if (checkbox4.State = cbchecked) then
    begin
        for i:=3 downto 0 do
        begin
           mypiccdata[i]:= x and 255;
           edc:=edc xor mypiccdata[i];
           x:=x shr 8;
        end;
    end
    else
    begin
        for i:=0 to 3 do
        begin
           mypiccdata[i]:= x and 255;
           edc:=edc xor mypiccdata[i];
           x:=x shr 8;
        end;
    end;
    mypiccdata[4]:= edc;

    myctrlword:=BLOCK0_EN;
    authmode:=0;
    status:=piccwriteserial(myctrlword,@mypiccserial,authmode,@mypicckey,@mypiccdata);
    case status of
          0:
          begin
              pcdbeep(38);
              strs:= 'UID卡号:'+RightStr('0000000000'+RichEdit2.Lines[0],10)+' 写卡成功!' ;
              ListBox1.Items.Add(strs);
              listbox1.ItemIndex:=listbox1.Count-1;
              Application.MessageBox(PAnsiChar(AnsiString(strs)) , '提示', MB_OK+MB_ICONINFORMATION);       MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
              if checkbox1.State = cbchecked then
              begin
                  x:=StrToInt64(RichEdit2.Lines[0])+1;
                  if (checkbox2.State = cbchecked)  and (RightStr(inttostr(x),1)='4') then x:=x+1;
                  if (checkbox3.State = cbchecked)  and (RightStr(inttostr(x),1)='7') then x:=x+1;
                  RichEdit2.Text :=rightstr('0000000000'+inttostr(x),10);
              end;
          end;
          8: Application.MessageBox('请将卡放在感应区', '提示', MB_OK+MB_ICONSTOP);
          12:Application.MessageBox('卡密码认证失败!', '提示', MB_OK+MB_ICONSTOP);
        else
          begin
              Application.MessageBox(PAnsiChar(AnsiString('操作失败,错误代码:'+IntToStr(status))), '提示', MB_OK+MB_ICONSTOP);
          end;
   end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

vx_13822155058

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

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

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

打赏作者

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

抵扣说明:

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

余额充值