Qt_C++读写NFC标签Ntag支持windows国产linux操作系统

news2024/11/24 19:07:06

本示例使用的发卡器:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报/-淘宝网 (taobao.com) 

ntag2标签存储结构说明
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QDebug>
#include "QLibrary"
#include "QMessageBox"

//本示例可在windows、linux系统内编译、运行
//判断windows、linux系统,声明动态库函数----------------------------------------------------------------------------------------------------------------------------------------------
//让读写器发出声音
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("./libOURIDR.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("./libOURIDR.so").resolve("pcdgetdevicenumber");
#endif

//读取卡片的uid
typedef  unsigned char (*piccrequest_ul)(unsigned char *serial);
#ifdef Q_OS_WIN
    piccrequest_ul mypiccrequest_ul = (piccrequest_ul)QLibrary("OUR_MIFARE.dll").resolve("piccrequest_ul");
#else
    piccrequest_ul mypiccrequest_ul = (piccrequest_ul)QLibrary("./libOURIDR.so").resolve("piccrequest_ul");
#endif

//读取卡片4页数据
typedef  unsigned char (*piccread_ul)(unsigned char blockadd,unsigned char *piccdata);
#ifdef Q_OS_WIN
    piccread_ul mypiccread_ul = (piccread_ul)QLibrary("OUR_MIFARE.dll").resolve("piccread_ul");
#else
    piccread_ul mypiccread_ul = (piccread_ul)QLibrary("./libOURIDR.so").resolve("piccread_ul");
#endif

//写1页数据
typedef  unsigned char (*piccwrite_ul)(unsigned char blockadd,unsigned char *piccdata);
#ifdef Q_OS_WIN
    piccwrite_ul mypiccwrite_ul = (piccwrite_ul)QLibrary("OUR_MIFARE.dll").resolve("piccwrite_ul");
#else
    piccwrite_ul mypiccwrite_ul = (piccwrite_ul)QLibrary("./libOURIDR.so").resolve("piccwrite_ul");
#endif

//认证卡片密码
typedef  unsigned char (*piccauthkey_ntag)(unsigned char *picckey,unsigned char *piccntagpack);
#ifdef Q_OS_WIN
    piccauthkey_ntag mypiccauthkey_ntag = (piccauthkey_ntag)QLibrary("OUR_MIFARE.dll").resolve("piccauthkey_ntag");
#else
    piccauthkey_ntag mypiccauthkey_ntag = (piccauthkey_ntag)QLibrary("./libOURIDR.so").resolve("piccauthkey_ntag");
#endif

//读取卡的版本号及型号
typedef  unsigned char (*piccgetversion_ntag)(unsigned char *mypiccversiondata);
#ifdef Q_OS_WIN
    piccgetversion_ntag mypiccgetversion_ntag = (piccgetversion_ntag)QLibrary("OUR_MIFARE.dll").resolve("piccgetversion_ntag");
#else
    piccgetversion_ntag mypiccgetversion_ntag = (piccgetversion_ntag)QLibrary("./libOURIDR.so").resolve("piccgetversion_ntag");
#endif

//读取卡的签字信息
typedef  unsigned char (*piccreadsig_ntag)(unsigned char *piccsigdata);
#ifdef Q_OS_WIN
    piccreadsig_ntag mypiccreadsig_ntag = (piccreadsig_ntag)QLibrary("OUR_MIFARE.dll").resolve("piccreadsig_ntag");
#else
    piccreadsig_ntag mypiccreadsig_ntag = (piccreadsig_ntag)QLibrary("./libOURIDR.so").resolve("piccreadsig_ntag");
#endif

//读取卡操作的单向计数器(卡操作流水号)
typedef  unsigned char (*piccreadcnt_ntag)(unsigned char *mypicccntdata);
#ifdef Q_OS_WIN
    piccreadcnt_ntag mypiccreadcnt_ntag = (piccreadcnt_ntag)QLibrary("OUR_MIFARE.dll").resolve("piccreadcnt_ntag");
#else
    piccreadcnt_ntag mypiccreadcnt_ntag = (piccreadcnt_ntag)QLibrary("./libOURIDR.so").resolve("piccreadcnt_ntag");
#endif

//锁定页数据
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("./libOURIDR.so").resolve("picclock_ntag");
#endif

//初始化卡,开启读写卡密码保护功能
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("./libOURIDR.so").resolve("piccinit_ntag");
#endif

//轻松读卡
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("./libOURIDR.so").resolve("piccreadex_ntag");
#endif

//轻松写卡
typedef  unsigned char (*piccwriteex_ntag)(unsigned char ctrlword,unsigned char *serial,unsigned char *pickey,unsigned char blockadd,unsigned char blocksize,unsigned char *picdata);
#ifdef Q_OS_WIN
    piccwriteex_ntag mypiccwriteex_ntag = (piccwriteex_ntag)QLibrary("OUR_MIFARE.dll").resolve("piccwriteex_ntag");
#else
    piccwriteex_ntag mypiccwriteex_ntag = (piccwriteex_ntag)QLibrary("./libOURIDR.so").resolve("piccwriteex_ntag");
#endif


//检测输入数据是否为16进制数------------------------------------------------------------------------------------------------------------------------------------------------------------
static bool checkinput(QString inputstr){
    QString inputyes="0123456789abcdefABCDEF";
    for(int i=0;i<inputstr.length();i++){
        if(inputyes.contains(inputstr.mid(i,1),Qt::CaseSensitive)){
        }else{return false;}
    }
    return true;
}

//检测动态库文件是否在运行目录内----------------------------------------------------------------------------------------------------------------------------------------------------------
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("./libOURIDR.so");
    if (!mylib.load()){                //判断linux系统下,libOURIDR.so是否正运行目录内
        QMessageBox::information(NULL, "提示", "请将libOURIDR.so文件拷贝到生成的运行文件相同目录下!");
        return false;
    }else{return true;}
#endif
}

