Qt C++读写NFC标签NDEF网址URI

news2025/1/14 0:25:41

本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.1d292c1biFgjSs&ft=t&id=615391857885 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "QLibrary"
#include "QMessageBox"
#include <exception>

unsigned char mypicoldkey[6]={0x19,0x74,0x02,0x02,0x01,0x11};   //旧密钥
unsigned char mypicnewkey[6]={0x19,0x74,0x02,0x02,0x01,0x11};   //新密钥,可自行设置

//本示例可在windows、linux系统内编译、运行,自动判断windows系统加载DLL库、linux系统加载SO库----------------------------------------------------------------------------------------------------------------------------------------------
//让读写器发出声音
typedef  unsigned char (*pcdbeep)(unsigned long xms);
#ifdef Q_OS_WIN
    pcdbeep mypcdbeep = (pcdbeep)QLibrary("OUR_MIFARE.dll").resolve("pcdbeep");
#else
    pcdbeep mypcdbeep = (pcdbeep)QLibrary("./libOURMIFARE.so").resolve("pcdbeep");
#endif

//返回本读写器独一无二的设备编号
typedef  unsigned char (*pcdgetdevicenumber)(unsigned char *devicenumber);
#ifdef Q_OS_WIN
    pcdgetdevicenumber mypcdgetdevicenumber = (pcdgetdevicenumber)QLibrary("OUR_MIFARE.dll").resolve("pcdgetdevicenumber");
#else
    pcdgetdevicenumber mypcdgetdevicenumber = (pcdgetdevicenumber)QLibrary("./libOURMIFARE.so").resolve("pcdgetdevicenumber");
#endif

//清空MifareClass卡类标签NDEF数据缓冲
    typedef  unsigned char (*tagbuf_clear)();
#ifdef Q_OS_WIN
    tagbuf_clear mytagbuf_clear = (tagbuf_clear)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_clear");
#else
    tagbuf_clear mytagbuf_clear = (tagbuf_clear)QLibrary("./libOURMIFARE.so").resolve("tagbuf_clear");
#endif

//清空ForumType4类标签NDEF数据缓冲
    typedef  unsigned char (*tagbuf_forumtype4_clear)();
#ifdef Q_OS_WIN
    tagbuf_forumtype4_clear mytagbuf_forumtype4_clear = (tagbuf_forumtype4_clear)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_forumtype4_clear");
#else
    tagbuf_forumtype4_clear mytagbuf_forumtype4_clear = (tagbuf_forumtype4_clear)QLibrary("./libOURMIFARE.so").resolve("tagbuf_forumtype4_clear");
#endif

//轻松读Ntag卡
typedef  unsigned char (*piccreadex_ntag)(unsigned char ctrlword,unsigned char *serial,unsigned char *pickey,unsigned char blockadd,unsigned char blocksize,unsigned char *picdata);
#ifdef Q_OS_WIN
    piccreadex_ntag mypiccreadex_ntag = (piccreadex_ntag)QLibrary("OUR_MIFARE.dll").resolve("piccreadex_ntag");
#else
    piccreadex_ntag mypiccreadex_ntag = (piccreadex_ntag)QLibrary("./libOURMIFARE.so").resolve("piccreadex_ntag");
#endif

//轻松读15693卡
typedef unsigned char (*iso15693readex)(unsigned char ctrlword,unsigned char afi,unsigned char startblock,unsigned char blocknum,unsigned char *uid,unsigned char *piccdatabuf);
#ifdef Q_OS_WIN
    iso15693readex myiso15693readex = (iso15693readex)QLibrary("OUR_MIFARE.dll").resolve("iso15693readex");
#else
    iso15693readex myiso15693readex = (iso15693readex)QLibrary("./libOURMIFARE.so").resolve("iso15693readex");
#endif

//轻松读M1卡
typedef unsigned char (*piccreadex)(unsigned char ctrlword,unsigned char *serial,unsigned char area,unsigned char keyA1B0,unsigned char *picckey,unsigned char *piccdata0_2);
#ifdef Q_OS_WIN
    piccreadex mypiccreadex = (piccreadex)QLibrary("OUR_MIFARE.dll").resolve("piccreadex");
#else
    piccreadex mypiccreadex = (piccreadex)QLibrary("./libOURMIFARE.so").resolve("piccreadex");
#endif

//寻forumtype4
typedef unsigned char (*forumtype4request)(unsigned char ctrlword,unsigned char *serial,unsigned char *seriallen);
#ifdef Q_OS_WIN
    forumtype4request myforumtype4request = (forumtype4request)QLibrary("OUR_MIFARE.dll").resolve("forumtype4request");
#else
    forumtype4request myforumtype4request = (forumtype4request)QLibrary("./libOURMIFARE.so").resolve("forumtype4request");
#endif

