信创系统基于QT5操作NFC读写器

在信创系统,如麒麟和统信中,使用QT工具编程操作NFC读写器YW-607HC.

首先编排界面ui

cpp 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>YOWORFID Linux Demo</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTabWidget" name="tabWidget">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>60</y>
      <width>741</width>
      <height>291</height>
     </rect>
    </property>
    <property name="currentIndex">
     <number>0</number>
    </property>
    <widget class="QWidget" name="tab">
     <property name="enabled">
      <bool>true</bool>
     </property>
     <attribute name="title">
      <string>Tab 1</string>
     </attribute>
     <widget class="QLineEdit" name="edtUID">
      <property name="geometry">
       <rect>
        <x>150</x>
        <y>10</y>
        <width>191</width>
        <height>31</height>
       </rect>
      </property>
     </widget>
     <widget class="QLabel" name="label">
      <property name="geometry">
       <rect>
        <x>90</x>
        <y>20</y>
        <width>55</width>
        <height>21</height>
       </rect>
      </property>
      <property name="text">
       <string> UID</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_2">
      <property name="geometry">
       <rect>
        <x>100</x>
        <y>80</y>
        <width>55</width>
        <height>21</height>
       </rect>
      </property>
      <property name="text">
       <string>KeyAB</string>
      </property>
     </widget>
     <widget class="QRadioButton" name="rbKeyA">
      <property name="geometry">
       <rect>
        <x>150</x>
        <y>80</y>
        <width>61</width>
        <height>27</height>
       </rect>
      </property>
      <property name="text">
       <string>Key A</string>
      </property>
      <property name="checked">
       <bool>true</bool>
      </property>
     </widget>
     <widget class="QRadioButton" name="rbKeyB">
      <property name="geometry">
       <rect>
        <x>210</x>
        <y>80</y>
        <width>51</width>
        <height>27</height>
       </rect>
      </property>
      <property name="text">
       <string>Key B</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_3">
      <property name="geometry">
       <rect>
        <x>280</x>
        <y>80</y>
        <width>31</width>
        <height>21</height>
       </rect>
      </property>
      <property name="text">
       <string>Key</string>
      </property>
     </widget>
     <widget class="QLineEdit" name="edtKey">
      <property name="geometry">
       <rect>
        <x>310</x>
        <y>80</y>
        <width>181</width>
        <height>31</height>
       </rect>
      </property>
      <property name="inputMask">
       <string/>
      </property>
      <property name="text">
       <string>FFFFFFFFFFFF</string>
      </property>
      <property name="maxLength">
       <number>12</number>
      </property>
     </widget>
     <widget class="QLabel" name="label_4">
      <property name="geometry">
       <rect>
        <x>110</x>
        <y>127</y>
        <width>31</width>
        <height>21</height>
       </rect>
      </property>
      <property name="text">
       <string>Data</string>
      </property>
     </widget>
     <widget class="QLineEdit" name="edtData">
      <property name="geometry">
       <rect>
        <x>150</x>
        <y>120</y>
        <width>341</width>
        <height>31</height>
       </rect>
      </property>
     </widget>
     <widget class="QPushButton" name="btnRead">
      <property name="geometry">
       <rect>
        <x>190</x>
        <y>160</y>
        <width>111</width>
        <height>51</height>
       </rect>
      </property>
      <property name="text">
       <string>Read</string>
      </property>
     </widget>
     <widget class="QPushButton" name="btnWrite">
      <property name="geometry">
       <rect>
        <x>330</x>
        <y>160</y>
        <width>111</width>
        <height>51</height>
       </rect>
      </property>
      <property name="text">
       <string>Write</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_5">
      <property name="geometry">
       <rect>
        <x>90</x>
        <y>50</y>
        <width>55</width>
        <height>21</height>
       </rect>
      </property>
      <property name="text">
       <string>BlockID</string>
      </property>
     </widget>
     <widget class="QSpinBox" name="edtBlockID">
      <property name="geometry">
       <rect>
        <x>150</x>
        <y>50</y>
        <width>91</width>
        <height>30</height>
       </rect>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="tab_2">
     <attribute name="title">
      <string>Tab 2</string>
     </attribute>
    </widget>
   </widget>
   <widget class="QWidget" name="widget" native="true">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>721</width>
      <height>51</height>
     </rect>
    </property>
    <widget class="QPushButton" name="btnCloseReader">
     <property name="geometry">
      <rect>
       <x>150</x>
       <y>10</y>
       <width>121</width>
       <height>29</height>
      </rect>
     </property>
     <property name="text">
      <string>Close Reader</string>
     </property>
    </widget>
    <widget class="QPushButton" name="btnOpenReader">
     <property name="geometry">
      <rect>
       <x>20</x>
       <y>10</y>
       <width>121</width>
       <height>29</height>
      </rect>
     </property>
     <property name="text">
      <string>Open Reader</string>
     </property>
    </widget>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

界面如图所示:

编写C++代码:

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets/QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->btnOpenReader,&QPushButton::clicked,this, &MainWindow::Click_OpenReader);
    connect(ui->btnCloseReader,&QPushButton::clicked,this, &MainWindow::Click_CloseReader);
    connect(ui->btnRead,&QPushButton::clicked,this, &MainWindow::Click_Read);
    connect(ui->btnWrite,&QPushButton::clicked,this, &MainWindow::Click_Write);
}

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

