C#轻松读写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函数声明
//------------------------------------------------------------------------------------------------------------------------------------------------------
//清空MifareClass卡类标签NDEF数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_clear", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_clear();//

//------------------------------------------------------------------------------------------------------------------------------------------------------
//清空ForumType4类标签NDEF数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_forumtype4_clear", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_forumtype4_clear();//

//------------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF文本类型数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addtext", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addtext(string languagecodestr, int languagecodestrlen, string textstr, int textstrlen);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_adddata", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_adddata(string typestr, int typestrlen, string datastr, int datastrlen);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF URI数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_adduri", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_adduri(string languagecodestr, int languagecodestrlen, string titlestr, int titlestrlen, int uriheaderindex, string uristr, int uristrlen);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF电子名片数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addbusinesscard", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addbusinesscard(string infostr, int infostrlen);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF热点连接数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addwifi", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addwifi(string ssidstr, int ssidstrlen,int authtype,int crypttype,string keystr,int keystrlen );

//------------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF蓝牙连接数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addbluetooth", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addbluetooth(string blenamestr, int blenamestrlen, byte[] blemac);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//生成NDEF启动应用数据缓冲
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_addapp", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_addapp(string packagestr, int packagestrlen);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//解析数据缓冲中的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "tagbuf_read", CallingConvention = CallingConvention.StdCall)]
static extern byte tagbuf_read(byte[] revstr, byte[] revstrlen, byte[] recordnumber);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//将NDEF数据缓冲写入MifareClasss标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccwrite_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte piccwrite_ndeftag(byte ctrlword, byte[] serial, byte[] oldkey, byte[] newkey);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//清空MifareClasss类NDEF标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccclear_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte piccclear_ndeftag(byte ctrlword, byte[] serial, byte[] oldkey);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//读取MifareClasss标签内的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccread_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte piccread_ndeftag(byte ctrlword, byte[] serial, byte[] oldkey);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//将NDEF数据缓冲写入ForumType4标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype4_write_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype4_write_ndeftag(byte ctrlword, byte[] serial, byte[] seriallen, byte[] ndefwritekey);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//读取ForumType4标签内的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype4_read_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype4_read_ndeftag(byte ctrlword, byte[] serial, byte[] seriallen, byte[] ndefwritekey);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//将NDEF数据缓冲写入ForumType5_15693 标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype5_write_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype5_write_ndeftag(byte ctrlword,byte afi, byte[] serial);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//读取ForumType5_15693标签内的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype5_read_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype5_read_ndeftag(byte ctrlword, byte afi, byte[] serial);

//锁15693块数据------------------------------------------------------------------------------------------------------------------------------------------
[DllImport("OUR_MIFARE.dll", EntryPoint = "iso15693lockblock", CallingConvention = CallingConvention.StdCall)]
static extern byte iso15693lockblock(byte flags, byte blockaddr, byte[] uid);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//将NDEF数据缓冲写入ForumType2_Ntag2x 标签
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype2_write_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype2_write_ndeftag(byte ctrlword, byte[] serial, byte[] picckey);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//读取ForumType2_ntag2x标签内的NDEF信息
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype2_read_ndeftag", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype2_read_ndeftag(byte ctrlword, byte[] serial, byte[] picckey);

//-------------------------------------------------------------------------------------------------------------------------------------------------------
//读取Ntag卡
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccreadex_ntag", CallingConvention = CallingConvention.StdCall)]
public static extern byte piccreadex_ntag(byte ctrlword, byte[] serial, byte[] picckey, byte blockadd, byte blocksize, byte[] piccdata);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//开启、关闭ntag卡密码保护功能
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccinit_ntag", CallingConvention = CallingConvention.StdCall)]
public static extern byte piccinit_ntag(byte ctrlword, byte[] serial, byte[] picckey, byte[] piccdata);

//-------------------------------------------------------------------------------------------------------------------------------------------------------
//读取15693卡
[DllImport("OUR_MIFARE.dll", EntryPoint = "iso15693readex", CallingConvention = CallingConvention.StdCall)]
static extern byte iso15693readex(byte flags, byte afi, byte startblock, byte blocknum, byte[] uid, byte[] revbuf);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//轻松读MifareClass卡
[DllImport("OUR_MIFARE.dll", EntryPoint = "piccreadex", CallingConvention = CallingConvention.StdCall)]
static extern byte piccreadex(byte ctrlword, byte[] serial, byte area, byte keyA1B0, byte[] picckey, byte[] piccdata0_2);

