本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.52de2c1b8bEEGz&ft=t&id=61539185785
Python
languagecodestr = "en".encode('gbk')
titlestr=self.lineEdit_title.text().strip().encode('gbk')
uriheaderindex = self.comboBox_headindex.currentIndex()
uristr=self.lineEdit_uri.text().strip().encode('gbk')
status = Objdll.tagbuf_adduri(languagecodestr, len(languagecodestr), titlestr, len(titlestr), uriheaderindex,uristr, len(uristr)) % 256apptypestr="ohos.com:pkg".encode('gbk')
packagestr = "weixin://dl/business/?ticket=la0041f2cedf9630c9c7cc8c8715ac0c6".encode('gbk')
status = Objdll.tagbuf_addapp1(packagestr, len(packagestr),apptypestr,len(apptypestr)) % 256self.WriteTag('NDEF缓冲区数据')def WriteTag(self,funcname): #将NDEF缓冲区数据写入各种不同类型的NFC标签,清除标签内NDEF数据mypiccserial = bytes(8)mypiccseriallen=bytes(1)try:cardtyep = self.checkcardtype() #判断发卡器上的标签类型if(cardtyep==1): #写forumtype2 Ntag2标签if self.checkBox_before.isChecked():myctrlword = 16else:myctrlword = 0status = Objdll.forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey) % 256if (status == 0):Objdll.pcdbeep(38)cardno = 'ForumType2,UID:'for num in range(0, 7):cardno = cardno + '%02X' % (mypiccserial[num])self.ListBottom('NDEF缓冲区数据写入标签成功!标签类型:'+cardno)if (self.checkBox_before.isChecked() and not self.checkBox_later.isChecked()) or (not self.checkBox_before.isChecked() and self.checkBox_later.isChecked() or (self.checkBox_before.isChecked() and self.checkBox_later.isChecked())):self.NtagkeyEn()else:self.ListErrInf(status)elif(cardtyep==2): #写forumtype5 15693标签myctrlword = 0afi=0status = Objdll.forumtype5_write_ndeftag(myctrlword,afi, mypiccserial) % 256if (status == 0):Objdll.pcdbeep(38)cardno = 'ForumType5,UID:'for num in range(0, 8):cardno = cardno + '%02X' % (mypiccserial[num])if self.checkBox_later.isChecked(): #15693卡锁定块数据后只能读取不可再修改,为防止卡片锁死,请谨慎锁定Objdll.iso15693lockblock(0,1,mypiccserial)self.ListBottom('NDEF缓冲区数据写入标签成功!标签类型:' + cardno)else:self.ListErrInf(status)elif (cardtyep == 3): # 写MifareClassic标签if(funcname=='NDEF_Clear'):if self.checkBox_before.isChecked():myctrlword = 210else:myctrlword = 146status = Objdll.piccclear_ndeftag(myctrlword, mypiccserial, oldpicckey) % 256else:if self.checkBox_before.isChecked():myctrlword = 210else:myctrlword = 146if self.checkBox_later.isChecked(): #写标签后给标签加密保护myctrlword=myctrlword+4status = Objdll.piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey,newpicckey) % 256if (status == 0):Objdll.pcdbeep(38)cardno = 'MifareClass,UID:'for num in range(0, 4):cardno = cardno + '%02X' % (mypiccserial[num])self.ListBottom('NDEF缓冲区数据写入标签成功!标签类型:' + cardno)else:self.ListErrInf(status)elif(cardtyep==4): #写forumtype4 标签if self.checkBox_before.isChecked():myctrlword = 64else:myctrlword = 0status = Objdll.forumtype4_write_ndeftag(myctrlword, mypiccserial,mypiccseriallen,newpicckey) % 256if (status == 0):Objdll.pcdbeep(38)cardno = 'ForumType4,UID:'for num in range(0, mypiccseriallen[0]):cardno = cardno + '%02X' % (mypiccserial[num])self.ListBottom('NDEF缓冲区数据写入标签成功!标签类型:' + cardno)else:self.ListErrInf(status)elif (cardtyep == 23):self.ListErrInf(23)else:self.ListErrInf(8)except:self.ListErrInf(8)
C#
string languagecodestr = "en"; //语言编码,英文为en,中文为zh
int languagecodestrlen = languagecodestr.Length;string titlestr = ""; //标题
int titlestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length; //标题长度int uriheaderindex = 0; //前缀string uristr = "weixin://dl/business/?ticket=la0041f2cedf9630c9c7cc8c8715ac0c6"; //uri
int uristrlen = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length; //uri长度string packagestr = "com.tencent.mm" ; //APP名称
int packagestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(packagestr).Length; //APP名称长度string apptypestr="ohos.com:pkg";
int apptypelen=System.Text.Encoding.GetEncoding(936).GetBytes(apptypestr).Length; byte status0 = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen);
byte status1= tagbuf_addapp1(packagestr, packagestrlen,apptypestr,apptypelen);
if (status0 +status1==0 ){bool havelock = checkBox1.Checked; //卡片是否已加锁 bool KeyEn = checkBox2.Checked; ; //是否启用密码保护NDEF信息WriteDataBufToTag("NDEF组合标签", havelock, KeyEn);
}private void WriteDataBufToTag(string NDEFinfo, bool havelock, bool keyEn)
{byte status;byte afi;byte myctrlword;//控制字byte[] mypiccserial = new byte[8];//卡序列号byte[] mypiccseriallen = new byte[1];byte[] oldpicckey = new byte[6]; //卡片旧密码byte[] newpicckey = new byte[6]; //卡片新密码oldpicckey[0] = 0x19; oldpicckey[1] = 0x74; oldpicckey[2] = 0x02; oldpicckey[3] = 0x02; oldpicckey[4] = 0x01; oldpicckey[5] = 0x11;//为防止测试中忘记以设定的密码,标签统一用此组密码加密,客户可自行设置其他的标签保护密码newpicckey[0] = 0x19; newpicckey[1] = 0x74; newpicckey[2] = 0x02; newpicckey[3] = 0x02; newpicckey[4] = 0x01; newpicckey[5] = 0x11;int cardtype = checkcardtype();switch (cardtype){case 1: //Ntag2x标签if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }status = forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey);if (status == 0){NtagKeyEn(mypiccserial); //开启或关闭Ntag2x标签密码保护功能//if (KeyEn) { NtagPageLock(); } 锁定Ntag2标签数据块,锁定后不可再改修改,请谨慎使用pcdbeep(38);string carduid = "Ntag2UID:";for (int i = 0; i < 7; i++){carduid = carduid + mypiccserial[i].ToString("X02");}MessageBox.Show(carduid + ",NDEF智能海报写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}else { disperrinf(status); }break;case 2: //15693标签myctrlword = 0;afi = 0;status = forumtype5_write_ndeftag(myctrlword, afi, mypiccserial);if (status == 0){//if (KeyEn) //15693卡锁定块数据后只能读取不可再修改,为防止卡片锁死,仅在确定需要才开启此段代码。//{// status = iso15693lockblock(34, 1, mypiccserial);//}pcdbeep(38);string carduid = "15693UID:";for (int i = 0; i < 8; i++){carduid = carduid + mypiccserial[i].ToString("X02");}MessageBox.Show(carduid + ",NDEF智能海报写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}else { disperrinf(status); }break;case 3: //MifareClass标签if (havelock) { myctrlword = 0x80 + 0x40 + 0x10; } else { myctrlword = 0x80 + 0x10; }if (keyEn) { myctrlword = (byte)(myctrlword + 0x04); }status = piccwrite_ndeftag(myctrlword, mypiccserial, oldpicckey, newpicckey);if (status == 0){pcdbeep(38);string carduid = "MifareClassUID:";for (int i = 0; i < 4; i++){carduid = carduid + mypiccserial[i].ToString("X02");}MessageBox.Show(carduid + ",NDEF智能海报写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}else { disperrinf(status); }break;case 4: //ForumType4标签myctrlword = 0; //0表示标签无密码,如设置密码取值 &H40 ,mypicckey 存放密码status = forumtype4_write_ndeftag(myctrlword, mypiccserial, mypiccseriallen, newpicckey);if (status == 0){pcdbeep(38);string carduid = "ForumType4UID:";for (int i = 0; i < mypiccseriallen[0]; i++){carduid = carduid + mypiccserial[i].ToString("X02");}MessageBox.Show(carduid + ",NDEF智能海报写入成功!!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}else { disperrinf(status); }break;default:MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);break;}
}
VB源码
Dim dispstr As String
Dim status As Byte
Dim status1 As Byte
Dim afi As Byte
Dim myctrlword As Byte
Dim mypiccserial(0 To 7) 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 LongDim taginfstr As StringDim packagestr As String
Dim packagestrlen As Longlanguagecodestr = "en" '语言编码,英文为en,中文为zh
languagecodestrlen = 2noteinf = ""
If isexcel Thengetexcelinf2
End IfIf Trim(Text18.Text) = "" And Trim(Text5.Text) = "" ThenexcelclosMsgBox "请输入要写入的URL!", vbCritical + vbOKOnly, "提示"Text18.SetFocusExit Sub
End IfCheckCardTypetitlestr = Trim(Text4.Text) '标题
titlestrlen = LenB(StrConv(titlestr, vbFromUnicode))uriheaderindex = Combo2.ListIndex '链接前缀uristr = Trim(Text18.Text) '链接
If Check4.Value > 0 Then uristr = uristr + cardstr
uristrlen = LenB(StrConv(uristr, vbFromUnicode))
If Combo2.ListIndex > 0 Then taginfstr = Trim(Combo2.Text) + uristr Else taginfstr = uristrpackagestr = Trim(Text5.Text)
packagestrlen = LenB(StrConv(packagestr, vbFromUnicode))
If packagestrlen > 0 Then taginfstr = taginfstr + "," + packagestr Else taginfstr = taginfstr + ","taginfstr = noteinf + "," + taginfstr + "," + cardstrIf Len(Trim(outnote1.Text)) > 0 Then taginfstr = taginfstr + "," + Trim(outnote1.Text) Else taginfstr = taginfstr + ", "
If Len(Trim(outnote2.Text)) > 0 Then taginfstr = taginfstr + "," + Trim(outnote2.Text) Else taginfstr = taginfstr + ", "
If Len(Trim(outnote3.Text)) > 0 Then taginfstr = taginfstr + "," + Trim(outnote3.Text) Else taginfstr = taginfstr + ", "If CardType = 1 Then 'ForumType2、Ntag2tagbuf_forumtype4_clearIf uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0If (status + status1 = 0) ThenIf Check3.Value > 0 Then myctrlword = &H10 Else myctrlword = 0status = forumtype2_write_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0))dispstr = "NFC_Forum_Type2Uid:" + cardstr + ",写入Url+App包名"dispriv dispstr, status' WritDevBufferInf taginfstrWritDevBufferCSV taginfstrIf (Check3.Value > 0 And Check2.Value < 1) Or (Check3.Value < 1 And Check2.Value > 0) Then NtagKeyEnElsedispstr = "NFC_Forum_Type2Uid:" + cardstr + ",生成Url+App包名NDEF记录"dispriv dispstr, statusEnd IfElseIf CardType = 2 Then 'ForumType5、15693tagbuf_forumtype4_clearIf uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0If (status + status1 = 0) Thenmyctrlword = 0afi = 0status = forumtype5_write_ndeftag(myctrlword, afi, mypiccserial(0))dispstr = "NFC_Forum_Type5Uid:" + cardstr + ",写入Url+App包名"dispriv dispstr, status' WritDevBufferInf taginfstrWritDevBufferCSV taginfstrIf Check2.Value > 0 Then status = iso15693lockblock(0, 1, VarPtr(mypiccserial(0))) '15693卡锁定块数据后只能读取不可再修改,为防止卡片锁死,请谨慎锁定Elsedispstr = "NFC_Forum_Type5Uid:" + cardstr + ",生成Url+App包名NDEF记录"dispriv dispstr, statusEnd IfElseIf CardType = 3 Then 'MifareClassIctagbuf_clearIf uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0If (status + status1 = 0) ThenIf Check3.Value > 0 Then myctrlword = &H80 + &H40 + &H10 + &H2 Else myctrlword = &H80 + &H10 + &H2 'MifareClass卡是否已经加有保护密码If Check2.Value > 0 Then myctrlword = myctrlword + &H4 '写入NDEF数据后 并加上保护密码status = piccwrite_ndeftag(myctrlword, mypiccserial(0), oldpicckey(0), newpicckey(0))dispstr = "MifareClassUid:" + cardstr + ",写入Url+App包名"' WritDevBufferInf taginfstrWritDevBufferCSV taginfstrdispriv dispstr, statusElsedispstr = "MifareClassUid:" + cardstr + ",生成Url+App包名NDEF记录"dispriv dispstr, statusEnd IfElseIf CardType = 4 Then 'ForumType4tagbuf_forumtype4_clearIf uristrlen > 0 Then status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen) Else status = 0If packagestrlen > 0 Then status1 = tagbuf_addapp(packagestr, packagestrlen) Else status1 = 0If (status + status1 = 0) ThenIf Check3.Value > 0 Then myctrlword = &H40 Else myctrlword = 0status = forumtype4_write_ndeftag(myctrlword, mypiccserial(0), mypiccseriallen(0), mypicckey(0))dispstr = "NFC_Forum_Type4Uid:" + cardstr + ",写入Url+App包名"' WritDevBufferInf taginfstrWritDevBufferCSV taginfstrdispriv dispstr, statusElsedispstr = "NFC_Forum_Type4Uid:" + cardstr + ",生成Url+App包名NDEF记录"dispriv dispstr, statusEnd If
End If