void MainWindow::BeepOk()
{
    yoworfid.YW_Buzzer(1,5,5,1);
}

void MainWindow::BeepFail()
{
    yoworfid.YW_Buzzer(1,1,1,3);
}

void MainWindow::Click_OpenReader()
{

    int rt =yoworfid.YW_USBHIDInitial();
    if(rt>0)
    {
        MsgBox("Connect OK");

    }
    else
    {
        MsgBox("Connect Fail");
    }

}

void MainWindow::Click_CloseReader()
{
    yoworfid.YW_USBHIDFree();

}

void MainWindow::MsgBox(const QString &Content)
{
    QMessageBox::information(NULL,"YOWORFID",Content);
}

QString MainWindow::HexToStr(unsigned char *Hex, unsigned char Len)
{
    QString ret;
    for(int i=0;i<Len;i++)
    {
        QString a = "0" + QString::number(*Hex,16);
        ret = ret + a.right(2);

        Hex++;
    }
    return ret;
}

int  MainWindow::StrToHexArray(const QString &Str, unsigned char *hex)
{
    int len = Str.length()/2;
    int arraylen=0;
    for(int i=0;i<len;i++)
    {
        char H = Str.at(i*2).toLatin1();
        char L = Str.at(i*2+1).toLatin1();

        *hex = ConvertHexChar(H)*16+ConvertHexChar(L);
        hex++;
        arraylen++;
    }
    return arraylen;

}

int MainWindow::ConvertHexChar(char ch)
    {
        if((ch >= '0') && (ch <= '9'))
         return ch-'0';
        else if((ch >= 'A') && (ch <= 'F'))
         return ch-'A'+10;
        else if((ch >= 'a') && (ch <= 'f'))
         return ch-'a'+10;
        else return (-1);
    }

bool MainWindow::RequestCard(unsigned char *uidlen,unsigned char *UID)
{

    unsigned short ATQA=0;
    unsigned char SAK=0;
    return yoworfid.YW_RequestAntiandSelect(1,0x52,0,&ATQA,&SAK,uidlen, UID)>0;

}
void MainWindow::Click_Read()
{
    unsigned char UID[10];
    unsigned char uidlen=0;

    unsigned char Key[6];
    unsigned char BlockData[16];
    if(!RequestCard(&uidlen, UID))
    {
        ui->edtUID->clear();
        MsgBox("Request Card Fail");
        return;
    }
    QString strUID = HexToStr(&UID[0],uidlen);

    ui->edtUID->setText(strUID);

    int BlockID = ui->edtBlockID->value();
    unsigned char KeyMode = PASSWORD_A;
    if(ui->rbKeyB->isChecked())KeyMode = PASSWORD_B;

    StrToHexArray(ui->edtKey->text(),&Key[0]);

    if(yoworfid.YW_KeyAuthorization(1,KeyMode, BlockID, &Key[0])<0)
    {
        MsgBox("Key Error");
        return;

    }

    if(yoworfid.YW_ReadaBlock(1,BlockID,16,BlockData)<0)
    {
        ui->edtData->clear();
        MsgBox("Read Error");
        return;
    }
    else
    {
        QString strData = HexToStr(&BlockData[0],16);

        ui->edtData->setText(strData);
         BeepOk();
    }


}

void MainWindow::Click_Write()
{
    unsigned char UID[10];
    unsigned char uidlen=0;

    unsigned char Key[6];
    unsigned char BlockData[16];
    if(!RequestCard(&uidlen, UID))
    {
        ui->edtUID->clear();
        MsgBox("Request Card Fail");
        return;
    }
    QString strUID = HexToStr(&UID[0],uidlen);

    ui->edtUID->setText(strUID);

    int BlockID = ui->edtBlockID->value();
    unsigned char KeyMode = PASSWORD_A;
    if(ui->rbKeyB->isChecked())KeyMode = PASSWORD_B;

    StrToHexArray(ui->edtKey->text(),&Key[0]);

    if(yoworfid.YW_KeyAuthorization(1,KeyMode, BlockID, &Key[0])<0)
    {
        MsgBox("Key Error");
        return;

    }

    StrToHexArray(ui->edtData->text(),&BlockData[0]);
    if(yoworfid.YW_WriteaBlock(1,BlockID,16,BlockData)<0)
    {

        MsgBox("Write Error");
        return;
    }
    else
    {
         BeepOk();
         MsgBox("Write OK");
    }


}

这样一个具有智能卡基本读写功能的程序就完成了,编译试试看吧。

相关推荐
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁2 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超2 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int2 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
慧都小项2 天前
当边缘AI上产线:QtitanDocking 如何让工业 HMI 实现多视图协同
人工智能·qt·ui·边缘计算·qt6.3
CoderYanger2 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
伞伞悦读2 天前
【第37期】Python JSON 与配置详解:序列化、反序列化、嵌套结构和配置文件
开发语言·python·json
CCCCCCCCharlie2 天前
Linux进程控制四大核心操作
linux·开发语言