//显示返回的错误代码信息-----------------------------------------------------------------------------------------------------------------------------------------------------------------
static void disperrinf(unsigned char result){
    switch (result) {
    case 1:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",0~2块都没读出来,可能刷卡太块。但卡序列号已被读出来!");
        break;
    case 2:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",第0块已被读出,但1~2块读取失败。卡序列号已被读出来!");
        break;
    case 3:
        QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",第0、1块已被读出,但2块读取失败。卡序列号已被读出来!");
        break;
    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_pb_beep_clicked()
{
    if(!checkdllos()){return;}      //判断运行目录内动态库文件是否存在

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


void MainWindow::on_pb_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_pb_request_clicked()
{
    if(!checkdllos()){return;}        //判断运行目录内动态库文件是否存在

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态

    ui->lineEdit_uid->setText("");
    status = mypiccrequest_ul(mypiccserial);
    if(status==0){
        mypcdbeep(30);
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);
    }
    else
    {
        disperrinf(status);
    }
}


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

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态
    unsigned char blockadd;           //读卡起始页地址
    unsigned char mypicdata[16];      //读卡数据缓冲

    ui->lineEdit_uid->setText("");
    ui->plainTextEdit_data->setPlainText("");

    status = mypiccrequest_ul(mypiccserial);
    if(status==0){
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);
        blockadd=ui->spinBox_star_rw->value();
        status = mypiccread_ul(blockadd,mypicdata);
        if(status==0){
            QString datastr="" ;
            for (int i = 0; i < 16; i++) {
                datastr=datastr+QString::asprintf("%02X ",mypicdata[i]);
            }
            ui->plainTextEdit_data->setPlainText(datastr);
            mypcdbeep(30);
            QMessageBox::information(NULL, "提示","读卡成功,卡号:"+uidstr );
        }else{
            disperrinf(status);
        }
    }
    else
    {
        disperrinf(status);
    }
}


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

    //检测写卡数据
    unsigned char piccdatabuf[255]; //写卡数据缓冲
    QChar ch;
    QString writinf = ui->plainTextEdit_data->toPlainText().trimmed();
    QString writinf1 = "";
    int m = 0;
    int n = 0;
    char s;

    if(writinf.length()<1){
        QMessageBox::critical(NULL, "提示", "请输入正确的16进制写卡数据!");
        ui->plainTextEdit_data->setFocus();
        return;
    }

    ch = writinf.at(0);
    s = writinf.at(0).toLatin1();
    for(int i=0; i<writinf.length() ; i++)
    {
        ch = writinf.at(i);
        s = writinf.at(i).toLatin1();
        if((('0' <= s) && (s <= '9')) || (('A' <= s) && (s <= 'F')) || (('a' <= s) && (s <= 'f')))
        {
            writinf1 += ch;
            n++;
            if(n==2){
                bool ok;
                piccdatabuf[m++] = writinf1.toInt(&ok,16);
                writinf1 = "";
                n = 0;
                if(m>=49){
                    break;
                }
            }
        }
        else if(s == ' '){

        }
        else
        {
            QMessageBox::critical(NULL, "提示", "写卡数据错误,请输入正确的16进制数据!");
            ui->plainTextEdit_data->setFocus();
            return;
        }
    }

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态
    unsigned char blockadd;           //写卡起始页地址

    ui->lineEdit_uid->setText("");

    status = mypiccrequest_ul(mypiccserial);
    if(status==0){
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);
        blockadd=ui->spinBox_star_rw->value();
        status = mypiccwrite_ul(blockadd,piccdatabuf);
        if(status==0){            
            mypcdbeep(30);
            QMessageBox::information(NULL, "提示","写卡成功,卡号:"+uidstr );
        }else{
            disperrinf(status);
        }
    }
    else
    {
        disperrinf(status);
    }
}


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

    unsigned char mypickey[4];      //认证密码
    QString keystr=ui->lineEdit_authkey->text().trimmed();
    if(keystr.length()==8 and checkinput(keystr)){
        bool ok;
        for(int i=0;i<4;i++){
            mypickey[i]=QString(keystr.mid(i*2,2)).toInt(&ok,16);
        }
    }else{
        QMessageBox::critical(NULL, "提示","请输入8位16进制卡认证密码!");
        ui->lineEdit_authkey->setFocus();
        return;
    }

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态
    unsigned char mypiccntagpack[2];  //认证后返回的确认码

    ui->lineEdit_uid->setText("");

    status = mypiccrequest_ul(mypiccserial);
    if(status==0){
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);

        status = mypiccauthkey_ntag(mypickey,mypiccntagpack);
        if(status==0){
            mypcdbeep(30);
            QMessageBox::information(NULL, "提示","卡密码认证成功,卡号:"+uidstr );
        }else{
            disperrinf(status);
        }
    }
    else
    {
        disperrinf(status);
    }
}


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

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态
    unsigned char mypiccversiondata[8];      //卡版本及型号缓冲

    ui->lineEdit_uid->setText("");

    status = mypiccrequest_ul(mypiccserial);
    if(status==0){
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);

        status = mypiccgetversion_ntag(mypiccversiondata);
        if(status==0){
            QString datastr="" ;
            for (int i = 0; i < 8; i++) {
                datastr=datastr+QString::asprintf("%02X",mypiccversiondata[i]);
            }
            mypcdbeep(30);
            QMessageBox::information(NULL, "提示","读卡成功,卡号:"+uidstr +",版本型号:"+datastr);
        }else{
            disperrinf(status);
        }
    }
    else
    {
        disperrinf(status);
    }
}


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

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态
    unsigned char piccsigdata[32];    //卡签名信息

    ui->lineEdit_uid->setText("");

    status = mypiccrequest_ul(mypiccserial);
    if(status==0){
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);

        status = mypiccreadsig_ntag(piccsigdata);
        if(status==0){
            QString datastr="" ;
            for (int i = 0; i < 32; i++) {
                datastr=datastr+QString::asprintf("%02X",piccsigdata[i]);
            }
            mypcdbeep(30);
            QMessageBox::information(NULL, "提示","卡签字信息:"+datastr );
        }else{
            disperrinf(status);
        }
    }
    else
    {
        disperrinf(status);
    }
}


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

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态
    unsigned char mypicccntdata[3];   //卡操作计数器缓冲

    ui->lineEdit_uid->setText("");

    status = mypiccrequest_ul(mypiccserial);
    if(status==0){
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);

        status = mypiccreadcnt_ntag(mypicccntdata);
        if(status==0){
            QString datastr="" ;
            for (int i = 0; i < 3; i++) {
                datastr=datastr+QString::asprintf("%02X",mypicccntdata[i]);
            }
            mypcdbeep(30);
            QMessageBox::information(NULL, "提示","读卡成功,卡号:"+uidstr +",卡操作的单向计数器:"+datastr);
        }else{
            if(status==13){
                 QMessageBox::critical(NULL, "提示","卡号:"+uidstr +",可能是计数器功能尚未启用或卡片不支持计数功能!");
            }else{
                disperrinf(status);
            }
        }
    }
    else
    {
        disperrinf(status);
    }
}


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

    int answ=QMessageBox::question (this, "警告", "你确定要开启卡片的静态锁吗 ?锁定后3-15页数据不能再次修改!", QMessageBox::Ok, QMessageBox::Cancel);
    if (answ ==1024){
        unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
        unsigned char status;             //函数返回状态
        unsigned char mypicclockdata[4];  //锁定状态

        ui->lineEdit_uid->setText("");

        status = mypiccrequest_ul(mypiccserial);
        if(status==0){
            QString uidstr="" ;
            for (int i = 0; i < 7; i++) {
                uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
            }
            ui->lineEdit_uid->setText(uidstr);

            mypicclockdata[0] = 0x00;
            mypicclockdata[1] = 0x00;
            mypicclockdata[2] = 0xF9;
            mypicclockdata[3] = 0x00;
            status = mypicclock_ntag(0,mypicclockdata);
            if(status==0){
                mypcdbeep(30);
                QMessageBox::information(NULL, "提示","3~15页静态锁数据写入成功!" );
            }else{
                disperrinf(status);
            }
        }
        else
        {
            disperrinf(status);
        }
    }
}


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

    int answ=QMessageBox::question (this, "警告", "你确定要开启卡片的动态锁吗 ?锁定后16及以后的页数据不能再次修改!", QMessageBox::Ok, QMessageBox::Cancel);
    if (answ ==1024){
        unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
        unsigned char status;             //函数返回状态
        unsigned char mypicclockdata[4];  //锁定状态

        ui->lineEdit_uid->setText("");

        status = mypiccrequest_ul(mypiccserial);
        if(status==0){
            QString uidstr="" ;
            for (int i = 0; i < 7; i++) {
                uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
            }
            ui->lineEdit_uid->setText(uidstr);

            mypicclockdata[0] = 0x01;
            mypicclockdata[1] = 0x01;
            mypicclockdata[2] = 0x00;
            mypicclockdata[3] = 0x00;
            status = mypicclock_ntag(1,mypicclockdata);
            if(status==0){
                mypcdbeep(30);
                QMessageBox::information(NULL, "提示","16之后页动态锁数据写入成功!" );
            }else{
                disperrinf(status);
            }
        }
        else
        {
            disperrinf(status);
        }
    }
}


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

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态
    unsigned char myctrlword;         //写卡控制字
    unsigned char mypiccdata[16];     //卡配置数据
    unsigned char mypickey[4];        //卡片认证密码

    ui->lineEdit_uid->setText("");

    if(ui->checkbox_sele->isChecked()){     //选择先认证卡密码成功再读写卡
        QString authkey=ui->lineEdit_authkey->text().trimmed();
        if(authkey.length()==8 and checkinput(authkey)){
            for(int i=0;i<4;i++){
                bool ok;
                mypickey[i]=QString(authkey.mid(i*2,2)).toInt(&ok,16);
            }
            myctrlword=0x10;
        }else{
            QMessageBox::critical(NULL, "提示","请输入8位16进制认证密码!");
            ui->lineEdit_authkey->setFocus();
            return;
        }
    }else{
        myctrlword=0x00;    //无需认证卡密码
    }

    if(ui->comboBox_init->currentIndex()==0){   //开启卡密码保护功能
        int answ=QMessageBox::question (this, "警告", "    您确定要开启卡片的密码保护功能吗 ?开启密码保护功能后需记住卡片的新密码,否则卡片将报废!", QMessageBox::Ok, QMessageBox::Cancel);
        if (answ !=1024){
            return;
        }
        mypiccdata[0] = 0x00;
        mypiccdata[1] = 0x00;
        mypiccdata[2] = 0x00;
        mypiccdata[3] = ui->spinBox_star->value();      //密码保护起始页地址
        myctrlword=myctrlword+0x01;

        mypiccdata[4] = (ui->spinBox_num->value() % 8); //认证次数
        if(ui->checkBox_readen->isChecked()){
            mypiccdata[4] =mypiccdata[4] +0x80;         //开启读密码保护
        }
        mypiccdata[5] = 0x00;    //启用计数器
        mypiccdata[6] = 0x00;
        mypiccdata[7] = 0x00;
        myctrlword=myctrlword+0x02;

        QString newkey=ui->lineEdit_authkey->text().trimmed();
        if(newkey.length()==8 and checkinput(newkey)){
            for(int i=0;i<4;i++){
                bool ok;
                mypiccdata[8+i]=QString(newkey.mid(i*2,2)).toInt(&ok,16);
            }
            mypiccdata[12] = 0x16;
            mypiccdata[13] = 0x16;
            mypiccdata[14] = 0x00;
            mypiccdata[15] = 0x00;
            myctrlword=myctrlword+0x04;

        }else{
            QMessageBox::critical(NULL, "提示","请输入8位16进制认证密码!");
            return;
        }
    }else{      //取消卡密码保护功能
        mypiccdata[0] = 0x00;
        mypiccdata[1] = 0x00;
        mypiccdata[2] = 0x00;
        mypiccdata[3] = 0xff;
        myctrlword=myctrlword+0x01;

        mypiccdata[4] = 0x00;
        mypiccdata[5] = 0x00;
        mypiccdata[6] = 0x00;
        mypiccdata[7] = 0x00;
        myctrlword=myctrlword+0x02;
    }

    status = mypiccinit_ntag(myctrlword,mypiccserial,mypickey,mypiccdata);
    if(status==0){
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);
        mypcdbeep(30);
        QMessageBox::information(NULL, "提示","卡片初始化成功,卡号:"+uidstr );
    }
    else
    {
        disperrinf(status);
    }
}


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

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态
    unsigned char myctrlword;         //写卡控制字
    unsigned char mypicdata[48];      //读卡数据缓冲
    unsigned char mypickey[4];        //卡片认证密码
    unsigned char blockadd;           //操作超始页地址
    unsigned char blocksize;          //操作页数

    if(ui->spinBox_num_rw->value()>12 || ui->spinBox_num_rw->value()<1){
        QMessageBox::critical(NULL, "提示","一次最多只能读12页!超过12页请分次读取。");
        return;
    }

    ui->lineEdit_uid->setText("");
    ui->plainTextEdit_data->setPlainText("");

    if(ui->checkbox_sele->isChecked()){     //选择先认证卡密码成功再读写卡
        QString authkey=ui->lineEdit_authkey->text().trimmed();
        if(authkey.length()==8 and checkinput(authkey)){
            for(int i=0;i<4;i++){
                bool ok;
                mypickey[i]=QString(authkey.mid(i*2,2)).toInt(&ok,16);
            }
            myctrlword=0x10;
        }else{
            QMessageBox::critical(NULL, "提示","请输入8位16进制认证密码!");
            ui->lineEdit_authkey->setFocus();
            return;
        }
    }else{
        myctrlword=0x00;    //无需认证卡密码
    }

    blockadd=ui->spinBox_star_rw->value();
    blocksize=ui->spinBox_num_rw->value();
    status = mypiccreadex_ntag(myctrlword, mypiccserial, mypickey, blockadd, blocksize, mypicdata);
    if(status==0){
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);
        QString datastr="" ;
        for (int i = 0; i <  blocksize*4; i++) {
            datastr=datastr+QString::asprintf("%02X ",mypicdata[i]);
        }
        ui->plainTextEdit_data->setPlainText(datastr);
        mypcdbeep(30);
        QMessageBox::information(NULL, "提示","读卡成功,卡号:"+uidstr );
    }
    else
    {
        disperrinf(status);
    }
}


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

    unsigned char mypiccserial[7];    //卡唯一序列号uid缓冲
    unsigned char status;             //函数返回状态
    unsigned char myctrlword;         //写卡控制字
    unsigned char mypickey[4];        //卡片认证密码
    unsigned char blockadd;           //操作超始页地址
    unsigned char blocksize;          //操作页数

    if(ui->spinBox_num_rw->value()>11 || ui->spinBox_num_rw->value()<1){
        QMessageBox::critical(NULL, "提示","一次最多只能写11页!超过11页请分次写入。");
        return;
    }

    //检测写卡数据
    unsigned char piccdatabuf[255]; //写卡数据缓冲
    QChar ch;
    QString writinf = ui->plainTextEdit_data->toPlainText().trimmed();
    QString writinf1 = "";
    int m = 0;
    int n = 0;
    char s;

    if(writinf.length()<1){
        QMessageBox::critical(NULL, "提示", "请输入正确的16进制写卡数据!");
        ui->plainTextEdit_data->setFocus();
        return;
    }

    ch = writinf.at(0);
    s = writinf.at(0).toLatin1();
    for(int i=0; i<writinf.length() ; i++)
    {
        ch = writinf.at(i);
        s = writinf.at(i).toLatin1();
        if((('0' <= s) && (s <= '9')) || (('A' <= s) && (s <= 'F')) || (('a' <= s) && (s <= 'f')))
        {
            writinf1 += ch;
            n++;
            if(n==2){
                bool ok;
                piccdatabuf[m++] = writinf1.toInt(&ok,16);
                writinf1 = "";
                n = 0;
                if(m>=49){
                    break;
                }
            }
        }
        else if(s == ' '){

        }
        else
        {
            QMessageBox::critical(NULL, "提示", "写卡数据错误,请输入正确的16进制数据!");
            ui->plainTextEdit_data->setFocus();
            return;
        }
    }

    ui->lineEdit_uid->setText("");

    if(ui->checkbox_sele->isChecked()){     //选择先认证卡密码成功再读写卡
        QString authkey=ui->lineEdit_authkey->text().trimmed();
        if(authkey.length()==8 and checkinput(authkey)){
            for(int i=0;i<4;i++){
                bool ok;
                mypickey[i]=QString(authkey.mid(i*2,2)).toInt(&ok,16);
            }
            myctrlword=0x10;
        }else{
            QMessageBox::critical(NULL, "提示","请输入8位16进制认证密码!");
            ui->lineEdit_authkey->setFocus();
            return;
        }
    }else{
        myctrlword=0x00;    //无需认证卡密码
    }

    blockadd=ui->spinBox_star_rw->value();
    blocksize=ui->spinBox_num_rw->value();
    status = mypiccwriteex_ntag(myctrlword, mypiccserial, mypickey, blockadd, blocksize, piccdatabuf);
    if(status==0){
        QString uidstr="" ;
        for (int i = 0; i < 7; i++) {
            uidstr=uidstr+QString::asprintf("%02X",mypiccserial[i]);
        }
        ui->lineEdit_uid->setText(uidstr);
        mypcdbeep(30);
        QMessageBox::information(NULL, "提示","写卡成功,卡号:"+uidstr );
    }
    else
    {
        disperrinf(status);
    }
}

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

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

