From a6e9af706cb45ac6265d682733a5bb33116dfad8 Mon Sep 17 00:00:00 2001 From: mhvhm <1308784381@qq.com> Date: Sat, 14 Sep 2024 00:34:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=83=A8=E5=88=86=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=8E=A5=E6=94=B6=E5=92=8C=E7=BA=BF=E7=A8=8B=E4=BF=A1?= =?UTF-8?q?=E5=8F=B7=E5=92=8C=E6=A7=BD=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CanTool/app/CANThread/canthread.cpp | 158 ++++++++++++++++++++++++++++ CanTool/app/CANThread/canthread.h | 15 ++- CanTool/mainwindow.cpp | 90 +++++++++++++++- CanTool/mainwindow.h | 13 ++- CanTool/mainwindow.ui | 14 +-- README.md | 1 + 6 files changed, 280 insertions(+), 11 deletions(-) diff --git a/CanTool/app/CANThread/canthread.cpp b/CanTool/app/CANThread/canthread.cpp index bba2ba3..d68afaf 100644 --- a/CanTool/app/CANThread/canthread.cpp +++ b/CanTool/app/CANThread/canthread.cpp @@ -20,8 +20,143 @@ bool CANThread::openCanDevice(DWORD deviceType, DWORD deviceIdx, DWORD bandrate) m_canDeviceIdx = deviceIdx; m_canBandrate = bandrate; + // 1. 打开can设备 DWORD ret = VCI_OpenDevice(m_canDeviceType, m_canDeviceIdx, 0); + if (ret != 1) + { + qDebug() << "[OpenDevice]错误代码:" << ret; + return false; + } + + // 2. 初始化can + VCI_INIT_CONFIG config; + config.AccCode = 0x80000008; // 验收码 + config.AccMask = 0xFFFFFFFF; // 屏蔽码 + config.Filter = 1; // 滤波方式-接收所有类型 + config.Mode = 0; // 工作模式-正常工作 + + // 根据波特率初始化定时器 + switch (m_canBandrate) { + case 10: + config.Timing0=0x31; + config.Timing1=0x1c; + break; + case 20: + config.Timing0=0x18; + config.Timing1=0x1c; + break; + case 40: + config.Timing0=0x87; + config.Timing1=0xff; + break; + case 50: + config.Timing0=0x09; + config.Timing1=0x1c; + break; + case 80: + config.Timing0=0x83; + config.Timing1=0xff; + break; + case 100: + config.Timing0=0x04; + config.Timing1=0x1c; + break; + case 125: + config.Timing0=0x03; + config.Timing1=0x1c; + break; + case 200: + config.Timing0=0x81; + config.Timing1=0xfa; + break; + case 250: + config.Timing0=0x01; + config.Timing1=0x1c; + break; + case 400: + config.Timing0=0x80; + config.Timing1=0xfa; + break; + case 500: + config.Timing0=0x00; + config.Timing1=0x1c; + break; + case 666: + config.Timing0=0x80; + config.Timing1=0xb6; + break; + case 800: + config.Timing0=0x00; + config.Timing1=0x16; + break; + case 1000: + config.Timing0=0x00; + config.Timing1=0x14; + break; + case 33: + config.Timing0=0x09; + config.Timing1=0x6f; + break; + case 66: + config.Timing0=0x04; + config.Timing1=0x6f; + break; + case 83: + config.Timing0=0x03; + config.Timing1=0x6f; + break; + default: + break; + } + + ret = VCI_InitCAN(m_canDeviceType, m_canDeviceIdx, CAN_1, &config); + if (ret != 1) + { + qDebug() << "[InitCAN]错误代码:" << ret; + return false; + } + ret = VCI_InitCAN(m_canDeviceType, m_canDeviceIdx, CAN_2, &config); + if (ret != 1) + { + qDebug() << "[InitCAN]错误代码:" << ret; + return false; + } + +// // 3. 读取can设备信息 +// VCI_BOARD_INFO info; +// ret = VCI_ReadBoardInfo(m_canDeviceType, m_canDeviceIdx, &info); +// if (ret != 1) +// { +// qDebug() << "[ReadBoardInfo]错误代码:" << ret; +// return false; +// } + + // 4. 启动can + ret = VCI_StartCAN(m_canDeviceType, m_canDeviceIdx, CAN_1); + if (ret != 1) + { + qDebug() << "[StartCAN1]错误代码:" << ret; + return false; + } + ret = VCI_StartCAN(m_canDeviceType, m_canDeviceIdx, CAN_2); + if (ret != 1) + { + qDebug() << "[StartCAN2]错误代码:" << ret; + return false; + } + + return true; +} + +/* @brief: 关闭can设备 + * @para: + * @return: + */ +bool CANThread::closeCanDevice() +{ + DWORD ret = VCI_CloseDevice(m_canDeviceType, m_canDeviceIdx); + if (ret == 1) { return true; @@ -32,3 +167,26 @@ bool CANThread::openCanDevice(DWORD deviceType, DWORD deviceIdx, DWORD bandrate) return false; } } + +/* @brief: 线程执行函数 + * @para: + * @return: + */ +void CANThread::doThreadWork() +{ + VCI_CAN_OBJ data[CAN_RECV_BUF_SISE]; // 接收缓存 + DWORD ret; + while (1) + { + ret = VCI_Receive(m_canDeviceType, m_canDeviceIdx, CAN_1, data, CAN_RECV_BUF_SISE, 0); + if (ret != -1) + { + emit recvFinished(data, ret, CAN_1); + } + ret = VCI_Receive(m_canDeviceType, m_canDeviceIdx, CAN_2, data, CAN_RECV_BUF_SISE, 0); + if (ret != -1) + { + emit recvFinished(data, ret, CAN_2); + } + } +} diff --git a/CanTool/app/CANThread/canthread.h b/CanTool/app/CANThread/canthread.h index 1666341..7c57790 100644 --- a/CanTool/app/CANThread/canthread.h +++ b/CanTool/app/CANThread/canthread.h @@ -6,20 +6,33 @@ #include "ControlCAN.h" +#define CAN_RECV_BUF_SISE (2500) + +typedef enum +{ + CAN_1 = 0, + CAN_2, +}CAN_x; + class CANThread : public QObject { Q_OBJECT public: explicit CANThread(QObject *parent = nullptr); - bool openCanDevice(uint deviceType, uint deviceIdx, uint bandrate); + bool openCanDevice(DWORD deviceType, DWORD deviceIdx, DWORD bandrate); + bool closeCanDevice(); private: DWORD m_canDeviceType; // 设备类型 DWORD m_canDeviceIdx; // 设备索引 DWORD m_canBandrate; // 波特率 signals: + void recvFinished(VCI_CAN_OBJ *data, DWORD retNum, CAN_x canChl); + +public slots: + void doThreadWork(); // 线程执行函数 }; diff --git a/CanTool/mainwindow.cpp b/CanTool/mainwindow.cpp index 79ee920..b2c7aaa 100644 --- a/CanTool/mainwindow.cpp +++ b/CanTool/mainwindow.cpp @@ -7,9 +7,9 @@ MainWindow::MainWindow(QWidget *parent) { ui->setupUi(this); + Var_Init(); UI_Init(); Slot_Init(); - Var_Init(); } MainWindow::~MainWindow() @@ -17,6 +17,32 @@ MainWindow::~MainWindow() delete ui; } +/* @brief: UI-打开设备 + * @para: + * @return: + */ +void MainWindow::UI_OpenCanDevice() +{ + ui->pushButton_open->setEnabled(false); + ui->pushButton_close->setEnabled(true); + ui->comboBox_type->setEnabled(false); + ui->comboBox_typeIdx->setEnabled(false); + ui->comboBox_bandrate->setEnabled(false); +} + +/* @brief: UI-关闭设备 + * @para: + * @return: + */ +void MainWindow::UI_CloseCanDevice() +{ + ui->pushButton_open->setEnabled(true); + ui->pushButton_close->setEnabled(false); + ui->comboBox_type->setEnabled(true); + ui->comboBox_typeIdx->setEnabled(true); + ui->comboBox_bandrate->setEnabled(true); +} + // ======================================================== <初始化> /* @brief: 变量初始化 * @para: @@ -25,6 +51,9 @@ MainWindow::~MainWindow() void MainWindow::Var_Init() { can_drv = new CANThread(); + recvThread = new QThread(); + + can_drv->moveToThread(recvThread); // 转移到线程中运行 } /* @brief: UI初始化 @@ -35,6 +64,17 @@ void MainWindow::UI_Init() { setWindowIcon(QIcon(":/img/logo.ico")); setWindowTitle("CANTool"); + + UI_CloseCanDevice(); + + if (isRolling == true) + { + ui->radioButton_roll->setChecked(true); + } + else + { + ui->radioButton_roll->setChecked(false); + } } /* @brief: 槽函数初始化 @@ -44,12 +84,18 @@ void MainWindow::UI_Init() void MainWindow::Slot_Init() { connect(ui->pushButton_open, &QPushButton::clicked, this, &MainWindow::PushButton_Open_Clicked_Callback); + connect(ui->pushButton_close, &QPushButton::clicked, this, &MainWindow::PushButton_Close_Clicked_Callback); + connect(ui->radioButton_roll, &QRadioButton::clicked, this, &MainWindow::RadioButton_Roll_Clicked_Callback); + + connect(recvThread, &QThread::started, can_drv, &CANThread::doThreadWork); + + connect(can_drv, &CANThread::recvFinished, this, &MainWindow::getCanRecvData); } // ======================================================== <槽函数> void MainWindow::PushButton_Open_Clicked_Callback() { - qDebug() << "<<< clicked >>> <<<" << sender()->objectName() << ">>>"; + qDebug() << "<<>> <<<" << sender()->objectName() << ">>>"; DWORD bandrate = ui->comboBox_bandrate->currentText().remove("Kbps").toUInt(); DWORD idx = ui->comboBox_typeIdx->currentText().toInt(); @@ -60,6 +106,8 @@ void MainWindow::PushButton_Open_Clicked_Callback() if (ret == true) { qDebug() << "can设备打开成功"; + UI_OpenCanDevice(); + recvThread->start(); } else { @@ -71,3 +119,41 @@ void MainWindow::PushButton_Open_Clicked_Callback() msgBox.exec(); } } + +void MainWindow::PushButton_Close_Clicked_Callback() +{ + qDebug() << "<<>> <<<" << sender()->objectName() << ">>>"; + + bool ret = can_drv->closeCanDevice(); + + if (ret == true) + { + qDebug() << "can设备关闭成功"; + UI_CloseCanDevice(); + } + else + { + qDebug() << "can设备关闭失败"; + QMessageBox msgBox; + msgBox.setWindowTitle("错误"); + msgBox.setText("CAN设备关闭失败"); + msgBox.setIcon(QMessageBox::Warning); + msgBox.exec(); + } +} + +void MainWindow::RadioButton_Roll_Clicked_Callback() +{ + qDebug() << "<<>> <<<" << sender()->objectName() << ">>>"; + isRolling = ui->radioButton_roll->isChecked(); + qDebug() << "isRolling" << isRolling; +} + +void MainWindow::getCanRecvData(VCI_CAN_OBJ *data, DWORD retNum, CAN_x canChl) +{ + qDebug() << "<<>> <<<" << sender()->objectName() << ">>>"; + for (DWORD i = 0; i < retNum; i++) + { + + } +} diff --git a/CanTool/mainwindow.h b/CanTool/mainwindow.h index 5b49424..a3094aa 100644 --- a/CanTool/mainwindow.h +++ b/CanTool/mainwindow.h @@ -4,7 +4,7 @@ #include #include #include - +#include #include "app/CANThread/canthread.h" @@ -27,9 +27,20 @@ public: private: Ui::MainWindow *ui; CANThread* can_drv; + QThread* recvThread; + bool isRolling = true; // 是否滚动显示 + void UI_OpenCanDevice(); + + void UI_CloseCanDevice(); private slots: // 槽函数 void PushButton_Open_Clicked_Callback(); + + void PushButton_Close_Clicked_Callback(); + + void RadioButton_Roll_Clicked_Callback(); + + void getCanRecvData(VCI_CAN_OBJ *data, DWORD retNum, CAN_x canChl); }; #endif // MAINWINDOW_H diff --git a/CanTool/mainwindow.ui b/CanTool/mainwindow.ui index f341a4a..a9c8222 100644 --- a/CanTool/mainwindow.ui +++ b/CanTool/mainwindow.ui @@ -93,13 +93,13 @@ - + 30 10 - 320 - 119 + 402 + 123 @@ -352,22 +352,22 @@ - 20 + 50 20 - + - 80 + 100 16777215 - 滚动刷新 + 刷新显示 diff --git a/README.md b/README.md index 7437c93..7b0308a 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,6 @@ | 更新时间 | 更新内容 | | ---------- | ------------------------------------------------------------ | +| 2024-09-14 | 1. 完成部分程序接收和线程信号和槽的逻辑
| | 2024-09-13 | 1.完成工程创建
2. 完成程序图标设置
3. 完成dll动态库的加入
4. 完成can设备的打开函数
|