From 6a524204172d0d7f835c1b0e6bb7c6631039d28e Mon Sep 17 00:00:00 2001 From: yuntang <123@qq.com> Date: Tue, 18 Mar 2025 17:28:40 +0800 Subject: [PATCH] 2025-03-18T17:28:39 --- src/data/filemanager.cpp | 40 ++++--- src/data/filemanager.h | 1 + src/main.cpp | 3 + src/mainwindow.cpp | 8 +- src/serialport/dataparser.cpp | 12 +- src/serialport/protocol.h | 2 + src/serialport/serialport.cpp | 32 ++--- src/serialport/serialport.h | 4 +- src/ui/experimentsettingform.cpp | 199 ++++++++++++++++++++++++++++--- src/ui/experimentsettingform.h | 2 +- 10 files changed, 242 insertions(+), 61 deletions(-) diff --git a/src/data/filemanager.cpp b/src/data/filemanager.cpp index eecd475..8207daf 100644 --- a/src/data/filemanager.cpp +++ b/src/data/filemanager.cpp @@ -12,17 +12,17 @@ QFile _expeFile; void writeExperimentFile(const CommonData &cd) { + if(!_expeFile.isOpen()){ + qDebug()<<"FileManager,file not open."; + + createExperimentFile(); + } QTextStream out(&_expeFile); out.setRealNumberPrecision(3); // 设置精度为三位小数 - out.setFieldWidth(12); // 设置字段宽度,确保对齐 - out.setPadChar(' '); // 设置填充字符为空格 + out.setRealNumberNotation(QTextStream::FixedNotation); - // 写入数据 - out << cd.sample_temp << ", " + out << cd.sample_temp << " " << cd.dsc << endl; - - // 关闭文件 - _expeFile.close(); } void createExperimentFile() @@ -32,28 +32,40 @@ void createExperimentFile() fileName = "sampleName"; } - fileName.append("-"); + fileName.append("_"); if(_expeInfo.date.isEmpty()){ - fileName.append(QDateTime::currentDateTime().toUTC().toString("yyyyMMdd_HHmmss")); +// fileName.append(QDateTime::currentDateTime().toUTC().toString("yyyyMMdd_HHmmss")); + fileName.append(QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss")); }else{ fileName.append(_expeInfo.date); } + fileName.append(".txt"); + + qDebug()<<"fileName:"<slotSendData(ba); // SerialPort::instance()->slotCloseSp(); + + FileManager::fileClose(); } void MainWindow::on_actionNew_triggered() @@ -117,10 +119,10 @@ void MainWindow::on_actionConnectToDev_triggered() { if (SerialPort::instance()->openSp()) { - ui->actionNew->setEnabled(true); + setActionEnable(true); Global::instance()->setMode(Global::Mode::ConnectedToDev); - QThread::msleep(300); - SerialPort::instance()->senddata(DataParser::inquirePhaseInfo()); + QByteArray ba = DataParser::inquirePhaseInfo(); + SerialPort::instance()->sendData(ba); } else { diff --git a/src/serialport/dataparser.cpp b/src/serialport/dataparser.cpp index cd3d131..a03bae0 100644 --- a/src/serialport/dataparser.cpp +++ b/src/serialport/dataparser.cpp @@ -195,18 +195,14 @@ bool DataParser::slotDataParser(const QByteArray &ba) spp.head = FRANE_HEAD; spp.cmd = READ_CMD; spp.addr = 0x0050; - + spp.len = 6; // - int inquireDataLength = (u8)sizeof(Phase) * 6; - - spp.data_buf[0] = inquireDataLength; - spp.len = 5 + inquireDataLength; + spp.data_buf[0] = PHASE_BYTE_SIZE * 6 + 2; // - int crcDataLength = 3 + inquireDataLength; u8 *dataPtr = (u8 *)&spp; - u16 crc = modbusCRC16((u8 *)(dataPtr + 3), crcDataLength); + u16 crc = modbusCRC16((u8 *)(dataPtr + 3), 4); // - int sppValidLength = 6 + inquireDataLength; + int sppValidLength = 6 + 1; return QByteArray((char *)&spp, sppValidLength) + QByteArray((char *)&crc, 2); } diff --git a/src/serialport/protocol.h b/src/serialport/protocol.h index faa39f0..0a985b1 100644 --- a/src/serialport/protocol.h +++ b/src/serialport/protocol.h @@ -6,7 +6,9 @@ #define FRANE_HEAD 0x5AA5 //枕头 #define WRITE_CMD 0x82 //写指令 #define READ_CMD 0x83 //读指令 + #define PHASE_START_ADDR 0X0050 +#define PHASE_BYTE_SIZE 12 #pragma pack(push) #pragma pack(1) diff --git a/src/serialport/serialport.cpp b/src/serialport/serialport.cpp index 2df4000..ffbf5fb 100644 --- a/src/serialport/serialport.cpp +++ b/src/serialport/serialport.cpp @@ -95,20 +95,26 @@ void SerialPort::slotReadData() { #if 1 QByteArray ba = _sp->readAll(); - -#if 0 + if(ba.size() == 0){ + return; + } +#if 1 QString hexData = ba.toHex(' '); // ' ' 作为分隔符,可选参数 qDebug() << "receive info (hex):" << hexData; #endif - SerialPortProtocol *spp = (SerialPortProtocol *)ba.data(); - // qDebug()<<"header:"<head,16).toUpper(); if (FRANE_HEAD != spp->head) { qDebug() << "Data header error."; return; } + //phase setting data + if(spp->addr == PHASE_START_ADDR){ + emit sigSendPhaseInfo(ba); + return; + } + int dataLength = spp->len - 5; CommonData cd; @@ -119,22 +125,17 @@ void SerialPort::slotReadData() { writeCmdHandle(dataLength, spp->addr, cd); } - else + else if (READ_CMD == spp->cmd) { // read data if (spp->addr == 0) { - if (Global::Mode::ExperimentStart == Global::instance()->getMode()) { emit sigSendCommonData(cd); } emit sigSendCommonDataToRealDataForm(cd); } - else if(spp->addr == PHASE_START_ADDR) - { - emit sigSendPhaseInfo(ba); - } } #endif } @@ -420,15 +421,18 @@ void SerialPort::slotDeliverData(const QByteArray &ba) void SerialPort::slotSendData(const QByteArray &ba) { - /*** - * 写入的数据有 - * 1、阶段设置。 - */ +#if 0 if (WRITE_CMD == ba.at(3)) { _lastWriteBa = ba; } +#endif + +#if 1 qDebug() << "slotSendData:" << ba.size(); + QString hexData = ba.toHex(' '); // ' ' 作为分隔符,可选参数 + qDebug() << "slotSendData:" << hexData; +#endif if (_sp != nullptr && _sp->isOpen()) { diff --git a/src/serialport/serialport.h b/src/serialport/serialport.h index 4ac6812..6eb8225 100644 --- a/src/serialport/serialport.h +++ b/src/serialport/serialport.h @@ -23,9 +23,7 @@ public: ~SerialPort(); void sendData(const QByteArray &data) - { - slotSendData(data); - } + {slotSendData(data); } void sendCmd(const E_CMD_TYPE e); static void parserTest(); bool openSp(); diff --git a/src/ui/experimentsettingform.cpp b/src/ui/experimentsettingform.cpp index 2e4bdba..e90c4db 100644 --- a/src/ui/experimentsettingform.cpp +++ b/src/ui/experimentsettingform.cpp @@ -8,8 +8,15 @@ #include "global.h" #include "filemanager.h" +QString extractObjectName(const QString& fullObjectName) { + return fullObjectName.mid(4); // 去掉前 4 个字符("ui->") +} + +#define SET_OBJECT_NAME(WIDGET) \ + WIDGET->setObjectName(extractObjectName(#WIDGET)) + ExperimentSettingForm::ExperimentSettingForm(QWidget *parent) : QWidget(parent), - ui(new Ui::ExperimentSettingForm) + ui(new Ui::ExperimentSettingForm) { ui->setupUi(this); uiReset(); @@ -62,7 +69,7 @@ void ExperimentSettingForm::uiReset() ui->phase_4_constant_temp->setText("0"); ui->phase_5_constant_temp->setText("0"); ui->phase_6_constant_temp->setText("0"); - + // ui->phase_2_cutoff_temp->setEnabled(false); ui->phase_3_cutoff_temp->setEnabled(false); ui->phase_4_cutoff_temp->setEnabled(false); @@ -94,22 +101,97 @@ void ExperimentSettingForm::uiReset() ui->checkBox_phase_4->setTristate(false); ui->checkBox_phase_5->setTristate(false); ui->checkBox_phase_6->setTristate(false); -} -void ExperimentSettingForm::uiUpdatePhase(QVector &vtr) -{ - for(int i = 0; i< vtr.size(); i++){ - const Phase &phase = vtr[i]; - if(phase.onoff == 0){ - return; - } - QString checkBoxName = QString("checkBox_phase_%1").arg(i); - QCheckBox *checkBox = ui->findChild(checkBoxName); - if (checkBox) { - checkBox->setTristate(false); - } + //set objectName + SET_OBJECT_NAME(ui->checkBox_phase_1); + SET_OBJECT_NAME(ui->checkBox_phase_2); + SET_OBJECT_NAME(ui->checkBox_phase_3); + SET_OBJECT_NAME(ui->checkBox_phase_4); + SET_OBJECT_NAME(ui->checkBox_phase_5); + SET_OBJECT_NAME(ui->checkBox_phase_6); + + SET_OBJECT_NAME(ui->phase_1_cutoff_temp); + SET_OBJECT_NAME(ui->phase_2_cutoff_temp); + SET_OBJECT_NAME(ui->phase_3_cutoff_temp); + SET_OBJECT_NAME(ui->phase_4_cutoff_temp); + SET_OBJECT_NAME(ui->phase_5_cutoff_temp); + SET_OBJECT_NAME(ui->phase_6_cutoff_temp); + + SET_OBJECT_NAME(ui->phase_1_scan_rate); + SET_OBJECT_NAME(ui->phase_2_scan_rate); + SET_OBJECT_NAME(ui->phase_3_scan_rate); + SET_OBJECT_NAME(ui->phase_4_scan_rate); + SET_OBJECT_NAME(ui->phase_5_scan_rate); + SET_OBJECT_NAME(ui->phase_6_scan_rate); + + SET_OBJECT_NAME(ui->phase_1_constant_temp); + SET_OBJECT_NAME(ui->phase_2_constant_temp); + SET_OBJECT_NAME(ui->phase_3_constant_temp); + SET_OBJECT_NAME(ui->phase_4_constant_temp); + SET_OBJECT_NAME(ui->phase_5_constant_temp); + SET_OBJECT_NAME(ui->phase_6_constant_temp); + + SET_OBJECT_NAME(ui->comboBox_phase_1_atmosphere); + SET_OBJECT_NAME(ui->comboBox_phase_2_atmosphere); + SET_OBJECT_NAME(ui->comboBox_phase_3_atmosphere); + SET_OBJECT_NAME(ui->comboBox_phase_4_atmosphere); + SET_OBJECT_NAME(ui->comboBox_phase_5_atmosphere); + SET_OBJECT_NAME(ui->comboBox_phase_6_atmosphere); + + SET_OBJECT_NAME(ui->radioButton_OIT_not); + SET_OBJECT_NAME(ui->comboBox_initial_atmosphere); +} + +void ExperimentSettingForm::uiSetPhaseEnable(const int index) +{ + switch (index) { + case 1: + ui->checkBox_phase_1->setTristate(true); + ui->phase_1_cutoff_temp->setEnabled(true); + ui->phase_1_scan_rate->setEnabled(true); + ui->phase_1_constant_temp->setEnabled(true); + ui->comboBox_phase_1_atmosphere->setEnabled(true); + break; + case 2: + ui->checkBox_phase_2->setTristate(true); + ui->phase_2_cutoff_temp->setEnabled(true); + ui->phase_2_scan_rate->setEnabled(true); + ui->phase_3_constant_temp->setEnabled(true); + ui->comboBox_phase_1_atmosphere->setEnabled(true); + break; + case 3: + ui->checkBox_phase_1->setTristate(true); + ui->phase_1_cutoff_temp->setEnabled(true); + ui->phase_1_scan_rate->setEnabled(true); + ui->phase_1_constant_temp->setEnabled(true); + ui->comboBox_phase_1_atmosphere->setEnabled(true); + break; + case 4: + ui->checkBox_phase_1->setTristate(true); + ui->phase_1_cutoff_temp->setEnabled(true); + ui->phase_1_scan_rate->setEnabled(true); + ui->phase_1_constant_temp->setEnabled(true); + ui->comboBox_phase_1_atmosphere->setEnabled(true); + break; + case 5: + ui->checkBox_phase_1->setTristate(true); + ui->phase_1_cutoff_temp->setEnabled(true); + ui->phase_1_scan_rate->setEnabled(true); + ui->phase_1_constant_temp->setEnabled(true); + ui->comboBox_phase_1_atmosphere->setEnabled(true); + break; + case 6: + ui->checkBox_phase_1->setTristate(true); + ui->phase_1_cutoff_temp->setEnabled(true); + ui->phase_1_scan_rate->setEnabled(true); + ui->phase_1_constant_temp->setEnabled(true); + ui->comboBox_phase_1_atmosphere->setEnabled(true); + break; + default: + break; } } + void ExperimentSettingForm::slotPhase2StateChanged(int state) { qDebug() << "slotPhase2StateChanged:" << state; @@ -358,14 +440,95 @@ void ExperimentSettingForm::on_pushButton_deliverData_clicked() void ExperimentSettingForm::slotRecvPhaseInfo(const QByteArray &ba) { QByteArray localba = ba; - SerialPortProtocol *spp = localba.data(); + SerialPortProtocol *spp = (SerialPortProtocol *)localba.data(); u8 *data = spp->data_buf; - vector phaseVtr; + QVector phaseVtr; for (int i = 0; i < 6; i++) { - Phase *phase = (Phase *)(data + i * sizeof(Phase)); + Phase *phase = (Phase *)(data + i * PHASE_BYTE_SIZE); phaseVtr.push_back(*phase); } + + //ui update + for(int i = 0; i< phaseVtr.size();){ + const Phase &phase = phaseVtr[i]; + if(phase.onoff == 0){ + break; + } + + i++; + qDebug()<<"index :"<(this->findChild(checkBoxName)); + if (checkBox_phase) { + checkBox_phase->setTristate(true); + qDebug()<<"found..."; + }else{ + qDebug()<<"not found..."; + } + + QString cutOffTempLineEditName = QString("phase_%1_cutoff_temp").arg(i); + QLineEdit *cutOffTempLineEdit = qobject_cast(this->findChild(cutOffTempLineEditName)); + if(cutOffTempLineEdit){ + cutOffTempLineEdit->setText(QString::number(phase.cutoff_temp,'f',3)); + cutOffTempLineEdit->setEnabled(true); + } + + QString scanRateLineEditName = QString("phase_%1_scan_rate").arg(i); + QLineEdit *scanRateLineEdit = + qobject_cast(this->findChild(scanRateLineEditName)); + if(scanRateLineEdit){ + scanRateLineEdit->setText(QString::number(phase.temp_flow,'f',3)); + scanRateLineEdit->setEnabled(true); + } + QString constantTempLineEditName = QString("phase_%1_constant_temp").arg(i); + QLineEdit *constantTempLineEdit = + qobject_cast(this->findChild(constantTempLineEditName)); + if(constantTempLineEdit){ + constantTempLineEdit->setText(QString::number(phase.constant_temp_time_min)); + constantTempLineEdit->setEnabled(true); + } + QString atmoshpereComboBoxName = QString("comboBox_phase_%1_atmosphere").arg(i); + QComboBox *atmoshpereComboBox = + qobject_cast(this->findChild(atmoshpereComboBoxName)); + if(atmoshpereComboBox){ + qDebug()<<"gas:"<setCurrentIndex(phase.gas % 256); + atmoshpereComboBox->setEnabled(true); + } + } + // + // radioButton_OIT_not + // radioButton_OIT + // comboBox_initial_atmosphere + + u8 *oitValue = data + (ba.size() - 2); + if(*oitValue){ + QString oitRadioButtonName("radioButton_OIT"); + QRadioButton *oitRadioButton = + qobject_cast(this->findChild(oitRadioButtonName)); + if(oitRadioButton){ + oitRadioButton->setChecked(true); + } + }else{ + QString oitNotRadioButtonName("radioButton_OIT_not"); + QRadioButton *oitNotRadioButton = + qobject_cast(this->findChild(oitNotRadioButtonName)); + if(oitNotRadioButton){ + oitNotRadioButton->setChecked(true); + } + } + + u8 *initAtmosphereValue = data + (ba.size() - 1); + QString initialAtmosphereComboBoxObjectName("comboBox_initial_atmosphere"); + QComboBox *initialAtmosphereComboBox = + qobject_cast(this->findChild(initialAtmosphereComboBoxObjectName)); + if(initialAtmosphereComboBox){ + initialAtmosphereComboBox->setCurrentIndex(*initAtmosphereValue); + } + + } diff --git a/src/ui/experimentsettingform.h b/src/ui/experimentsettingform.h index 2b9396c..510a996 100644 --- a/src/ui/experimentsettingform.h +++ b/src/ui/experimentsettingform.h @@ -29,7 +29,7 @@ private slots: private: //ui void uiReset(); - void uiUpdatePhase(QVector &); + void uiSetPhaseEnable(const int index); // slot void slotPhaseCheck(); void slotPhase2StateChanged(int state);