From d9a9815a89998c8001c66236c66bcec251b66599 Mon Sep 17 00:00:00 2001 From: luocai Date: Fri, 21 Jun 2024 11:02:28 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=AE=9E=E7=8E=B0=E5=9B=BE=E7=89=87=E4=B8=8A?= =?UTF-8?q?=E6=8A=A5=E5=8F=8A=E4=B8=8B=E5=8F=91=E6=B3=A8=E5=86=8C=E7=9A=84?= =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E5=AE=9E=E7=8E=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Analyser/Application.h | 2 +- Analyser/ModuleCommunication.cpp | 22 ++++++++++++++-------- Analyser/ModuleCommunication.h | 6 ++++++ Analyser/qml/OperationItem.qml | 17 ++++++++++------- OtaUpdate/CdcUpdater.cpp | 24 +++++++++++++----------- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/Analyser/Application.h b/Analyser/Application.h index 4692391..6ceb084 100644 --- a/Analyser/Application.h +++ b/Analyser/Application.h @@ -17,7 +17,7 @@ class QTimer; class Application : public QObject { Q_OBJECT QML_ELEMENT - Q_PROPERTY(ModuleCommunication *module READ module CONSTANT) + Q_PROPERTY(ModuleCommunication *module READ module NOTIFY connectedChanged) Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged) Q_PROPERTY(bool uvcOpened READ uvcOpened NOTIFY uvcOpenedChanged) Q_PROPERTY(bool persistenceMode READ persistenceMode WRITE setPersistenceMode NOTIFY persistenceModeChanged) diff --git a/Analyser/ModuleCommunication.cpp b/Analyser/ModuleCommunication.cpp index 06939b7..84eaafb 100644 --- a/Analyser/ModuleCommunication.cpp +++ b/Analyser/ModuleCommunication.cpp @@ -39,8 +39,7 @@ void ModuleCommunication::verify(uint8_t timeout) { auto [frameData, frameSize] = generateFrame(Verify, reinterpret_cast(&data), sizeof(data)); m_serialPort->write(reinterpret_cast(frameData), frameSize); - m_currentMessageId = Verify; - emit commandStarted(Verify); + setCurrentMessageIdStatus(Verify); LOG_CAT(info, GUI) << "发送识别指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << Separator; @@ -49,6 +48,7 @@ void ModuleCommunication::verify(uint8_t timeout) { void ModuleCommunication::reset() { auto [frameData, frameSize] = generateFrame(Reset); m_serialPort->write(reinterpret_cast(frameData), frameSize); + setCurrentMessageIdStatus(Reset); LOG_CAT(info, GUI) << "发送复位指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << Separator; } @@ -59,8 +59,7 @@ void ModuleCommunication::enroll(const std::string &username, uint8_t timeout) { strncpy(reinterpret_cast(data.username), username.c_str(), sizeof(data.username)); auto [frameData, frameSize] = generateFrame(EnrollSingle, reinterpret_cast(&data), sizeof(data)); m_serialPort->write(reinterpret_cast(frameData), frameSize); - m_currentMessageId = EnrollSingle; - emit commandStarted(EnrollSingle); + setCurrentMessageIdStatus(EnrollSingle); LOG_CAT(info, GUI) << "发送注册指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << "用户名: " << username << ", 超时时间: " << static_cast(timeout) << "s"; @@ -83,8 +82,7 @@ void ModuleCommunication::deleteUser(uint16_t userid) { uint16_t n = htons(userid); auto [frameData, frameSize] = generateFrame(DeleteUser, reinterpret_cast(&n), sizeof(n)); m_serialPort->write(reinterpret_cast(frameData), frameSize); - m_currentMessageId = DeleteUser; - emit commandStarted(DeleteUser); + setCurrentMessageIdStatus(DeleteUser); LOG_CAT(info, GUI) << "发送删除用户指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << "删除用户ID: " << userid; LOG_CAT(info, GUI) << Separator; @@ -93,8 +91,7 @@ void ModuleCommunication::deleteUser(uint16_t userid) { void ModuleCommunication::deleteAll() { auto [frameData, frameSize] = generateFrame(DeleteAll); m_serialPort->write(reinterpret_cast(frameData), frameSize); - m_currentMessageId = DeleteAll; - emit commandStarted(DeleteAll); + setCurrentMessageIdStatus(DeleteAll); LOG_CAT(info, GUI) << "发送删除所有指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << Separator; } @@ -303,6 +300,7 @@ void ModuleCommunication::processPackage(const uint8_t *data, uint16_t size) { break; } m_currentMessageId = Idle; + emit currentMessageIdChanged(); emit commandFinished(static_cast(replyId), static_cast(result)); break; } @@ -400,6 +398,14 @@ std::pair ModuleCommunication::generateFrame(MessageId comm return std::make_pair(sendBuffer, size + 6); } +void ModuleCommunication::setCurrentMessageIdStatus(MessageId messageId) { + if (m_currentMessageId != messageId) { + m_currentMessageId = messageId; + emit commandStarted(messageId); + emit currentMessageIdChanged(); + } +} + std::string ModuleCommunication::protocolDataFormatString(const uint8_t *data, int size) { std::ostringstream oss; for (int i = 0; i < size; i++) { diff --git a/Analyser/ModuleCommunication.h b/Analyser/ModuleCommunication.h index e42de85..c9ff104 100644 --- a/Analyser/ModuleCommunication.h +++ b/Analyser/ModuleCommunication.h @@ -3,13 +3,16 @@ #include "DataStructure.h" #include +#include #include #include class ModuleCommunication : public QObject { Q_OBJECT + QML_ELEMENT static constexpr uint32_t UsernameSize = 32; static constexpr const char *Separator = "----------"; + Q_PROPERTY(MessageId currentMessageId READ currentMessageId NOTIFY currentMessageIdChanged) public: constexpr static uint16_t InvalidUserId = std::numeric_limits::max(); @@ -29,6 +32,7 @@ public: RequestPalmFeature = 0xFA, Idle = 0xFF, }; + Q_ENUM(MessageId) enum NoteId : uint8_t { Ready = 0x00, @@ -161,12 +165,14 @@ signals: void errorOccurred(const QString &error); void commandStarted(ModuleCommunication::MessageId messageId); void commandFinished(MessageId messageId, MessageStatus status); + void currentMessageIdChanged(); protected: void processPackage(const uint8_t *data, uint16_t size); void onReadyRead(); void onErrorOccurred(QSerialPort::SerialPortError error); std::pair generateFrame(MessageId command, const uint8_t *data = nullptr, uint16_t size = 0); + void setCurrentMessageIdStatus(ModuleCommunication::MessageId messageId); private: std::shared_ptr m_serialPort; diff --git a/Analyser/qml/OperationItem.qml b/Analyser/qml/OperationItem.qml index 7504c12..6ea2f9c 100644 --- a/Analyser/qml/OperationItem.qml +++ b/Analyser/qml/OperationItem.qml @@ -32,9 +32,12 @@ ColumnLayout { text: "10" } Button { - text: "注册" - onClicked: App.enroll(enrollName.text, - parseInt(enrollTimeout.text)) + text: !App.module ? "注册" : App.module.currentMessageId + === 0x1d ? "取消" : "注册" + onClicked: App.module.currentMessageId + === 0x1d ? App.module.reset() : App.enroll( + enrollName.text, + parseInt(enrollTimeout.text)) } } } @@ -67,11 +70,11 @@ ColumnLayout { } Item {} Button { - text: App.isVerifying?"停止": "识别" + text: App.isVerifying ? "停止" : "识别" onClicked: { - if(App.isVerifying){ + if (App.isVerifying) { App.module.reset() - }else { + } else { App.persistenceVerifyInterval = parseInt( verifyIntetval.text) App.verify(parseInt(verifyTimeout.text)) @@ -107,7 +110,7 @@ ColumnLayout { columns: 1 Button { text: "录入图片上报" - onClicked: App.getEnrolledImage("",60) + onClicked: App.getEnrolledImage("", 60) } Button { text: "图片下发注册" diff --git a/OtaUpdate/CdcUpdater.cpp b/OtaUpdate/CdcUpdater.cpp index b875329..0509d4d 100644 --- a/OtaUpdate/CdcUpdater.cpp +++ b/OtaUpdate/CdcUpdater.cpp @@ -74,11 +74,13 @@ bool CdcUpdater::write(Command command, const uint8_t *data, uint32_t size) { if (data != nullptr) { memcpy(&packet[3], data, size); } - std::ostringstream oss; - for (int i = 0; i < packet.size(); i++) { - oss << "0x" << std::hex << (static_cast(packet[i]) & 0xff) << " "; + if (command != TransferBin) { + std::ostringstream oss; + for (int i = 0; i < packet.size(); i++) { + oss << "0x" << std::hex << (static_cast(packet[i]) & 0xff) << " "; + } + LOG(info) << "write " << oss.str(); } - LOG(info) << "write " << oss.str(); return m_serialPort->write(reinterpret_cast(packet.data()), packet.size()); } @@ -121,14 +123,14 @@ void CdcUpdater::transferBin() { void CdcUpdater::onReadyRead() { auto data = m_serialPort->readAll(); - - std::ostringstream oss; - for (int i = 0; i < data.size(); i++) { - oss << "0x" << std::hex << (static_cast(data[i]) & 0xff) << " "; - } - LOG(info) << "onReadyRead, size: " << data.size() << ", " << oss.str(); - uint8_t command = static_cast(data[2]); + if (command != TransferBinReply) { + std::ostringstream oss; + for (int i = 0; i < data.size(); i++) { + oss << "0x" << std::hex << (static_cast(data[i]) & 0xff) << " "; + } + LOG(info) << "onReadyRead, size: " << data.size() << ", " << oss.str(); + } if (command == EnterUpgradeReply) { write(GetVersion); emit progressChanged(++m_progress);