//------------------------------------------------------------------------------------------------------------------------------------------------------
//轻松读forumtype4卡
[DllImport("OUR_MIFARE.dll", EntryPoint = "forumtype4request", CallingConvention = CallingConvention.StdCall)]
static extern byte forumtype4request(byte ctrlword, byte[] serial, byte[] seriallen);
写入NDEF文本
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];  //卡片新密码
bool havelock = checkBox1.Checked ;             //卡片是否已加锁
bool KeyEn = checkBox2.Checked; ;               //是否启用密码保护NDEF信息

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;

string languagecodestr = "en";   //语言编码,英文为en,中文为zh
int languagecodestrlen = languagecodestr.Length;

string textstr = textBox1.Text.Trim();
int textstrlen = System.Text.Encoding.GetEncoding(936).GetBytes(textstr).Length;

int cardtype = checkcardtype();
Switch (cardtype)
{
    case 1:     //Ntag2x标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen);
        if (status == 0)
        {
            if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }
            status = forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey);
            if (status == 0)
            {
                NtagKeyEn(mypiccserial);   //开启或关闭Ntag2x标签密码保护功能

                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); }
        }
        else { MessageBox.Show("生成NDEF纯文本标签数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 2:     //15693标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen);
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF纯文本标签数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 3:             //MifareClass标签
        tagbuf_clear(); //清空标签数据缓冲
        status = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen);
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF纯文本标签数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 4:     //ForumType4标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addtext(languagecodestr, languagecodestrlen, textstr, textstrlen);
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF纯文本标签数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
default:
        MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        break;
}
写入NDEF智能海报
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];  //卡片新密码
bool havelock = checkBox1.Checked;             //卡片是否已加锁
bool KeyEn = checkBox2.Checked; ;               //是否启用密码保护NDEF信息

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;

string languagecodestr = "en";  //语言编码,英文为en,中文为zh
int languagecodestrlen = languagecodestr.Length;

string titlestr = textBox4.Text.Trim(); //标题
int titlestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length; //标题长度

int uriheaderindex = comboBox1.SelectedIndex;   //前缀

string uristr = textBox5.Text.Trim();   //uri
int uristrlen = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length; //uri长度

int cardtype = checkcardtype();
Switch (cardtype)
{
    case 1:     //Ntag2x标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen); //可以用此方法写入多条记录
        if (status == 0)
        {
            if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }
            status = forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey);
            if (status == 0)
            {
                NtagKeyEn(mypiccserial);   //开启或关闭Ntag2x标签密码保护功能

                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); }
        }
        else { MessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 2:     //15693标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen); //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 3:             //MifareClass标签
        tagbuf_clear(); //清空标签数据缓冲
        status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen); //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 4:     //ForumType4标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen); //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF智能海报数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    default:
        MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        break;
}
写入电子名片
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];  //卡片新密码
bool havelock = checkBox1.Checked;             //卡片是否已加锁
bool KeyEn = checkBox2.Checked; ;               //是否启用密码保护NDEF信息

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;

string infostr = "BEGIN:VCARD"+"\n";  //语言编码,英文为en,中文为zh
infostr = infostr + "VERSION:3.0" + "\n";
infostr = infostr + "FN:" +textBox12.Text.Trim()  + "\n";   //姓名
infostr = infostr + "TEL:" + textBox11.Text.Trim() + "\n";  //电话
infostr = infostr + "ORG:" + textBox10.Text.Trim() + "\n";  //单位名称
infostr = infostr + "ADR:" + textBox15.Text.Trim() + "\n";  //地址
infostr = infostr + "EMAIL:" + textBox13.Text.Trim() + "\n";//邮箱
infostr = infostr + "URL:" + textBox14.Text.Trim() + "\n";  //官网
infostr = infostr + "END:VCARD" + "\n";

int infostrlen = System.Text.Encoding.GetEncoding(936).GetBytes(infostr).Length; //名片长度

