本示例使用的设备:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报/-淘宝网 (taobao.com)
函数声明
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';//读7字节卡号function piccrequest_ul(piccdata:pbyte):byte; stdcall;external 'OUR_MIFARE.dll';//读身份证UIDfunction sfzrequest(piccdata:pbyte):byte; stdcall;external 'OUR_MIFARE.dll';//读iCLass CSNfunction iso15693iclassreadcsn(piccdata:pbyte):byte; stdcall;external 'OUR_MIFARE.dll';
const//以下控制字的含义请查看本公司网站提供的动态库说明BLOCK0_EN = $01; //读块0BLOCK1_EN = $02; //读块1BLOCK2_EN = $04; //读块2NEEDSERIAL = $08; //是否需要只对指定系列号的卡操作EXTERNKEY = $10; //是否使用外部密码NEEDHALT = $20; //是否休眠本卡implementationend.
轻松读卡
procedure TForm1.Button9Click(Sender: TObject);
vari:integer;status:byte;//存放返回值myareano:byte;//区号authmode:byte;//密码类型,用A密码或B密码myctrlword:byte;//控制字mypicckey:array[0..5] of byte;//密码mypiccserial:array[0..3] of byte;//卡序列号mypiccdata:array[0..47] of byte;//卡数据缓冲str:string;
begin//控制字指定,控制字的含义请查看本公司网站提供的动态库说明myctrlword := BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY;//指定区号myareano := 8;//指定为第8区//批定密码模式authmode := 1;//大于0表示用A密码认证,推荐用A密码认证//指定密码mypicckey[0] := $ff;mypicckey[1] := $ff;mypicckey[2] := $ff;mypicckey[3] := $ff;mypicckey[4] := $ff;mypicckey[5] := $ff;status := piccreadex(myctrlword,@mypiccserial,myareano,authmode,@mypicckey,@mypiccdata);case status of0:beginstr:='';for i:=0 to 47 dobeginstr:=str+IntToHex(mypiccdata[i],2);end;memo1.Text :=str;ShowMessage('读卡操作成功,卡内信息已显示在右栏。');end;8: ShowMessage('请将卡放在感应区');12:ShowMessage('卡密码认证失败!');elsebeginShowMessage(IntToStr(status));end;end;//返回解释{#define ERR_REQUEST 8//寻卡错误#define ERR_READSERIAL 9//读序列吗错误#define ERR_SELECTCARD 10//选卡错误#define ERR_LOADKEY 11//装载密码错误#define ERR_AUTHKEY 12//密码认证错误#define ERR_READ 13//读卡错误#define ERR_WRITE 14//写卡错误#define ERR_NONEDLL 21//没有动态库#define ERR_DRIVERORDLL 22//动态库或驱动程序异常#define ERR_DRIVERNULL 23//驱动程序错误或尚未安装#define ERR_TIMEOUT 24//操作超时,一般是动态库没有反映#define ERR_TXSIZE 25//发送字数不够#define ERR_TXCRC 26//发送的CRC错#define ERR_RXSIZE 27//接收的字数不够#define ERR_RXCRC 28//接收的CRC错}
end;
轻松写卡
procedure TForm1.Button8Click(Sender: TObject);
vari:integer;status:byte;//存放返回值myareano:byte;//区号authmode:byte;//密码类型,用A密码或B密码myctrlword:byte;//控制字mypicckey:array[0..5] of byte;//密码mypiccserial:array[0..3] of byte;//卡序列号mypiccdata:array[0..47] of byte;//卡数据缓冲strls:string;
begin//控制字指定,控制字的含义请查看本公司网站提供的动态库说明myctrlword := BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY;//指定区号myareano := 8;//指定为第8区//批定密码模式authmode := 1;//大于0表示用A密码认证,推荐用A密码认证//指定密码mypicckey[0] := $ff;mypicckey[1] := $ff;mypicckey[2] := $ff;mypicckey[3] := $ff;mypicckey[4] := $ff;mypicckey[5] := $ff;strls := StringReplace(memo1.Lines.Text, #13#10, '', [rfReplaceAll]) + '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';for i := 0 to 47 dobeginmypiccdata[i] := strtoint('$'+midstr(strls,i*2+1,2))end;status := piccwriteex(myctrlword,@mypiccserial,myareano,authmode,@mypicckey,@mypiccdata);case status of0:beginpcdbeep(38);ShowMessage('将右栏16进制数据写入卡内成功!');end;8: ShowMessage('请将卡放在感应区');12:ShowMessage('卡密码认证失败!');elsebeginShowMessage('操作失败,错误代码:'+IntToStr(status));end;end;}
end;
修改卡片密钥
procedure TForm1.Button7Click(Sender: TObject);
varstatus:byte;//存放返回值myareano:byte;//区号authmode:byte;//密码类型,用A密码或B密码myctrlword:byte;//控制字mypiccoldkey:array[0..5] of byte;//新密码mypiccserial:array[0..5] of byte;//卡序列号mypiccdata:array[0..16] of byte;//旧密码
begin//控制字指定,控制字的含义请查看本公司网站提供的动态库说明myctrlword := EXTERNKEY;myareano := 1;authmode := 1;//指定旧密码mypiccoldkey[0] := $FF;mypiccoldkey[1] := $FF;mypiccoldkey[2] := $FF;mypiccoldkey[3] := $FF;mypiccoldkey[4] := $FF;mypiccoldkey[5] := $FF;//'指定新A密码mypiccdata[0] := $FF;mypiccdata[1] := $FF;mypiccdata[2] := $FF;mypiccdata[3] := $FF;mypiccdata[4] := $FF;mypiccdata[5] := $FF;//访问控制位,请慎重,改错可能导致卡做废************************************************************************************mypiccdata[6] := $FF;mypiccdata[7] := $07;mypiccdata[8] := $80;mypiccdata[9] := $69;//'指定新B密码mypiccdata[10] := $FF;mypiccdata[11] := $FF;mypiccdata[12] := $FF;mypiccdata[13] := $FF;mypiccdata[14] := $FF;mypiccdata[15] := $FF;mypiccdata[16] := 3; //为1表示更改访问控制位,为2表示更改B密码,为3表示同时更改访问控制位及B密码,A密码在任何情况下都更改!status := piccchangesinglekeyex(myctrlword,@mypiccserial,myareano,authmode,@mypiccoldkey,@mypiccdata);If status = 0 thenbeginpcdbeep(100);ShowMessage('卡片密钥修改成功!');endelse if status = 8 thenbeginShowMessage('请将卡放在感应区');endelsebeginShowMessage('错误代码:' + IntToStr(status));end;
end;