相关文章

IMU评估肌肉骨骼疾病风险

马来西亚是世界上最大的棕榈油生产国之一。棕榈工人使用凿子和镰刀收割棕榈树的动作&#xff0c;存在引起肌肉骨骼疾病(WMSDs)的风险。但大多数研究采用间接测量和定性方法来确定疼痛分布&#xff0c;这并不精确。在可穿戴传感器技术的最新进展中&#xff0c;惯性运动单元(IMU)…

9.26 牛客Java题库day 3

1.类变量&#xff08;static&#xff09;在不设置初始值时,会进行默认值赋值&#xff0c;而局部方法中声明的变量则必须进行初始化&#xff0c;它不会进行默认值赋值 2.了解forward,redirect: URL:统一资源定位符&#xff0c;又是也被俗称为网页地址 http://www.runoob.com/…

python 探索分形世界|曼德布洛特|np.frompyfunc()

文章目录 分形的重要特征曼德布洛特集合曼德布洛特集合有一个以证明的结论&#xff1a;图像展示np.ogrid[]np.frompyfunc()集合转图像 julia集合 无边的奇迹源自简单规则的无限重复 ---- 分形之父Benoit B.Mandelbrot 分形的重要特征 自相似性无标度性非线性 曼德布洛特集合…

大疆御3(DJI Mavic 3)照片格式,设置默认JPG格式

