本文主要是介绍VB6轻松读写Mifare ClassIc芯片NDEF标签源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
NDEF 全称 NFC data exchange format 即 nfc 数据交换格式,是一种标准化的数据格式,可用于在任何兼容的NFC设备与另一个NFC设备或标签之间交换信息。数据格式由NDEF消息和NDEF记录组成。
NDEF信息可以写到不同类型的NFC芯片中,如Ntag系列芯片标、15693系列芯片、MifareClassic系列芯片、Forum_Type4_Tag标签等,不同类型的芯片NDEF信息的存储方式也略有不同,这就大大增加了NDEF信息写入、读取的难度。
广州荣士电子将各种不同类型的NDEF记录类型的写入、读取方式都函数化,开发人员不需再了解复杂的NDEF记录格式,只需调用相应的函数就可快速写入正确的NDEF信息。
本示例使用的发卡器:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报/-淘宝网 (taobao.com)
一、NDEF函数声明
Public Declare Function piccclear_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef oldkey As Byte) As BytePublic Declare Function piccwrite_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef oldkey As Byte, ByRef newkey As Byte) As BytePublic Declare Function piccread_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef oldkey As Byte) As BytePublic Declare Sub tagbuf_clear Lib "OUR_MIFARE.dll" ()Public Declare Function tagbuf_addtext Lib "OUR_MIFARE.dll" (ByVal languagecodestr As String, ByVal languagecodestrlen As Long, ByVal textstr As String, ByVal textstrlen As Long) As BytePublic Declare Function tagbuf_adduri Lib "OUR_MIFARE.dll" (ByVal languagecodestr As String, ByVal languagecodestrlen As Long, ByVal titlestr As String, ByVal titlestrlen As Long, ByVal uriheaderindex As Long, ByVal uristr As String, ByVal uristrlen As Long) As BytePublic Declare Function tagbuf_addbusinesscard Lib "OUR_MIFARE.dll" (ByVal infostr As String, ByVal infostrlen As Long) As BytePublic Declare Function tagbuf_addwifi Lib "OUR_MIFARE.dll" (ByVal ssidstr As String, ByVal ssidstrlen As Long, ByVal authtype As Long, ByVal crypttype As Long, ByVal keystr As String, ByVal keystrlen As Long) As BytePublic Declare Function tagbuf_addbluetooth Lib "OUR_MIFARE.dll" (ByVal blenamestr As String, ByVal blenamestrlen As Long, ByRef blemac As Byte) As BytePublic Declare Function tagbuf_addapp Lib "OUR_MIFARE.dll" (ByVal packagestr As String, ByVal packagestrlen As Long) As BytePublic Declare Function tagbuf_adddata Lib "OUR_MIFARE.dll" (ByVal typestr As String, ByVal typestrlen As Long, ByVal datastr As String, ByVal datastrlen As Long) As BytePublic Declare Sub tagbuf_read Lib "OUR_MIFARE.dll" (ByVal revstr As String, ByRef revstrlen As Long, ByRef recordnumber As Long)Public Declare Sub tagbuf_forumtype4_clear Lib "OUR_MIFARE.dll" ()Public Declare Function forumtype4request Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef seriallen As Byte) As BytePublic Declare Function forumtype4_write_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef seriallen As Byte, ByRef ndefwritekey As Byte) As BytePublic Declare Function forumtype4_read_ndeftag Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByRef seriallen As Byte, ByRef ndefreadkey As Byte) As Byte
二、写入NDEF文本类型标签
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As ByteDim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码Dim dispstr As String
Dim status As Byte
Dim myctrlword As Byte
Dim languagecodestr As String
Dim languagecodestrlen As Long
Dim textstr As String
Dim textstrlen As Longlanguagecodestr = "en"
languagecodestrlen = 2textstr = Trim(Text1.Text) '文本
textstrlen = LenB(StrConv(textstr, vbFromUnicode))CheckCardTypeIf CardType = 3 Then 'MifareClasstagbuf_clear status = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen)If (status = 0) Thenmyctrlword = &H80 + &H10status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))dispstr = "MifareClassUid:" + cardstr + ",写入NDEF文本"dispriv dispstr, statusElsedispstr = "MifareClassUid:" + cardstr + ",生成NDEF文本数据"dispriv dispstr, statusEnd If
ElseIf CardType = 4 Then 'forumtype4tagbuf_forumtype4_clear status = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen)If (status = 0) Thenmyctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF文本"dispriv dispstr, statusElsedispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF文本数据"dispriv dispstr, statusEnd If
End If
三、写入NDEF智能海报
Dim dispstr As String
Dim status As Byte
Dim myctrlword As Byte
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As ByteDim languagecodestr As String
Dim languagecodestrlen As Long
Dim titlestr As String
Dim titlestrlen As Long
Dim uriheaderindex As Long
Dim uristr As String
Dim uristrlen As Longlanguagecodestr = "en" '语言编码,英文为en,中文为zh
languagecodestrlen = 2titlestr = Trim(Text4.Text) '标题
titlestrlen = LenB(StrConv(titlestr, vbFromUnicode))uriheaderindex = Combo2.ListIndex '链接前缀uristr = Trim(Text18.Text) '链接
uristrlen = LenB(StrConv(uristr, vbFromUnicode))CheckCardTypeIf CardType = 3 Thentagbuf_clear status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen)If (status = 0) Thenmyctrlword = &H80 + &H10status = piccwrite_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "MifareClassUid:" + cardstr + ",写入NDEF访问网址"dispriv dispstr, statusElsedispstr = "MifareClassUid:" + cardstr + ",生成NDEF访问网址数据"dispriv dispstr, statusEnd If
ElseIf CardType = 4 Thentagbuf_forumtype4_clear status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen)If (status = 0) Thenmyctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF访问网址"dispriv dispstr, statusElsedispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF访问网址数据"dispriv dispstr, statusEnd If
End If
四、写入NDEF地图座标
Dim dispstr As String
Dim status As Byte
Dim myctrlword As ByteDim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As ByteDim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码Dim languagecodestr As String
Dim languagecodestrlen As Long
Dim titlestr As String
Dim titlestrlen As Long
Dim uriheaderindex As Long
Dim uristr As String
Dim uristrlen As Longlanguagecodestr = "en" '语言编码,英文为en,中文为zh
languagecodestrlen = 2titlestr = Trim(Text6.Text) '标题
titlestrlen = LenB(StrConv(titlestr, vbFromUnicode))uriheaderindex = 0 '地理位置没有链接前缀uristr = "geo:" & Trim(Text2.Text) & "," & Trim(Text3.Text) '地址位置
uristrlen = LenB(StrConv(uristr, vbFromUnicode))CheckCardTypeIf CardType = 3 Thentagbuf_clear status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen)If (status = 0) Thenmyctrlword = &H80 + &H10status = piccwrite_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "MifareClassUid:" + cardstr + ",写入NDEF地图坐标"dispriv dispstr, statusElsedispstr = "MifareClassUid:" + cardstr + ",生成NDEF地图坐标数据"dispriv dispstr, statusEnd If
ElseIf CardType = 4 Thentagbuf_forumtype4_clearstatus = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen)If (status = 0) Thenmyctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF地图坐标"dispriv dispstr, statusElsedispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF地图坐标数据"dispriv dispstr, statusEnd If
End If
五、写入NDEF控制标签蓝牙连接
Dim dispstr As String
Dim status As Byte
Dim myctrlword As ByteDim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As ByteDim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码Dim blenamestr As String
Dim blenamestrlen As Long
Dim blemac(0 To 5) As Byte '蓝牙MAC地址blenamestr = Trim(Text8.Text)
blenamestrlen = LenB(StrConv(blenamestr, vbFromUnicode))macstr = Split(Text9, ":")
blemac(0) = "&H" & macstr(0)
blemac(1) = "&H" & macstr(1)
blemac(2) = "&H" & macstr(2)
blemac(3) = "&H" & macstr(3)
blemac(4) = "&H" & macstr(4)
blemac(5) = "&H" & macstr(5)CheckCardTypeIf CardType = 3 Thentagbuf_clear status = tagbuf_addbluetooth(blenamestr, blenamestrlen, blemac(0))If (status = 0) Thenmyctrlword = &H80 + &H10status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))dispstr = "MifareClassUid:" + cardstr + ",写入NDEF蓝牙连接"dispriv dispstr, statusElsedispstr = "MifareClassUid:" + cardstr + ",生成NDEF蓝牙连接数据"dispriv dispstr, statusEnd If
ElseIf CardType = 4 Thentagbuf_forumtype4_clear status = tagbuf_addbluetooth(blenamestr, blenamestrlen, blemac(0))If (status = 0) Thenmyctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF蓝牙连接"dispriv dispstr, statusElsedispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF蓝牙连接数据"dispriv dispstr, statusEnd If
End If
六、写入NDEF控制标签连接WIFI热点
Dim dispstr As String
Dim status As Byte
Dim myctrlword As ByteDim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As ByteDim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码Dim ssidstr As String
Dim ssidstrlen As LongDim authtype As Long
Dim crypttype As LongDim keystr As String
Dim keystrlen As Longssidstr = Trim(Text12.Text) 'WIFI名称
ssidstrlen = LenB(StrConv(ssidstr, vbFromUnicode))authtype = Combo3.ListIndex '加密方式crypttype = Combo4.ListIndex '加密算法keystr = Trim(Text11.Text) '密码
keystrlen = LenB(StrConv(keystr, vbFromUnicode))CheckCardTypeIf CardType = 3 Thentagbuf_clear status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen)If (status = 0) Thenmyctrlword = &H80 + &H10status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))dispstr = "MifareClassUid:" + cardstr + ",写入NDEF无线热点连接"dispriv dispstr, statusElsedispstr = "MifareClassUid:" + cardstr + ",生成NDEF无线热点连接数据"dispriv dispstr, statusEnd If
ElseIf CardType = 4 Thentagbuf_forumtype4_clear status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen)If (status = 0) Thenmyctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF无线热点连接"dispriv dispstr, statusElsedispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF无线热点连接数据"dispriv dispstr, statusEnd If
End If
七、写入NDEF控制标签电子名片
Dim dispstr As String
Dim status As Byte
Dim myctrlword As ByteDim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As ByteDim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码Dim infostr As String
Dim infostrlen As Long'名片信息
infostr = "BEGIN:VCARD" & Chr(10)
infostr = infostr & "VERSION:3.0" & Chr(10)
infostr = infostr & "FN:" & Trim(Text14.Text) & Chr(10) '姓名
infostr = infostr & "TEL:" & Trim(Text13.Text) & Chr(10) '电话
infostr = infostr & "ORG:" & Trim(Text15.Text) & Chr(10) '单位名称
infostr = infostr & "ADR:" & Trim(Text19.Text) & Chr(10) '地址
infostr = infostr & "EMAIL:" & Trim(Text16.Text) & Chr(10) '邮箱
infostr = infostr & "URL:" & Trim(Text17.Text) & Chr(10) '网址
infostr = infostr & "END:VCARD"infostrlen = LenB(StrConv(infostr, vbFromUnicode))CheckCardTypeIf CardType = 3 Thentagbuf_clear status = tagbuf_addbusinesscard(infostr, infostrlen) '可以写入多条记录If (status = 0) Thenmyctrlword = &H80 + &H10status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))dispstr = "MifareClassUid:" + cardstr + ",写入NDEF电子名片"dispriv dispstr, statusElsedispstr = "MifareClassUid:" + cardstr + ",生成NDEF电子名片数据"dispriv dispstr, statusEnd If
ElseIf CardType = 4 Thentagbuf_forumtype4_clear status = tagbuf_addbusinesscard(infostr, infostrlen) '可以写入多条记录If (status = 0) Thenmyctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF电子名片"dispriv dispstr, statusElsedispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF电子名片数据"dispriv dispstr, statusEnd If
End If
八、写入NDEF控制标签呼叫电话
Dim dispstr As String
Dim status As Byte
Dim myctrlword As ByteDim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As ByteDim oldpicckey(0 To 5) As Byte '需要认证的密码
Dim newpicckey(0 To 5) As Byte '需要认证的密码Dim languagecodestr As String
Dim languagecodestrlen As Long
Dim uristr As String
Dim uristrlen As Longlanguagecodestr = "en" '语言编码,英文为en,中文为zh
languagecodestrlen = 2uristr = Trim(Text10.Text) '呼叫电话
uristrlen = LenB(StrConv(uristr, vbFromUnicode))CheckCardTypeIf CardType = 3 Thentagbuf_clear status = tagbuf_adduri(languagecodestr, languagecodestrlen, "", 0, 5, uristr, uristrlen)If (status = 0) Thenmyctrlword = &H80 + &H10status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))dispstr = "MifareClassUid:" + cardstr + ",写入NDEF呼叫电话"dispriv dispstr, statusElsedispstr = "MifareClassUid:" + cardstr + ",生成NDEF呼叫电话数据"dispriv dispstr, statusEnd IfElseIf CardType = 4 Thentagbuf_forumtype4_clear status = tagbuf_adduri(languagecodestr, languagecodestrlen, "", 0, 5, uristr, uristrlen)If (status = 0) Thenmyctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",写入NDEF呼叫电话"dispriv dispstr, statusElsedispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",生成NDEF呼叫电话数据"dispriv dispstr, statusEnd If
End If
九、读取NDEF标签信息
Dim dispstr As String
Dim status As Byte
Dim mypiccserial(0 To 6) As Byte
Dim mypicckey(0 To 15) As Byte
Dim mypiccseriallen(1) As Byte
Dim myctrlword As Byte
Dim oldpicckey(0 To 5) As Byte '需要认证的密码Dim ndefstr As String
Dim revstrlen(1) As Long
Dim recordnumber(1) As LongCheckCardTypeIf CardType = 3 Thenmyctrlword = &H80 + &H10status = piccread_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))If (status = 0) Thenndefstr = String(2048, 0)tagbuf_read ndefstr, revstrlen(0), recordnumber(0)dispstr = "MifareClassUid:" + cardstr + ",读取卡内NDEF信息"dispriv dispstr, statusText22 = ndefstrElsedispstr = "MifareClassUid:" + cardstr + ",读取卡内NDEF信息"dispriv dispstr, statusEnd If
ElseIf CardType = 4 Thenmyctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_read_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))If (status = 0) Thenndefstr = String(2048, 0)tagbuf_read ndefstr, revstrlen(0), recordnumber(0)dispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",读取卡内NDEF信息"dispriv dispstr, statusText22 = ndefstrElsedispstr = "NFC_Forum_Type4_Tag卡:" + cardstr + ",读取卡内NDEF信息"dispriv dispstr, statusEnd If
End If
这篇关于VB6轻松读写Mifare ClassIc芯片NDEF标签源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!