From 02c4f0120ea43440e096cd07bb0eb766a633660c Mon Sep 17 00:00:00 2001 From: zhaohe Date: Thu, 11 Apr 2024 21:07:46 +0800 Subject: [PATCH] update --- README.md | 23 +++++ iflytop_canbus/iflytop_canbus_master.cpp | 60 +++++++++++++ iflytop_canbus/iflytop_canbus_master.hpp | 21 ++++- libzqt/logger.cpp | 2 +- libzqt/logger.hpp | 5 +- mainwindow.cpp | 20 ++++- mainwindow.ui | 149 +++++++++++++++++++++++++------ 7 files changed, 244 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index c825475..64697cc 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,27 @@ V4: 4. 上报某条记录 5. 打印记录的checksum +``` + +``` + 模块名: + + + 模块基本操作: + 打印寄存器列表 + + 初始化 + 1. 脚本解析器 + + 电机初始化(人造方法....),设定好所有必要的初始化参数,按下按键即可。 + 必要参数: + + 方法..... + ......... + + + + + + ``` \ No newline at end of file diff --git a/iflytop_canbus/iflytop_canbus_master.cpp b/iflytop_canbus/iflytop_canbus_master.cpp index 8c9e7f1..30b9ffc 100644 --- a/iflytop_canbus/iflytop_canbus_master.cpp +++ b/iflytop_canbus/iflytop_canbus_master.cpp @@ -19,9 +19,67 @@ void IflytopCanbusMaster::initialize(IDataChannel *channel) { // m_waveCan.init(channel, [this](WaveshareCan::can_rx_frame_t *canframe) { // ZLOGI(TAG, "can rx: 0x%08x %d %s", canframe->id, canframe->dlc, zhex2str(canframe->data, canframe->dlc).c_str()); + + /** + * @brief 消息格式 + * + * [2] [3bit] [8bit] [8bit] [8bit] + * , from frameNum frameId + */ + uint8_t from = (canframe->id >> 16 & 0xFF); + uint8_t nframe = (canframe->id & 0xFF00) >> 8; + uint8_t frameId = (canframe->id & 0x00FF); + CanPacketRxBuffer *rxbuf = nullptr; + + rxbuf = findRxBuff(from); + if (frameId == 0) { + rxbuf->rxdataSize = 0; + rxbuf->rxPacketNum = 0; + } + + if (!rxbuf) { + ZLOGE(TAG, "board[%d] packetId[%d] find rx buff fail", from, frameId); + return; + } + + memcpy(rxbuf->rxdata + rxbuf->rxdataSize, canframe->data, canframe->dlc); + rxbuf->rxdataSize += canframe->dlc; + + /** + * @brief 接收满了 + */ + rxbuf->rxPacketNum++; + if (nframe == frameId + 1) { + if (rxbuf->rxPacketNum == nframe) { + processRxPacket((iflytop_canbus_frame_t *)rxbuf->rxdata, rxbuf->rxdataSize); + } else { + ZLOGE(TAG, "lost packet from %d", from); + } + } + }); } +CanPacketRxBuffer *IflytopCanbusMaster::findRxBuff(int deviceId) { + for (int i = 0; i < ZARRAY_SIZE(m_rxbuf); i++) { + if (m_rxbuf[i].id == deviceId) { + return &m_rxbuf[i]; + } + + if (m_rxbuf[i].id == 0) { + m_rxbuf[i].id = deviceId; + return &m_rxbuf[i]; + } + } + + ZASSERT(0); + return nullptr; +} + +void IflytopCanbusMaster::processRxPacket(iflytop_canbus_frame_t *frame, size_t len) { + if (on_rx_raw) on_rx_raw(kcmd_receipt, (uint8_t *)frame, len); +} + void IflytopCanbusMaster::updateChannelConfig() { // m_waveCan.setCanpPrameter(CANUSB_SPEED_500000, CANUSB_MODE_NORMAL, CANUSB_FRAME_EXTENDED); } @@ -62,6 +120,8 @@ void IflytopCanbusMaster::callcmd(int32_t device_id, int32_t cmdid, uint8_t *dat } void IflytopCanbusMaster::sendframe(iflytop_canbus_frame_t *frame, size_t len) { + if (on_rx_raw) on_rx_raw(kcmd_cmd, (uint8_t *)frame, len); + uint8_t *packet = (uint8_t *)frame; int npacket = len / 8 + (len % 8 == 0 ? 0 : 1); diff --git a/iflytop_canbus/iflytop_canbus_master.hpp b/iflytop_canbus/iflytop_canbus_master.hpp index 198f76b..14ccca6 100644 --- a/iflytop_canbus/iflytop_canbus_master.hpp +++ b/iflytop_canbus/iflytop_canbus_master.hpp @@ -30,11 +30,20 @@ class RxReceiptContext { size_t receiptLen; }; +class CanPacketRxBuffer { + public: + int32_t id = 0; + + uint8_t rxdata[2048] = {0}; + int rxdataSize = 0; + + int rxPacketNum; +}; + typedef enum { kcmd_cmd, kcmd_receipt, kcmd_report, - kcmd_ch4_data, } raw_data_type_t; typedef function on_raw_data_t; @@ -60,16 +69,20 @@ class IflytopCanbusMaster { *******************************************************************************/ mutex m_tx_lock; uint8_t m_txbuf[1024]; - uint8_t m_rxbuf[1024]; int32_t m_rxsize; uint8_t m_txindex = 0; WaveshareCan m_waveCan; + CanPacketRxBuffer m_rxbuf[255]; + + on_raw_data_t on_rx_raw; + public: static IflytopCanbusMaster *ins(); void initialize(IDataChannel *channel); + void regOnRawData(on_raw_data_t cb) { on_rx_raw = cb; } void updateChannelConfig(); void callcmd(int32_t device_id, int32_t cmdid, uint8_t *data, size_t len); @@ -81,6 +94,10 @@ class IflytopCanbusMaster { private: void sendframe(iflytop_canbus_frame_t *frame, size_t len); void sendsubframe(int npacket, int packetIndex, uint8_t *packet, size_t len); + void processRxPacket(iflytop_canbus_frame_t *frame, size_t len); + + private: + CanPacketRxBuffer *findRxBuff(int deviceId); }; } // namespace iflytop diff --git a/libzqt/logger.cpp b/libzqt/logger.cpp index 2d7b173..26822df 100644 --- a/libzqt/logger.cpp +++ b/libzqt/logger.cpp @@ -14,7 +14,7 @@ void zos_log(const char* fmt, ...) { va_end(args); } -int32_t zos_get_ticket() { return (int32_t)QDateTime::currentMSecsSinceEpoch(); } +uint32_t zos_get_ticket() { return (uint32_t)QDateTime::currentMSecsSinceEpoch(); } std::string zhex2str(const uint8_t* hex, size_t len) { std::string str; diff --git a/libzqt/logger.hpp b/libzqt/logger.hpp index 5a20883..8b52340 100644 --- a/libzqt/logger.hpp +++ b/libzqt/logger.hpp @@ -5,7 +5,7 @@ #include void zos_log(const char* fmt, ...); -int32_t zos_get_ticket(); +uint32_t zos_get_ticket(); std::string zhex2str(const uint8_t* hex, size_t len); std::string zhex2binary(uint8_t hex); @@ -20,3 +20,6 @@ std::string zhex32ToBinary(uint32_t hex); ZLOGE("ASSERT", "condition: %s", #cond); \ exit(-1); \ } + + +#define ZARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) \ No newline at end of file diff --git a/mainwindow.cpp b/mainwindow.cpp index f33fc03..b2a8d69 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -24,8 +24,8 @@ typedef enum { static MainWindow *m_mainWindow; static QTDataChannel G_QTDataChannel; static QTDataChannel G_WaveDataChannel; -static device_type_t m_devicetype; -static IflytopCanbusMaster *m_canbusMaster; + + QT_REQUIRE_CONFIG(groupbox); @@ -313,7 +313,7 @@ QWidget *MainWindow::allocNewTab(QString zh_name) { newtab->setLayout(verticalLayout); // 添加Tab - ui->buttonTabWidget->addTab(newtab, QString()); + ui->buttonTabWidget->insertTab(0,newtab, QString()); ui->buttonTabWidget->setTabText(ui->buttonTabWidget->indexOf(newtab), zh_name); return newtab; } @@ -410,6 +410,16 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi G_QTDataChannel.init(); G_WaveDataChannel.init(); IflytopCanbusMaster::ins()->initialize(&G_QTDataChannel); + + IflytopCanbusMaster::ins()->regOnRawData([this](raw_data_type_t type, uint8_t *hex, uint32_t hexlen) { + if (type == kcmd_cmd) { + rawDataPreviewShow("[CMD ] %s", zhex2str(hex, hexlen).c_str()); + } else if (type == kcmd_receipt) { + rawDataPreviewShow("[RECEIPT] %s", zhex2str(hex, hexlen).c_str()); + } else if (type == kcmd_report) { + rawDataPreviewShow("[REPORT ] %s", zhex2str(hex, hexlen).c_str()); + } + }); } MainWindow::~MainWindow() { delete ui; } @@ -425,6 +435,10 @@ void MainWindow::constructAppUI() { addNewButtonStyle1(box, "扫描模块", {"deviceId"}, [](int argn, const char **args) { // IflytopCanbusMaster::ins()->callcmd(atoi(args[0]), kmodule_ping); }); + + + + endAllocNewBox(box); } endAllocNewTab(tab); diff --git a/mainwindow.ui b/mainwindow.ui index 52cd606..bc35ec3 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -659,48 +659,79 @@ QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { - + + + + 0 + 0 + + + + + 0 + 0 + + 16777215 16777215 + + false + - 波形串口设置 + 通用参数 - - - - - - - - 打开 + + + + + + 1 + 0 + - - - - - - - - - 波特率 + + + 0 + 25 + + + + + 16777215 + 25 + - - - - - 串口 + 设备ID - - - - 刷新 + + + + + 1 + 0 + + + + + 0 + 25 + + + + + 16777215 + 25 + + + + 2 @@ -881,8 +912,68 @@ p, li { white-space: pre-wrap; } Qt::DefaultContextMenu - -1 + 0 + + + 波形显示器 + + + + + 20 + 20 + 182 + 126 + + + + + 16777215 + 16777215 + + + + 波形串口设置 + + + + + + + + + 打开 + + + + + + + + + + 波特率 + + + + + + + 串口 + + + + + + + 刷新 + + + + + +