大疆御3(DJI Mavic 3)照片格式&#xff0c;设置默认JPG格式 一、照片格式。 御3提供两种照片格式&#xff0c;一种是常见的JPG格式&#xff1b;还有一种是DNG格式&#xff0c;这是一种无人机拍摄照片的原始格式&#xff0c;具有较高的图像质量和更多的后期处理空间&#xff0…

【乳腺超声、乳腺钼靶、宫颈癌】等项目数据调研,及相关参考内容整理汇总

一、乳腺超声内容整理 1.1、数据集 Breast Ultrasound Images Dataset;下载地址2STU-Hospital处理和训练参考文档:https://blog.csdn.net/weixin_51511389/article/details/127594654 1.2、可以参考的论文 AAU-net: An Adaptive Attention U-net for Breast Lesions Segmen…

GeoServer运行报错503,……Unmapped relationship: 7

Windows11运行GeoServer-2.19.0报错[org.geoserver.system.status.OSHISystemInfoCollector]……Unmapped relationship: 7 问题说明解决方法 问题说明 最近换了新电脑&#xff0c;在电脑上安装了一个geoserver2.19.0版本&#xff0c;但是运行就是报错&#xff0c;虽然最后提示…

乱收费被市场惩罚,互联网电视被用户抛弃,传统电视再度崛起!