//将ndef记录缓冲数据写入forumtype2标签
typedef unsigned char (*forumtype2_write_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *oldkey);
#ifdef Q_OS_WIN
    forumtype2_write_ndeftag myforumtype2_write_ndeftag = (forumtype2_write_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype2_write_ndeftag");
#else
    forumtype2_write_ndeftag myforumtype2_write_ndeftag = (forumtype2_write_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype2_write_ndeftag");
#endif

//读取forumtype2标签信息
typedef unsigned char (*forumtype2_read_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *oldkey);
#ifdef Q_OS_WIN
    forumtype2_read_ndeftag myforumtype2_read_ndeftag = (forumtype2_read_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype2_read_ndeftag");
#else
    forumtype2_read_ndeftag myforumtype2_read_ndeftag = (forumtype2_read_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype2_read_ndeftag");
#endif

//将ndef记录缓冲数据写入forumtype5 15693标签
typedef unsigned char (*forumtype5_write_ndeftag)(unsigned char ctrlword,unsigned char afi,unsigned char *serial);
#ifdef Q_OS_WIN
    forumtype5_write_ndeftag myforumtype5_write_ndeftag = (forumtype5_write_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype5_write_ndeftag");
#else
    forumtype5_write_ndeftag myforumtype5_write_ndeftag = (forumtype5_write_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype5_write_ndeftag");
#endif

//读取forumtype5 15693标签信息
typedef unsigned char (*forumtype5_read_ndeftag)(unsigned char ctrlword,unsigned char afi,unsigned char *serial);
#ifdef Q_OS_WIN
    forumtype5_read_ndeftag myforumtype5_read_ndeftag = (forumtype5_read_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype5_read_ndeftag");
#else
    forumtype5_read_ndeftag myforumtype5_read_ndeftag = (forumtype5_read_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype5_read_ndeftag");
#endif

//将ndef记录缓冲数据写入mifareclass标签
typedef unsigned char (*piccwrite_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *oldkey,unsigned char *newkey);
#ifdef Q_OS_WIN
    piccwrite_ndeftag mypiccwrite_ndeftag = (piccwrite_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("piccwrite_ndeftag");
#else
    piccwrite_ndeftag mypiccwrite_ndeftag = (piccwrite_ndeftag)QLibrary("./libOURMIFARE.so").resolve("piccwrite_ndeftag");
#endif

//读取mifareclass标签信息
typedef unsigned char (*piccread_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *oldkey);
#ifdef Q_OS_WIN
    piccread_ndeftag mypiccread_ndeftag = (piccread_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("piccread_ndeftag");
#else
    piccread_ndeftag mypiccread_ndeftag = (piccread_ndeftag)QLibrary("./libOURMIFARE.so").resolve("piccread_ndeftag");
#endif

//将ndef记录缓冲数据写入forumtype4标签
typedef unsigned char (*forumtype4_write_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *seriallen,unsigned char *newkey);
#ifdef Q_OS_WIN
    forumtype4_write_ndeftag myforumtype4_write_ndeftag = (forumtype4_write_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype4_write_ndeftag");
#else
    forumtype4_write_ndeftag myforumtype4_write_ndeftag = (forumtype4_write_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype4_write_ndeftag");
#endif

//读取forumtype4标签信息
typedef unsigned char (*forumtype4_read_ndeftag)(unsigned char ctrlword,unsigned char *serial,unsigned char *seriallen,unsigned char *oldkey);
#ifdef Q_OS_WIN
    forumtype4_read_ndeftag myforumtype4_read_ndeftag = (forumtype4_read_ndeftag)QLibrary("OUR_MIFARE.dll").resolve("forumtype4_read_ndeftag");
#else
    forumtype4_read_ndeftag myforumtype4_read_ndeftag = (forumtype4_read_ndeftag)QLibrary("./libOURMIFARE.so").resolve("forumtype4_read_ndeftag");
#endif

//解析缓冲区内NDEF记录tagbuf_read
typedef unsigned char (*tagbuf_read)(unsigned char *mypiccdata,unsigned char *revstrlen,unsigned char *recordnumber);
#ifdef Q_OS_WIN
    tagbuf_read mytagbuf_read = (tagbuf_read)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_read");
#else
    tagbuf_read mytagbuf_read = (tagbuf_read)QLibrary("./libOURMIFARE.so").resolve("tagbuf_read");
#endif

//锁定Ntag标签页数据
typedef  unsigned char (*picclock_ntag)(unsigned char locktype,unsigned char *mypicclockdata);
#ifdef Q_OS_WIN
    picclock_ntag mypicclock_ntag = (picclock_ntag)QLibrary("OUR_MIFARE.dll").resolve("picclock_ntag");
#else
    picclock_ntag mypicclock_ntag = (picclock_ntag)QLibrary("./libOURMIFARE.so").resolve("picclock_ntag");
#endif

//初始化Ntag卡,开启读写卡密码保护功能
typedef  unsigned char (*piccinit_ntag)(unsigned char ctrlword,unsigned char *serial,unsigned char *pickey,unsigned char *configdata);
#ifdef Q_OS_WIN
    piccinit_ntag mypiccinit_ntag = (piccinit_ntag)QLibrary("OUR_MIFARE.dll").resolve("piccinit_ntag");
#else
    piccinit_ntag mypiccinit_ntag = (piccinit_ntag)QLibrary("./libOURMIFARE.so").resolve("piccinit_ntag");
#endif

//生成NDEF文本记录数据缓冲tagbuf_addtext
typedef unsigned char (*tagbuf_addtext)(unsigned char *languagecodestr,int languagecodestrlen,unsigned char *textstr,int textstrlen);
#ifdef Q_OS_WIN
    tagbuf_addtext mytagbuf_addtext = (tagbuf_addtext)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addtext");
#else
    tagbuf_addtext mytagbuf_addtext = (tagbuf_addtext)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addtext");
#endif

//生成NDEF唤醒android.com:pkg APP
typedef unsigned char (*tagbuf_addapp)(unsigned char *packagestr,int packagestrlen);
#ifdef Q_OS_WIN
    tagbuf_addapp mytagbuf_addapp = (tagbuf_addapp)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addapp");
#else
    tagbuf_addapp mytagbuf_addapp = (tagbuf_addapp)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addapp");
#endif

//生成NDEF唤醒任意类型APP
typedef unsigned char (*tagbuf_addapp1)(unsigned char *packagestr,int packagestrlen,unsigned char *typestr,int typelen);
#ifdef Q_OS_WIN
    tagbuf_addapp1 mytagbuf_addapp1 = (tagbuf_addapp1)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addapp1");
#else
    tagbuf_addapp1 mytagbuf_addapp1 = (tagbuf_addapp1)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addapp1");
#endif

//生成NDEF网址URI记录数据缓冲tagbuf_adduri
typedef unsigned char (*tagbuf_adduri)(unsigned char *languagecodestr,int languagecodestrlen,unsigned char *textstr,int textstrlen,int uriheaderindex,unsigned char *uristr,int uristrlen);
#ifdef Q_OS_WIN
    tagbuf_adduri mytagbuf_adduri = (tagbuf_adduri)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_adduri");
#else
    tagbuf_adduri mytagbuf_adduri = (tagbuf_adduri)QLibrary("./libOURMIFARE.so").resolve("tagbuf_adduri");
#endif

//生成NDEF蓝牙连接数据缓冲
typedef unsigned char (*tagbuf_addbluetooth)(unsigned char *blenamestr,int blenamestrlen,unsigned char *blemac);
#ifdef Q_OS_WIN
    tagbuf_addbluetooth mytagbuf_addbluetooth = (tagbuf_addbluetooth)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addbluetooth");
#else
    tagbuf_addbluetooth mytagbuf_addbluetooth = (tagbuf_addbluetooth)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addbluetooth");
#endif

//生成NDEF热点wifi连接数据缓冲
typedef unsigned char (*tagbuf_addwifi)(unsigned char *ssidstr,int ssidstrlen,int authtype,int crypttype,unsigned char *keystr,int keystrlen);
#ifdef Q_OS_WIN
    tagbuf_addwifi mytagbuf_addwifi = (tagbuf_addwifi)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addwifi");
#else
    tagbuf_addwifi mytagbuf_addwifi = (tagbuf_addwifi)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addwifi");
#endif

//生成NDEF电子名片数据缓冲
typedef unsigned char (*tagbuf_addbusinesscard)(unsigned char *infostr,int infostrlen);
#ifdef Q_OS_WIN
    tagbuf_addbusinesscard mytagbuf_addbusinesscard = (tagbuf_addbusinesscard)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_addbusinesscard");
#else
    tagbuf_addbusinesscard mytagbuf_addbusinesscard = (tagbuf_addbusinesscard)QLibrary("./libOURMIFARE.so").resolve("tagbuf_addbusinesscard");
#endif

//生成NDEF新数据类型
typedef unsigned char (*tagbuf_adddata)(unsigned char *typestr,int typestrlen,unsigned char *datastr,int datastrlen);
#ifdef Q_OS_WIN
    tagbuf_adddata mytagbuf_adddata = (tagbuf_adddata)QLibrary("OUR_MIFARE.dll").resolve("tagbuf_adddata");
#else
    tagbuf_adddata mytagbuf_adddata = (tagbuf_adddata)QLibrary("./libOURMIFARE.so").resolve("tagbuf_adddata");
#endif


//检测动态库文件是否在运行目录内----------------------------------------------------------------------------------------------------------------------------------------------------------
static bool checkdllos(){
    #ifdef Q_OS_WIN
        QLibrary mylib("OUR_MIFARE.dll");
        if (!mylib.load()){                //判断windows系统下,OUR_MIFARE.dll是否在运行目录内
            QMessageBox::information(NULL, "提示", "请将OUR_MIFARE.dll文件拷贝到生成exe文件相同目录下!");
            return false;
        }else{return true;}
    #else
        QLibrary mylib("./libOURMIFARE.so");
        if (!mylib.load()){                //判断linux系统下,libOURMIFARE.so是否正运行目录内
            QMessageBox::information(NULL, "提示", "请将libOURMIFARE.so文件拷贝到生成的运行文件相同目录下!");
            return false;
        }else{return true;}
    #endif
}

//判断发卡器感应区内标签类型----------------------------------------------------------------------------------------------------------------------------------------------------------
static int checkcardtype(){
    unsigned char status;
    unsigned char myctrlword;
    unsigned char mypiccserial[8] ;
    unsigned char mypicdata[48] ;
    unsigned char mypickey[6]={0xff,0xff,0xff,0xff,0xff,0xff};
    unsigned char mypiccseriallen[1] ;

    myctrlword=0;
    status = mypiccreadex_ntag(myctrlword, mypiccserial, mypicoldkey, 4, 1, mypicdata);
    if(status==0){
        return 1;
    }else{
        status = myiso15693readex(myctrlword,0,0,1,mypiccserial,mypicdata);
        if(status==0){
            return 2;
        }else{
            myctrlword=23;
            status = mypiccreadex(myctrlword,mypiccserial,0,1,mypickey,mypicdata);
            if(status==0){
                return 3;
            }else{
                mypickey[0]=0xa0;mypickey[1]=0xa1;mypickey[2]=0xa2;mypickey[3]=0xa3;mypickey[4]=0xa4;mypickey[5]=0xa5;
                status = mypiccreadex(myctrlword,mypiccserial,0,1,mypickey,mypicdata);
                if(status==0){
                    return 3;
                }else{
                    myctrlword=0;
                    status=myforumtype4request(myctrlword,mypiccserial,mypiccseriallen);
                    if(status==0 || status==52){
                        return 4;
                    }else{
                        return 0;
                    }
                }
            }
        }
    }
}

//Ntag标签开启或关闭密钥保护功能,推荐用此方法保护写入标签内的数据------------------------------------------------------------------------------------------------------------------------------------------------------
static int NtagKeyEn(unsigned char *mypiccserial,bool havekey,bool addkey){
    unsigned char myctrlword;
    unsigned char mypiccdata[16];

    if(havekey){myctrlword=0x10;}else{myctrlword=0;}
    if(addkey){  //开启密钥保护功能
        mypiccdata[0]=0;mypiccdata[1]=0;mypiccdata[2]=0;mypiccdata[3]=4;    //有效数据是从第4块开始的
        myctrlword=myctrlword+1;
        mypiccdata[4]=0;   //允许密钥认证失败次数 ,0表示不限制次数
        mypiccdata[5]=0;mypiccdata[6]=0;mypiccdata[7]=0;   //启用计数器
        myctrlword=myctrlword+2;
        for(int i=0;i<4;i++){
            mypiccdata[8+i]=mypicnewkey[i];   //新认证密钥
        }
        mypiccdata[12]=0x16;mypiccdata[13]=0x16;mypiccdata[14]=0;mypiccdata[15]=4;
        myctrlword=myctrlword+4;
    }else{
        mypiccdata[0]=0;mypiccdata[1]=0;mypiccdata[2]=0;mypiccdata[3]=0xff;
        myctrlword=myctrlword+1;
        mypiccdata[4]=0;mypiccdata[5]=0;mypiccdata[6]=0;mypiccdata[7]=0;
        myctrlword=myctrlword+2;
    }
    unsigned char status=mypiccinit_ntag(myctrlword, mypiccserial, mypicoldkey, mypiccdata);
}

//锁定ntag2数据页,锁定后标签不可以再次修改,请谨慎使用锁定功能----------------------------------------------------------------------------------------------------------------------------------------------------------
static int NtagPageLock(){
    unsigned char status;
    unsigned char mypicclockdata[4] ;
    mypicclockdata[0]=0;mypicclockdata[1]=0;mypicclockdata[2]=0xff;mypicclockdata[3]=0xff;
    status=mypicclock_ntag(0,mypicclockdata);   //静态锁15块之前

    mypicclockdata[0]=0xff;mypicclockdata[1]=0xff;mypicclockdata[2]=0xff;mypicclockdata[3]=0;
    status=mypicclock_ntag(1,mypicclockdata);   //动态锁15块之后
}

//将ndef记录缓冲数据写入不同类型的NFC标签----------------------------------------------------------------------------------------------------------------------------------------------------------
void MainWindow::WriteDataBufToTag(){
    bool havelock;
    bool keyen;
    unsigned char status;
    unsigned char myctrlword;
    unsigned char mypiccserial[8] ;
    unsigned char mypiccseriallen[1] ;
    QString uidstr="" ;
    int i;

    if(ui->checkBox_before->isChecked()){havelock=true;}else{havelock=false;}
    if(ui->checkBox_later->isChecked()){keyen=true;}else{keyen=false;}

    int cardtype=checkcardtype();
    switch (cardtype){
    case 1:
        if(havelock){myctrlword=0x10;} else{myctrlword=0x00;}
        status=myforumtype2_write_ndeftag(myctrlword,mypiccserial,mypicoldkey);
        if(status==0){
            NtagKeyEn(mypiccserial,havelock,keyen);
            mypcdbeep(30);
            for (i = 0; i < 7; i++) {
                uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
            }
            ui->listWidget->addItem("ForumType2Tag:"+uidstr+",NDEF记录缓冲写入成功!");
        }else{
            disperrinf(status);
        }
        break;
    case 2:
        myctrlword=0;
        status=myforumtype5_write_ndeftag(myctrlword,0,mypiccserial);
        if(status==0){
            mypcdbeep(30);
            for (i = 0; i < 8; i++) {
                uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
            }
            ui->listWidget->addItem("ForumType5Tag:"+uidstr+",NDEF记录缓冲写入成功!");
        }else{
            disperrinf(status);
        }
        break;
    case 3:
        if(havelock){myctrlword=208;} else{myctrlword=144;}
        if(keyen){myctrlword=myctrlword+4;}
        status=mypiccwrite_ndeftag(myctrlword,mypiccserial,mypicoldkey,mypicnewkey);
        if(status==0){
            mypcdbeep(30);
            for (i = 0; i < 4; i++) {
                uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
            }
            ui->listWidget->addItem("MifareclassTag:"+uidstr+",NDEF记录缓冲写入成功!");
        }else{
            disperrinf(status);
        }
        break;
    case 4:
        myctrlword=0;
        status=myforumtype4_write_ndeftag(myctrlword,mypiccserial,mypiccseriallen,mypicnewkey);
        if(status==0){
            mypcdbeep(30);
            for (i = 0; i < mypiccseriallen[0]; i++) {
                uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
            }
            ui->listWidget->addItem("ForumType4Tag:"+uidstr+",NDEF记录缓冲写入成功!");
        }else{
            disperrinf(status);
        }
        break;
    default:
        QMessageBox::critical(NULL, "提示", "请刷有效的NFC标签");
        break;
    }
}


//显示返回的错误代码信息-----------------------------------------------------------------------------------------------------------------------------------------------------------------
void MainWindow:: disperrinf(unsigned char result){
    switch (result) {
    case 8:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",未寻到卡,请重新拿开卡后再放到感应区!");
        break;
    case 9:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",有多张卡在感应区,寻卡过程中防冲突失败,读序列吗错误!");
        break;
    case 10:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",该卡可能已被休眠,无法选中卡片!");
        break;
    case 11:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",密码装载失败!");
        break;
    case 12:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",卡片密码认证失败!");
        break;
    case 13:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",读页数据失败,可能需要验证密码!");
        break;
    case 14:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",写页数据失败,可能需要验证密码!");
        break;
    case 18:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",写UID失败,此卡可能不是UID卡!");
        break;
    case 22:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",动态库或驱动程序异常!");
        break;
    case 23:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",驱动程序错误或发卡器尚未安装!");
        break;
    case 24:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",操作超时,一般是动态库没有反应!");
        break;
    case 25:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",发送字数不够!");
        break;
    case 26:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",驱动程序错误或发卡器尚未安装!");
        break;
    case 27:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",操作超时,一般是动态库没有反应!");
        break;
    case 28:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",发送字数不够!");
        break;
    default:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",未知的错误信息!");
        break;
    }
}


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_beep_clicked()
{
    if(!checkdllos()){return;}      //判断运行目录内动态库文件是否存在

    unsigned char status;
    status = mypcdbeep(30);
    if(status==0){
    }else{
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_getdevnum_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    unsigned char devicenumber[4];    //设备编号缓冲
    unsigned char status;             //函数返回状态
    status = mypcdgetdevicenumber(devicenumber);
    if(status==0){
        mypcdbeep(30);
        QMessageBox::information(NULL, "提示", QString::asprintf("设备编号:%03d-%03d-%03d-%03d", devicenumber[0],devicenumber[1],devicenumber[2],devicenumber[3]));
    }else{
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_clearbuff_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在
    mytagbuf_forumtype4_clear();
    mytagbuf_clear();
    QMessageBox::information(NULL, "提示", "NDEF数据缓冲已经清除!");
}


void MainWindow::on_pushButton_writetag_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在
    WriteDataBufToTag();
}

void MainWindow::on_pushButton_text_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    QString languagecodestr = "en";   //语言编码,英文为en,中文为zh
    QByteArray languagecodebuf=languagecodestr.toLocal8Bit();
    int languagecodestrlen=languagecodebuf.length();

    QString textstr=ui->lineEdit_text->text();    //文本
    QByteArray textbuf=textstr.toLocal8Bit();
    int textstrlen=textbuf.length();

    unsigned char status=mytagbuf_addtext((unsigned char *)languagecodebuf.data(),languagecodestrlen,(unsigned char *)textbuf.data(),textstrlen);
    if(status==0){
        QMessageBox::information(NULL, "提示","        生成NDEF纯文本标签数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
    }else {
        disperrinf(status);
    }
}


void MainWindow::on_comboBox_seleapp_currentTextChanged(const QString &arg1)
{
    QString seleappstr=ui->comboBox_seleapp->currentText();
    QStringList list = seleappstr.split("|");
    ui->lineEdit_appname->setText(list.at(1));
}


void MainWindow::on_pushButton_App_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    QString packagestr=ui->lineEdit_appname->text().trimmed();   //app包名称
    QByteArray packagebuf=packagestr.toLocal8Bit();
    int packagestrlen=packagebuf.length();

    QString typestr=ui->comboBox_appstype->currentText();     //app类型
    QByteArray typebuf=typestr.toLocal8Bit();
    int typestrlen=typebuf.length();

    unsigned char status=mytagbuf_addapp1((unsigned char *)packagebuf.data(),packagestrlen,(unsigned char *)typebuf.data(),typestrlen);
    if(status==0){
        QMessageBox::information(NULL, "提示","        生成NDEF启动应用数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
    }else {
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_Url_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    QString languagecodestr = "en";   //语言编码,英文为en,中文为zh
    QByteArray languagecodebuf=languagecodestr.toLocal8Bit();
    int languagecodestrlen=languagecodebuf.length();

    QString textstr=ui->lineEdit_title->text();    //标题
    QByteArray textbuf=textstr.toLocal8Bit();
    int textstrlen=textbuf.length();

    int uriheaderindex = ui->comboBox_headindex->currentIndex();   //前缀

    QString uristr=ui->lineEdit_uri->text();        //uri
    QByteArray uribuf=uristr.toLocal8Bit();
    int uristrlen=uribuf.length();

    unsigned char status=mytagbuf_adduri((unsigned char *)languagecodebuf.data(),languagecodestrlen,(unsigned char *)textbuf.data(),textstrlen,uriheaderindex,(unsigned char *)uribuf.data(),uristrlen);
    if(status==0){
        QMessageBox::information(NULL, "提示","        生成NDEF网址URI记录数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
    }else {
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_Map_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    QString languagecodestr = "en";   //语言编码,英文为en,中文为zh
    QByteArray languagecodebuf=languagecodestr.toLocal8Bit();
    int languagecodestrlen=languagecodebuf.length();

    QString textstr=ui->lineEdit_mapname->text();    //标题
    QByteArray textbuf=textstr.toLocal8Bit();
    int textstrlen=textbuf.length();

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

    QString uristr="geo:" +ui->lineEdit_latitude->text().trimmed()+ ","+ui->lineEdit_longitude->text().trimmed() ;        //uri
    QByteArray uribuf=uristr.toLocal8Bit();
    int uristrlen=uribuf.length();

    unsigned char status=mytagbuf_adduri((unsigned char *)languagecodebuf.data(),languagecodestrlen,(unsigned char *)textbuf.data(),textstrlen,uriheaderindex,(unsigned char *)uribuf.data(),uristrlen);
    if(status==0){
        QMessageBox::information(NULL, "提示","        生成NDEF地图坐标数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
    }else {
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_Bluetooth_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    QString blenamestr=ui->lineEdit_Bluetoothname->text().trimmed();   //设备名称
    QByteArray blenamebuf=blenamestr.toLocal8Bit();
    int blenamestrlen=blenamebuf.length();

    QString macstr=ui->lineEdit_Bluetoothmac->text().trimmed();     //mac
    QStringList list = macstr.split(":");
    unsigned char macbuf[6];
    try{
        for(int i=0;i<6;i++){
            bool ok;
            macbuf[i]=QString(list.at(i)).toInt(&ok,16);
        }
    }catch(_exception){
        QMessageBox::critical(NULL, "提示", "MAC地址输入错误!");
        return;
    }

    unsigned char status=mytagbuf_addbluetooth((unsigned char *)blenamebuf.data(),blenamestrlen,macbuf);
    if(status==0){
        QMessageBox::information(NULL, "提示","        生成NDEF蓝牙连接数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
    }else {
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_Wifi_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    QString ssidstr = ui->lineEdit_hostname->text();   //热点名称
    QByteArray ssidbuf=ssidstr.toLocal8Bit();
    int ssidstrlen=ssidbuf.length();

    int authtype=ui->comboBox_auth->currentIndex();     //认证方式
    int crypttype=ui->comboBox_Calculation->currentIndex();     //加密算法

    QString keystr=ui->lineEdit_password->text();      //密钥
    QByteArray keybuf=keystr.toLocal8Bit();
    int keystrlen=keybuf.length();

    unsigned char status=mytagbuf_addwifi((unsigned char *)ssidbuf.data(),ssidstrlen,authtype,crypttype,(unsigned char *)keybuf.data(),keystrlen);
    if(status==0){
        QMessageBox::information(NULL, "提示","        生成WIFI无线连接数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
    }else {
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_Card_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    QString infostr = "BEGIN:VCARD\n" ;
    infostr=infostr+"VERSION:3.0" + "\n";
    infostr=infostr+"FN:" +ui->lineEdit_name->text()+ "\n";
    infostr=infostr+"TEL:"+ui->lineEdit_tel->text() + "\n";
    infostr=infostr+"ORG:"+ui->lineEdit_unilname->text() + "\n";
    infostr=infostr+"ADR:" +ui->lineEdit_address->text()+ "\n";
    infostr=infostr+"EMAIL:"+ui->lineEdit_email->text() + "\n";
    infostr=infostr+"URL:" +ui->lineEdit_net->text()+ "\n";
    infostr=infostr+"END:VCARD\n";
    QByteArray infobuf=infostr.toLocal8Bit();
    int infostrlen=infobuf.length();

    unsigned char status=mytagbuf_addbusinesscard((unsigned char *)infobuf.data(),infostrlen);
    if(status==0){
        QMessageBox::information(NULL, "提示","        生成NDEF电子名片数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
    }else {
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_Data_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    QString typestr=ui->lineEdit_datatype->text().trimmed();   //类型名称
    QByteArray typebuf=typestr.toLocal8Bit();
    int typestrlen=typebuf.length();

    QString datastr=ui->lineEdit_data->text();     //数据
    QByteArray databuf=datastr.toLocal8Bit();
    int datastrlen=databuf.length();

    unsigned char status=mytagbuf_adddata((unsigned char *)typebuf.data(),typestrlen,(unsigned char *)databuf.data(),datastrlen);
    if(status==0){
        QMessageBox::information(NULL, "提示","        生成NDEF数据标签缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
    }else {
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_Call_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    QString languagecodestr = "en";   //语言编码,英文为en,中文为zh
    QByteArray languagecodebuf=languagecodestr.toLocal8Bit();
    int languagecodestrlen=languagecodebuf.length();

    QString textstr="";    //标题
    QByteArray textbuf=textstr.toLocal8Bit();
    int textstrlen=textbuf.length();

    int uriheaderindex = 5;   //呼叫电话的链接前缀为5

    QString uristr=ui->lineEdit_callnum->text().trimmed() ;        //uri
    QByteArray uribuf=uristr.toLocal8Bit();
    int uristrlen=uribuf.length();

    unsigned char status=mytagbuf_adduri((unsigned char *)languagecodebuf.data(),languagecodestrlen,(unsigned char *)textbuf.data(),textstrlen,uriheaderindex,(unsigned char *)uribuf.data(),uristrlen);
    if(status==0){
        QMessageBox::information(NULL, "提示","        生成NDEF呼叫电话数据缓冲成功,可以向缓冲区继续添加记录,也可以将缓冲区内数据写标签。");
    }else {
        disperrinf(status);
    }
}


void MainWindow::on_pushButton_cleatag_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在
    mytagbuf_forumtype4_clear();
    mytagbuf_clear();
    WriteDataBufToTag();
}


void MainWindow::on_pushButton_clearinf_clicked()
{
    ui->listWidget->clear();
    ui->plainTextEdit_taginf->setPlainText("");
}


void MainWindow::on_pushButton_readtag_clicked()
{
    bool havelock;
    unsigned char status=8;
    unsigned char myctrlword;
    unsigned char mypiccserial[8] ;
    unsigned char mypiccseriallen[1] ;
    QString uidstr="" ;
    int i;

    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    ui->plainTextEdit_taginf->setPlainText("");

    if(ui->checkBox_before->isChecked()){havelock=true;}else{havelock=false;}   //标签是否已加密钥

    int cardtype=checkcardtype();
    switch (cardtype){
    case 1:
        if(havelock){myctrlword=0x10;} else{myctrlword=0x00;}
        status=myforumtype2_read_ndeftag(myctrlword,mypiccserial,mypicoldkey);
        uidstr="ForumType2Tag:";
        for (i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        break;
    case 2:
        myctrlword=0;
        status=myforumtype5_read_ndeftag(myctrlword,0,mypiccserial);
        uidstr="ForumType5Tag:";
        for (i = 0; i < 8; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        break;
    case 3:
        myctrlword=144;
        status=mypiccread_ndeftag(myctrlword,mypiccserial,mypicoldkey);
        uidstr="MifareclassTag:";
        for (i = 0; i < 4; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        break;
    case 4:
        myctrlword=0;
        status=myforumtype4_read_ndeftag(myctrlword,mypiccserial,mypiccseriallen,mypicoldkey);
        uidstr="ForumType4Tag:";
        for (i = 0; i < mypiccseriallen[0]; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        break;
    default:
        QMessageBox::critical(NULL, "提示", "请刷有效的NFC标签");
        break;
    }

    if(status==0){
        ui->listWidget->addItem(uidstr+",读取标签内信息成功!");
        mypcdbeep(30);
        unsigned char mypiccdata[1024];
        unsigned char revstrlen[2];
        unsigned char recordnumber[2];
        mytagbuf_read(mypiccdata,revstrlen,recordnumber);
        QString str =   QString::fromLocal8Bit(reinterpret_cast<const char *>(mypiccdata));
        ui->plainTextEdit_taginf->setPlainText(str);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2276215.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java 如何传参xml调用接口获取数据

传参和返参的效果图如下&#xff1a; 传参&#xff1a; 返参&#xff1a; 代码实现&#xff1a; 1、最外层类 /*** 外层DATA类*/ XmlRootElement(name "DATA") public class PointsXmlData {private int rltFlag;private int failType;private String failMemo;p…

2024年度漏洞态势分析报告,需要访问自取即可!(PDF版本)

2024年度漏洞态势分析报告&#xff0c;需要访问自取即可!(PDF版本),大家有什么好的也可以发一下看看

不同音频振幅dBFS计算方法

1. 振幅的基本概念 振幅是描述音频信号强度的一个重要参数。它通常表示为信号的幅度值&#xff0c;幅度越大&#xff0c;声音听起来就越响。为了更好地理解和处理音频信号&#xff0c;通常会将振幅转换为分贝&#xff08;dB&#xff09;单位。分贝是一个对数单位&#xff0c;能…

Nginx反向代理请求头有下划线_导致丢失问题处理

后端发来消息说前端已经发了但是后端没收到请求。 发现是下划线的都没收到&#xff0c;搜索之后发现nginx默认request的header中包含’_’时&#xff0c;会自动忽略掉。 解决方法是&#xff1a;在nginx里的nginx.conf配置文件中的http部分中添加如下配置&#xff1a; unders…

C语言程序环境和预处理详解

本章重点&#xff1a; 程序的翻译环境 程序的执行环境 详解&#xff1a;C语言程序的编译链接 预定义符号介绍 预处理指令 #define 宏和函数的对比 预处理操作符#和##的介绍 命令定义 预处理指令 #include 预处理指令 #undef 条件编译 程序的翻译环境和执行环…

计算机组成原理(1)

系统概述 计算机硬件基本组成早期冯诺依曼机现代计算机 计算机各部分工作原理主存储器运算器控制器计算机工作过程 此文章的图片资源获取来自于王道考研 计算机硬件基本组成 早期冯诺依曼机 存储程序是指将指令以二进制的形式事先输入到计算机的主存储器&#xff0c;然后按照…

基于element UI el-dropdown打造表格操作列的“更多⌵”上下文关联菜单

<template><div :class"$options.name"><el-table :data"tableData"><el-table-column type"index" label"序号" width"60" /><!-- 主要列 BEGIN---------------------------------------- --&g…

Oracle 表分区简介

目录 一. 前置知识1.1 什么是表分区1.2 表分区的优势1.3 表分区的使用条件 二. 表分区的方法2.1 范围分区&#xff08;Range Partitioning&#xff09;2.2 列表分区&#xff08;List Partitioning&#xff09;2.3 哈希分区&#xff08;Hash Partitioning&#xff09;2.4 复合分…

罗永浩再创业,这次盯上了 AI?

罗永浩&#xff0c;1972年7月9日生于中国延边朝鲜族自治州的一个军人家庭&#xff0c;是一名朝鲜族人&#xff1b;早年在新东方授课&#xff0c;2004年当选 “网络十大红人” &#xff1b;2006年8月1日&#xff0c;罗永浩创办牛博网&#xff1b;2008年5月&#xff0c;罗永浩注册…

自然语言处理基础:全面概述

自然语言处理基础&#xff1a;全面概述 什么是NLP及其重要性、NLP的核心组件、NLU与NLG、NLU与NLG的集成、NLP的挑战以及NLP的未来 自然语言处理&#xff08;NLP&#xff09;是人工智能&#xff08;AI&#xff09;中最引人入胜且具有影响力的领域之一。它驱动着我们日常使用的…

WPF系列八:图形控件Path

简介 Path控件支持一种称为路径迷你语言&#xff08;Path Mini-Language&#xff09;的紧凑字符串格式&#xff0c;用于描述复杂的几何图形。这种语言通过一系列命令字母和坐标来定义路径上的点和线段&#xff0c;最终绘制出想要的图形。 绘制任意形状&#xff1a;可以用来绘…

计算机图形学【绘制立方体和正六边形】

工具介绍 OpenGL&#xff1a;一个跨语言的图形API&#xff0c;用于渲染2D和3D图形。它提供了绘制图形所需的底层功能。 GLUT&#xff1a;OpenGL的一个工具库&#xff0c;简化了窗口创建、输入处理和其他与图形环境相关的任务。 使用的函数 1. glClear(GL_COLOR_BUFFER_BIT |…

有限元分析学习——Anasys Workbanch第一阶段笔记(10)桌子载荷案例分析_实际载荷与均布载荷的对比

目录 0 序言 1 桌子案例 2 模型简化 3 方案A 前处理 1&#xff09;分析类型选择 2&#xff09;材料加载 3&#xff09;约束、载荷及接触 4&#xff09;控制网格(网格大小需要根据结果不断调整) 初始计算结果 加密后计算结果 4 方案B、C 前处理 1&#xff09;分析…

Docker compose 使用 --force-recreate --no-recreate 控制重启容器时的行为【后续】

前情&#xff1a;上一篇实际是让AI工具帮我总结了一下讨论的内容&#xff0c;这里把讨论的过程贴出来&#xff0c;这个讨论是为解决实际问题 前文https://blog.csdn.net/wgdzg/article/details/145039446 问题说明&#xff1a; 我使用 docker compose 管理我的容器&#xff0…

Mysql--基础篇--多表查询(JOIN,笛卡尔积)

在MySQL中&#xff0c;多表查询&#xff08;也称为联表查询或JOIN操作&#xff09;是数据库操作中非常常见的需求。通过多表查询&#xff0c;你可以从多个表中获取相关数据&#xff0c;并根据一定的条件将它们组合在一起。MySQL支持多种类型的JOIN操作&#xff0c;每种JOIN都有…

postgresql|数据库|利用sqlparse和psycopg2库批量按顺序执行SQL语句(psyconpg2新优化版本)

一、 旧版批量执行SQL脚本的python文件缺点&#xff0c;优点&#xff0c;以及更新内容 书接上回&#xff0c;postgresql|数据库开发|python的psycopg2库按指定顺序批量执行SQL文件(可离线化部署)_python sql psycopg2-CSDN博客 这个python脚本写了很久了&#xff0c;最近开始…

5个不同类型的数据库安装

各种社区版本下载官方地址&#xff1a;MySQL :: MySQL Community Downloads 一、在线YUM仓库&#xff08;Linux&#xff09; 选择 MySQL Yum Repository 选择对应版本下载仓库安装包&#xff08;No thanks, just start my download.&#xff09; 下载方法1&#xff1a;下载到本…

shell基础使用及vim的常用快捷键

一、shell简介 参考博文1 参考博文2——shell语法及应用 参考博文3——vi的使用 在linux中有很多类型的shell&#xff0c;不同的shell具备不同的功能&#xff0c;shell还决定了脚本中函数的语法&#xff0c;Linux中默认的shell是 / b in/ b a s h &#xff0c;流行的shell…

Spring Data Elasticsearch简介

一、Spring Data Elasticsearch简介 1 SpringData ElasticSearch简介 Elasticsearch是一个实时的分布式搜索和分析引擎。它底层封装了Lucene框架,可以提供分布式多用户的全文搜索服务。 Spring Data ElasticSearch是SpringData技术对ElasticSearch原生API封装之后的产物,它通…

【巨实用】Git客户端基本操作

本文主要分享Git的一些基本常规操作&#xff0c;手把手教你如何配置~ ● 一个文件夹中初始化Git git init ● 为了方便以后提交代码需要对git进行配置&#xff08;第一次使用或者需求变更的时候&#xff09;&#xff0c;告诉git未来是谁在提交代码 git config --global user.na…