本示例使用的发卡器:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "QDebug"
#include "QLibrary"
#include "QMessageBox"
//本示例可在windows、linux系统内编译、运行
//判断windows、linux系统,声明动态库函数----------------------------------------------------------------------------------------------------------------------------------------------
//让读写器发出声音
typedef unsigned char (*idr_beep)(unsigned long xms);
#ifdef Q_OS_WIN
idr_beep myidr_beep = (idr_beep)QLibrary("OUR_IDR.dll").resolve("idr_beep");
#else
idr_beep myidr_beep = (idr_beep)QLibrary("./libOURIDR.so").resolve("idr_beep");
#endif
//获取发卡器唯一编号
typedef unsigned char (*pcdgetdevicenumber)(unsigned char *devicenumber);
#ifdef Q_OS_WIN
pcdgetdevicenumber mypcdgetdevicenumber = (pcdgetdevicenumber)QLibrary("OUR_IDR.dll").resolve("pcdgetdevicenumber");
#else
pcdgetdevicenumber mypcdgetdevicenumber = (pcdgetdevicenumber)QLibrary("./libOURIDR.so").resolve("pcdgetdevicenumber");
#endif
//读取RFID卡号
typedef unsigned char (*idr_read)(unsigned char *serial);
#ifdef Q_OS_WIN
idr_read myidr_read = (idr_read)QLibrary("OUR_IDR.dll").resolve("idr_read");
#else
idr_read myidr_read = (idr_read)QLibrary("./libOURIDR.so").resolve("idr_read");
#endif
//读取RFID卡号一次,如果卡一直在感应区第二次读取不返回卡号
typedef unsigned char (*idr_read_once)(unsigned char *serial);
#ifdef Q_OS_WIN
idr_read_once myidr_read_once = (idr_read_once)QLibrary("OUR_IDR.dll").resolve("idr_read_once");
#else
idr_read_once myidr_read_once = (idr_read_once)QLibrary("./libOURIDR.so").resolve("idr_read_once");
#endif
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
//检测动态库文件是否在运行目录内----------------------------------------------------------------------------------------------------------------------------------------------------------
static bool checkdllos(){
#ifdef Q_OS_WIN
QLibrary mylib("OUR_IDR.dll");
if (!mylib.load()){ //判断windows系统下,OUR_IDR.dll是否在运行目录内
QMessageBox::information(NULL, "提示", "请将OUR_IDR.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 8:
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;
default:
QMessageBox::critical(NULL, "提示", QString::asprintf("错误代码:%d", result)+",未知的错误信息!");
break;
}
}
void MainWindow::on_pushButton_clicked()//让设备发出声
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
unsigned char status;
if (myidr_beep != NULL){ //是否成功连接上 pcdbeep()函数
status = myidr_beep(30);
if(status==0){
}
else
{
disperrinf(status);
}
} else{
QMessageBox::critical(NULL, "提示", "函数‘idr_beep’未正确载入,请查询DLL文件版本是否正确!");
}
}
void MainWindow::on_pushButton_2_clicked() //读出设备全球唯一的设备编号,作为加密狗使用
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
unsigned char devicenumber[4]; //设备编号缓冲
unsigned char status; //函数返回状态
if (mypcdgetdevicenumber != NULL){ //是否成功连接上 pcdgetdevicenumber()函数
status = mypcdgetdevicenumber(devicenumber);
if(status==0){
if (myidr_beep != NULL){myidr_beep(30);}
QMessageBox::information(NULL, "提示", QString::asprintf("设备编号:%03d-%03d-%03d-%03d", devicenumber[0],devicenumber[1],devicenumber[2],devicenumber[3]));
}
else
{
disperrinf(status);
}
}else{
QMessageBox::critical(NULL, "提示", "函数‘pcdgetdevicenumber’未正确载入,请查询DLL文件版本是否正确!");
}
}
void MainWindow::on_pushButton_3_clicked() //读卡
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
unsigned char serial[5]; //卡编号缓冲
unsigned char status; //函数返回状态
if (myidr_read != NULL){ //是否成功连接上 idr_read()函数
status = myidr_read(serial);
if(status==0){
if (myidr_beep != NULL){myidr_beep(30);}
QString cardstr="16进制卡号:"+QString::asprintf("%02X%02X%02X%02X%02X",serial[0],serial[1],serial[2],serial[3],serial[4]);
unsigned int card10=serial[1]*256*256*256+serial[2]*256*256+serial[3]*256+serial[4];
cardstr=cardstr+"\n10进制卡号:"+QString::asprintf("%010d",card10);
ui->textEdit->setText(cardstr);
}
else
{
disperrinf(status);
}
}else{
QMessageBox::critical(NULL, "提示", "函数‘idr_read’未正确载入,请查询DLL文件版本是否正确!");
}
}
void MainWindow::on_pushButton_4_clicked() //仅读一次,重新取放卡才能读到第二次[返回
{
if(!checkdllos()){return;} //判断运行目录内动态库文件是否存在
unsigned char serial[5]; //卡编号缓冲
unsigned char status; //函数返回状态
if (myidr_read_once != NULL){ //是否成功连接上 idr_read_once()函数
status = myidr_read_once(serial);
if(status==0){
if (myidr_beep != NULL){myidr_beep(30);}
QString cardstr="16进制卡号:"+QString::asprintf("%02X%02X%02X%02X%02X",serial[0],serial[1],serial[2],serial[3],serial[4]);
unsigned int card10=serial[1]*256*256*256+serial[2]*256*256+serial[3]*256+serial[4];
cardstr=cardstr+"\n10进制卡号:"+QString::asprintf("%010d",card10);
ui->textEdit->setText(cardstr);
}
else
{
disperrinf(status);
}
}else{
QMessageBox::critical(NULL, "提示", "函数‘idr_read_once’未正确载入,请查询DLL文件版本是否正确!");
}
}