洛图科技&#xff08;RUNTO&#xff09;公布了8月份国内电视市场的数据&#xff0c;数据显示互联网电视/智能电视被消费者抛弃导致出货量大跌&#xff0c;而传统电视品牌则获得了认可&#xff0c;显示出互联网电视乱收费正被市场惩罚。 洛图科技&#xff08;RUNTO&#xff09;公…

CV经典任务(一) 语义分割、实例分割 | 全卷积

文章目录 1 语义分割1.1 思路1 滑动窗口1.2 思路2 全卷积网络 2 代码实现3 实例分割 之前讲了分类 实际中除了分类还有几大视觉任务 语义分割&#xff0c;实例分割&#xff0c;目标检测 以上任务基本也都基于前面讲的卷积网络去做的 1 语义分割 语义分割&#xff08;Semant…

基于微信小程序的大学生科技竞赛竞技报名系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言系统主要功能&#xff1a;具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09;有保障的售后福利 代码参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计…

【高阶数据结构】红黑树(C++实现)

⭐博客主页&#xff1a;️CS semi主页 ⭐欢迎关注&#xff1a;点赞收藏留言 ⭐系列专栏&#xff1a;C进阶 ⭐代码仓库&#xff1a;C进阶 家人们更新不易&#xff0c;你们的点赞和关注对我而言十分重要&#xff0c;友友们麻烦多多点赞&#xff0b;关注&#xff0c;你们的支持是我…

