diff --git a/SP713_Upper.pro b/SP713_Upper.pro index 80ccced..c707d41 100644 --- a/SP713_Upper.pro +++ b/SP713_Upper.pro @@ -10,24 +10,27 @@ CONFIG += c++17 SOURCES += \ dialogcalibrate.cpp \ + dialogtest.cpp \ + src/sources/dachandler.cpp \ src/sources/framehandler.cpp \ main.cpp \ mainwindow.cpp \ - src/sources/crc.cpp \ src/sources/drv_spi.cpp \ src/sources/drv_uart.cpp HEADERS += \ dialogcalibrate.h \ + dialogtest.h \ + src/headers/dachandler.h \ src/headers/framehandler.h \ mainwindow.h \ src/headers/CH347DLL.H \ - src/headers/crc.h \ src/headers/drv_spi.h \ src/headers/drv_uart.h FORMS += \ dialogcalibrate.ui \ + dialogtest.ui \ mainwindow.ui LIBS += -L$$PWD/lib -lCH347DLLA64 diff --git a/dialogcalibrate.h b/dialogcalibrate.h index 05abed3..80abec7 100644 --- a/dialogcalibrate.h +++ b/dialogcalibrate.h @@ -15,6 +15,7 @@ public: explicit DialogCalibrate(QWidget *parent = nullptr); ~DialogCalibrate(); + private slots: void on_pushButton_clicked(); diff --git a/dialogtest.cpp b/dialogtest.cpp new file mode 100644 index 0000000..ca9d9a1 --- /dev/null +++ b/dialogtest.cpp @@ -0,0 +1,45 @@ +#include "dialogtest.h" +#include "ui_dialogtest.h" + +DialogTest::DialogTest(QWidget *parent) + : QDialog(parent) + , ui(new Ui::DialogTest) +{ + ui->setupUi(this); +} + +DialogTest::~DialogTest() +{ + delete ui; +} + +void DialogTest::on_pushButton_clicked() +{ + deleteLater(); +} + + +void DialogTest::on_pushButton_2_clicked() +{ + QByteArray data(4,0); + data[0]=ui->lineEdit->text().toUInt(); + data[1]=ui->lineEdit_2->text().toUInt(); + data[2]=ui->lineEdit_3->text().toUInt(); + data[3]=ui->lineEdit_4->text().toUInt(); + emit testSendRegSPI(data); +} + + +void DialogTest::on_pushButton_3_clicked() +{ + emit testSendSPI0(); +} + + +void DialogTest::on_pushButton_4_clicked() +{ + QByteArray data; + data.append(ui->lineEdit->text().toUInt()); + emit testReadRegSPI(data); +} + diff --git a/dialogtest.h b/dialogtest.h new file mode 100644 index 0000000..9d2fe65 --- /dev/null +++ b/dialogtest.h @@ -0,0 +1,36 @@ +#ifndef DIALOGTEST_H +#define DIALOGTEST_H + +#include + +namespace Ui { +class DialogTest; +} + +class DialogTest : public QDialog +{ + Q_OBJECT + +public: + explicit DialogTest(QWidget *parent = nullptr); + ~DialogTest(); + +private slots: + void on_pushButton_clicked(); + + void on_pushButton_2_clicked(); + + + void on_pushButton_3_clicked(); + + void on_pushButton_4_clicked(); + +signals: + void testSendRegSPI(QByteArray data); + void testReadRegSPI(QByteArray data); + void testSendSPI0(); +private: + Ui::DialogTest *ui; +}; + +#endif // DIALOGTEST_H diff --git a/dialogtest.ui b/dialogtest.ui new file mode 100644 index 0000000..4947975 --- /dev/null +++ b/dialogtest.ui @@ -0,0 +1,98 @@ + + + DialogTest + + + + 0 + 0 + 493 + 443 + + + + Dialog + + + + + + SPI + + + + + + Fun2: + + + + + + + + + + + + + + + + + + + Send 0 + + + + + + + Set Reg + + + + + + + DAC Channel + + + + + + + DAC # + + + + + + + Fun1: + + + + + + + Read Reg + + + + + + + + + + Back + + + + + + + + diff --git a/mainwindow.cpp b/mainwindow.cpp index 1990074..22830bb 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -7,10 +7,25 @@ MainWindow::MainWindow(QWidget *parent) , ui(new Ui::MainWindow) { ui->setupUi(this); - drvSPI = new DRV_Spi(this); + dacHandler= new DACHandler(this); drvUart = new DRV_Uart(this); + drvSPI = new DRV_Spi(this); fHandler = new FrameHandler(this); - fHandler = new FrameHandler(this); + + + connect(dacHandler,&DACHandler::logMsg,this,&MainWindow::onLogPrint); + + + for (int i = 0; i < 16; ++i) { + for (int j = 0; j < 16; ++j) { + // 将 int 转换为 QString + QString data = QString::number(0); + // 创建 QTableWidgetItem 并设置数据 + QTableWidgetItem *item = new QTableWidgetItem(data); + // 将 item 放入指定的单元格 + ui->tableWidget->setItem(i, j, item); + } + } } MainWindow::~MainWindow() @@ -27,6 +42,33 @@ void MainWindow::onLogPrint(QString msg){ } +void MainWindow::handleTestSendRegSPI(QByteArray data) +{ + QByteArray tmp = fHandler->genREGWrite(data[0],data[1],data[2],data[3]); + QByteArray tmp1=tmp; + drvSPI->spi_Write(tmp1); + onLogPrint(" [SPI] --> Send data success, data is (hex) :" + tmp.toHex(' ')); + +} + +void MainWindow::handleTestSendSPI0() +{ + QByteArray tmp(7,0); + QByteArray tmp1=tmp; + drvSPI->spi_Write(tmp1); + onLogPrint(" [SPI] --> Send data success, data is (hex) :" + tmp.toHex(' ')); + onLogPrint(" [SPI] <-- Read Reg data data is (hex) :" + tmp1.toHex(' ')); +} + +void MainWindow::handleTestReadRegSPI(QByteArray data) +{ + QByteArray tmp = fHandler->genREGRead(data[0]); + QByteArray tmp1=tmp; + drvSPI->spi_Write(tmp1); + onLogPrint(" [SPI] --> Send data success, data is (hex) :" + tmp.toHex(' ')); + +} + void MainWindow::on_pushButton_14_clicked() { dc= new DialogCalibrate(this); @@ -50,7 +92,7 @@ void MainWindow::on_pushButton_6_clicked() for(int i = 0; i < drvUart->m_DRV_Uart_Infors.ulDevCnt; i++) ui->comboBox_7->addItem(drvUart->m_DRV_Uart_Infors.bDeviceName[i]); }else{ - onLogPrint("No devices"); + onLogPrint("No UART devices"); } } @@ -72,9 +114,9 @@ void MainWindow::on_pushButton_7_clicked() ui->lineEdit->setEnabled(true); ui->pushButton_5->setEnabled(true); ui->pushButton_7->setEnabled(false); - onLogPrint("Open device success"); + onLogPrint("Open UART device success"); }else{ - onLogPrint("Open device failed"); + onLogPrint("Fail to open UART device"); } } @@ -89,9 +131,9 @@ void MainWindow::on_pushButton_8_clicked() UCHAR Timeout = this->ui->lineEdit->text().toUInt(); // 调用配置参数函数进行配置 if(drvUart->Uart_setPara(Baudrate, StopBits, Parity, DataBits, Timeout)){ - onLogPrint("Set parameters success"); + onLogPrint("Set UART parameters success"); }else{ - onLogPrint("Fail to set parameters"); + onLogPrint("Fail to set UART parameters"); } } @@ -110,9 +152,9 @@ void MainWindow::on_pushButton_5_clicked() ui->pushButton_5->setEnabled(false); ui->pushButton_7->setEnabled(true); ui->btnSpiSaveSetting->setEnabled(false); - onLogPrint("Close device success"); + onLogPrint("Close UART device success"); }else{ - onLogPrint("Close device failed"); + onLogPrint("Fail to close UART device "); } } @@ -125,7 +167,7 @@ void MainWindow::on_pushButton_clicked() // 将串口设备的名称添加至列表中 ui->comboBox_2->addItem(drvSPI->m_DRV_Spi_Infors.bDeviceName); }else{ - onLogPrint("No devices"); + onLogPrint("No SPI devices"); } } @@ -133,10 +175,10 @@ void MainWindow::on_pushButton_clicked() void MainWindow::on_pushButton_2_clicked() { ULONG index = ui->comboBox_2->currentIndex(); - // 调用串口打开函数 + // 调用打开函数 BOOL status = drvSPI->spi_openDevice(index); - // 判断打开串口设备是否成功 + // 判断打开设备是否成功 if(status == TRUE){ ui->comboSpiMode->setEnabled(true); ui->comboSpiClock->setEnabled(true); @@ -147,15 +189,13 @@ void MainWindow::on_pushButton_2_clicked() ui->pushButton_3->setEnabled(true); ui->pushButton_2->setEnabled(false); ui->btnSpiSaveSetting->setEnabled(true); - onLogPrint("Open device success"); - on_btnSpiSaveSetting_clicked(); // 初始化SPI参数 + onLogPrint("Open SPI device success"); }else{ - onLogPrint("Open device failed"); + onLogPrint("Fail to open SPI device."); } } - void MainWindow::on_pushButton_3_clicked() { @@ -172,9 +212,9 @@ void MainWindow::on_pushButton_3_clicked() ui->pushButton_3->setEnabled(false); ui->pushButton_2->setEnabled(true); - onLogPrint("Close device success"); + onLogPrint("Close SPI device success"); }else{ - onLogPrint("Close device failed"); + onLogPrint("Fail to close device."); } } @@ -239,23 +279,22 @@ void MainWindow::on_pushButton_9_clicked() { for (auto i : REG) { sendRegData += "0x" + QString::number(i, 16) + " "; } - onLogPrint("Ready to write REG[1] - REG[4] : " + sendRegData); + onLogPrint("Ready to set DAC REG[1] - REG[4] is :"+sendRegData); + qDebug()<<"parameter is "<genREGWrite(ucDAC, ucChannel, ucFun1, ucFun2); + - QByteArray tmp1 = fHandler->genREGWrite(ucDAC, ucChannel, ucFun1, ucFun2); switch (ui->comboBox->currentIndex()) { case 0: - if (drvUart->Uart_Write(tmp1)) { - onLogPrint("Send data success"); - } else { - onLogPrint("Send data fail"); + if (!dacHandler->setDACReg(drvUart,tmp1)) + { + return; } break; case 1: - onLogPrint("write data is " + tmp1.toHex()); - if (drvSPI->spi_Write(tmp1)) { - onLogPrint("Send data success"); - } else { - onLogPrint("Send data failed"); + if (!dacHandler->setDACReg(drvSPI,tmp1)) + { + return; } break; } @@ -264,34 +303,78 @@ void MainWindow::on_pushButton_9_clicked() { } void MainWindow::on_pushButton_10_clicked() { - unsigned char ucDAC = ui->comboBox_15->currentText().toUInt(NULL, 10); - QByteArray tmp = fHandler->genREGRead(ucDAC); + unsigned char ucDAC = ui->comboBox_15->currentText().toUInt(NULL, 10); + QByteArray tmp = fHandler->genREGRead(ucDAC); + QByteArray revDatatmp; switch (ui->comboBox->currentIndex()) { - case 0: - if (drvUart->Uart_Write(tmp)) { - onLogPrint("Send data success"); - } else { - onLogPrint("Send data fail"); - } - break; + case 0: + { + revDatatmp = dacHandler->getDACReg(drvUart,tmp); + onLogPrint(" [UART] <-- Read Reg data is :"+revDatatmp.toHex(' ')); + break; + } + case 1: - onLogPrint("write data is " + tmp.toHex()); - if (drvSPI->spi_Write(tmp)) { - onLogPrint("Send data success"); - QTimer::singleShot(1000, [this]() { - QByteArray ba; - drvSPI->spi_Read(ba); - ui->textBrowser->append("SPI read: " + ba.toHex()); - }); - } else { - onLogPrint("Send data fail"); - } - break; + { + revDatatmp= dacHandler->getDACReg(drvSPI,tmp); + onLogPrint(" [SPI] <-- Read Reg data is :"+revDatatmp.toHex(' ')); + break; + } } + + // revDatatmp.append(static_cast(0x00)); + // revDatatmp.append(static_cast(0x81)); + // revDatatmp.append(static_cast(0x11)); + // revDatatmp.append(static_cast(0x01)); + // revDatatmp.append(static_cast(0x21)); + // revDatatmp.append(static_cast(0xa0)); + // revDatatmp.append(static_cast(0x00)); + + // onLogPrint(" [UART] <-- Read Reg data is :"+revDatatmp.toHex(' ')); + + + // for(int i=0;i(revDatatmp[i])); // 正确 + // onLogPrint("DEBUG: revDatatmp is "+hexString); + // } + + if(revDatatmp.length()>=7){ + QByteArray tempHexArray; + + // 设置第一个 lineEdit_3 + tempHexArray = revDatatmp.mid(0, 1); + ui->lineEdit_3->setText("0x" + tempHexArray.toHex()); + + // 设置 lineEdit_8 + tempHexArray = revDatatmp.mid(1, 1); + ui->lineEdit_8->setText("0x" + tempHexArray.toHex()); + + // 设置 lineEdit_13 + tempHexArray = revDatatmp.mid(2, 1); + ui->lineEdit_13->setText("0x" + tempHexArray.toHex()); + + // 设置 lineEdit_10 + tempHexArray = revDatatmp.mid(3, 1); + ui->lineEdit_10->setText("0x" + tempHexArray.toHex()); + + // 设置 lineEdit_12 + tempHexArray = revDatatmp.mid(4, 1); + ui->lineEdit_12->setText("0x" + tempHexArray.toHex()); + + // 设置 lineEdit_9 + tempHexArray = revDatatmp.mid(5, 1); + ui->lineEdit_9->setText("0x" + tempHexArray.toHex()); + + // 设置 lineEdit_11 + tempHexArray = revDatatmp.mid(6, 1); + ui->lineEdit_11->setText("0x" + tempHexArray.toHex()); + } + } void MainWindow::on_btnSpiSaveSetting_clicked() { - drvSPI->spi_Init( + BOOL ret = false; + ret= drvSPI->spi_Init( ui->comboSpiMode->currentIndex(), ui->comboSpiClock->currentIndex(), ui->comboSpiByteOrder->currentIndex(), @@ -299,31 +382,294 @@ void MainWindow::on_btnSpiSaveSetting_clicked() { ui->comboSpiCS1Pol->currentIndex(), ui->comboSpiCS2Pol->currentIndex() ); + if(ret){ + onLogPrint("Save SPI parameters success"); + }else{ + onLogPrint("Fail to set SPI parameters."); + } } -void MainWindow::onWriteRegButtonClicked() { - QByteArray tmp = fHandler->genREGWrite(0x01, 0x01, 0x09, 0x81); - ui->textBrowser->append("SPI write: " + tmp.toHex()); - drvSPI->spi_Write(tmp); + + +void MainWindow::on_pushButton_11_clicked() +{ + // 弹出“打开文件”对话框 + QString filePath = QFileDialog::getOpenFileName( + this, + tr("Choose csv file"), + QDir::homePath(), + tr("csv文件 (*.csv);") + ); + + if(filePath.isEmpty()){ + onLogPrint("Empty file."); + return; + } + + int voltageValue[16][16]={0}; + const int rows = 16; + const int cols = 16; + + QFile file(filePath); + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + qWarning() << "Can't open file" << file.errorString(); + return ; + } + + QTextStream in(&file); + int rowCount = 0; + + // 逐行读取文件,直到文件末尾或达到最大行数 + while (!in.atEnd() && rowCount < rows) { + QString line = in.readLine(); + QStringList fields = line.split(','); + + + // 遍历当前行的所有字段,并将其转换为整数 + for (int colCount = 0; colCount < cols; ++colCount) { + bool ok; + int value = fields.at(colCount).toInt(&ok); + + if (ok) { + voltageValue[rowCount][colCount] = value; + } else { + onLogPrint("Row is "+QString::number(rowCount + 1)+" Column is "+QString::number(colCount + 1)+" has invalid data"); + voltageValue[rowCount][colCount] = 0; // 或者设置为其他默认值 + } + } + + rowCount++; + } + + file.close(); + + ui->tableWidget->clearContents(); + + for (int i = 0; i < 16; ++i) { + for (int j = 0; j < 16; ++j) { + // 将 int 转换为 QString + QString data = QString::number(voltageValue[i][j]); + // 创建 QTableWidgetItem 并设置数据 + QTableWidgetItem *item = new QTableWidgetItem(data); + // 将 item 放入指定的单元格 + ui->tableWidget->setItem(i, j, item); + } + } + ui->label_13->setText("Ready to write : "); + + } -void MainWindow::onReadRegButtonClicked() { - QByteArray tmp = fHandler->genREGRead(0x01); - ui->textBrowser->append("SPI write: " + tmp.toHex()); - QTimer::singleShot(2000, [this]() { - QByteArray ba; - drvSPI->spi_Read(ba); - ui->textBrowser->append("SPI read: " + ba.toHex()); - }); + +void MainWindow::on_pushButton_12_clicked() +{ + // // 从1至256通道依次读取电压值 + // for(ch_index = 1; ch_index <= 256; ch_index++) + // { + // // 设置配置项 + // this->ui->comboBox_icdDacCh->setCurrentIndex(0); // 设置通道值为全0 + // this->ui->checkBox_icdTRIG_TADC->setCheckState(Qt::Unchecked); // 关闭TADC + // this->ui->pushButton_icdRegConfig->click(); // 触发配置寄存器按钮 + // QThread::msleep(10); // 延时10毫秒 + // this->ui->checkBox_icdTRIG_TADC->setCheckState(Qt::Checked); // 开启TADC + // this->ui->comboBox_icdDacCh->setCurrentIndex(ch_index); //设置当前通道 + // this->ui->pushButton_icdRegConfig->click(); // 触发配置寄存器按钮 + // QThread::msleep(10); // 延时10毫秒 + // this->ui->pushButton_icdRegConfig->click(); // 再次触发配置寄存器按钮 + // QThread::msleep(10); // 延时10毫秒 + // this->ui->pushButton_icdRegRead->click(); // 读取寄存器值 + // QThread::msleep(10); // 延时10毫秒 + // } + + int voltage[256]={0}; + +#if 1 + QByteArray regData[256]; + unsigned char ucDAC = ui->comboBox_15->currentText().toUInt(NULL, 10); + QByteArray cmd1 = fHandler->genREGWrite(ucDAC,0x00,0x09,0x11); //关闭TADC + + QByteArray cmd3 = fHandler->genREGRead(ucDAC); // 读取寄存器值 + int mode = ui->comboBox->currentIndex(); + for(int i=1;i<=256;i++){ + // ui->lineEdit_2->setText(QString::number(0)); + // ui->checkBox_5->setCheckState(Qt::Unchecked); + // on_pushButton_9_clicked(); + // QThread::msleep(10); // 延时10毫秒 + // ui->checkBox_5->setCheckState(Qt::Checked); + // ui->lineEdit_2->setText(QString::number(i)); + // on_pushButton_9_clicked(); + // QThread::msleep(10); // 延时10毫秒 + // on_pushButton_9_clicked(); + // QThread::msleep(10); // 延时10毫秒 + // on_pushButton_10_clicked(); //读取寄存器的值 + + QByteArray cmd2; + if(i!=256){ + cmd2= fHandler->genREGWrite(ucDAC,(unsigned char)i,0x19,0x11); //开启TADC + }else{ + cmd2 =fHandler->genREGWrite(ucDAC|0x80,0x00,0x19,0x11); + } + + switch(mode){ + case 0:{ + dacHandler->setDACReg(drvUart,cmd1); + QThread::msleep(10); // 延时10毫秒 + dacHandler->setDACReg(drvUart,cmd2); + QThread::msleep(10); // 延时10毫秒 + dacHandler->setDACReg(drvUart,cmd2); + QThread::msleep(10); // 延时10毫秒 + QByteArray revData = dacHandler->getDACReg(drvUart,cmd3); + onLogPrint(" [UART] <-- Read Reg data is :"+revData.toHex(' ')); + if(revData.length()>=7){ + regData[i-1]=revData; + }else{ + QByteArray tmp(7,0); + regData[i-1]=tmp; + } + break; + } + case 1: + { + dacHandler->setDACReg(drvSPI,cmd1); + QThread::msleep(10); // 延时10毫秒 + dacHandler->setDACReg(drvSPI,cmd2); + QThread::msleep(10); // 延时10毫秒 + dacHandler->setDACReg(drvSPI,cmd2); + QThread::msleep(10); // 延时10毫秒 + regData[i-1]=dacHandler->getDACReg(drvSPI,cmd3); + onLogPrint(" [SPI] <-- Read Reg data is :"+regData[i-1].toHex(' ')); + break; + } + } + + // qDebug()<<"index is "+QString::number(i); + } + + //再次关闭TADC + switch(mode){ + case 0 :{ + dacHandler->setDACReg(drvUart,cmd1); + break; + } + case 1:{ + dacHandler->setDACReg(drvSPI,cmd1); + break; + } + } +#endif + + for(int j=0;j<256;j++){ + // voltage[j]=((regData[j].at(1)&0x3)<<8)|regData[j].at(2); + voltage[j]=((static_cast((regData[j].at(1))&0x03))<<8)|(static_cast((regData[j].at(2))&0xff));; + } + + + + for (int i = 0; i < 16; ++i) { + for (int j = 0; j < 16; ++j) { + // 将 int 转换为 QString + QString data = QString::number(voltage[i*16+j]); + // qDebug()<tableWidget->setItem(i, j, item); + } + } + ui->label_13->setText("Read voltage from chip : "); } -void MainWindow::on_comboBox_currentTextChanged(const QString& text) { -// if (text.toLower() == "spi") { -// connect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::onWriteRegButtonClicked); -// connect(ui->pushButton_10, &QPushButton::clicked, this, &MainWindow::onReadRegButtonClicked); -// } else { -// disconnect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::onWriteRegButtonClicked); -// disconnect(ui->pushButton_10, &QPushButton::clicked, this, &MainWindow::onReadRegButtonClicked); -// } + +void MainWindow::on_pushButton_4_clicked() +{ + unsigned int voltageValue[16][16]={0}; + // 遍历表格的每一行和每一列 + for (int row = 0; row < 16; ++row) { + for (int col = 0; col < 16; ++col) { + // 检查单元格是否存在 + if (QTableWidgetItem *item = ui->tableWidget->item(row, col)) { + // 获取单元格文本并转换为整数 + bool ok; + unsigned int value = item->text().toUInt(&ok); + if (ok) { + // 转换成功,将值存入数组 + voltageValue[row][col] = value; + } else { + // 转换失败(例如单元格为空或包含非数字字符), + // 在这里可以处理错误,比如设置一个默认值 + voltageValue[row][col] = 0; // 默认值 + } + } else { + // 单元格不存在,也设置一个默认值 + voltageValue[row][col] = 0; // 默认值 + } + } + } + unsigned char ucDAC = ui->comboBox_15->currentText().toUInt(NULL, 10); + + QByteArray tmp = fHandler->genVoltage(ucDAC,voltageValue); + + + switch (ui->comboBox->currentIndex()) { + case 0:{ + if(!dacHandler->setDACVoltage(drvUart,tmp)) + { + return; + } + break; + } + + case 1: + { + + if(!dacHandler->setDACVoltage(drvSPI,tmp)) + { + return; + } + break; + } + } +} + + +void MainWindow::on_pushButton_13_clicked() +{ + ui->textBrowser->clear(); +} + + +void MainWindow::on_pushButton_16_clicked() +{ + // 1. 获取当前时间并格式化 + QString currentDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss"); + + // 2. 构建完整的文件名 + QString fileName = currentDateTime + ".txt"; + + // 3. 创建 QFileDialog 并打开文件 + QFile file(fileName); + if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { + // 4. 使用 QTextStream 写入文本内容 + QTextStream out(&file); + + // 获取 QTextBrowser 的纯文本内容 + out << ui->textBrowser->toPlainText(); + file.close(); + onLogPrint("Save log success, filename is "+fileName); + }else{ + onLogPrint("Fail to save log."); + } +} + + +void MainWindow::on_pushButton_15_clicked() +{ + dt = new DialogTest(this); + connect(dt,&DialogTest::testReadRegSPI,this,&MainWindow::handleTestReadRegSPI); + connect(dt,&DialogTest::testSendRegSPI,this,&MainWindow::handleTestSendRegSPI); + connect(dt,&DialogTest::testSendSPI0,this,&MainWindow::handleTestSendSPI0); + dt->show(); + } diff --git a/mainwindow.h b/mainwindow.h index 14081e1..99020d7 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -3,12 +3,12 @@ #include #include "dialogcalibrate.h" -#include "src/headers/drv_uart.h" -#include "src/headers/drv_spi.h" #include "QDateTime" -#include "src/headers/framehandler.h"#include "src/headers/framehandler.h" - - +#include "QFile" +#include "QFileDialog" +#include "QTextStream" +#include "dachandler.h" +#include "dialogtest.h" QT_BEGIN_NAMESPACE namespace Ui { @@ -28,6 +28,9 @@ public: public slots: void onLogPrint(QString msg); + void handleTestSendRegSPI(QByteArray data); + void handleTestReadRegSPI(QByteArray data); + void handleTestSendSPI0(); private slots: void on_pushButton_14_clicked(); @@ -54,20 +57,30 @@ private slots: void on_pushButton_10_clicked(); void on_btnSpiSaveSetting_clicked(); - void onWriteRegButtonClicked(); - void onReadRegButtonClicked(); - void on_comboBox_currentTextChanged(const QString& text); + void on_pushButton_11_clicked(); -private: - Ui::MainWindow *ui; - DialogCalibrate *dc; + void on_pushButton_12_clicked(); + + void on_pushButton_4_clicked(); + + void on_pushButton_13_clicked(); + + void on_pushButton_16_clicked(); + + void on_pushButton_15_clicked(); + +public : DRV_Uart *drvUart; DRV_Spi *drvSPI; FrameHandler *fHandler; - FrameHandler *fHandler; +private: + Ui::MainWindow *ui; + DialogCalibrate *dc; + DACHandler *dacHandler; + DialogTest *dt; }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 68ddaea..69e3483 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -90,7 +90,7 @@ Timeout(ms): - Qt::AlignmentFlag::AlignCenter + Qt::AlignCenter @@ -149,7 +149,7 @@ Data Bits: - Qt::AlignmentFlag::AlignCenter + Qt::AlignCenter @@ -159,7 +159,7 @@ Stop Bits: - Qt::AlignmentFlag::AlignCenter + Qt::AlignCenter @@ -258,7 +258,7 @@ Baud Rate: - Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter @@ -268,7 +268,7 @@ Parity Bits: - Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter @@ -288,7 +288,7 @@ UART Device List: - Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter @@ -311,7 +311,7 @@ SPI Device List: - Qt::AlignmentFlag::AlignCenter + Qt::AlignCenter @@ -358,7 +358,7 @@ SPI Mode: - Qt::AlignmentFlag::AlignCenter + Qt::AlignCenter @@ -444,7 +444,7 @@ Clock: - Qt::AlignmentFlag::AlignCenter + Qt::AlignCenter @@ -454,7 +454,7 @@ LSB/MSB - Qt::AlignmentFlag::AlignCenter + Qt::AlignCenter @@ -491,7 +491,7 @@ DataBits: - Qt::AlignmentFlag::AlignCenter + Qt::AlignCenter @@ -572,21 +572,35 @@ Voltage Check - - - - Import Voltage From File - - - - + Read Voltage From Chip - + + + + Send Voltage + + + + + + + Start Calibrate + + + + + + + Ready to write: + + + + 16 @@ -628,10 +642,10 @@ - - + + - Start Calibrate + Import Voltage From File @@ -641,10 +655,10 @@ - Qt::Orientation::Horizontal + Qt::Horizontal - QSizePolicy::Policy::MinimumExpanding + QSizePolicy::MinimumExpanding @@ -666,6 +680,13 @@ Log + + + + Clear + + + @@ -680,9 +701,9 @@ - + - Clear + Save @@ -728,16 +749,16 @@ Reg Value - - - - - + + - Reg 3: + Reg 4: + + + @@ -745,11 +766,14 @@ - - + + - - + + + + + @@ -758,11 +782,25 @@ - - + + - - + + + + Reg 1: + + + + + + + + + + Reg 3: + + @@ -771,22 +809,15 @@ - - - - - + + - Reg 4: + Reg 7: - - - - Reg 1: - - + + @@ -1092,6 +1123,13 @@ + + + + Test Mode + + + @@ -1100,7 +1138,7 @@ 0 0 839 - 33 + 21 diff --git a/src/headers/crc.h b/src/headers/crc.h deleted file mode 100644 index 6d37821..0000000 --- a/src/headers/crc.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef CRC_H -#define CRC_H - -#include - -// 直接计算反射型CRC16-CCITT,输出低字节在前的QByteArray -QByteArray crc16Reverse(const QByteArray& data); -// void generate_crc16_reverse_table(quint16 table[256]); - -#endif // CRC_H diff --git a/src/headers/dachandler.h b/src/headers/dachandler.h new file mode 100644 index 0000000..bfe520b --- /dev/null +++ b/src/headers/dachandler.h @@ -0,0 +1,41 @@ +#ifndef DACHANDLER_H +#define DACHANDLER_H + +#include +#include "src/headers/framehandler.h" +#include "src/headers/drv_uart.h" +#include "src/headers/drv_spi.h" +#include "QThread" + +class DACHandler : public QObject +{ + Q_OBJECT +public: + explicit DACHandler(QObject *parent = nullptr); + + //UART + bool setDACVoltage(DRV_Uart *drvUart, QByteArray &data); //发送一个DAC的电压,done + bool setVoltage(DRV_Uart *drvUart, QByteArray &data); //发送所有DAC的电压 + bool setDACReg(DRV_Uart *drvUart, QByteArray &data); //设置DAC寄存器,done + + QByteArray getDACReg(DRV_Uart *drvUart, QByteArray &data); // 回读DAC寄存器,done + bool getChannelVoltage(DRV_Uart *drvUart,QByteArray &data); //回读指定通道的电压值 + bool getDACVoltage(DRV_Uart *drvUart, QByteArray &data); //回读指定DAC的所有电压值 + + //SPI + bool setDACVoltage(DRV_Spi *drvSPI, QByteArray &data); //发送一个DAC的电压,done + bool setVoltage(DRV_Spi *drvSPI,QByteArray &data); //发送所有DAC的电压 + bool setDACReg(DRV_Spi *drvSPI, QByteArray &data); //设置DAC寄存器,done + + QByteArray getDACReg(DRV_Spi *drvSPI, QByteArray &data); // 回读DAC寄存器,done + bool getChannelVoltage(DRV_Spi *drvSPI,QByteArray &data); //回读指定通道的电压值 + bool getDACVoltage(DRV_Spi *drvSPI, QByteArray &data); //回读指定DAC的所有电压值 + + + + +signals: + void logMsg(QString msg); +}; + +#endif // DACHANDLER_H diff --git a/src/headers/framehandler.h b/src/headers/framehandler.h index 4c763a0..2b2fc67 100644 --- a/src/headers/framehandler.h +++ b/src/headers/framehandler.h @@ -12,6 +12,7 @@ public: explicit FrameHandler(QObject *parent = nullptr); QByteArray genREGWrite(unsigned char ucDAC,unsigned char ucChannel,unsigned char ucFun1,unsigned char ucFun2); QByteArray genREGRead(unsigned char ucDAC); + QByteArray genVoltage(unsigned char ucDAC, unsigned int (*voltage)[16]); //fixed lenth is 16*16; QByteArray genCrc16(const QByteArray& data); QByteArray genSum(const QByteArray& data); static void genCrc16table(quint16 table[256], quint16 poly); diff --git a/src/sources/crc.cpp b/src/sources/crc.cpp deleted file mode 100644 index e8ba122..0000000 --- a/src/sources/crc.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "crc.h" - -// 函数声明 -// void generate_crc16_reverse_table(quint16 *table); -// QByteArray crc16Reverse(const QByteArray& data); - - diff --git a/src/sources/dachandler.cpp b/src/sources/dachandler.cpp new file mode 100644 index 0000000..4671d51 --- /dev/null +++ b/src/sources/dachandler.cpp @@ -0,0 +1,91 @@ +#include "dachandler.h" +#include "dachandler.h" + +DACHandler::DACHandler(QObject *parent) + : QObject{parent} +{ + +} + +bool DACHandler::setDACVoltage(DRV_Uart *drvUart, QByteArray &data) +{ + + if (drvUart->Uart_Write(data)) { + emit logMsg(" [UART] --> Send data success, data is (hex) :" + data.toHex(' ')); + return true; + } else { + emit logMsg(" [UART] Send data fail"); + return false; + } +} + + + +bool DACHandler::setDACReg(DRV_Uart *drvUart, QByteArray &data) +{ + if(drvUart->Uart_Write(data)){ + emit logMsg(" [UART] --> Send data success, data is (hex) :" + data.toHex(' ')); + return true; + }else{ + emit logMsg(" [UART] Send data failed"); + return false; + } +} + +QByteArray DACHandler::getDACReg(DRV_Uart *drvUart, QByteArray &data) +{ + if (drvUart->Uart_Write(data)) { + emit logMsg(" [UART] --> Send data success, data is (hex) :" + data.toHex(' ')); + } else { + emit logMsg(" [UART] Send data failed"); + } + QThread::msleep(2); + QByteArray revDatatmp; + drvUart->Uart_Read(revDatatmp); + return revDatatmp; +} + +bool DACHandler::setDACVoltage(DRV_Spi *drvSPI, QByteArray &data) +{ + QByteArray data1= data; + if (drvSPI->spi_Write(data1)) { + emit logMsg(" [SPI] --> Send data success, data is (hex) :" + data.toHex(' ')); + return true; + } else { + emit logMsg(" [SPI] Send data fail"); + return false; + } +} + + +bool DACHandler::setDACReg(DRV_Spi *drvSPI, QByteArray &data) +{ + QByteArray data1= data; + if(drvSPI->spi_Write(data1)){ + emit logMsg(" [SPI] --> Send data success, data is (hex) :" + data.toHex(' ')); + return true; + }else{ + emit logMsg(" [SPI] Send data failed"); + return false; + } +} + +QByteArray DACHandler::getDACReg(DRV_Spi *drvSPI, QByteArray &data) +{ + QByteArray revData(7,0); + QByteArray data1= data; + if(drvSPI->spi_Write(data1)){ + emit logMsg(" [SPI] --> Send data success, data is (hex) :" + data.toHex(' ')); + QThread::msleep(2); + if(drvSPI->spi_Write(revData)){ + emit logMsg(" [SPI] --> Send data success, data is (hex) :" + revData.toHex(' ')); + }else{ + emit logMsg(" [SPI] Send data failed"); + } + }else{ + emit logMsg(" [SPI] Send data failed"); + } + return revData; +} + + diff --git a/src/sources/framehandler.cpp b/src/sources/framehandler.cpp index c3b164d..bfd4ac1 100644 --- a/src/sources/framehandler.cpp +++ b/src/sources/framehandler.cpp @@ -91,3 +91,56 @@ QByteArray FrameHandler::genREGRead(unsigned char ucDAC) nFrameCount++; return cmd1; } + +QByteArray FrameHandler::genVoltage(unsigned char ucDAC, unsigned int (*voltage)[16]) +{ + QByteArray cmd1(17,0); + cmd1[0]=0x9F; + cmd1[1]=0xE4; + cmd1[2]=0x03; + cmd1[3]=0x03; + cmd1[4]=ucDAC; + cmd1[14]=nFrameCount; + QByteArray sumResult = genSum(cmd1); + cmd1[15]=static_cast(sumResult.at(0)); + cmd1[16]=0x02; + + uint16_t data_in[256]; // 16x16=256个元素 + for(int i = 0; i < 16; i++) { + for(int j = 0; j < 16; j++) { + // 确保10bit数据在0-1023范围内 + data_in[i*16 + j] = static_cast(voltage[i][j] & 0x3FF); + } + } + + // 2. 准备输出缓冲区 + QByteArray data_out; + data_out.resize(320); // 64*5=320字节 + + // 3. 执行原始的数据转换,已经顺序交换 + // for(uint16_t i = 0; i < 64; i++) { + // data_out[i*5] = static_cast(data_in[i*4+3] >> 2); + // data_out[i*5+1] = static_cast((data_in[i*4+3] << 6) | (data_in[i*4+2] >> 4)); + // data_out[i*5+2] = static_cast((data_in[i*4+2] << 4) | (data_in[i*4+1] >> 6)); + // data_out[i*5+3] = static_cast((data_in[i*4+1] << 2) | (data_in[i*4] >> 8)); + // data_out[i*5+4] = static_cast(data_in[i*4] & 0xFF); + // } + + for(uint16_t i = 0; i < 64; i++) { + data_out[i*5] = static_cast(data_in[i*4] & 0xFF); + data_out[i*5+1] = static_cast((data_in[i*4+1] << 2) | (data_in[i*4] >> 8)); + data_out[i*5+2] = static_cast((data_in[i*4+2] << 4) | (data_in[i*4+1] >> 6)); + data_out[i*5+3] = static_cast((data_in[i*4+3] << 6) | (data_in[i*4+2] >> 4)); + data_out[i*5+4] = static_cast(data_in[i*4+3] >> 2); + } + + QByteArray crcRes= genCrc16(data_out); + + + QByteArray cmd = cmd1+data_out+crcRes; + cmd.append(genSum(cmd)); + + nFrameCount++; + + return cmd; +} diff --git a/voltageTmp/voltagetmp_1.csv b/voltageTmp/voltagetmp_1.csv new file mode 100644 index 0000000..284a12a --- /dev/null +++ b/voltageTmp/voltagetmp_1.csv @@ -0,0 +1,16 @@ +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1