本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.11.7154789eKODONP&id=615391857885
https://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块UIDfunction piccwriteserial(ctrlword:byte;pserial:pbyte;keyA1B0:byte;picckey:pbyte;piccdata:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';const//以下控制字的含义请查看本公司网站提供的动态库说明BLOCK0_EN = $01; //读块0BLOCK1_EN = $02; //读块1BLOCK2_EN = $04; //读块2NEEDSERIAL = $08; //是否需要只对指定系列号的卡操作EXTERNKEY = $10; //是否使用外部密码NEEDHALT = $20; //是否休眠本卡implementationprocedure TForm1.Button11Click(Sender: TObject);
//用UID卡复制M1卡,写入物理卡号到UID卡的0扇区0块vari: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;beginfor i:=0 to 16 dobeginif i<4 then mypiccserial[i]:=0;if i<6 then mypicckey[i]:=0;mypiccdata[i]:=0;end;if RichEdit2.Lines[0]>'4294967295' thenbeginApplication.MessageBox('卡号最大取值:4294967295', '提示', MB_OK+MB_ICONSTOP) ;exit;end;x:= StrToInt64(RichEdit2.Lines[0]) ;if x>4294967295 thenbeginShowmessage('卡号最大取值:4294967295') ;exit;end;edc:=0;if (checkbox4.State = cbchecked) thenbeginfor i:=3 downto 0 dobeginmypiccdata[i]:= x and 255;edc:=edc xor mypiccdata[i];x:=x shr 8;end;endelsebeginfor i:=0 to 3 dobeginmypiccdata[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 of0:beginpcdbeep(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_ICONSTOPif checkbox1.State = cbchecked thenbeginx:=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);elsebeginApplication.MessageBox(PAnsiChar(AnsiString('操作失败,错误代码:'+IntToStr(status))), '提示', MB_OK+MB_ICONSTOP);end;end;
end;