Docker(三)、Dockerfile探究

Dockerfile探究 一、镜像层概念1、通过执行命令显化docker的机制 二、Dockerfile基础命令1、FROM 基于基准镜像【即构建镜像的时候&#xff0c;依托原有镜像做拓展】2、LABEL & MAINTAINER -说明信息3、WORKDIR 设置工作目录4、ADD & COPY 复制文件5、ENV 设置环境常量…

Java面向对象高级

文章目录 面向对象高级Object类的常用方法常用方法一&#xff08;面向对象阶段&#xff09;** 和 equals 的区别** 关键字native**单例设计模式&#xff08;Singleton&#xff09;**前情回顾&#xff08;学习基础&#xff09;静态修饰符Static设计模式概念开发步骤**两种实现方…

标准化、逻辑回归、随机梯度参数估计

机器学习入门 数据预处理&#xff1a; 将&#xff1f;替换为缺失值 data data.replace(to_replace"?",valuenp.nan)丢掉缺失值 data.dropna(how"any) #howall删除全是缺失值的行和列 #haowany删除有缺失值的行和列将数据集划分成测试集和训练集 data[colu…

自动混剪多段视频、合并音频、添加文案的技巧分享

在如今的社交媒体时代&#xff0c;视频的重要性越来越被人们所重视。许多人喜欢记录生活中的美好瞬间&#xff0c;并将其制作成视频分享给朋友和家人。然而&#xff0c;对于那些拍摄了大量视频的人来说&#xff0c;一个一个地进行剪辑和合并可能是一项令人头痛的任务。但是&…

