本示例使用的读写器:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报
声明函数:
set proc to funprogram
declare integer pcdbeep in OUR_MIFARE.dll integer xms &&让设备发出声响
declare integer pcdgetdevicenumber in OUR_MIFARE.dll string @pdevicenumber &&读取设备编号
declare integer piccrequest_ul in OUR_MIFARE.dll string @mypiccserial &&寻卡选卡激活卡
declare integer piccread_ul in OUR_MIFARE.dll integer blockaddr,string @mypiccdata &&读块
declare integer piccwrite_ul in OUR_MIFARE.dll integer blockaddr,string @mypiccdata &&写块
declare integer piccauthkey_ntag in OUR_MIFARE.dll string @picckey,string @piccntagpack &&NTAG21x密码认证
declare integer piccgetversion_ntag in OUR_MIFARE.dll string @piccversiondata &&读取卡版本信息
declare integer piccreadsig_ntag in OUR_MIFARE.dll string @piccversiondata &&读取卡的签字信息
declare integer piccreadcnt_ntag in OUR_MIFARE.dll string @picccntdata &&读取卡的单向操作计数器(操作流水号)
declare integer picclock_ntag in OUR_MIFARE.dll integer locktype,string @serial &&锁定页数据
declare integer piccinit_ntag in OUR_MIFARE.dll integer ctrlword,string @serial,string @picckey,string @piccdata &&初始化卡
declare integer piccreadex_ntag in OUR_MIFARE.dll integer ctrlword,string @serial,string @picckey,integer blockaddr,integer blocksize1,string @piccdata &&轻松读卡
declare integer piccwriteex_ntag in OUR_MIFARE.dll integer ctrlword,string @serial,string @picckey,integer blockaddr,integer blocksize1,string @piccdata &&轻松写卡
thisform.pagF1.page1.text3.Value="00000000"
thisform.pagF1.page1.text6.Value="12345678"
thisform.pagF1.page2.combo1.AddItem ("开启密码保护功能")
thisform.pagF1.page2.combo1.AddItem ("取消密码保护功能")
thisform.pagF1.page2.combo1.ListIndex =1
thisform.pagF1.page2.text1.Value="12345678"
thisform.pagF1.page2.text2.Value=30
thisform.pagF1.page2.text3.Value=0
thisform.pagF1.page2.text5.Value="12345678"
thisform.pagF1.page2.text6.Value="12345678"
thisform.pagF1.page2.text7.Value=30
thisform.pagF1.page2.text8.Value=10
初始化Ntag卡,设置卡片是否需要认证密码读、写
mypiccserial=spac(7) &&返回7字节卡序列号
mypicckey=SPACE(4) &&卡认证密码
mypiccdata=SPACE(16) &&读卡的数据缓冲,Ultralight卡及NTAG21x卡的数组长度必须为16个字节,写卡的为4个字节
IF thisform.pagF1.page2.check1.Value >0
IF LEN(ALLTRIM(thisform.pagF1.page2.text6.Value))<>8
= MESSAGEBOX(' 卡密码必须为8个字符!',16,'提示:')
thisform.pagF1.page2.text6.SetFocus
RETURN
ENDIF
Pas1str=ALLTRIM(thisform.pagF1.page2.text6.Value)
mypicckey=""
FOR i=1 TO 4
mypicckey=mypicckey+CHR(HexToDec(SUBSTR(Pas1str,i*2-1,2)+"00"))
NEXT
myctrlword=16 &&需要密码
ELSE
myctrlword=0
ENDIF
IF thisform.pagF1.page2.combo1.ListIndex =1
IF LEN(ALLTRIM(thisform.pagF1.page2.text1.Value))<>8
= MESSAGEBOX(' 卡密码必须为8个字符!',16,'提示:')
thisform.pagF1.page2.text1.SetFocus
RETURN
ENDIF
mypiccdata=CHR(0) &&MIRROR
mypiccdata=mypiccdata+CHR(0) &&RFUI
mypiccdata=mypiccdata+CHR(0) &&MIRROR_PAGE
mypiccdata=mypiccdata+CHR(thisform.pagF1.page2.text2.Value) &&AUTH0
myctrlword=myctrlword+1
ACCESS=MOD(thisform.pagF1.page2.text3.Value, 8)
IF thisform.pagF1.page2.check2.Value >0
ACCESS=ACCESS+128
ENDIF
mypiccdata=mypiccdata+CHR(ACCESS) &&ACCESS
mypiccdata=mypiccdata+CHR(0)+CHR(0)+CHR(0) &&启用计数器
myctrlword=myctrlword+2
Pas1str=ALLTRIM(thisform.pagF1.page2.text1.Value)
FOR i=1 TO 4
mypiccdata=mypiccdata+CHR(HexToDec(SUBSTR(Pas1str,i*2-1,2)+"00"))
NEXT
mypiccdata=mypiccdata+CHR(22) &&PACK
mypiccdata=mypiccdata+CHR(22) &&PACK
mypiccdata=mypiccdata+CHR(0) &&RFUI
mypiccdata=mypiccdata+CHR(0) &&RFUI
myctrlword=myctrlword+4
ELSE
mypiccdata=CHR(0) &&MIRROR
mypiccdata=mypiccdata+CHR(0) &&RFUI
mypiccdata=mypiccdata+CHR(0) &&MIRROR_PAGE
mypiccdata=mypiccdata+CHR(255) &&AUTH0
myctrlword=myctrlword+1
FOR i=5 TO 16
mypiccdata=mypiccdata+CHR(0)
NEXT
myctrlword=myctrlword+2
ENDIF
&&-------------------------------------------------------------- 显示16进制的串口调试指令
comedc=22
dispstr="1D16"
dispstr=dispstr+LEFT(DecToHex(myctrlword),2)
comedc=BITXOR(comedc,myctrlword)
FOR i=1 TO 7
dispstr=dispstr+"00"
comedc=BITXOR(comedc,0)
NEXT
FOR i=1 TO 4
dispstr=dispstr+LEFT(DecToHex(ASC(SUBSTR(mypicckey,i,1))),2)
comedc=BITXOR(comedc,ASC(SUBSTR(mypicckey,i,1)))
NEXT
FOR i=1 TO 16
dispstr=dispstr+LEFT(DecToHex(ASC(SUBSTR(mypiccdata,i,1))),2)
comedc=BITXOR(comedc,ASC(SUBSTR(mypiccdata,i,1)))
NEXT
dispstr=dispstr+LEFT(DecToHex(comedc),2)
thisform.pagF1.page2.edit2.Value =dispstr
&&--------------------------------------------------------------------
QuestRec= piccinit_ntag(myctrlword,@mypiccserial,@mypicckey,@mypiccdata)
DO case
CASE QuestRec=0
=pcdbeep(50)
CardIdH=""
FOR i=1 TO 7
CardIdH=CardIdH+LEFT(DecToHex(ASC(SUBSTR(mypiccserial,i,1))),2)
NEXT
thisform.pagF1.page2.text4.Value =CardIdH
= MESSAGEBOX('卡号:'+CardIdH +",初始化卡成功!",64,'提示:')
CASE QuestRec=8
= MESSAGEBOX(' 请将卡放在感应区! ',16,'提示:')
CASE QuestRec=12
= MESSAGEBOX(' 密码错误! ',16,'提示:')
CASE QuestRec=14
= MESSAGEBOX(' 设置失败,可能需要验证密码! ',16,'提示:')
CASE QuestRec=23
= MESSAGEBOX(' 请连上USB 读写器! ',16,'提示:')
OTHERWISE
= MESSAGEBOX(' 操作失败,错误代码:'+STR(QuestRec),16,'提示:')
ENDCASE
轻松读卡操作:
mypiccserial=spac(7) &&返回7字节卡序列号
mypicckey=SPACE(4) &&卡认证密码
mypiccdata=SPACE(48) &&读卡的数据缓冲
IF thisform.pagF1.page2.check3.Value >0
IF LEN(ALLTRIM(thisform.pagF1.page2.text5.Value))<>8
= MESSAGEBOX(' 卡密码必须为8个字符!',16,'提示:')
thisform.pagF1.page2.text5.SetFocus
RETURN
ENDIF
Pas1str=ALLTRIM(thisform.pagF1.page2.text5.Value)
mypicckey=""
FOR i=1 TO 4
mypicckey=mypicckey+CHR(HexToDec(SUBSTR(Pas1str,i*2-1,2)+"00"))
NEXT
myctrlword=16 &&需要密码
ELSE
mypicckey=CHR(0)+CHR(0)+CHR(0)+CHR(0)
myctrlword=0
ENDIF
myblockaddr=thisform.pagF1.page2.text7.Value
myblocksize=thisform.pagF1.page2.text8.Value
IF myblocksize=0 OR myblocksize>12
= MESSAGEBOX('读卡页数据要大于0小于12!',16,'提示:')
thisform.pagF1.page2.text8.SetFocus
RETURN
ENDIF
&&-------------------------------------------------------------- 显示16进制的串口调试指令
comedc=27
dispstr="0F1B"
dispstr=dispstr+LEFT(DecToHex(myctrlword),2)
comedc=BITXOR(comedc,myctrlword)
FOR i=1 TO 7
dispstr=dispstr+"00"
comedc=BITXOR(comedc,0)
NEXT
FOR i=1 TO 4
dispstr=dispstr+LEFT(DecToHex(ASC(SUBSTR(mypicckey,i,1))),2)
comedc=BITXOR(comedc,ASC(SUBSTR(mypicckey,i,1)))
NEXT
dispstr=dispstr+LEFT(DecToHex(myblockaddr),2)
comedc=BITXOR(comedc,myblockaddr)
dispstr=dispstr+LEFT(DecToHex(myblocksize),2)
comedc=BITXOR(comedc,myblocksize)
dispstr=dispstr+LEFT(DecToHex(comedc),2)
thisform.pagF1.page2.edit2.Value =dispstr
&&--------------------------------------------------------------------
QuestRec= piccreadex_ntag(myctrlword,@mypiccserial,@mypicckey,myblockaddr,myblocksize,@mypiccdata)
DO case
CASE QuestRec=0
=pcdbeep(50)
CardIdH=""
FOR i=1 TO 7
CardIdH=CardIdH+LEFT(DecToHex(ASC(SUBSTR(mypiccserial,i,1))),2)
NEXT
thisform.pagF1.page2.text4.Value =CardIdH
CardDa=""
FOR i=1 TO myblocksize*4
CardDa=CardDa+LEFT(DecToHex(ASC(SUBSTR(mypiccdata,i,1))),2)
NEXT
thisform.pagF1.page2.edit1.Value =CardDa
CASE QuestRec=8
= MESSAGEBOX(' 请将卡放在感应区! ',16,'提示:')
CASE QuestRec=23
= MESSAGEBOX(' 请连上USB 读写器! ',16,'提示:')
OTHERWISE
= MESSAGEBOX(' 操作失败,错误代码:'+STR(QuestRec),16,'提示:')
ENDCASE
轻松写卡操作
mypiccserial=spac(7) &&返回7字节卡序列号
mypicckey=SPACE(4) &&卡认证密码
mypiccdata=SPACE(48) &&读卡的数据缓冲
IF thisform.pagF1.page2.check3.Value >0
IF LEN(ALLTRIM(thisform.pagF1.page2.text5.Value))<>8
= MESSAGEBOX(' 卡密码必须为8个字符!',16,'提示:')
thisform.pagF1.page2.text5.SetFocus
RETURN
ENDIF
Pas1str=ALLTRIM(thisform.pagF1.page2.text5.Value)
mypicckey=""
FOR i=1 TO 4
mypicckey=mypicckey+CHR(HexToDec(SUBSTR(Pas1str,i*2-1,2)+"00"))
NEXT
myctrlword=16 &&需要密码
ELSE
mypicckey=CHR(0)+CHR(0)+CHR(0)+CHR(0)
myctrlword=0
ENDIF
myblockaddr=thisform.pagF1.page2.text7.Value
myblocksize=thisform.pagF1.page2.text8.Value
IF myblocksize=0 OR myblocksize>11
= MESSAGEBOX('写卡页数据要大于0小于12!',16,'提示:')
thisform.pagF1.page2.text8.SetFocus
RETURN
ENDIF
IF LEN(ALLTRIM(thisform.pagF1.page2.edit1.Value))<myblocksize*4*2
answ= MESSAGEBOX(' 写卡数据不足,是否要自动补0?',0+32+0,'提示:')
IF answ<>1 THEN
RETURN
ELSE
thisform.pagF1.page2.edit1.Value=LEFT(ALLTRIM(thisform.pagF1.page2.edit1.Value)+"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",myblocksize*4*2)
ENDIF
ENDIF
writstr=ALLTRIM(thisform.pagF1.page2.edit1.Value)
mypiccdata=""
FOR i=1 TO myblocksize*4
mypiccdata=mypiccdata+CHR(HexToDec(SUBSTR(writstr,i*2-1,2)+"00"))
NEXT
&&-------------------------------------------------------------- 显示16进制的串口调试指令
comedc=28
dispstr="3B1C"
dispstr=dispstr+LEFT(DecToHex(myctrlword),2)
comedc=BITXOR(comedc,myctrlword)
FOR i=1 TO 7
dispstr=dispstr+"00"
comedc=BITXOR(comedc,0)
NEXT
FOR i=1 TO 4
dispstr=dispstr+LEFT(DecToHex(ASC(SUBSTR(mypicckey,i,1))),2)
comedc=BITXOR(comedc,ASC(SUBSTR(mypicckey,i,1)))
NEXT
dispstr=dispstr+LEFT(DecToHex(myblockaddr),2)
comedc=BITXOR(comedc,myblockaddr)
dispstr=dispstr+LEFT(DecToHex(myblocksize),2)
comedc=BITXOR(comedc,myblocksize)
FOR i=1 TO myblocksize*4
dispstr=dispstr+LEFT(DecToHex(ASC(SUBSTR(mypiccdata,i,1))),2)
comedc=BITXOR(comedc,ASC(SUBSTR(mypiccdata,i,1)))
NEXT
FOR i=myblocksize*4+1 TO 44
dispstr=dispstr+"00"
comedc=BITXOR(comedc,0)
NEXT
dispstr=dispstr+LEFT(DecToHex(comedc),2)
thisform.pagF1.page2.edit2.Value =dispstr
&&--------------------------------------------------------------------
QuestRec= piccwriteex_ntag(myctrlword,@mypiccserial,@mypicckey,myblockaddr,myblocksize,@mypiccdata)
DO case
CASE QuestRec=0
=pcdbeep(50)
CardIdH=""
FOR i=1 TO 7
CardIdH=CardIdH+LEFT(DecToHex(ASC(SUBSTR(mypiccserial,i,1))),2)
NEXT
thisform.pagF1.page2.text4.Value =CardIdH
CASE QuestRec=8
= MESSAGEBOX(' 请将卡放在感应区! ',16,'提示:')
CASE QuestRec=23
= MESSAGEBOX(' 请连上USB 读写器! ',16,'提示:')
OTHERWISE
= MESSAGEBOX(' 操作失败,错误代码:'+STR(QuestRec),16,'提示:')
ENDCASE