int cardtype = checkcardtype();
Switch (cardtype)
{
    case 1:     //Ntag2x标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addbusinesscard(infostr, infostrlen);  //可以用此方法写入多条记录
        if (status == 0)
        {
            if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }
            status = forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey);
            if (status == 0)
            {
                NtagKeyEn(mypiccserial);   //开启或关闭Ntag2x标签密码保护功能

                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); }
        }
        else { MessageBox.Show("生成NDEF电子名片数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 2:     //15693标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addbusinesscard(infostr, infostrlen);  //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF电子名片数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 3:             //MifareClass标签
        tagbuf_clear(); //清空标签数据缓冲
        status = tagbuf_addbusinesscard(infostr, infostrlen);  //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF电子名片数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 4:     //ForumType4标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addbusinesscard(infostr, infostrlen);  //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF电子名片数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    default:
        MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        break;
}
写入WIFI连接 
  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];  //卡片新密码
bool havelock = checkBox1.Checked;             //卡片是否已加锁
bool KeyEn = checkBox2.Checked; ;               //是否启用密码保护NDEF信息

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;

string ssidstr =  textBox16.Text.Trim() ;   //热点名称
int ssidstrlen = System.Text.Encoding.GetEncoding(936).GetBytes(ssidstr).Length; //热点名称长度

int authtype = comboBox2.SelectedIndex; //认证方式
int crypttype = comboBox3.SelectedIndex;//加密算法

string keystr = textBox17.Text.Trim(); //密码
int keystrlen = System.Text.Encoding.GetEncoding(936).GetBytes(keystr).Length; //密码长度

int cardtype = checkcardtype();
Switch (cardtype)
{
    case 1:     //Ntag2x标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen);  //可以用此方法写入多条记录
        if (status == 0)
        {
            if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }
            status = forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey);
            if (status == 0)
            {
                NtagKeyEn(mypiccserial);   //开启或关闭Ntag2x标签密码保护功能

                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); }
        }
        else { MessageBox.Show("生成NDEF无线连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 2:     //15693标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen);  //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF无线连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 3:             //MifareClass标签
        tagbuf_clear(); //清空标签数据缓冲
        status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen);  //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF无线连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 4:     //ForumType4标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addwifi(ssidstr, ssidstrlen, authtype, crypttype, keystr, keystrlen);  //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF无线连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    default:
        MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        break;
}
 写入地图坐标
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];  //卡片新密码
bool havelock = checkBox1.Checked;             //卡片是否已加锁
bool KeyEn = checkBox2.Checked; ;               //是否启用密码保护NDEF信息

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;

string languagecodestr = "en";  //语言编码,英文为en,中文为zh
int languagecodestrlen = languagecodestr.Length;

string titlestr = textBox7.Text.Trim(); //标题
int titlestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(titlestr).Length; //标题长度

int uriheaderindex = 0;   //地理位置没有链接前缀

string uristr = "geo:" + textBox6.Text.Trim() + "," + textBox8.Text.Trim();
int uristrlen = System.Text.Encoding.GetEncoding(936).GetBytes(uristr).Length; //uri长度

int cardtype = checkcardtype();
Switch (cardtype)
{
    case 1:     //Ntag2x标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen);
        if (status == 0)
        {
            if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }
            status = forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey);
            if (status == 0)
            {
                NtagKeyEn(mypiccserial);   //开启或关闭Ntag2x标签密码保护功能

                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); }
        }
        else { MessageBox.Show("生成NDEF地图坐标数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 2:     //15693标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen);
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF地图坐标数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 3:             //MifareClass标签
        tagbuf_clear(); //清空标签数据缓冲
        status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen);
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF地图坐标数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 4:     //ForumType4标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_adduri(languagecodestr, languagecodestrlen, titlestr, titlestrlen, uriheaderindex, uristr, uristrlen);
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF地图坐标数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    default:
        MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        break;
}
写入蓝牙连接
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];  //卡片新密码
bool havelock = checkBox1.Checked;             //卡片是否已加锁
bool KeyEn = checkBox2.Checked; ;               //是否启用密码保护NDEF信息

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;

byte[] macbuf = new byte[6];        //设备MAC地址

string blenamestr = textBox19.Text.Trim();   //设备名称
int blenamestrlen = System.Text.Encoding.GetEncoding(936).GetBytes(blenamestr).Length; //设备名称长度

