1428 lines
44 KiB
C++
1428 lines
44 KiB
C++
#include "rfidfunction.h"
|
|
#include "rfidmanager.h"
|
|
|
|
#include <LogInfo.h>
|
|
#include <QApplication>
|
|
#include <QElapsedTimer>
|
|
#include <QDebug>
|
|
#include <QMessageBox>
|
|
#include "runtimectrl.h"
|
|
#include "SysPara.h"
|
|
|
|
// #define HLPDEBUG
|
|
constexpr char* SUPER_CARD_ID = "397F4A8E5B9F6CA07D395B28E4A6";
|
|
|
|
RFIDManager::RFIDManager()
|
|
{
|
|
comForRFID_init();
|
|
|
|
m_confirmBox = new QMessageBox(QMessageBox::Warning, tr("警告"), QString(),
|
|
QMessageBox::Ok | QMessageBox::Cancel);
|
|
m_confirmBox->setWindowModality(Qt::ApplicationModal);
|
|
m_confirmTimer = new QTimer(this);
|
|
|
|
connect(m_confirmTimer, &QTimer::timeout, this, [this]() {
|
|
int count = m_confirmBox->property("rfidCountDown").toInt();
|
|
m_confirmBox->setText(m_confirmBoxTxt.arg(count));
|
|
m_confirmBox->update();
|
|
count--;
|
|
m_confirmBox->setProperty("rfidCountDown", count);
|
|
if(count == 0) {
|
|
m_confirmBox->accept();
|
|
}
|
|
});
|
|
|
|
connect(m_confirmBox, &QMessageBox::accepted, this, [this]() {
|
|
if(m_confirmCallback) {
|
|
m_confirmCallback();
|
|
}
|
|
});
|
|
|
|
connect(m_confirmBox, &QMessageBox::finished, this, [this](int result) {
|
|
m_confirmTimer->stop();
|
|
m_confirmCallback = nullptr;
|
|
});
|
|
}
|
|
|
|
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, 100);
|
|
}
|
|
}
|
|
|
|
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->setPortName("/dev/ttyAS1");
|
|
|
|
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, const QString& originalCardData)
|
|
{
|
|
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
|
|
|
|
// 超级IC卡:数据以397F4A8E5B9F6CA07D395B28E4A6结尾,直接原样写回数据
|
|
bool isSuperCard = !originalCardData.isEmpty()
|
|
&& originalCardData.endsWith(SUPER_CARD_ID);
|
|
|
|
if (isSuperCard) {
|
|
// 超级IC卡:将原始数据原样写回,不写入使用时间
|
|
QByteArray hexData = originalCardData.toLatin1();
|
|
UCHAR dataBytes[16] = {0};
|
|
HexStrToByte((const UCHAR*)hexData.constData(), dataBytes, hexData.size());
|
|
for (int i = 0; i < 16; i++) {
|
|
Cmd[i + 6] = dataBytes[i];
|
|
}
|
|
LogInfo::setRunStatusText(tr("Super IC card, write back original data"));
|
|
qDebug() << "Super IC card detected, writing back original data:" << originalCardData;
|
|
} else {
|
|
//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();
|
|
}
|
|
|
|
#ifdef ARCH_X86
|
|
// 可以让一张卡反复刷
|
|
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];
|
|
int n = recResponse.size();
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @brief 先对所有卡执行余额减一操作,如果余额减一成功,表明这是一张超级卡,否则是一张普通卡
|
|
* 超级卡,写入 SUPER_CARD_ID
|
|
* 普通卡,写入当前日期, 标识该卡片已被使用
|
|
*
|
|
* @param type
|
|
* @param cardData 卡片ID
|
|
* @return int >=0 成功,返回余额;<0 失败。
|
|
*/
|
|
int RFIDManager::RFIDmarkCardUsedAsync(int type, const QString& cardData)
|
|
{
|
|
if (!comForRFID || !comForRFID->isOpen()) {
|
|
qDebug()<<"read uart for RFID not open!";
|
|
return -1;
|
|
}
|
|
|
|
int ret = superCardDeduct();
|
|
LogInfo::setRunStatusText("Card balance: "+QString::number(ret));
|
|
|
|
// -1 表示逻辑错误,结束执行
|
|
if(ret == -1) {
|
|
return -1;
|
|
}
|
|
|
|
// 注意 ret > 0, 含义是,如果余额减一之后,余额为0,即使它是超级卡,也会因为余额用完,被强制标记为普通卡
|
|
bool isSuperCard = (ret > 0 || ret == -11);
|
|
|
|
// 避免重复写入 SUPER_CARD_ID
|
|
if(isSuperCard && cardData.endsWith(SUPER_CARD_ID)) {
|
|
LogInfo::setRunStatusText("Super IC card, already marked as correct, skip write");
|
|
return 0;
|
|
}
|
|
|
|
// 构建命令(不硬编码块号) 读块6
|
|
|
|
// 对于超级卡,强制写入 SUPER_CARD_ID,因为用户可能在旧版软件上使用过此超级卡,导致超级卡的 SUPER_CARD_ID 丢失
|
|
// 对于普通卡,写入当前日期, 标识该卡片已被使用
|
|
QString originData = isSuperCard ? cardData.left(4) + SUPER_CARD_ID : QString("");
|
|
QByteArray cmd = buildMarkCardCommand(0x06, type, originData);
|
|
if (cmd.isEmpty()) {
|
|
qDebug() << "命令构建失败";
|
|
return -1;
|
|
}
|
|
|
|
// 临时断开信号,避免干扰
|
|
bool wasConnected = comForRFID->disconnect(this);
|
|
wasConnected = true;
|
|
|
|
// 清空缓冲区
|
|
comForRFID->clear(QSerialPort::Input);
|
|
comForRFID->clear(QSerialPort::Output);
|
|
|
|
MySleep(200);
|
|
|
|
// 发送命令
|
|
qint64 written = comForRFID->write(cmd);
|
|
if (written != cmd.size()) {
|
|
qCritical() << "发送数据失败,发送字节数:" << written << "预期:" << cmd.size();
|
|
if (wasConnected) {
|
|
connect(comForRFID, &QSerialPort::readyRead,
|
|
this, &RFIDManager::slt_readRFIDData);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
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 -1;
|
|
}
|
|
qDebug() << "卡片标记成功,块:" << 06 << "耗时:" << elapsedTimer.elapsed() << "ms";
|
|
return 0;
|
|
}
|
|
|
|
|
|
int RFIDManager::superCardDeduct()
|
|
{
|
|
uint8_t cmd[] = {0x01, 0x0B, 0xA7, 0x20, 0x04, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};
|
|
CheckSumOut(cmd, cmd[1]);
|
|
|
|
// 临时断开信号,避免干扰
|
|
bool wasConnected = comForRFID->disconnect(this);
|
|
wasConnected = true;
|
|
|
|
// 清空缓冲区
|
|
comForRFID->clear(QSerialPort::Input);
|
|
comForRFID->clear(QSerialPort::Output);
|
|
|
|
// 发送命令
|
|
qint64 written = comForRFID->write((const char*)(cmd), sizeof(cmd));
|
|
if (written != ARRAY_SIZE(cmd)) {
|
|
qCritical() << "发送数据失败,发送字节数:" << written << "预期:" << ARRAY_SIZE(cmd);
|
|
if (wasConnected) {
|
|
connect(comForRFID, &QSerialPort::readyRead,
|
|
this, &RFIDManager::slt_readRFIDData);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
QByteArray recResponse;
|
|
QElapsedTimer elapsedTimer;
|
|
int expectSize = 0;
|
|
elapsedTimer.start();
|
|
while (!elapsedTimer.hasExpired(2000)) {
|
|
if (comForRFID->bytesAvailable() >= 8 && recResponse.size() == 0) {
|
|
recResponse = comForRFID->read(2);
|
|
expectSize = recResponse[1] - 2;
|
|
continue;
|
|
}
|
|
|
|
if(comForRFID->bytesAvailable() >= expectSize && recResponse.size() == 2) {
|
|
recResponse.append(comForRFID->read(expectSize));
|
|
break;
|
|
}
|
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
}
|
|
|
|
qDebug() << "expectSize:" << expectSize + 2;
|
|
|
|
// 恢复信号连接
|
|
if (wasConnected) {
|
|
connect(comForRFID, &QSerialPort::readyRead,
|
|
this, &RFIDManager::slt_readRFIDData);
|
|
}
|
|
|
|
if(recResponse.size() <= 2) {
|
|
LogInfo::setRunStatusText("Response data length error, too short.");
|
|
qWarning() << "Response data length error, too short.";
|
|
return -1;
|
|
}
|
|
|
|
if(CheckSumIn((UCHAR*)(recResponse.data()), recResponse.at(1)) == false) {
|
|
// 校验和错误
|
|
LogInfo::setRunStatusText("Failed to check sum");
|
|
qWarning() << "Failed to check sum";
|
|
return -1;
|
|
}
|
|
|
|
qDebug() << "Super card response data size: " << recResponse.size() << "in content: " << recResponse[1];
|
|
uint8_t recData [10] = {0};
|
|
memccpy(recData, recResponse.data(), sizeof(uint8_t), qMin(recResponse.size(), 10));
|
|
|
|
int remains = 0;
|
|
|
|
// 01 0A A7 20 00 2A 00 00 00 59 成功
|
|
// 01 08 A7 20 01 00 00 70 失败
|
|
if(recResponse.at(1) == 0x0A && recResponse[2] == 0xA7 && recResponse[3] == 0x20 && recResponse[4] == 0x00) {
|
|
// 成功
|
|
// 响应数据是4字节的小端整数
|
|
remains = recResponse[5] | (recResponse[6] << 8) | (recResponse[7] << 16) | (recResponse[8] << 24);
|
|
LogInfo::setRunStatusText(QString("Super card remaining times:%1").arg(remains));
|
|
if(remains < 0) {
|
|
remains -= 100;
|
|
}
|
|
|
|
} else if(recResponse.at(1) == 0x08 && recResponse[2] == 0xA7 && recResponse[3] == 0x20) {
|
|
// 失败
|
|
/*
|
|
0x01:扣款失败
|
|
0x03:扣款成功,读取余额失败
|
|
*/
|
|
if(recResponse[4] == 0x01) {
|
|
LogInfo::setRunStatusText(("Super card times not enough"));
|
|
qWarning() << "Super card times not enough";
|
|
remains = -10;
|
|
} else if(recResponse[4] == 0x03) {
|
|
LogInfo::setRunStatusText(("Super card times deduct success, but remaining times unknown"));
|
|
qWarning() << "Super card times deduct success, but remaining times unknown";
|
|
remains = -11;
|
|
}
|
|
}
|
|
|
|
return remains;
|
|
}
|
|
|
|
void RFIDManager::setInkCardFirstPrompt(bool firstPrompt)
|
|
{
|
|
status.firstPromptInkCard = firstPrompt;
|
|
}
|
|
|
|
void RFIDManager::setSolventCardFirstPrompt(bool firstPrompt)
|
|
{
|
|
status.firstPromptSolventCard = firstPrompt;
|
|
}
|
|
|
|
void RFIDManager::initRFIDMoudle(unsigned int addr, SysPara *sysPara)
|
|
{
|
|
this->sysPara = sysPara;
|
|
setICReaderModeFlag = true;
|
|
setRFIDReaderWorkMode();
|
|
setRFIDReaderKey(addr,1);
|
|
setICReaderModeFlag = false;
|
|
//报警定时器一定要在初始化之后启动,不然就崩
|
|
#ifdef __RFID_CARD
|
|
if (warningTimer)
|
|
warningTimer->start(3000);
|
|
#endif
|
|
}
|
|
|
|
void RFIDManager::handleCheck(unsigned int addr)
|
|
{
|
|
if(!sysPara)
|
|
return;
|
|
|
|
if(!setICReaderModeFlag)
|
|
{
|
|
strRevdata ="";
|
|
auto res = receiveReadRFID_data();
|
|
if(strRevdata.length() > 3)
|
|
{
|
|
if((strRevdata.left(3) == QString::number(addr).left(3))
|
|
|| strRevdata.left(3) == QString("100A")
|
|
|| strRevdata.left(3) == QString("100B")
|
|
|| strRevdata.left(3) == QString("100C"))
|
|
{
|
|
if(strRevdata.at(3) == 'A') {
|
|
if (sysPara->RFIDPara.cardInk) {
|
|
emit sig_showMsg(tr("已经刷过墨水卡,需添加墨水"));
|
|
return;
|
|
}
|
|
|
|
if(!sysPara->RFIDPara.needInkCard){
|
|
emit sig_showMsg(tr("不需要刷墨水卡!"));
|
|
}
|
|
else {
|
|
auto blockData = strRevdata;
|
|
cardTouchConfirmAsync(tr("确认要刷墨水卡吗?"), tr("确认要刷墨水卡吗? %1 秒后将自动确认"), [this, blockData]{
|
|
markInkCard(blockData);
|
|
});
|
|
}
|
|
}
|
|
else if(strRevdata.at(3) == 'B')
|
|
{
|
|
if (sysPara->RFIDPara.cardSolvent) {
|
|
emit sig_showMsg(tr("已经刷过溶剂卡,需添加溶剂"));
|
|
return;
|
|
}
|
|
|
|
if(!sysPara->RFIDPara.needSolventCard){
|
|
emit sig_showMsg(tr("不需要刷溶剂卡!"));
|
|
}
|
|
else {
|
|
auto blockData = strRevdata;
|
|
cardTouchConfirmAsync(tr("确认要刷溶剂卡吗?"), tr("确认要刷溶剂卡吗? %1 秒后将自动确认"), [this, blockData](){
|
|
markSolventCard(blockData);
|
|
});
|
|
}
|
|
|
|
}
|
|
else if(strRevdata.at(3) == 'C')
|
|
{
|
|
auto blockData = strRevdata;
|
|
cardTouchConfirmAsync(tr("确认要刷保养卡吗?"), tr("确认要刷保养卡吗? %1 秒后将自动确认"), [this, blockData](){
|
|
markMaintenanceCard(blockData);
|
|
});
|
|
}
|
|
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);
|
|
recResponse = RFIDReadData;
|
|
RFIDReadData.clear();
|
|
int n = recResponse.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);
|
|
}
|
|
|
|
if (warningTimer == nullptr) {
|
|
warningTimer = new QTimer(this);
|
|
connect(warningTimer, &QTimer::timeout,
|
|
this, &RFIDManager::slt_intervalTimerHandle);
|
|
}
|
|
}
|
|
|
|
bool RFIDManager::cardTouchConfirm(QString msgTxt, QString variableMsgTxt)
|
|
{
|
|
QMessageBox box(QMessageBox::Warning, tr("警告"),
|
|
msgTxt, QMessageBox::Ok | QMessageBox::Cancel);
|
|
|
|
int rfidCountDown = sysPara->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->para.rfidCountDown > 0) {
|
|
box.setText(boxTxt.arg(sysPara->para.rfidCountDown));
|
|
msgTimer.start(1000);
|
|
}
|
|
|
|
box.exec();
|
|
|
|
return box.result() == QMessageBox::Ok || box.result() == QMessageBox::Accepted;
|
|
}
|
|
|
|
void RFIDManager::cardTouchConfirmAsync(QString msgTxt, QString variableMsgTxt, std::function<void()> callback)
|
|
{
|
|
if(m_confirmBox->isVisible()) {
|
|
qDebug() << "A dialog is already being displayed. Ignoring the new request.";
|
|
return;
|
|
}
|
|
|
|
int rfidCountDown = sysPara->para.rfidCountDown;
|
|
m_confirmBoxTxt = variableMsgTxt;
|
|
m_confirmCallback = callback;
|
|
m_confirmBox->setText(msgTxt);
|
|
m_confirmBox->setProperty("rfidCountDown", rfidCountDown);
|
|
|
|
if(rfidCountDown > 0) {
|
|
m_confirmBox->setText(m_confirmBoxTxt.arg(rfidCountDown));
|
|
m_confirmTimer->start(1000);
|
|
}
|
|
|
|
m_confirmBox->show();
|
|
}
|
|
|
|
void RFIDManager::markInkCard(const QString &blockData)
|
|
{
|
|
auto ret = RFIDmarkCardUsedAsync(0, blockData);
|
|
if(ret >= 0 || ret == -11){
|
|
const QString msg = sysPara->RFIDPara.addedInk ? tr("刷墨水卡成功!") : tr("刷墨水卡成功, 请及时将该墨水卡对应的墨水添加到机器中");
|
|
emit sig_showMsg(msg);
|
|
sysPara->RFIDPara.cardInk = true;
|
|
sysPara->saveRFIDPara();
|
|
}
|
|
else if(ret == -10) {
|
|
emit sig_showMsg(tr("刷超级墨水卡失败,请联系厂商。"));
|
|
}
|
|
}
|
|
|
|
void RFIDManager::markSolventCard(const QString &blockData)
|
|
{
|
|
auto ret = RFIDmarkCardUsedAsync(1, blockData);
|
|
if(ret >= 0 || ret == -11) {
|
|
const QString msg = sysPara->RFIDPara.addedSolvent ? tr("刷溶剂卡成功!") : tr("刷溶剂卡成功, 请及时将该溶剂卡对应的溶剂添加到机器中");
|
|
emit sig_showMsg(msg);
|
|
sysPara->RFIDPara.cardSolvent = true;
|
|
sysPara->saveRFIDPara();
|
|
}
|
|
else if(ret == -10) {
|
|
emit sig_showMsg(tr("刷超级溶剂水卡失败,请联系厂商。"));
|
|
}
|
|
}
|
|
|
|
void RFIDManager::markMaintenanceCard(const QString &blockData)
|
|
{
|
|
auto ret = RFIDmarkCardUsedAsync(2, blockData);
|
|
if(ret >= 0 || ret == -11) {//把卡数据清零
|
|
|
|
emit sig_maintenanceShow();
|
|
|
|
sysPara->RFIDPara.needMaintenance = false;
|
|
sysPara->status.needMaintenanceRemind = false;
|
|
|
|
QDate curDate = QDate::currentDate();
|
|
sysPara->RFIDPara.maintenanceDate = curDate.toString("yyMMdd").toInt();
|
|
QDate mtEndDate = curDate.addMonths(sysPara->RFIDPara.maintenanceInterval);
|
|
sysPara->RFIDPara.maintenanceEndDate = mtEndDate.toString("yyMMdd").toInt();
|
|
sysPara->RFIDPara.runTime = 0;
|
|
RunTimeCtrl::instance()->updatePara(&sysPara->RFIDPara);
|
|
sysPara->saveRFIDPara();
|
|
// emit sig_showMsg(tr("刷保养卡成功!"));
|
|
}
|
|
else if(ret == -10) {
|
|
emit sig_showMsg(tr("刷超级保养卡失败,请联系厂商。"));
|
|
}
|
|
else {
|
|
LogInfo::setRunStatusText("Failed to mark maintenance card!");
|
|
}
|
|
}
|
|
|
|
void RFIDManager::updateStatus()
|
|
{
|
|
if (sysPara) {
|
|
status.addedInk = sysPara->RFIDPara.addedInk;
|
|
status.addedSolvent = sysPara->RFIDPara.addedSolvent;
|
|
status.cardInk = sysPara->RFIDPara.cardInk;
|
|
status.cardSolvent = sysPara->RFIDPara.cardSolvent;
|
|
}
|
|
}
|
|
|
|
int RFIDManager::waitStatus()
|
|
{
|
|
// updateStatus();
|
|
int res = 0;
|
|
bool inkFlag = !(sysPara->status.statWarn & WARN_STATUS_INK_WARN);
|
|
bool solventFlag = !(sysPara->status.statWarn & WARN_STATUS_SOLV_WARN);
|
|
if (status.addedInk && status.cardInk) {
|
|
if (sysPara && inkFlag) {
|
|
sysPara->RFIDPara.needInkCard = false;
|
|
sysPara->RFIDPara.inkCardTimeOut = false;
|
|
sysPara->RFIDPara.addedInk = false;
|
|
sysPara->RFIDPara.cardInk = false;
|
|
sysPara->saveRFIDPara();
|
|
sysPara->para.inkWarnDate = 0;
|
|
sysPara->para.inkWarnTime = 0;
|
|
sysPara->para.flagOfInkWarning = false;
|
|
sysPara->saveParaToFile();
|
|
|
|
QDir dir("/home/plg");
|
|
dir.remove("inkRFIDTime.txt");
|
|
|
|
res = 1;
|
|
}
|
|
} else if (status.addedSolvent && status.cardSolvent) {
|
|
if (sysPara && solventFlag) {
|
|
sysPara->RFIDPara.needSolventCard = false;
|
|
sysPara->RFIDPara.solventCardTimeOut = false;
|
|
sysPara->RFIDPara.addedSolvent = false;
|
|
sysPara->RFIDPara.cardSolvent = false;
|
|
sysPara->saveRFIDPara();
|
|
sysPara->para.flagOfSolventWarning = false;
|
|
sysPara->para.solventWarnDate = 0;
|
|
sysPara->para.solventWarnTime = 0;
|
|
sysPara->saveParaToFile();
|
|
|
|
QDir dir("/home/plg");
|
|
dir.remove("solventRFIDTime.txt");
|
|
res = 2;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
bool RFIDManager::waitCard()
|
|
{
|
|
updateStatus();
|
|
bool res = false;
|
|
if (status.addedInk) {
|
|
if (sysPara && status.cardInk) {
|
|
sysPara->RFIDPara.needInkCard = false;
|
|
sysPara->RFIDPara.inkCardTimeOut = false;
|
|
sysPara->RFIDPara.addedInk = false;
|
|
sysPara->RFIDPara.cardInk = false;
|
|
sysPara->saveRFIDPara();
|
|
sysPara->para.inkWarnDate = 0;
|
|
sysPara->para.inkWarnTime = 0;
|
|
sysPara->para.flagOfInkWarning = false;
|
|
sysPara->saveParaToFile();
|
|
res = true;
|
|
emit sig_showMsg(tr("先加墨水后刷卡:成功"));
|
|
}
|
|
|
|
} else if (status.addedSolvent) {
|
|
if (sysPara && status.cardSolvent) {
|
|
sysPara->RFIDPara.needSolventCard = false;
|
|
sysPara->RFIDPara.solventCardTimeOut = false;
|
|
sysPara->RFIDPara.addedSolvent = false;
|
|
sysPara->RFIDPara.cardSolvent = false;
|
|
sysPara->saveRFIDPara();
|
|
sysPara->para.flagOfSolventWarning = false;
|
|
sysPara->para.solventWarnDate = 0;
|
|
sysPara->para.solventWarnTime = 0;
|
|
sysPara->saveParaToFile();
|
|
res = true;
|
|
emit sig_showMsg(tr("先加溶剂后刷卡:成功"));
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
//static int totalTime = 0;
|
|
void RFIDManager::slt_intervalTimerHandle()
|
|
{
|
|
updateStatus();
|
|
if(status.addedInk && !status.cardInk && status.firstPromptInkCard){
|
|
emit sig_showMsg(tr("请及时使用墨水盒附带的IC卡激活墨水盒"));
|
|
status.firstPromptInkCard = false;
|
|
}
|
|
|
|
if(status.addedSolvent && !status.cardSolvent && status.firstPromptSolventCard){
|
|
emit sig_showMsg(tr("请及时使用溶剂盒附带的IC卡激活溶剂盒"));
|
|
status.firstPromptSolventCard = false;
|
|
}
|
|
|
|
//不需要通知先后,成功即可
|
|
auto res = waitStatus();
|
|
if (res == 1) {
|
|
LogInfo::setRunStatusText("Ink progress finished, and the ink card has been used.");
|
|
emit sig_inkCardProgressFinished();
|
|
return;
|
|
}
|
|
if (res == 2) {
|
|
LogInfo::setRunStatusText("Solvent progress finished, and the solvent card has been used.");
|
|
emit sig_solventProgressFinished();
|
|
return;
|
|
}
|
|
}
|
|
|
|
int RFIDManager::RFIDQueryWalletAsync()
|
|
{
|
|
if (!comForRFID || !comForRFID->isOpen()) {
|
|
qDebug() << "read uart for RFID not open!";
|
|
return -1;
|
|
}
|
|
|
|
// 构建钱包余额查询命令 0xA9(手册 3.9 钱包值(余额)查询)
|
|
// 命令格式: 01 08 A9 20 [块号] [状态提示] 00 [校验和]
|
|
UCHAR Cmd[8];
|
|
Cmd[0] = 0x01;
|
|
Cmd[1] = 0x08;
|
|
Cmd[2] = 0xA9; // 钱包余额查询命令
|
|
Cmd[3] = 0x20;
|
|
Cmd[4] = 0x04; // 块4
|
|
Cmd[5] = 0x00; // 状态提示: 0x01=LED和蜂鸣器开
|
|
Cmd[6] = 0x00; // 保留
|
|
Cmd[7] = 0x00;
|
|
|
|
CheckSumOut(Cmd, Cmd[1]);
|
|
|
|
QByteArray cmd;
|
|
cmd.append((const char*)Cmd, Cmd[1]);
|
|
|
|
// 临时断开信号,避免干扰
|
|
bool wasConnected = disconnect(comForRFID, nullptr,
|
|
this, nullptr);
|
|
|
|
wasConnected = true;
|
|
|
|
comForRFID->clear(QSerialPort::Input);
|
|
comForRFID->clear(QSerialPort::Output);
|
|
|
|
qint64 written = comForRFID->write(cmd);
|
|
if (written != cmd.size()) {
|
|
LogInfo::setRunStatusText("Failed to query card balance: send data failed");
|
|
qDebug() << "Failed to query card balance: send data failed";
|
|
qDebug() << "written:" << written << "expected:" << cmd.size();
|
|
if (wasConnected) {
|
|
connect(comForRFID, &QSerialPort::readyRead,
|
|
this, &RFIDManager::slt_readRFIDData, Qt::UniqueConnection);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
QByteArray recResponse;
|
|
QElapsedTimer elapsedTimer;
|
|
elapsedTimer.start();
|
|
while (!elapsedTimer.hasExpired(2000)) {
|
|
if (comForRFID->bytesAvailable() >= 8) {
|
|
recResponse = comForRFID->read(8);
|
|
// 包长度字段为0x0A(10字节)时,等剩余2字节;否则(0x08)已完成
|
|
if (recResponse[1] == 0x0A) {
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
|
|
if (recResponse.size() == 8 && recResponse[1] == 0x0A
|
|
&& comForRFID->bytesAvailable() >= 2) {
|
|
recResponse += comForRFID->read(2);
|
|
break;
|
|
}
|
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
}
|
|
|
|
// 恢复信号连接
|
|
if (wasConnected) {
|
|
connect(comForRFID, &QSerialPort::readyRead,
|
|
this, &RFIDManager::slt_readRFIDData, Qt::UniqueConnection);
|
|
}
|
|
|
|
int n = recResponse.size();
|
|
if (n < 8) {
|
|
LogInfo::setRunStatusText("Failed to query card balance: data length is less than 8 bytes");
|
|
qDebug() << "Failed to query card balance: data length is less than 8 bytes";
|
|
return -1;
|
|
}
|
|
|
|
UCHAR revdata[100];
|
|
if (n > (int)sizeof(revdata)) return -1;
|
|
memcpy(revdata, recResponse.constData(), n);
|
|
|
|
// 校验并解析返回包
|
|
// 成功响应: 01 0A A9 20 00 [钱包值4字节小端序] [校验和] (10字节)
|
|
// 失败响应: 01 08 A9 20 01 00 00 [校验和] (8字节)
|
|
if ((revdata[0] == 0x01) && (revdata[1] == n)
|
|
&& (revdata[2] == 0xA9) && (revdata[3] == 0x20))
|
|
{
|
|
bool status = CheckSumIn(revdata, revdata[1]);
|
|
if (status && revdata[4] == 0x00) {
|
|
// 成功响应,包长度为0x0A(10字节)
|
|
// 钱包值在revdata[5]~revdata[8],低位在前(小端序)
|
|
unsigned int val = (unsigned char)revdata[5]
|
|
| ((unsigned char)revdata[6] << 8)
|
|
| ((unsigned char)revdata[7] << 16)
|
|
| ((unsigned char)revdata[8] << 24);
|
|
LogInfo::setRunStatusText(QString("Succeeded to query card balance: %1").arg(val));
|
|
qDebug() << "Succeeded to query card balance:" << val;
|
|
return static_cast<int>(val);
|
|
}
|
|
else if(status && revdata[4] == 0x01) {
|
|
LogInfo::setRunStatusText("Failed to query card balance: card is invalid or not initialized");
|
|
qDebug() << "Failed to query card balance: card is invalid or not initialized";
|
|
return -2;
|
|
}
|
|
else {
|
|
LogInfo::setRunStatusText("Failed to query card balance: checksum error");
|
|
qDebug() << "Failed to query card balance: checksum error";
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
LogInfo::setRunStatusText(("Failed to query card balance, logic error"));
|
|
qDebug() << "Failed to query card balance, logic error";
|
|
return -1;
|
|
} |