本示例使用的发卡器:https://item.taobao.com/item.htm?ft=t&id=615391857885
Public Class Form1Dim oldpicckey(0 To 5) As Byte '卡片旧密码Dim newpicckey(0 To 5) As Byte '卡片新密码Function GetTagUID() As StringDim status As ByteDim myctrlword As ByteDim mypiccserial(0 To 7) As ByteDim mypickey(0 To 5) As ByteDim mypicdata(0 To 47) As ByteDim mypiccseriallen(0 To 1) As ByteGetTagUID = ""myctrlword = 0status = piccreadex_ntag(myctrlword, mypiccserial(0), mypickey(0), 4, 1, mypicdata(0))If status = 0 Then 'Ntag21x卡 For i = 0 To 6GetTagUID = GetTagUID + mypiccserial(i).ToString("X02")NextElsestatus = iso15693readex(myctrlword, 0, 0, 1, mypiccserial(0), mypicdata(0))If status = 0 Then '15693卡For i = 0 To 7GetTagUID = GetTagUID + mypiccserial(i).ToString("X02")NextElsemyctrlword = 23mypickey(0) = "&HFF" : mypickey(1) = "&HFF" : mypickey(2) = "&HFF" : mypickey(3) = "&HFF" : mypickey(4) = "&HFF" : mypickey(5) = "&HFF"status = piccreadex(myctrlword, mypiccserial(0), 0, 1, mypickey(0), mypicdata(0))If status = 0 Then 'MifareClass卡For i = 0 To 3GetTagUID = GetTagUID + mypiccserial(i).ToString("X02")NextElsemyctrlword = 23mypickey(0) = "&HA0" : mypickey(1) = "&HA1" : mypickey(2) = "&HA2" : mypickey(3) = "&HA3" : mypickey(4) = "&HA4" : mypickey(5) = "&HA5"status = piccreadex(myctrlword, mypiccserial(0), 0, 1, mypickey(0), mypicdata(0))If status = 0 ThenFor i = 0 To 3GetTagUID = GetTagUID + mypiccserial(i).ToString("X02")NextElsemyctrlword = 0If (forumtype4request(myctrlword, mypiccserial(0), mypiccseriallen(0)) = 0) Or (forumtype4request(myctrlword, mypiccserial(0), mypiccseriallen(0)) = 52) ThenFor i = 0 To mypiccseriallen(0) - 1 'forumtype4卡GetTagUID = GetTagUID + mypiccserial(i).ToString("X02")NextEnd IfEnd IfEnd IfEnd IfEnd IfEnd FunctionFunction checkcardtype() As IntegerDim status As ByteDim myctrlword As ByteDim mypiccserial(0 To 7) As ByteDim mypickey(0 To 5) As ByteDim mypicdata(0 To 47) As ByteDim mypiccseriallen(0 To 1) As Bytecheckcardtype = 0myctrlword = 0status = piccreadex_ntag(myctrlword, mypiccserial(0), mypickey(0), 4, 1, mypicdata(0))If status = 0 Thencheckcardtype = 1 'Ntag21x卡Elsestatus = iso15693readex(myctrlword, 0, 0, 1, mypiccserial(0), mypicdata(0))If status = 0 Thencheckcardtype = 2 '15693卡Elsemyctrlword = 23mypickey(0) = "&HFF" : mypickey(1) = "&HFF" : mypickey(2) = "&HFF" : mypickey(3) = "&HFF" : mypickey(4) = "&HFF" : mypickey(5) = "&HFF"status = piccreadex(myctrlword, mypiccserial(0), 0, 1, mypickey(0), mypicdata(0))If status = 0 Thencheckcardtype = 3 'MifareClass卡Elsemyctrlword = 23mypickey(0) = "&HA0" : mypickey(1) = "&HA1" : mypickey(2) = "&HA2" : mypickey(3) = "&HA3" : mypickey(4) = "&HA4" : mypickey(5) = "&HA5"status = piccreadex(myctrlword, mypiccserial(0), 0, 1, mypickey(0), mypicdata(0))If status = 0 Thencheckcardtype = 3Elsemyctrlword = 0If (forumtype4request(myctrlword, mypiccserial(0), mypiccseriallen(0)) = 0) Or (forumtype4request(myctrlword, mypiccserial(0), mypiccseriallen(0)) = 52) Thencheckcardtype = 4 'forumtype4卡Elsecheckcardtype = 0End IfEnd IfEnd IfEnd IfEnd IfEnd FunctionPrivate Sub NtagPageLock() '锁定ntag2数据页,锁定后标签不可以再次修改,请谨慎使用锁定功能Dim status As ByteDim mypicclockdata(0 To 3) As Bytemypicclockdata(0) = "&H00" : mypicclockdata(1) = "&H00" : mypicclockdata(2) = "&HFF" : mypicclockdata(3) = "&HFF"status = picclock_ntag(0, mypicclockdata(0)) '静态锁mypicclockdata(0) = "&HFF" : mypicclockdata(1) = "&HFF" : mypicclockdata(2) = "&HFF" : mypicclockdata(3) = "&H00"status = picclock_ntag(1, mypicclockdata(0)) '动态锁End SubPrivate Sub NtagKeyEn(ByRef mypiccserial As Byte, ByVal havekey As Boolean, ByVal addkey As Boolean) '开启或关ntag2x卡密码保护功能Dim myctrlword As ByteDim status As ByteDim mypiccdata(0 To 15) As ByteIf havekey Then '卡片已经有密码保护myctrlword = "&H10"Elsemyctrlword = 0End IfIf addkey Then '开启卡密码保护功能mypiccdata(0) = 0 : mypiccdata(1) = 0 : mypiccdata(2) = 0 : mypiccdata(3) = 4 '密码保护起始页myctrlword = myctrlword + 1mypiccdata(4) = 0 / 8 '认证次数mypiccdata(5) = 0 : mypiccdata(6) = 0 : mypiccdata(7) = 0 '启用计数器myctrlword = myctrlword + 2For i = 0 To 3mypiccdata(8 + i) = newpicckey(i)Nextmypiccdata(12) = "&H16" : mypiccdata(13) = "&H16" : mypiccdata(14) = 0 : mypiccdata(15) = 0myctrlword = myctrlword + 4Elsemypiccdata(0) = 0 : mypiccdata(1) = 0 : mypiccdata(2) = 0 : mypiccdata(3) = "&HFF"myctrlword = myctrlword + 1mypiccdata(4) = 0 : mypiccdata(5) = 0 : mypiccdata(6) = 0 : mypiccdata(7) = 0myctrlword = myctrlword + 2End Ifstatus = piccinit_ntag(myctrlword, mypiccserial, oldpicckey(0), mypiccdata(0))End SubPrivate Sub button4_Click(sender As Object, e As EventArgs) Handles button4.ClickDim xms As Integerxms = 50Dim status As Byte = pcdbeep(xms)If status <> 0 Thendisperrinf(status)End IfEnd SubPrivate Sub button8_Click(sender As Object, e As EventArgs) Handles button8.ClickDim status As Byte '存放返回值Dim devno(3) As Byte '设备编号status = pcdgetdevicenumber(devno(0))If status = 0 ThenMsgBox("设备编号:" + devno(0).ToString("D3") + "-" + devno(1).ToString("D3") + "-" + devno(2).ToString("D3") + "-" + devno(3).ToString("D3"), vbInformation + vbOKOnly, "提示")Elsedisperrinf(status)End IfEnd SubPrivate Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Loadoldpicckey(0) = "&H19" : oldpicckey(1) = "&H74" : oldpicckey(2) = "&H02" : oldpicckey(3) = "&H02" : oldpicckey(4) = "&H01" : oldpicckey(5) = "&H11"newpicckey(0) = "&H19" : newpicckey(1) = "&H74" : newpicckey(2) = "&H02" : newpicckey(3) = "&H02" : newpicckey(4) = "&H01" : newpicckey(5) = "&H11" '为防止测试中忘记以设定的密码,标签统一用此组密码加密,客户可自行设置其他的标签保护密码comboBox1.SelectedIndex = 2comboBox2.SelectedIndex = 7comboBox3.SelectedIndex = 4comboBox4.SelectedIndex = 3End SubPrivate Sub WriteDataBufToTag(ByVal NDEFinfo As String, ByVal havelock As Boolean, ByVal keyEn As Boolean)Dim status As ByteDim afi As ByteDim i As IntegerDim myctrlword As Byte '控制字Dim mypiccserial(0 To 7) As Byte '卡序列号Dim mypiccseriallen(0 To 1) As ByteDim carduid As StringDim cardtype As Byte = checkcardtype()Select Case cardtypeCase 1 'Ntag2x标签If havelock Then myctrlword = "&H10" Else myctrlword = 0status = forumtype2_write_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))If status = 0 ThenIf CheckBox5.Checked = False Then NtagKeyEn(mypiccserial(0), havelock, keyEn) '开启或关闭Ntag2x标签密码保护功能'If (keyEn) Then NtagPageLock() '锁定Ntag2标签数据块,锁定后不可再改修改,请谨慎使用pcdbeep(38)carduid = "Ntag2UID:"For i = 0 To 6carduid = carduid + mypiccserial(i).ToString("X02")NextMessageBox.Show(carduid + "," + NDEFinfo + "写入成功!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfCase 2 '15693标签myctrlword = 0afi = 0status = forumtype5_write_ndeftag(myctrlword, afi, mypiccserial(0))If status = 0 Then'If (keyEn) Then iso15693lockblock(34, 1, mypiccserial(0)) '15693卡锁定块数据后只能读取不可再修改,为防止卡片锁死,仅在确定需要才开启此段代码。pcdbeep(38)carduid = "15693UID:"For i = 0 To 7carduid = carduid + mypiccserial(i).ToString("X02")NextMessageBox.Show(carduid + "," + NDEFinfo + "写入成功!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfCase 3 'MifareClass标签If havelock Then myctrlword = 208 Else myctrlword = 144If keyEn Then myctrlword = myctrlword + 4status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))If status = 0 Thenpcdbeep(38)carduid = "MifareClassUID:"For i = 0 To 3carduid = carduid + mypiccserial(i).ToString("X02")NextMessageBox.Show(carduid + "," + NDEFinfo + "写入成功!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfCase 4 'ForumType4标签myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), newpicckey(0))If status = 0 Thenpcdbeep(38)carduid = "ForumType4UID:"For i = 0 To mypiccseriallen(0) - 1carduid = carduid + mypiccserial(i).ToString("X02")NextMessageBox.Show(carduid + "," + NDEFinfo + "写入成功!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfCase ElseMessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)End SelectEnd SubPrivate Sub button1_Click(sender As Object, e As EventArgs) Handles button1.ClickDim status As ByteDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 Dim languagecodestr As String = "en" '语言编码,英文为en,中文为zhDim languagecodestrlen As Integer = languagecodestr.LengthDim textstr As String = textBox1.Text.TrimDim textstrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(textstr).Lengthstatus = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF纯文本标签数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseWriteDataBufToTag("NDEF纯文本标签", havelock, keyEn)End IfEnd SubPrivate Sub button3_Click(sender As Object, e As EventArgs) Handles button3.ClickDim status As ByteDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息Dim taguidstr As String = ""tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 If CheckBox5.Checked Thentaguidstr = "?uid=" & GetTagUID()End IfDim languagecodestr As String = "en" '语言编码,英文为en,中文为zhDim languagecodestrlen As Integer = languagecodestr.LengthDim titlestr As String = textBox4.Text.Trim '标题Dim titlestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length '标题长度Dim uriheaderindex As Integer = comboBox1.SelectedIndex '前缀Dim uristr As String = textBox5.Text.Trim & taguidstr 'uriDim uristrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length 'uri长度Dim ntag21xmirrorbuf(0 To 6) As Byte 'Ntag21x UID或计数器镜像功能配置值缓冲Dim i As Integerntag21xmirrorbuf(0) = 0If CheckBox3.Checked Then '启用UID镜像ntag21xmirrorbuf(0) = ntag21xmirrorbuf(0) + "&H40"End IfIf CheckBox4.Checked Thenntag21xmirrorbuf(0) = ntag21xmirrorbuf(0) + "&H80"End IfFor i = 1 To 6ntag21xmirrorbuf(i) = Asc("0")NextIf ntag21xmirrorbuf(0) > 0 Thenstatus = tagbuf_adduri1(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen, ntag21xmirrorbuf(0)) '可以用此方法写入多条记录到数据缓冲Elsestatus = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) '可以用此方法写入多条记录到数据缓冲End IfIf status <> 0 ThenMessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseWriteDataBufToTag("NDEF智能海报标签", havelock, keyEn)End IfEnd SubPrivate Sub button5_Click(sender As Object, e As EventArgs) Handles button5.ClickDim status As ByteDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 Dim languagecodestr As String = "en" '语言编码,英文为en,中文为zhDim languagecodestrlen As Integer = languagecodestr.LengthDim titlestr As String = textBox7.Text.Trim '标题Dim titlestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length '标题长度Dim uriheaderindex As Integer = 0 '地理位置没有链接前缀Dim uristr As String = "geo:" + textBox6.Text.Trim + "," + textBox8.Text.Trim 'uriDim uristrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length 'uri长度status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF地图坐标数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseWriteDataBufToTag("NDEF地图坐标标签", havelock, keyEn)End IfEnd SubPrivate Sub button6_Click(sender As Object, e As EventArgs) Handles button6.ClickDim status As ByteDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 Dim languagecodestr As String = "en" '语言编码,英文为en,中文为zhDim languagecodestrlen As Integer = languagecodestr.LengthDim titlestr As String = "" '标题Dim titlestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length '标题长度Dim uriheaderindex As Integer = 5 '呼叫电话的链接前缀为5Dim uristr As String = textBox9.Text.Trim 'uri呼叫电话Dim uristrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length 'uri长度status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF呼叫电话数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseWriteDataBufToTag("NDEF呼叫电话标签", havelock, keyEn)End IfEnd SubPrivate Sub button7_Click(sender As Object, e As EventArgs) Handles button7.ClickDim status As ByteDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 Dim infostr As String = "BEGIN:VCARD" & Chr(10) 'infostr = infostr + "VERSION:3.0" & Chr(10)infostr = infostr + "FN:" + textBox12.Text.Trim() & Chr(10) '姓名infostr = infostr + "TEL:" + textBox11.Text.Trim() & Chr(10) '电话infostr = infostr + "ORG:" + textBox10.Text.Trim() & Chr(10) '单位名称infostr = infostr + "ADR:" + textBox15.Text.Trim() & Chr(10) '地址infostr = infostr + "EMAIL:" + textBox13.Text.Trim() & Chr(10) '邮箱infostr = infostr + "URL:" + textBox14.Text.Trim() & Chr(10) '官网infostr = infostr + "END:VCARD" & Chr(10)Dim infostrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(infostr).Length '名片长度status = tagbuf_addbusinesscard(infostr, infostrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF电子名片数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseWriteDataBufToTag("NDEF电子名片标签", havelock, keyEn)End IfEnd SubPrivate Sub button9_Click(sender As Object, e As EventArgs) Handles button9.ClickDim status As ByteDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 Dim ssidstr As String = textBox16.Text.Trim '热点名称 Dim ssidstrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(ssidstr).Length '热点名称长度Dim authtype As Integer = comboBox2.SelectedIndex '认证方式Dim crypttype As Integer = comboBox3.SelectedIndex '加密算法Dim keystr As String = textBox17.Text.Trim '密码Dim keystrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(keystr).Length '密码长度status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF无线连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseWriteDataBufToTag("NDEF无线连接标签", havelock, keyEn)End IfEnd SubPrivate Sub button10_Click(sender As Object, e As EventArgs) Handles button10.ClickDim status As ByteDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 Dim blenamestr As String = textBox19.Text.Trim '设备名称 Dim blenamestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(blenamestr).Length '设备名称长度Dim macstr() As String = Split(textBox18.Text.Trim, ":")Dim macbuf(0 To 5) As ByteTrymacbuf(0) = "&H" & macstr(0)macbuf(1) = "&H" & macstr(1)macbuf(2) = "&H" & macstr(2)macbuf(3) = "&H" & macstr(3)macbuf(4) = "&H" & macstr(4)macbuf(5) = "&H" & macstr(5)Catch ex As ExceptionMessageBox.Show("蓝牙设备的MAC地址输入错误,请输入正确的MAC地址!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)textBox18.Select()End Trystatus = tagbuf_addbluetooth(blenamestr, blenamestrlen, macbuf(0)) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF蓝牙连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseWriteDataBufToTag("NDEF蓝牙连接标签", havelock, keyEn)End IfEnd SubPrivate Sub comboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBox4.SelectedIndexChangedDim appstr() As String = Split(comboBox4.Text.Trim, ":")textBox20.Text = appstr(1)End SubPrivate Sub button11_Click(sender As Object, e As EventArgs) Handles button11.ClickDim status As ByteDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 Dim packagestr As String = textBox20.Text.Trim 'APP名称 Dim packagestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(packagestr).Length 'APP名称长度status = tagbuf_addapp(packagestr, packagestrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF启动应用数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseWriteDataBufToTag("NDEF启动应用标签", havelock, keyEn)End IfEnd SubPrivate Sub button2_Click(sender As Object, e As EventArgs) Handles button2.ClickDim status As ByteDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 Dim typestr As String = textBox2.Text.Trim '数据类型 Dim typestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(typestr).Length '数据类型长度Dim datastr As String = textBox3.Text.Trim '数据Dim datastrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(datastr).Length '数据长度status = tagbuf_adddata(typestr, typestrlen, datastr, datastrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF数据标签缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseWriteDataBufToTag("NDEF数据标签", havelock, keyEn)End IfEnd SubPrivate Sub button12_Click(sender As Object, e As EventArgs) Handles button12.ClickDim status As ByteDim afi As ByteDim myctrlword As ByteDim mypiccserial(0 To 7) As ByteDim mypiccseriallen(0 To 1) As ByteDim revstrlen(0 To 1) As ByteDim recordnumber(0 To 1) As ByteDim mypiccdata(0 To 2047) As ByteDim carduid As String = ""Dim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护textBox21.Text = ""status = 255Dim cardtype As Byte = checkcardtype()Select Case cardtypeCase 1 'Ntag2x标签If havelock Then myctrlword = "&H10" Else myctrlword = 0status = forumtype2_read_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))carduid = "Ntag2UID:"For i = 0 To 6carduid = carduid + mypiccserial(i).ToString("X02")NextCase 2 '15693标签myctrlword = 0afi = 0status = forumtype5_read_ndeftag(myctrlword, afi, mypiccserial(0))carduid = "15693UID:"For i = 0 To 7carduid = carduid + mypiccserial(i).ToString("X02")NextCase 3 'MifareClass标签myctrlword = 144status = piccread_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))carduid = "MifareClassUID:"For i = 0 To 3carduid = carduid + mypiccserial(i).ToString("X02")NextCase 4 'ForumType4标签myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_read_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), oldpicckey(0))carduid = "ForumType4UID:"For i = 0 To mypiccseriallen(0) - 1carduid = carduid + mypiccserial(i).ToString("X02")NextCase ElseMessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)End SelectIf status = 0 Thenpcdbeep(38)tagbuf_read(mypiccdata(0), revstrlen(0), recordnumber(0))Dim ndefstr As String = System.Text.Encoding.Default.GetString(mypiccdata)textBox21.Text = carduid & Chr(13) & Chr(10) & ndefstrEnd IfEnd SubPrivate Sub button13_Click(sender As Object, e As EventArgs) Handles button13.ClickDim status As ByteDim afi As ByteDim i As IntegerDim myctrlword As Byte '控制字Dim mypiccserial(0 To 7) As Byte '卡序列号Dim mypiccseriallen(0 To 1) As ByteDim carduid As StringDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = False '清空标签,强制清空密钥checkBox2.Checked = False '清空标签,强制清空密钥tagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲 Dim cardtype As Byte = checkcardtype()Select Case cardtypeCase 1 'Ntag2x标签If havelock Then myctrlword = "&H10" Else myctrlword = 0status = forumtype2_write_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))If status = 0 ThenNtagKeyEn(mypiccserial(0), havelock, keyEn) '开启或关闭Ntag2x标签密码保护功能pcdbeep(38)carduid = "Ntag2UID:"For i = 0 To 6carduid = carduid + mypiccserial(i).ToString("X02")NextMessageBox.Show(carduid + ",NDEF标签信息已清空!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfCase 2 '15693标签myctrlword = 0afi = 0status = forumtype5_write_ndeftag(myctrlword, afi, mypiccserial(0))If status = 0 Thenpcdbeep(38)carduid = "15693UID:"For i = 0 To 7carduid = carduid + mypiccserial(i).ToString("X02")NextMessageBox.Show(carduid + ",NDEF标签信息已清空!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfCase 3 'MifareClass标签If havelock Then myctrlword = 210 Else myctrlword = 146status = piccclear_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))If status = 0 Thenpcdbeep(38)carduid = "MifareClassUID:"For i = 0 To 3carduid = carduid + mypiccserial(i).ToString("X02")NextMessageBox.Show(carduid + ",NDEF标签信息已清空!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfCase 4 'ForumType4标签myctrlword = 0 '0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), oldpicckey(0))If status = 0 Thenpcdbeep(38)carduid = "ForumType4UID:"For i = 0 To mypiccseriallen(0) - 1carduid = carduid + mypiccserial(i).ToString("X02")NextMessageBox.Show(carduid + ",NDEF标签信息已清空!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfCase ElseMessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)End SelectEnd SubPrivate Sub Button23_Click(sender As Object, e As EventArgs) Handles Button23.Clicktagbuf_forumtype4_clear() '清空现有标签数据缓冲tagbuf_clear() '清空现有标签数据缓冲MessageBox.Show("NDEF数据缓冲已经清除!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End SubPrivate Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.ClickDim havelock As Boolean = checkBox1.Checked '卡片是否已加密保护Dim keyEn As Boolean = checkBox2.Checked '是否启用密码保护写入的NDEF信息WriteDataBufToTag("NDEF组合标签", havelock, keyEn)End SubPrivate Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.ClickDim status As ByteDim languagecodestr As String = "en" '语言编码,英文为en,中文为zhDim languagecodestrlen As Integer = languagecodestr.LengthDim textstr As String = textBox1.Text.TrimDim textstrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(textstr).Lengthstatus = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF纯文本标签数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseMessageBox.Show(" 生成NDEF纯文本标签数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfEnd SubPrivate Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.ClickDim status As ByteDim languagecodestr As String = "en" '语言编码,英文为en,中文为zhDim languagecodestrlen As Integer = languagecodestr.LengthDim titlestr As String = textBox4.Text.Trim '标题Dim titlestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length '标题长度Dim uriheaderindex As Integer = comboBox1.SelectedIndex '前缀Dim uristr As String = textBox5.Text.Trim 'uriDim uristrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length 'uri长度status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseMessageBox.Show(" 生成NDEF智能海报数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfEnd SubPrivate Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.ClickDim status As ByteDim languagecodestr As String = "en" '语言编码,英文为en,中文为zhDim languagecodestrlen As Integer = languagecodestr.LengthDim titlestr As String = textBox7.Text.Trim '标题Dim titlestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length '标题长度Dim uriheaderindex As Integer = 0 '地理位置没有链接前缀Dim uristr As String = "geo:" + textBox6.Text.Trim + "," + textBox8.Text.Trim 'uriDim uristrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length 'uri长度status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF地图坐标数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseMessageBox.Show(" 生成NDEF地图坐标数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfEnd SubPrivate Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.ClickDim status As ByteDim languagecodestr As String = "en" '语言编码,英文为en,中文为zhDim languagecodestrlen As Integer = languagecodestr.LengthDim titlestr As String = "" '标题Dim titlestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length '标题长度Dim uriheaderindex As Integer = 5 '呼叫电话的链接前缀为5Dim uristr As String = textBox9.Text.Trim 'uri呼叫电话Dim uristrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length 'uri长度status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF呼叫电话数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseMessageBox.Show(" 生成NDEF呼叫电话数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfEnd SubPrivate Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.ClickDim status As ByteDim infostr As String = "BEGIN:VCARD" & Chr(10) 'infostr = infostr + "VERSION:3.0" & Chr(10)infostr = infostr + "FN:" + textBox12.Text.Trim() & Chr(10) '姓名infostr = infostr + "TEL:" + textBox11.Text.Trim() & Chr(10) '电话infostr = infostr + "ORG:" + textBox10.Text.Trim() & Chr(10) '单位名称infostr = infostr + "ADR:" + textBox15.Text.Trim() & Chr(10) '地址infostr = infostr + "EMAIL:" + textBox13.Text.Trim() & Chr(10) '邮箱infostr = infostr + "URL:" + textBox14.Text.Trim() & Chr(10) '官网infostr = infostr + "END:VCARD" & Chr(10)Dim infostrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(infostr).Length '名片长度status = tagbuf_addbusinesscard(infostr, infostrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF电子名片数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseMessageBox.Show(" 生成NDEF电子名片数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfEnd SubPrivate Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.ClickDim status As ByteDim ssidstr As String = textBox16.Text.Trim '热点名称 Dim ssidstrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(ssidstr).Length '热点名称长度Dim authtype As Integer = comboBox2.SelectedIndex '认证方式Dim crypttype As Integer = comboBox3.SelectedIndex '加密算法Dim keystr As String = textBox17.Text.Trim '密码Dim keystrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(keystr).Length '密码长度status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成WIFI无线连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseMessageBox.Show(" 生成WIFI无线连接数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfEnd SubPrivate Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.ClickDim status As ByteDim blenamestr As String = textBox19.Text.Trim '设备名称 Dim blenamestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(blenamestr).Length '设备名称长度Dim macstr() As String = Split(textBox18.Text.Trim, ":")Dim macbuf(0 To 5) As ByteTrymacbuf(0) = "&H" & macstr(0)macbuf(1) = "&H" & macstr(1)macbuf(2) = "&H" & macstr(2)macbuf(3) = "&H" & macstr(3)macbuf(4) = "&H" & macstr(4)macbuf(5) = "&H" & macstr(5)Catch ex As ExceptionMessageBox.Show("蓝牙设备的MAC地址输入错误,请输入正确的MAC地址!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)textBox18.Select()End Trystatus = tagbuf_addbluetooth(blenamestr, blenamestrlen, macbuf(0)) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF蓝牙连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseMessageBox.Show(" 生成NDEF蓝牙连接数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfEnd SubPrivate Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.ClickDim status As ByteDim packagestr As String = textBox20.Text.Trim 'APP名称 Dim packagestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(packagestr).Length 'APP名称长度status = tagbuf_addapp(packagestr, packagestrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF启动应用数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseMessageBox.Show(" 生成NDEF启动应用数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfEnd SubPrivate Sub Button22_Click(sender As Object, e As EventArgs) Handles Button22.ClickDim status As ByteDim typestr As String = textBox2.Text.Trim '数据类型 Dim typestrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(typestr).Length '数据类型长度Dim datastr As String = textBox3.Text.Trim '数据Dim datastrlen As Integer = System.Text.Encoding.GetEncoding(936).GetBytes(datastr).Length '数据长度status = tagbuf_adddata(typestr, typestrlen, datastr, datastrlen) '可以用此方法写入多条记录到数据缓冲If status <> 0 ThenMessageBox.Show("生成NDEF数据标签缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseMessageBox.Show(" 生成NDEF数据标签缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)End IfEnd SubPrivate Sub disperrinf(ByVal errcode As Byte)Select Case errcodeCase 1MessageBox.Show("错误代码:" + errcode.ToString() + ",0~2块都没读出来,可能刷卡太块。但卡序列号已被读出来!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 2MessageBox.Show("错误代码:" + errcode.ToString() + ",第0块已被读出,但1~2块读取失败。卡序列号已被读出来!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 3MessageBox.Show("错误代码:" + errcode.ToString() + ",第0、1块已被读出,但2块读取失败。卡序列号已被读出来!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 8MessageBox.Show("错误代码:" + errcode.ToString() + ",未寻到卡,请重新拿开卡后再放到感应区!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 9MessageBox.Show("错误代码:" + errcode.ToString() + ",寻卡过程中防冲突失败,读序列吗错误!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 10MessageBox.Show("错误代码:" + errcode.ToString() + ",该卡可能已被休眠,无法选中卡片!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 11MessageBox.Show("错误代码:" + errcode.ToString() + ",密码装载失败!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 12MessageBox.Show("错误代码:" + errcode.ToString() + ",标签密码认证错误!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 13MessageBox.Show("错误代码:" + errcode.ToString() + ",读标签失败,原因是刷卡太快或本块所对应的区还没通过密码认证!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 14MessageBox.Show("错误代码:" + errcode.ToString() + ",写标签失败,可能需要验证密码!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 21MessageBox.Show("错误代码:" + errcode.ToString() + ",没有动态库!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 22MessageBox.Show("错误代码:" + errcode.ToString() + ",动态库或驱动程序异常!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 23MessageBox.Show("错误代码:" + errcode.ToString() + ",未检测到发卡器!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 24MessageBox.Show("错误代码:" + errcode.ToString() + ",操作超时,一般是动态库没有反映!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 25MessageBox.Show("错误代码:" + errcode.ToString() + ",发送字数不够!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 26MessageBox.Show("错误代码:" + errcode.ToString() + ",发送的CRC错!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 27MessageBox.Show("错误代码:" + errcode.ToString() + ",接收的字数不够!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 28MessageBox.Show("错误代码:" + errcode.ToString() + ",接收的CRC错!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 45MessageBox.Show("错误代码:" + errcode.ToString() + ",此卡不支持更改UID号或UID块已被锁定!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 46MessageBox.Show("错误代码:" + errcode.ToString() + ",标签存储空间不足!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case 254MessageBox.Show("错误代码:" + errcode.ToString() + ",标签存储空间不足!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)Case ElseMessageBox.Show("错误代码:" + errcode.ToString() + ",未知错误!", "Note:", MessageBoxButtons.OK, MessageBoxIcon.Stop)End SelectEnd Sub
End Class
源码下载:Vb.net读写Ndef标签源码资源-CSDN文库