string[] macstr = textBox18.Text.Split(new char[2] { ':', ':' });//设备MAC地址
try
{
    macbuf[0] = (byte)Convert.ToInt32(macstr[0], 16);
    macbuf[1] = (byte)Convert.ToInt32(macstr[1], 16);
    macbuf[2] = (byte)Convert.ToInt32(macstr[2], 16);
    macbuf[3] = (byte)Convert.ToInt32(macstr[3], 16);
    macbuf[4] = (byte)Convert.ToInt32(macstr[4], 16);
    macbuf[5] = (byte)Convert.ToInt32(macstr[5], 16);
}
catch
{
    MessageBox.Show("蓝牙设备的MAC地址输入错误,请输入正确的MAC地址!" , "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    textBox18.Select();
    return;
}

int cardtype = checkcardtype();
Switch (cardtype)
{
    case 1:     //Ntag2x标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addbluetooth(blenamestr, blenamestrlen, macbuf);   //可以用此方法写入多条记录
        if (status == 0)
        {
            if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }
            status = forumtype2_write_ndeftag(myctrlword, mypiccserial, oldpicckey);
            if (status == 0)
            {
                NtagKeyEn(mypiccserial);   //开启或关闭Ntag2x标签密码保护功能

                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); }
        }
        else { MessageBox.Show("生成NDEF蓝牙连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 2:     //15693标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addbluetooth(blenamestr, blenamestrlen, macbuf);   //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF蓝牙连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 3:             //MifareClass标签
        tagbuf_clear(); //清空标签数据缓冲
        status = tagbuf_addbluetooth(blenamestr, blenamestrlen, macbuf);   //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF蓝牙连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    case 4:     //ForumType4标签
        tagbuf_forumtype4_clear(); //清空标签数据缓冲
        status = tagbuf_addbluetooth(blenamestr, blenamestrlen, macbuf);   //可以用此方法写入多条记录
        if (status == 0)
        {
            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); }
        }
        else { MessageBox.Show("生成NDEF蓝牙连接数据缓冲时返回码错误代码:" + status, "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        break;
    default:
        MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        break;
}
 读取NDEF标签信息
byte status;
byte afi;
byte myctrlword;//控制字
byte[] mypiccserial = new byte[8];//卡序列号
byte[] oldpicckey = new byte[6];  //卡片旧密码
byte[] mypiccseriallen = new byte[1];
byte[] revstrlen=new byte[1];
byte[] recordnumber=new byte[1] ;
byte[] mypiccdata=new byte[2048];
string carduid = "";

bool havelock = checkBox1.Checked;             //卡片是否已加锁
oldpicckey[0] = 0x19; oldpicckey[1] = 0x74; oldpicckey[2] = 0x02; oldpicckey[3] = 0x02; oldpicckey[4] = 0x01; oldpicckey[5] = 0x11;//为防止测试中忘记以设定的密码,标签统一用此组密码加密,客户可自行设置其他的标签保护密码

textBox21.Text = "";
status = 255;
int cardtype = checkcardtype();
Switch (cardtype)
{
    case 1:     //Ntag2x标签
        if (havelock) { myctrlword = 0x10; } else { myctrlword = 0; }
        status = forumtype2_read_ndeftag(myctrlword, mypiccserial, oldpicckey);
        carduid = "Ntag2UID:";
        for (int i = 0; i < 7; i++)
        {
            carduid = carduid + mypiccserial[i].ToString("X02");
        }
        break;
    case 2:     //15693标签
        myctrlword = 0;
        afi = 0;
        status = forumtype5_read_ndeftag(myctrlword, afi, mypiccserial);
        carduid = "15693UID:";
        for (int i = 0; i < 8; i++)
        {
            carduid = carduid + mypiccserial[i].ToString("X02");
        }
        break;
    case 3:     //MifareClass标签
        myctrlword = 0x80 + 0x10;
        status = piccread_ndeftag(myctrlword, mypiccserial, oldpicckey);
        carduid = "MifareClassUID:";
        for (int i = 0; i < 4; i++)
        {
            carduid = carduid + mypiccserial[i].ToString("X02");
        }
        break;
    case 4:     //ForumType4标签
        myctrlword = 0;     //0表示标签无密码,如设置密码取值  &H40 ,mypicckey 存放密码
        status = forumtype4_read_ndeftag(myctrlword, mypiccserial, mypiccseriallen, oldpicckey);
        carduid = "ForumType4UID:";
        for (int i = 0; i < mypiccseriallen[0]; i++)
        {
            carduid = carduid + mypiccserial[i].ToString("X02");
        }
        break;
default:
        MessageBox.Show("请刷有效的NFC标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        break;
}

if (status == 0)
{
    pcdbeep(38);
    tagbuf_read(mypiccdata, revstrlen, recordnumber);
    string ndefstr = Encoding.Default.GetString(mypiccdata);
    textBox21.Text =carduid+"\r\n"+ ndefstr;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值