2026年6月4日 commit
This commit is contained in:
@@ -0,0 +1,951 @@
|
||||
#include "rfidfunction.h"
|
||||
#include "rfidmanager.h"
|
||||
|
||||
#include <LogInfo.h>
|
||||
#include <QApplication>
|
||||
#include <QElapsedTimer>
|
||||
#include <QDebug>
|
||||
#include <QMessageBox>
|
||||
#include "runtimectrl.h"
|
||||
|
||||
//#define HLPDEBUG
|
||||
|
||||
|
||||
RFIDManager::RFIDManager()
|
||||
{
|
||||
comForRFID_init();
|
||||
}
|
||||
|
||||
void RFIDManager::CheckSumOut(UCHAR *buf, UCHAR len)
|
||||
{
|
||||
UCHAR i;
|
||||
UCHAR checksum;
|
||||
checksum = 0;
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
for (i = 0; i < (len - 1); i++)
|
||||
{
|
||||
checksum ^= buf[i];
|
||||
}
|
||||
buf[len - 1] = (UCHAR)~checksum;
|
||||
}
|
||||
|
||||
bool RFIDManager::CheckSumIn(UCHAR *buf, UCHAR len)
|
||||
{
|
||||
UCHAR i;
|
||||
UCHAR checksum;
|
||||
checksum = 0;
|
||||
if (len == 0) return false;
|
||||
for (i = 0; i < (len - 1); i++)
|
||||
{
|
||||
checksum ^= buf[i];
|
||||
}
|
||||
if (buf[len - 1] == (UCHAR)~checksum)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void RFIDManager::MySleep(unsigned int msec, int expSize)
|
||||
{
|
||||
// QTime dieTime = QTime::currentTime().addMSecs(msec);
|
||||
// while( QTime::currentTime() < dieTime ) {
|
||||
// QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
||||
// }
|
||||
|
||||
// QByteArray recResponse;
|
||||
RFIDReadData.clear();
|
||||
QElapsedTimer elapsedTimer;
|
||||
elapsedTimer.start();
|
||||
while (!elapsedTimer.hasExpired(msec)) {
|
||||
if (RFIDReadData.size() >= expSize) {
|
||||
// recResponse = comForRFID->read(8);
|
||||
break;
|
||||
}
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
|
||||
}
|
||||
}
|
||||
|
||||
void RFIDManager::Hex2Str(const UCHAR *sSrc, UCHAR *sDest, int nSrcLen)
|
||||
{
|
||||
int i;
|
||||
char szTmp[3];
|
||||
for (i = 0; i < nSrcLen; i++)
|
||||
{
|
||||
sprintf(szTmp, "%02X", (unsigned char)sSrc[i]);
|
||||
memcpy(&sDest[i * 2], szTmp, 2);
|
||||
}
|
||||
sDest[nSrcLen * 2] = '\0';
|
||||
return;
|
||||
}
|
||||
|
||||
void RFIDManager::HexStrToByte(const UCHAR *source, UCHAR *dest, int sourceLen)
|
||||
{
|
||||
short i;
|
||||
unsigned char highByte, lowByte;
|
||||
|
||||
for (i = 0; i < sourceLen; i += 2)
|
||||
{
|
||||
highByte = toupper(source[i]);
|
||||
lowByte = toupper(source[i + 1]);
|
||||
|
||||
if (highByte > 0x39)
|
||||
highByte -= 0x37;
|
||||
else
|
||||
highByte -= 0x30;
|
||||
|
||||
if (lowByte > 0x39)
|
||||
lowByte -= 0x37;
|
||||
else
|
||||
lowByte -= 0x30;
|
||||
|
||||
dest[i / 2] = (highByte << 4) | lowByte;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void RFIDManager::comForRFID_init()
|
||||
{
|
||||
if (comForRFID) {
|
||||
delete comForRFID;
|
||||
comForRFID = nullptr;
|
||||
}
|
||||
|
||||
checkCardIntervalTime = 0;
|
||||
setICReaderModeFlag = false;
|
||||
|
||||
comForRFID = new QSerialPort();
|
||||
|
||||
#ifndef HLPDEBUG
|
||||
comForRFID->setPortName("/dev/ttyAS1");
|
||||
#else
|
||||
comForRFID->setPortName("/dev/ttyUSB0");
|
||||
#endif
|
||||
|
||||
comForRFID->setBaudRate(9600);
|
||||
comForRFID->setDataBits(QSerialPort::Data8);
|
||||
comForRFID->setParity(QSerialPort::NoParity);
|
||||
comForRFID->setStopBits(QSerialPort::OneStop);
|
||||
comForRFID->setFlowControl(QSerialPort::NoFlowControl);
|
||||
if (comForRFID->open(QIODevice::ReadWrite)) {
|
||||
qDebug() << "RFID串口打开成功";
|
||||
connect(comForRFID,SIGNAL(readyRead()),this,SLOT(slt_readRFIDData()));
|
||||
} else {
|
||||
qDebug() << "RFID串口打开失败:" << comForRFID->errorString();
|
||||
delete comForRFID;
|
||||
comForRFID = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
initTimer();
|
||||
}
|
||||
|
||||
int RFIDManager::comReadRFID_data()
|
||||
{
|
||||
UCHAR Cmd[23] = { 0x01, 0x17, 0xA4, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
UCHAR revdata[100];
|
||||
Cmd[1] = 0x08;
|
||||
Cmd[2] = 0xA3;
|
||||
Cmd[4] = 0x06; //读块6
|
||||
CheckSumOut(Cmd, Cmd[1]);
|
||||
QByteArray temp;
|
||||
QByteArray recResponse;
|
||||
temp.append((const char*)Cmd,Cmd[1]);
|
||||
if(comForRFID->isOpen())
|
||||
{
|
||||
LogInfo::setRunStatusText(tr("readB6Req"));
|
||||
comForRFID->clear(QSerialPort::Input);
|
||||
comForRFID->write(temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug()<<"read uart for RFID not open!";
|
||||
}
|
||||
|
||||
MySleep(2000, 0x16);
|
||||
|
||||
int n = RFIDReadData.size();
|
||||
if(n < 0x16)
|
||||
{
|
||||
LogInfo::setRunStatusText("RFID invalied data");
|
||||
return 0;
|
||||
}
|
||||
|
||||
n = std::max(0x16, n);
|
||||
|
||||
if (n > sizeof(revdata)) {
|
||||
return 0; // 防止后面拷贝越界
|
||||
}
|
||||
memcpy(revdata, recResponse.data(), n);
|
||||
if ((revdata[0] == 0x01) && ((revdata[1] == 8)
|
||||
|| (revdata[1] == 22)) && (revdata[1] == n)
|
||||
&& (revdata[2] == 0xA3) && (revdata[3] == 0x20))//判断是否为读数据块返回的数据包
|
||||
{
|
||||
LogInfo::setRunStatusText(tr("readB6suc"));
|
||||
bool status = CheckSumIn(revdata, revdata[1]); //计算校验和
|
||||
if (status)
|
||||
{
|
||||
if (revdata[4] == 0x00) //读数据块成功
|
||||
{
|
||||
UCHAR blockdata[16];
|
||||
UCHAR temp[33];
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
blockdata[i] = revdata[5 + i]; //复制数据到数组
|
||||
}
|
||||
Hex2Str(&blockdata[0], &temp[0], 16); // 数据块数据转换为字符
|
||||
|
||||
strRevdata = QString((const char*)temp);
|
||||
// qDebug() << "读数据块成功,数据块6" << "数据为:" << temp << endl << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
QByteArray RFIDManager::buildMarkCardCommand(int block, int cardType)
|
||||
{
|
||||
UCHAR Cmd[23] = { 0x01, 0x17, 0xA4, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
Cmd[1] = 0x17;
|
||||
Cmd[2] = 0x5B;
|
||||
Cmd[4] = block; //读块6
|
||||
|
||||
//save current date and time
|
||||
QDateTime curDateTime = QDateTime::currentDateTime();
|
||||
QString str = curDateTime.toString("yy:MM:dd:hh:mm:ss");
|
||||
QList<QString> list = str.split(":");
|
||||
for(int i = 0;i < list.size();i++){
|
||||
Cmd[i+6] = list.at(i).toInt();
|
||||
LogInfo::setRunStatusText(tr("Cmd[%1]: %2").arg(i+6).arg(Cmd[i+6]));
|
||||
qDebug()<<"Cmd["<<i+6<<"]: "<<Cmd[i+6];
|
||||
}
|
||||
|
||||
#ifdef HLPDEBUG
|
||||
// 可以让一张卡反复刷
|
||||
if(cardType == 0) {
|
||||
Cmd[6] = 0x10;
|
||||
Cmd[7] = 0x0A;
|
||||
}
|
||||
else if(cardType == 1) {
|
||||
Cmd[6] = 0x10;
|
||||
Cmd[7] = 0x0B;
|
||||
}
|
||||
#endif
|
||||
CheckSumOut(Cmd, Cmd[1]);
|
||||
|
||||
return QByteArray((char*)(const char*)Cmd,Cmd[1]);
|
||||
}
|
||||
|
||||
bool RFIDManager::verifyWriteResponse(const QByteArray &recResponse, QString notice)
|
||||
{
|
||||
// 验证响应
|
||||
if (recResponse.isEmpty()) {
|
||||
//qDebug() << "RFIDReader response time: " << elapsedTimer.elapsed() << "ms";
|
||||
return false;
|
||||
}
|
||||
|
||||
int n = comForRFID->bytesAvailable();
|
||||
if(n < 8) {
|
||||
qDebug() << "读取数据失败, 数据长度不足8字节" << endl << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
UCHAR revdata[100];
|
||||
if (n > sizeof(revdata)) return false;
|
||||
memcpy(revdata, recResponse.constData(), n);
|
||||
|
||||
if ((revdata[0] == 0x01) && (revdata[1] == 8)
|
||||
&& (revdata[1] == n) && (revdata[2] == 0x5B)
|
||||
&& (revdata[3] == 0x20)) //判断是否为写数据返回的数据包
|
||||
{
|
||||
bool status = CheckSumIn(revdata, revdata[1]); //计算校验和
|
||||
if (status && revdata[4] == 0x00)
|
||||
{
|
||||
//写数据块成功
|
||||
qDebug() << notice << "成功!" << endl << endl;
|
||||
LogInfo::setRunStatusText(notice+"成功!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
qDebug() << notice << "失败!" << endl << endl;
|
||||
LogInfo::setRunStatusText(notice+"失败!");
|
||||
return false;
|
||||
}
|
||||
|
||||
RFIDManager *RFIDManager::instance()
|
||||
{
|
||||
static RFIDManager* ins = nullptr;
|
||||
if(ins == nullptr) {
|
||||
ins = new RFIDManager();
|
||||
}
|
||||
|
||||
return ins;
|
||||
}
|
||||
|
||||
int RFIDManager::RFIDmarkCardUsedAsync(int type)
|
||||
{
|
||||
if (!comForRFID || !comForRFID->isOpen()) {
|
||||
qDebug()<<"read uart for RFID not open!";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 构建命令(不硬编码块号) 读块6
|
||||
QByteArray cmd = buildMarkCardCommand(0x06, type);
|
||||
if (cmd.isEmpty()) {
|
||||
qDebug() << "命令构建失败";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 临时断开信号,避免干扰
|
||||
bool wasConnected = disconnect(comForRFID, &QSerialPort::readyRead,
|
||||
this, &RFIDManager::slt_readRFIDData);
|
||||
|
||||
// 清空缓冲区
|
||||
comForRFID->clear(QSerialPort::Input);
|
||||
comForRFID->clear(QSerialPort::Output);
|
||||
|
||||
// 发送命令
|
||||
qint64 written = comForRFID->write(cmd);
|
||||
if (written != cmd.size()) {
|
||||
qCritical() << "发送数据失败,发送字节数:" << written << "预期:" << cmd.size();
|
||||
if (wasConnected) {
|
||||
connect(comForRFID, &QSerialPort::readyRead,
|
||||
this, &RFIDManager::slt_readRFIDData);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
QByteArray recResponse;
|
||||
QElapsedTimer elapsedTimer;
|
||||
elapsedTimer.start();
|
||||
while (!elapsedTimer.hasExpired(2000)) {
|
||||
if (comForRFID->bytesAvailable() >= 8) {
|
||||
recResponse = comForRFID->read(8);
|
||||
break;
|
||||
}
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
||||
}
|
||||
|
||||
// 恢复信号连接
|
||||
if (wasConnected) {
|
||||
connect(comForRFID, &QSerialPort::readyRead,
|
||||
this, &RFIDManager::slt_readRFIDData);
|
||||
}
|
||||
|
||||
bool res = verifyWriteResponse(recResponse, tr("写数据到数据块6"));
|
||||
if (!res) {
|
||||
qDebug() << "响应验证失败";
|
||||
return 0;
|
||||
}
|
||||
qDebug() << "卡片标记成功,块:" << 06 << "耗时:" << elapsedTimer.elapsed() << "ms";
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
void RFIDManager::initRFIDMoudle(unsigned int addr)
|
||||
{
|
||||
setICReaderModeFlag = true;
|
||||
setRFIDReaderWorkMode();
|
||||
setRFIDReaderKey(addr,1);
|
||||
setICReaderModeFlag = false;
|
||||
}
|
||||
|
||||
void RFIDManager::handleCheck(unsigned int addr)
|
||||
{
|
||||
if(!setICReaderModeFlag)
|
||||
{
|
||||
strRevdata ="";
|
||||
receiveReadRFID_data();
|
||||
if(strRevdata.length() > 3)
|
||||
{
|
||||
if((strRevdata.left(3) == QString::number(addr).left(3))\
|
||||
|| strRevdata.left(3) == "100")
|
||||
{
|
||||
if(strRevdata.at(3) == 'A') {
|
||||
if(!SysPara::ins()->RFIDPara.needInkCard && false){
|
||||
emit sig_showMsg(tr("不需要刷墨水卡!"));
|
||||
}
|
||||
else if(SysPara::ins()->status.statWarn & WARN_STATUS_INK_WARN && false){
|
||||
emit sig_showMsg(tr("请先添加墨水再刷墨水卡!"));
|
||||
}
|
||||
else
|
||||
{
|
||||
auto ok = cardTouchConfirm(tr("确认要刷墨水卡吗?"), tr("确认要刷墨水卡吗? %1 秒后将自动确认"));
|
||||
if (!ok){
|
||||
LogInfo::setRunStatusText("The user has canceled the operation about marking the ink card.");
|
||||
}
|
||||
else if(RFIDmarkCardUsedAsync(0) == 1){
|
||||
SysPara::ins()->RFIDPara.needInkCard = false;
|
||||
SysPara::ins()->RFIDPara.inkCardTimeOut = false;
|
||||
SysPara::ins()->saveRFIDPara();
|
||||
emit sig_showMsg(tr("刷墨水卡成功!"));
|
||||
SysPara::ins()->para.inkWarnDate = 0;
|
||||
SysPara::ins()->para.inkWarnTime = 0;
|
||||
SysPara::ins()->saveParaToFile();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(strRevdata.at(3) == 'B')
|
||||
{
|
||||
SysPara::ins()->para.flagOfInkWarning = false;
|
||||
|
||||
if(!SysPara::ins()->RFIDPara.needSolventCard && false){
|
||||
emit sig_showMsg(tr("不需要刷溶剂卡!"));
|
||||
}
|
||||
else if(SysPara::ins()->status.statWarn & WARN_STATUS_SOLV_WARN && false){
|
||||
emit sig_showMsg(tr("请先添加溶剂再刷溶剂卡!"));
|
||||
}
|
||||
else
|
||||
{
|
||||
auto ok = cardTouchConfirm(tr("确认要刷溶剂卡吗?"), tr("确认要刷溶剂卡吗? %1 秒后将自动确认"));
|
||||
if (!ok){
|
||||
LogInfo::setRunStatusText("The user has canceled the operation about marking the solvent card.");
|
||||
}
|
||||
else if(RFIDmarkCardUsedAsync(1) == 1) {
|
||||
SysPara::ins()->RFIDPara.needSolventCard = false;
|
||||
SysPara::ins()->RFIDPara.solventCardTimeOut = false;
|
||||
SysPara::ins()->saveRFIDPara();
|
||||
emit sig_showMsg(tr("刷溶剂卡成功!"));
|
||||
SysPara::ins()->para.flagOfSolventWarning = false;
|
||||
SysPara::ins()->para.solventWarnDate = 0;
|
||||
SysPara::ins()->para.solventWarnTime = 0;
|
||||
SysPara::ins()->saveParaToFile();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else if(strRevdata.at(3) == 'C')
|
||||
{
|
||||
auto ok = cardTouchConfirm(tr("确认要刷保养卡吗?"), tr("确认要刷保养卡吗? %1 秒后将自动确认"));
|
||||
if (ok){
|
||||
auto r = RFIDmarkCardUsedAsync(2);
|
||||
if(r == 1) {//把卡数据清零
|
||||
|
||||
emit sig_maintenanceShow();
|
||||
|
||||
SysPara::ins()->RFIDPara.needMaintenance = false;
|
||||
SysPara::ins()->status.needMaintenanceRemind = false;
|
||||
|
||||
QDate curDate = QDate::currentDate();
|
||||
SysPara::ins()->RFIDPara.maintenanceDate = curDate.toString("yyMMdd").toInt();
|
||||
QDate mtEndDate = curDate.addMonths(SysPara::ins()->RFIDPara.maintenanceInterval);
|
||||
SysPara::ins()->RFIDPara.maintenanceEndDate = mtEndDate.toString("yyMMdd").toInt();
|
||||
SysPara::ins()->RFIDPara.runTime = 0;
|
||||
RunTimeCtrl::instance()->updatePara(&SysPara::ins()->RFIDPara);
|
||||
SysPara::ins()->saveRFIDPara();
|
||||
emit sig_showMsg(tr("刷保养卡成功!"));
|
||||
}
|
||||
else {
|
||||
LogInfo::setRunStatusText("Failed to mark maintenance card!");
|
||||
}
|
||||
}
|
||||
else {
|
||||
qDebug() << "用户取消了刷卡";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
emit sig_showMsg(tr("MAC无效卡!"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
QString str1 = QString::number(strRevdata.mid(0,2).toInt(0,16)).rightJustified(2,'0');
|
||||
QString str2 =QString::number(strRevdata.mid(2,2).toInt(0,16)).rightJustified(2,'0');
|
||||
QString str3 = QString::number(strRevdata.mid(4,2).toInt(0,16)).rightJustified(2,'0');
|
||||
QString str4 = QString::number(strRevdata.mid(6,2).toInt(0,16)).rightJustified(2,'0');
|
||||
QString str5 = QString::number(strRevdata.mid(8,2).toInt(0,16)).rightJustified(2,'0');
|
||||
QString str6 =QString::number(strRevdata.mid(10,2).toInt(0,16)).rightJustified(2,'0');
|
||||
QString strDateTime = str1 + "-" + str2 + "-" + str3 + " " + str4 + ":" + str5 + ":" + str6;
|
||||
QString strShow = tr("无效卡,这张卡已经在%1时使用过").arg(strDateTime);
|
||||
emit sig_showMsg(strShow);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int RFIDManager::setRFIDCarKey()
|
||||
{
|
||||
UCHAR revdata[100];
|
||||
UCHAR Cmd[8] ;
|
||||
Cmd[0] = 0x01; //命令格式请核对协议手册充值章节
|
||||
Cmd[1] = 0x08;
|
||||
Cmd[2] = 0xA5; //加密扇区
|
||||
Cmd[3] = 0x20;
|
||||
Cmd[4] = 0x07; //块7
|
||||
Cmd[5] = 0x01;
|
||||
Cmd[6] = 0x00;
|
||||
Cmd[7] = 0x00;
|
||||
|
||||
CheckSumOut(Cmd, Cmd[1]);
|
||||
QByteArray temp;
|
||||
QByteArray recResponse;
|
||||
temp.append((const char*)Cmd,Cmd[1]);
|
||||
if(comForRFID->isOpen())
|
||||
{
|
||||
comForRFID->clear(QSerialPort::Input);
|
||||
comForRFID->write(temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug()<<"read uart for RFID not open!";
|
||||
}
|
||||
|
||||
MySleep(2000);
|
||||
|
||||
int n = RFIDReadData.size();
|
||||
if(n>2)
|
||||
{
|
||||
recResponse = RFIDReadData;
|
||||
RFIDReadData.clear();
|
||||
QString str;
|
||||
str.sprintf("%d - %d - %d", recResponse.at(0),recResponse.at(1),recResponse.at(2));
|
||||
qDebug()<<"加密IC卡: "<<str;
|
||||
}
|
||||
if (n > sizeof(revdata)) {
|
||||
return 0; // 防止后面拷贝越界
|
||||
}
|
||||
memcpy(revdata, recResponse.data(), n);
|
||||
if ((revdata[0] == 0x01) &&( revdata[1] == 8 )&& (revdata[1] == n) && (revdata[2] == 0xA5) && (revdata[3] == 0x20)) //判断是否为设置地址返回的数据包
|
||||
{
|
||||
bool status = CheckSumIn(revdata, revdata[1]); //计算校验和
|
||||
if (status)
|
||||
{
|
||||
if (revdata[4] == 0x00) //成功
|
||||
{
|
||||
qDebug() << "加密IC卡成功!" << endl << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "加密IC卡失败!" << endl << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "加密IC卡失败2!" << endl << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int RFIDManager::setRFIDReaderKey(int mac,int type)
|
||||
{
|
||||
if (!comForRFID) return;
|
||||
if(mac == 0) mac = 100;
|
||||
QString strMac1 = QString::number(mac).mid(0,3);
|
||||
strMac1 = "Vernon" + strMac1 + "Bach";
|
||||
strMac1 = strMac1.leftJustified(16,'6');
|
||||
QString strMac2 =QString::number(mac).mid(0,3);
|
||||
strMac2 = "VernonBach" + strMac2;
|
||||
strMac2 = strMac2.leftJustified(16,'6');
|
||||
|
||||
QByteArray password = "";
|
||||
if(type == 1)
|
||||
password = RFIDFunction::createPassword((const QByteArray)strMac1.toUtf8());
|
||||
else if(type == 2)
|
||||
password = RFIDFunction::createPassword((const QByteArray)strMac2.toUtf8());
|
||||
const QByteArray pw = password.mid(12,12);
|
||||
|
||||
UCHAR revdata[100];
|
||||
UCHAR Cmd[21] ;
|
||||
Cmd[0] = 0x03; //命令格式请核对协议手册充值章节
|
||||
Cmd[1] = 0x15;
|
||||
Cmd[2] = 0xC3; //设置密钥
|
||||
Cmd[3] = 0x20;
|
||||
Cmd[4] = 0x99;
|
||||
Cmd[5] = 0x88;
|
||||
Cmd[6] = 0x77;
|
||||
Cmd[7] = 0x66;
|
||||
Cmd[8] = 0x55;
|
||||
Cmd[9] = 0x44;
|
||||
Cmd[10] = 0xFF;
|
||||
Cmd[11] = 0x07;
|
||||
Cmd[12] = 0x80;
|
||||
Cmd[13] = 0x69;
|
||||
Cmd[14] = 0xAA;
|
||||
Cmd[15] = 0xBB;
|
||||
Cmd[16] = 0xCC;
|
||||
Cmd[17] = 0xDD;
|
||||
Cmd[18] = 0xEE;
|
||||
Cmd[19] = 0xFF;
|
||||
Cmd[20] = 0x1B;
|
||||
|
||||
#ifndef __RFID_OLD
|
||||
bool ok;
|
||||
for(int i = 0;i<6;i++){
|
||||
int pwInt = Cmd[i+14] = pw.mid(i*2,2).toInt(&ok,16);
|
||||
if (!ok) {
|
||||
qDebug() << "Error: Invalid hex value: "<<pw.mid(i*2,2);
|
||||
return -1;
|
||||
}
|
||||
if(pwInt < 256)
|
||||
Cmd[i+4] = Cmd[i+14] = pwInt;
|
||||
else{
|
||||
qDebug() << "Error: Invalid pwInt value: "<<pwInt;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
CheckSumOut(Cmd, Cmd[1]);
|
||||
QByteArray temp;
|
||||
QByteArray recResponse;
|
||||
temp.append((const char*)Cmd,Cmd[1]);
|
||||
if(comForRFID->isOpen())
|
||||
{
|
||||
comForRFID->clear(QSerialPort::Input);
|
||||
RFIDReadData.clear();
|
||||
comForRFID->write(temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug()<<"read uart for RFID not open!";
|
||||
}
|
||||
|
||||
MySleep(2000);
|
||||
int n = RFIDReadData.size();
|
||||
if(n < 8) {
|
||||
LogInfo::setRunStatusText(tr("设置读写器密码失败!"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
n = std::max(8, n);
|
||||
if (n > sizeof(revdata)) {
|
||||
return 0; // 防止后面拷贝越界
|
||||
}
|
||||
|
||||
memcpy(revdata, recResponse.data(), n);
|
||||
if ((revdata[0] == 0x03) &&( revdata[1] == 8 )&& (revdata[1] == n) && (revdata[2] == 0xC3) && (revdata[3] == 0x20)) //判断是否为设置地址返回的数据包
|
||||
{
|
||||
bool status = CheckSumIn(revdata, revdata[1]); //计算校验和
|
||||
if (status)
|
||||
{
|
||||
if (revdata[4] == 0x00) //成功
|
||||
{
|
||||
qDebug() << "设置读写器密码成功!" << endl << endl;
|
||||
LogInfo::setRunStatusText(tr("设置读写器密码成功!"));
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "设置读写器密码失败!" << endl << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "设置读写器密码失败!" << endl << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RFIDManager::setRFIDReaderOriginalKey(void)
|
||||
{
|
||||
UCHAR revdata[100];
|
||||
UCHAR Cmd[11] ;
|
||||
Cmd[0] = 0x03; //命令格式请核对协议手册充值章节
|
||||
Cmd[1] = 0x0B;
|
||||
Cmd[2] = 0xC4; //设置密钥
|
||||
Cmd[3] = 0x20;
|
||||
Cmd[4] = 0xFF;
|
||||
Cmd[5] = 0xFF;
|
||||
Cmd[6] = 0xFF;
|
||||
Cmd[7] = 0xFF;
|
||||
Cmd[8] = 0xFF;
|
||||
Cmd[9] = 0xFF;
|
||||
Cmd[10] = 0x00;
|
||||
|
||||
CheckSumOut(Cmd, Cmd[1]);
|
||||
QByteArray temp;
|
||||
QByteArray recResponse;
|
||||
temp.append((const char*)Cmd,Cmd[1]);
|
||||
if(comForRFID->isOpen())
|
||||
{
|
||||
comForRFID->clear(QSerialPort::Input);
|
||||
comForRFID->write(temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug()<<"read uart for RFID not open!";
|
||||
}
|
||||
MySleep(2000);
|
||||
int n = RFIDReadData.size();
|
||||
if(n < 8) {
|
||||
LogInfo::setRunStatusText(tr("设置读写器原始密码失败!"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
n = std::max(8, n);
|
||||
if (n > sizeof(revdata)) {
|
||||
return 0; // 防止后面拷贝越界
|
||||
}
|
||||
|
||||
memcpy(revdata, recResponse.data(), n);
|
||||
if ((revdata[0] == 0x03) &&( revdata[1] == 8 )&& (revdata[1] == n) && (revdata[2] == 0xC4) && (revdata[3] == 0x20)) //判断是否为设置地址返回的数据包
|
||||
{
|
||||
bool status = CheckSumIn(revdata, revdata[1]); //计算校验和
|
||||
if (status)
|
||||
{
|
||||
if (revdata[4] == 0x00) //成功
|
||||
{
|
||||
qDebug() << "设置读写器原始密码成功!" << endl << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "设置读写器原始密码失败!" << endl << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "设置读写器原始密码失败!" << endl << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int RFIDManager::setRFIDReaderWorkMode(void)
|
||||
{
|
||||
if (!comForRFID) return;
|
||||
UCHAR revdata[100];
|
||||
UCHAR Cmd[8] ;
|
||||
Cmd[0] = 0x03; //命令格式请核对协议手册充值章节
|
||||
Cmd[1] = 0x08;
|
||||
Cmd[2] = 0xC1; //设置工作模式
|
||||
Cmd[3] = 0x20;
|
||||
Cmd[4] = 0x03;
|
||||
Cmd[5] = 0x06; //读块6
|
||||
Cmd[6] = 0x00; //主动发送数据
|
||||
Cmd[7] = 0x00;
|
||||
|
||||
CheckSumOut(Cmd, Cmd[1]);
|
||||
QByteArray temp;
|
||||
QByteArray recResponse;
|
||||
temp.append((const char*)Cmd,Cmd[1]);
|
||||
if(comForRFID->isOpen())
|
||||
{
|
||||
comForRFID->clear(QSerialPort::Input);
|
||||
comForRFID->write(temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug()<<"read uart for RFID not open!";
|
||||
}
|
||||
// 延时200毫秒等待读写器返回数据,延时太小可能无法接收完整的数据包
|
||||
MySleep(2000);
|
||||
int n = RFIDReadData.size();
|
||||
|
||||
if(n < 8) {
|
||||
LogInfo::setRunStatusText(tr("设置读写器工作模式失败!"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
n = std::max(8, n);
|
||||
if (n > sizeof(revdata)) {
|
||||
return 0; // 防止后面拷贝越界
|
||||
}
|
||||
|
||||
if(n>2)
|
||||
{
|
||||
recResponse = RFIDReadData;
|
||||
RFIDReadData.clear();
|
||||
QString str;
|
||||
str.sprintf("%d - %d - %d", recResponse.at(0),recResponse.at(1),recResponse.at(2));
|
||||
qDebug()<<"设置读写器工作模式: "<<str;
|
||||
}
|
||||
|
||||
// bool res = verifyWriteResponse(recResponse, tr("设置读写器工作模式"));
|
||||
if (n > sizeof(revdata)) {
|
||||
return 0; // 防止后面拷贝越界
|
||||
}
|
||||
memcpy(revdata, recResponse.data(), n);
|
||||
if ((revdata[0] == 0x03) &&( revdata[1] == 8 )
|
||||
&& (revdata[1] == n) && (revdata[2] == 0xC1)
|
||||
&& (revdata[3] == 0x20)) //判断是否为设置地址返回的数据包
|
||||
{
|
||||
bool status = CheckSumIn(revdata, revdata[1]); //计算校验和
|
||||
if (status && revdata[4] == 0x00)
|
||||
{
|
||||
qDebug() << "设置读写器工作模式成功!" << endl << endl;
|
||||
LogInfo::setRunStatusText(tr("设置读写器工作模式成功!"));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
qDebug() << "设置读写器工作模式失败!" << endl << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RFIDManager::receiveReadRFID_data(void)
|
||||
{
|
||||
UCHAR revdata[100];
|
||||
QByteArray recResponse;
|
||||
int n = RFIDReadData.size();
|
||||
if(n>0)
|
||||
{
|
||||
recResponse = RFIDReadData;
|
||||
RFIDReadData.clear();
|
||||
// showData(recResponse);
|
||||
}
|
||||
if (n > sizeof(revdata)) {
|
||||
return 0; // 防止后面拷贝越界
|
||||
}
|
||||
memcpy(revdata, recResponse.data(), n);
|
||||
|
||||
if ((revdata[0] == 0x04) && ((revdata[1] == 8)
|
||||
|| (revdata[1] == 22)) && (revdata[1] == n)
|
||||
&& (revdata[2] == 0x03) && (revdata[3] == 0x20))//判断是否为读数据块返回的数据包
|
||||
{
|
||||
bool status = CheckSumIn(revdata, revdata[1]); //计算校验和
|
||||
if (status)
|
||||
{
|
||||
if (revdata[4] == 0x00) //读数据块成功
|
||||
{
|
||||
UCHAR blockdata[16];
|
||||
UCHAR temp[33];
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
blockdata[i] = revdata[5 + i]; //复制数据到数组
|
||||
}
|
||||
Hex2Str(&blockdata[0], &temp[0], 16); // 数据块数据转换为字符
|
||||
strRevdata = QString((const char*)temp);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RFIDManager::queryReadRFID_address(void)
|
||||
{
|
||||
if (!comForRFID) return;
|
||||
UCHAR Cmd[8] ;
|
||||
Cmd[0] = 0x02; //命令格式请核对协议手册充值章节
|
||||
Cmd[1] = 0x08;
|
||||
Cmd[2] = 0xB0; //查询读写器地址
|
||||
Cmd[3] = 0x00;
|
||||
Cmd[4] = 0x00;
|
||||
Cmd[5] = 0x00;
|
||||
Cmd[6] = 0x00;
|
||||
Cmd[7] = 0x00;
|
||||
CheckSumOut(Cmd, Cmd[1]);
|
||||
QByteArray temp;
|
||||
|
||||
UCHAR revdata[100];
|
||||
|
||||
temp.append((const char*)Cmd,Cmd[1]);
|
||||
if(comForRFID->isOpen())
|
||||
{
|
||||
comForRFID->clear(QSerialPort::Input);
|
||||
RFIDReadData.clear();
|
||||
comForRFID->write(temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug()<<"read uart for RFID not open!";
|
||||
}
|
||||
MySleep(2000);
|
||||
int n = RFIDReadData.size();
|
||||
if(n < 8) {
|
||||
LogInfo::setRunStatusText(tr("查询读写器地址失败"));
|
||||
return 1;
|
||||
}
|
||||
|
||||
n = std::max(8, n);
|
||||
if (n > sizeof(revdata)) {
|
||||
return 0; // 防止后面拷贝越界
|
||||
}
|
||||
|
||||
if (n > sizeof(revdata)) return;
|
||||
memcpy(revdata, RFIDReadData.data(), n);
|
||||
if(n>0)
|
||||
{
|
||||
// showData(RFIDReadData);
|
||||
if(CheckSumIn(revdata, revdata[1]) == false){
|
||||
RFIDReadData.clear();
|
||||
return 0;
|
||||
}
|
||||
if(revdata[0] == 0x02 && revdata[1] == 0x08
|
||||
&& revdata[2] == 0xB0 && revdata[4] == 0x00){
|
||||
RFIDReadData.clear();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else //没有收到数据
|
||||
return 2;
|
||||
|
||||
}
|
||||
|
||||
void RFIDManager::initTimer()
|
||||
{
|
||||
if (RFIDTimer == nullptr) {
|
||||
RFIDTimer = new QTimer(this);
|
||||
connect(RFIDTimer, &QTimer::timeout,
|
||||
this, &RFIDManager::slt_timerHandle);
|
||||
RFIDTimer->start(10 * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
bool RFIDManager::cardTouchConfirm(QString msgTxt, QString variableMsgTxt)
|
||||
{
|
||||
QMessageBox box(QMessageBox::Warning, tr("警告"),
|
||||
msgTxt, QMessageBox::Ok | QMessageBox::Cancel);
|
||||
|
||||
int rfidCountDown = SysPara::ins()->para.rfidCountDown;
|
||||
QTimer msgTimer;
|
||||
QString boxTxt = variableMsgTxt;
|
||||
connect(&msgTimer, &QTimer::timeout, [this, &box, &rfidCountDown, boxTxt]() {
|
||||
box.setText(boxTxt.arg(rfidCountDown));
|
||||
box.update();
|
||||
rfidCountDown--;
|
||||
if(rfidCountDown == 0) {
|
||||
box.accept();
|
||||
}
|
||||
});
|
||||
|
||||
if(SysPara::ins()->para.rfidCountDown > 0) {
|
||||
box.setText(boxTxt.arg(SysPara::ins()->para.rfidCountDown));
|
||||
msgTimer.start(1000);
|
||||
}
|
||||
|
||||
box.exec();
|
||||
|
||||
return box.result() == QMessageBox::Ok || box.result() == QMessageBox::Accepted;
|
||||
}
|
||||
|
||||
void RFIDManager::slt_readRFIDData()
|
||||
{
|
||||
QMutexLocker locker(&m_rfidMutex);
|
||||
RFIDReadData.append(comForRFID->readAll());
|
||||
}
|
||||
|
||||
void RFIDManager::slt_timerHandle()
|
||||
{
|
||||
static int faultyNum = 0;
|
||||
static uint noticeICReaderErrorTime = QDateTime::currentDateTime().toTime_t();
|
||||
if((QDateTime::currentDateTime().toTime_t() - noticeICReaderErrorTime > 7200
|
||||
|| QDateTime::currentDateTime().toTime_t() == noticeICReaderErrorTime)
|
||||
|| (faultyNum > 0 && faultyNum < 10))
|
||||
{
|
||||
noticeICReaderErrorTime = QDateTime::currentDateTime().toTime_t();
|
||||
if(queryReadRFID_address() == 2)
|
||||
{
|
||||
faultyNum++;
|
||||
if(faultyNum >= 10){
|
||||
emit sig_showMsg(tr("13.读卡器故障!"));
|
||||
qDebug()<<"读卡器故障";
|
||||
}
|
||||
}
|
||||
else
|
||||
faultyNum = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
#ifndef RFIDMANAGER_H
|
||||
#define RFIDMANAGER_H
|
||||
|
||||
#include "myinclude.h"
|
||||
#include <QSerialPort>
|
||||
#include <QObject>
|
||||
#include <QTimer>
|
||||
#include <QMutex>
|
||||
|
||||
typedef unsigned char UCHAR;
|
||||
class RFIDManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static RFIDManager* instance();
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief 向卡片写入当前日期时间,标识该卡片已被使用
|
||||
*/
|
||||
int RFIDmarkCardUsedAsync(int type);
|
||||
|
||||
// InkJetPrinter定时器timerHandler触发,检查刷卡状态
|
||||
// 初始化刷卡模块
|
||||
void initRFIDMoudle(unsigned int addr);
|
||||
|
||||
// 300ms检查处理
|
||||
void handleCheck(unsigned int addr);
|
||||
|
||||
private:
|
||||
RFIDManager();
|
||||
|
||||
// 工具类函数
|
||||
void CheckSumOut(UCHAR *buf, UCHAR len);
|
||||
bool CheckSumIn(UCHAR *buf, UCHAR len);
|
||||
void MySleep(unsigned int msec, int expSize = 8);
|
||||
void Hex2Str(const UCHAR *sSrc, UCHAR *sDest, int nSrcLen);
|
||||
void HexStrToByte(const UCHAR* source, UCHAR* dest, int sourceLen);
|
||||
|
||||
// 串口
|
||||
QSerialPort *comForRFID = nullptr;
|
||||
void comForRFID_init();
|
||||
int comReadRFID_data();
|
||||
|
||||
QByteArray buildMarkCardCommand(int block, int cardType);
|
||||
bool verifyWriteResponse(const QByteArray& response, QString notice);
|
||||
|
||||
int setRFIDCarKey(void);
|
||||
int setRFIDReaderKey(int,int);
|
||||
int setRFIDReaderOriginalKey(void);
|
||||
int setRFIDReaderWorkMode(void);
|
||||
int receiveReadRFID_data(void);
|
||||
int queryReadRFID_address(void);
|
||||
|
||||
QMutex m_rfidMutex;
|
||||
QByteArray RFIDReadData;
|
||||
QString strRevdata;
|
||||
bool setICReaderModeFlag;
|
||||
uint checkCardIntervalTime;
|
||||
|
||||
QTimer* RFIDTimer = nullptr;
|
||||
void initTimer();
|
||||
|
||||
bool cardTouchConfirm(QString msgTxt, QString variableMsgTxt);
|
||||
|
||||
signals:
|
||||
void sig_showMsg(QString msg);
|
||||
|
||||
void sig_maintenanceShow();
|
||||
|
||||
private slots:
|
||||
//接收刷卡数据槽函数
|
||||
void slt_readRFIDData();
|
||||
|
||||
//超时处理槽函数
|
||||
void slt_timerHandle();
|
||||
|
||||
};
|
||||
|
||||
#endif // RFIDMANAGER_H
|
||||
Reference in New Issue
Block a user