第一步:新建一个qt C++项目:
在此需要注意,名称和路径不能有中文
第二步:添加控件完成UI布局
第三步:开始写代码
使设备发出滴滴响声
#define BLOCK0_EN 0x01
#define BLOCK1_EN 0x02
#define BLOCK2_EN 0x04
#define NEEDSERIAL 0x08
#define EXTERNKEY 0x10
#define NEEDHALT 0x20
//#define NEEDSERIAL 0x08
#define EXTERNKEY 0x10
//让读写器发出声音
typedef unsigned char (*pcdbeep)(unsigned long xms);
//返回本读写器独一无二的设备编号
typedef unsigned char (*pcdgetdevicenumber)(unsigned long devicenumber);
//轻松读卡
typedef unsigned char (*piccreadex)(unsigned char ctrlword,unsigned char *serial,unsigned char area,unsigned char keyA1B0,unsigned char *picckey,unsigned char *piccdata0_2);
//轻松写卡
typedef unsigned char (*piccwriteex)(unsigned char ctrlword,unsigned char *serial,unsigned char area, unsigned char keyA1B0,unsigned char *picckey,unsigned char *piccdata0_2);
//改单区密码
typedef unsigned char (*piccchangesinglekeyex)(unsigned char ctrlword,unsigned char *serial,unsigned char area,unsigned char keyA1B0,unsigned char *piccoldkey,unsigned char *piccdata);
//读出一块的数据,也就是16个字节
typedef unsigned char (*piccread)(unsigned char block,unsigned char *piccdata);
//检测输入数据是否为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;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_4_clicked()//让设备发出声响
{
unsigned char result;
QLibrary mylib("OUR_MIFARE.dll");
if (!mylib.load()) //判断是否正确加载
{
QMessageBox::information(NULL, "提示", "动态库没装载!原因是动态库OUR_MIFARE.dll没放在生成exe文件相同目录下!");
return;
}
pcdbeep mypcdbeep = (pcdbeep)mylib.resolve("pcdbeep");
if (mypcdbeep == NULL) //是否成功连接上 idr_beep()函数
{
mylib.unload();
QMessageBox::information(NULL, "提示", "请检查函数名是否正确!");
return;
}
result = mypcdbeep(30); //这里函数指针调用dll中的idr_beep() 函数
mylib.unload();
if(result > 0)
{
if(result == 23)
{
QMessageBox::information(NULL, "提示", "机器没连上或者驱动没下载");
}
else
{
QMessageBox::information(NULL, "提示", "错误代码:" + QString::number(result));
};
}
//qDebug()<<result;
}
读卡功能
void MainWindow::on_pushButton_3_clicked()//轻松读卡
{
unsigned char ctrlword;//控制字
unsigned char serial[4];//可写数组
unsigned char area;//读出的区号
unsigned char keyA1B0;//AB密码认证
unsigned char picckey[6];//存放卡密码的数组
unsigned char piccdata0_2[48];//下标0~15存放作为读出的块0的数据,下标16~31存放作为读出的块1的数据,下标32~47存放作为读出的块2的数据
unsigned char status;//返回
unsigned char status1;
unsigned char result;
QString piccdata[16]; //卡数据缓冲
//unsigned char myareano = ui->lineEdit_5 ; '指定为第8区
//以下控制字含义:读块0、块1、块2,仅读指定序列号的卡,需要每次指定密码
if (ui->radioButton->isChecked())
{
ctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY;
}
else
{//内部密码
ctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN;
}
area = ui->comboBox->currentText().toUShort();
keyA1B0 = 1;//用A密码
//qDebug()<<area;
//qDebug()<<ui->lineEdit->text().mid(0,2);
//qDebug()<<ui->lineEdit->text().mid(2,2);
// qDebug()<<ui->lineEdit->text().mid(2,2);
// qDebug()<<ui->lineEdit->text().mid(2,2);
QString Key_str = ui->lineEdit->text().trimmed();
if(Key_str.length() < 12)
{
QMessageBox::information(NULL, "提示", "密码长度不够12位!");
ui->lineEdit->setFocus();
return;
}
char c;
for (int var = 0; var < 12; ++var)
{
c = Key_str.at(var).toLatin1();
if(!((('0' <= c) && (c <= '9')) || (('A' <= c) && (c <= 'F')) || (('a' <= c) && (c <= 'f'))))
{
QMessageBox::information(NULL, "提示", "密码输入字符不正确!");
//ui->lineEdit->setSelection(var,1);
ui->lineEdit->setSelection(var,1);
ui->lineEdit->setFocus();
return;
}
}
bool ok;
// picckey[0]=(unsigned char)(Key_str.mid(0,2).toUShort(nullptr,16));
// picckey[1] = Key_str.mid(2,2).toShort();
// picckey[2] = Key_str.mid(4,2).toShort();
// picckey[3] = Key_str.mid(6,2).toShort();
// picckey[4] = Key_str.mid(8,2).toShort();
// picckey[5] = Key_str.mid(10,2).toShort();
for(int i = 0; i < 6; i++)
{
picckey[i]=QString(Key_str.mid(i*2,2)).toInt(&ok,16);
}
QLibrary mylib("OUR_MIFARE.dll");
if (!mylib.load()) //判断是否正确加载
{
QMessageBox::information(NULL, "提示", "动态库没装载!原因是动态库OUR_MIFARE.dll没放在生成exe文件相同目录下!");
return;
}
piccreadex mypiccreadex = (piccreadex)mylib.resolve("piccreadex");
status = mypiccreadex(ctrlword,serial,area,keyA1B0,picckey,piccdata0_2);
if(status == 0)
{
//qDebug()<<status;
//ui->lineEdit_5->setText(QString::asprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",picckey[0],picckey[1],picckey[2],picckey[3],picckey[4],picckey[5],picckey[6],serial[1],serial[2],serial[3]));
ui->lineEdit_2->setText(QString::asprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X ",piccdata0_2[0],piccdata0_2[1],piccdata0_2[2],piccdata0_2[3],piccdata0_2[4],piccdata0_2[5],piccdata0_2[6],piccdata0_2[7],piccdata0_2[8],piccdata0_2[9],piccdata0_2[10],piccdata0_2[11],piccdata0_2[12],piccdata0_2[13],piccdata0_2[14],piccdata0_2[15]));
ui->lineEdit_3->setText(QString::asprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X ",piccdata0_2[16],piccdata0_2[17],piccdata0_2[18],piccdata0_2[19],piccdata0_2[20],piccdata0_2[21],piccdata0_2[22],piccdata0_2[23],piccdata0_2[24],piccdata0_2[25],piccdata0_2[26],piccdata0_2[27],piccdata0_2[28],piccdata0_2[29],piccdata0_2[30],piccdata0_2[31]));
ui->lineEdit_4->setText(QString::asprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X ",piccdata0_2[32],piccdata0_2[33],piccdata0_2[34],piccdata0_2[35],piccdata0_2[36],piccdata0_2[37],piccdata0_2[38],piccdata0_2[39],piccdata0_2[40],piccdata0_2[41],piccdata0_2[42],piccdata0_2[43],piccdata0_2[44],piccdata0_2[45],piccdata0_2[46],piccdata0_2[47]));
piccread mypiccread = (piccread)mylib.resolve("piccread");
//typedef unsigned char (*piccread)(unsigned char block,unsigned char *piccdata);
status1 = piccread(area * 4 + 3,piccdata);
pcdbeep mypcdbeep = (pcdbeep)mylib.resolve("pcdbeep");
if (mypcdbeep == NULL) //是否成功连接上 idr_beep()函数
{
mylib.unload();
QMessageBox::information(NULL, "提示", "请检查函数名是否正确!");
return;
}
result = mypcdbeep(30); //这里函数指针调用dll中的idr_beep() 函数
mylib.unload();
}
else if(status == 8)
{
ui->lineEdit_2->clear();
ui->lineEdit_3->clear();
ui->lineEdit_4->clear();
QMessageBox::information(NULL, "提示", "寻卡错误,根本就没有卡在感应区");
}
else if(status == 12)
{
ui->lineEdit_2->clear();
ui->lineEdit_3->clear();
ui->lineEdit_4->clear();
QMessageBox::information(NULL, "提示", "密码认证失败");
}
//qDebug()<<result;
//qDebug()<<status;
}