Vue3最佳实践 第五章 Vue 组件应用 4 ( provide 和 inject )

5.5 provide 和 inject 前面的知识告诉我们vue中组件之间传递值需要使用props来完成&#xff0c;但是props也有一定局限性。这个时候在vue3中还有另外的解决方法。那就是使用 provide 和 inject 允许父组件将数据传递给所有后代组件&#xff0c;而不管组件层次结构有多深。你要…

CSS之伪类和伪元素 | :before和::before

例子&#xff1a; & 表示嵌套的上一级。如 &:hover 相当于 上一级元素:hover :hover 伪类 :before 伪元素&#xff0c;在元素之前加入某内容&#xff08;一定要写 content &#xff09; display:none&#xff1b; 隐藏对象。display隐藏元素后&#xff0c;不占原先位置…

如何开发物联网 APP?

如何开发物联网 APP? 这个问题本身是不严谨的&#xff0c;APP只是手机端的一个控制或者用于显示的人机交互页面&#xff0c;物联网是通过传感器&#xff0c;物联网卡等模块把物体接入网络以方便远程监控或者控制等。 你问的应该是怎么开发出来一个远程控制物体的APP吧&#x…

每日一练 | 网络工程师软考真题Day37

1、TCP协议在建立连接的过程中可能处于不同的状态&#xff0c;用netstat命令显示出TCP连接的状态为SYN_SEND&#xff0c;那么这个连接正处于 。 A&#xff0e;监听对方的建立连接请求 B&#xff0e;已主动发出连接建立请求 C&#xff0e;等待对方的连接释放请求 D&#xff…

cocoapods引擎插件所管理的开源库内新增声明文件 对外公开

cocoapods引擎插件所管理的开源库内新增声明文件&#xff0c;供外部业务层直接/间接访问 DemoDemo-Prefix.pch 备注&#xff1a;业务层项目(比如&#xff1a;BaseFramesDemo) target Build settings搜索Search Paths,然后点击它&#xff0c;看到Always Search User Paths&a…

【lesson7】yum的介绍及使用

文章目录 预备工作yum的基本过程yum的操作**yum源问题&#xff1a;****yum三板斧&#xff1a;**yum listyum searchyum list | grepyum installyum install -yyum removeyum remove -y 预备工作 首先有三个问题&#xff1a; 问题解答&#xff1a; 这里我们联想到了手机 问题…