2025-03-18T17:28:39

This commit is contained in:
yuntang 2025-03-18 17:28:40 +08:00
parent 3a229c17c7
commit 6a52420417
10 changed files with 242 additions and 61 deletions

View File

@ -12,17 +12,17 @@ QFile _expeFile;
void writeExperimentFile(const CommonData &cd) void writeExperimentFile(const CommonData &cd)
{ {
if(!_expeFile.isOpen()){
qDebug()<<"FileManager,file not open.";
createExperimentFile();
}
QTextStream out(&_expeFile); QTextStream out(&_expeFile);
out.setRealNumberPrecision(3); // 设置精度为三位小数 out.setRealNumberPrecision(3); // 设置精度为三位小数
out.setFieldWidth(12); // 设置字段宽度,确保对齐 out.setRealNumberNotation(QTextStream::FixedNotation);
out.setPadChar(' '); // 设置填充字符为空格
// 写入数据 out << cd.sample_temp << " "
out << cd.sample_temp << ", "
<< cd.dsc << endl; << cd.dsc << endl;
// 关闭文件
_expeFile.close();
} }
void createExperimentFile() void createExperimentFile()
@ -32,28 +32,40 @@ void createExperimentFile()
fileName = "sampleName"; fileName = "sampleName";
} }
fileName.append("-"); fileName.append("_");
if(_expeInfo.date.isEmpty()){ 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{ }else{
fileName.append(_expeInfo.date); fileName.append(_expeInfo.date);
} }
fileName.append(".txt");
qDebug()<<"fileName:"<<fileName;
_expeFile.setFileName(fileName); _expeFile.setFileName(fileName);
// 尝试以写入文本模式打开文件,如果文件不存在则创建它 // 尝试以写入文本模式打开文件,如果文件不存在则创建它
if (!_expeFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) { if (!_expeFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
qDebug() << "无法打开文件"; qDebug() << "FileManager file open failed.";
} else { } else {
qDebug() << "文件打开成功"; qDebug() << "FileManager file open succ.";
} }
} }
void test() void test()
{ {
QString fileName = "experiment.txt"; createExperimentFile();
CommonData cd;
cd.dsc = 1.0;
writeExperimentFile(cd);
}
QFile file; void fileClose()
{
if(_expeFile.isOpen()){
_expeFile.close();
}
} }
} }

View File

@ -19,6 +19,7 @@ extern QFile _expeFile;
void createExperimentFile(); void createExperimentFile();
void writeExperimentFile(const CommonData&); void writeExperimentFile(const CommonData&);
void fileClose();
void test(); void test();
}; };

View File

@ -5,6 +5,7 @@
#include "serialport/serialport.h" #include "serialport/serialport.h"
#include "realtimedataform.h" #include "realtimedataform.h"
#include "experimentsettingform.h" #include "experimentsettingform.h"
#include "filemanager.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -14,6 +15,8 @@ int main(int argc, char *argv[])
MainWindow w; MainWindow w;
w.show(); w.show();
// FileManager::test();
// ExperimentSettingForm es; // ExperimentSettingForm es;
// es.show(); // es.show();

View File

@ -83,6 +83,8 @@ void MainWindow::on_actionStop_triggered()
QByteArray ba = DataParser::setDeviceStartStop(DeviceStartMode::Stop); QByteArray ba = DataParser::setDeviceStartStop(DeviceStartMode::Stop);
SerialPort::instance()->slotSendData(ba); SerialPort::instance()->slotSendData(ba);
// SerialPort::instance()->slotCloseSp(); // SerialPort::instance()->slotCloseSp();
FileManager::fileClose();
} }
void MainWindow::on_actionNew_triggered() void MainWindow::on_actionNew_triggered()
@ -117,10 +119,10 @@ void MainWindow::on_actionConnectToDev_triggered()
{ {
if (SerialPort::instance()->openSp()) if (SerialPort::instance()->openSp())
{ {
ui->actionNew->setEnabled(true); setActionEnable(true);
Global::instance()->setMode(Global::Mode::ConnectedToDev); Global::instance()->setMode(Global::Mode::ConnectedToDev);
QThread::msleep(300); QByteArray ba = DataParser::inquirePhaseInfo();
SerialPort::instance()->senddata(DataParser::inquirePhaseInfo()); SerialPort::instance()->sendData(ba);
} }
else else
{ {

View File

@ -195,18 +195,14 @@ bool DataParser::slotDataParser(const QByteArray &ba)
spp.head = FRANE_HEAD; spp.head = FRANE_HEAD;
spp.cmd = READ_CMD; spp.cmd = READ_CMD;
spp.addr = 0x0050; spp.addr = 0x0050;
spp.len = 6;
// //
int inquireDataLength = (u8)sizeof(Phase) * 6; spp.data_buf[0] = PHASE_BYTE_SIZE * 6 + 2;
spp.data_buf[0] = inquireDataLength;
spp.len = 5 + inquireDataLength;
// //
int crcDataLength = 3 + inquireDataLength;
u8 *dataPtr = (u8 *)&spp; 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) + return QByteArray((char *)&spp, sppValidLength) +
QByteArray((char *)&crc, 2); QByteArray((char *)&crc, 2);
} }

View File

@ -6,7 +6,9 @@
#define FRANE_HEAD 0x5AA5 //枕头 #define FRANE_HEAD 0x5AA5 //枕头
#define WRITE_CMD 0x82 //写指令 #define WRITE_CMD 0x82 //写指令
#define READ_CMD 0x83 //读指令 #define READ_CMD 0x83 //读指令
#define PHASE_START_ADDR 0X0050 #define PHASE_START_ADDR 0X0050
#define PHASE_BYTE_SIZE 12
#pragma pack(push) #pragma pack(push)
#pragma pack(1) #pragma pack(1)

View File

@ -95,20 +95,26 @@ void SerialPort::slotReadData()
{ {
#if 1 #if 1
QByteArray ba = _sp->readAll(); QByteArray ba = _sp->readAll();
if(ba.size() == 0){
#if 0 return;
}
#if 1
QString hexData = ba.toHex(' '); // ' ' 作为分隔符,可选参数 QString hexData = ba.toHex(' '); // ' ' 作为分隔符,可选参数
qDebug() << "receive info (hex):" << hexData; qDebug() << "receive info (hex):" << hexData;
#endif #endif
SerialPortProtocol *spp = (SerialPortProtocol *)ba.data(); SerialPortProtocol *spp = (SerialPortProtocol *)ba.data();
// qDebug()<<"header:"<<QString::number(spp->head,16).toUpper();
if (FRANE_HEAD != spp->head) if (FRANE_HEAD != spp->head)
{ {
qDebug() << "Data header error."; qDebug() << "Data header error.";
return; return;
} }
//phase setting data
if(spp->addr == PHASE_START_ADDR){
emit sigSendPhaseInfo(ba);
return;
}
int dataLength = spp->len - 5; int dataLength = spp->len - 5;
CommonData cd; CommonData cd;
@ -119,22 +125,17 @@ void SerialPort::slotReadData()
{ {
writeCmdHandle(dataLength, spp->addr, cd); writeCmdHandle(dataLength, spp->addr, cd);
} }
else else if (READ_CMD == spp->cmd)
{ {
// read data // read data
if (spp->addr == 0) if (spp->addr == 0)
{ {
if (Global::Mode::ExperimentStart == Global::instance()->getMode()) if (Global::Mode::ExperimentStart == Global::instance()->getMode())
{ {
emit sigSendCommonData(cd); emit sigSendCommonData(cd);
} }
emit sigSendCommonDataToRealDataForm(cd); emit sigSendCommonDataToRealDataForm(cd);
} }
else if(spp->addr == PHASE_START_ADDR)
{
emit sigSendPhaseInfo(ba);
}
} }
#endif #endif
} }
@ -420,15 +421,18 @@ void SerialPort::slotDeliverData(const QByteArray &ba)
void SerialPort::slotSendData(const QByteArray &ba) void SerialPort::slotSendData(const QByteArray &ba)
{ {
/*** #if 0
*
* 1
*/
if (WRITE_CMD == ba.at(3)) if (WRITE_CMD == ba.at(3))
{ {
_lastWriteBa = ba; _lastWriteBa = ba;
} }
#endif
#if 1
qDebug() << "slotSendData:" << ba.size(); qDebug() << "slotSendData:" << ba.size();
QString hexData = ba.toHex(' '); // ' ' 作为分隔符,可选参数
qDebug() << "slotSendData:" << hexData;
#endif
if (_sp != nullptr && _sp->isOpen()) if (_sp != nullptr && _sp->isOpen())
{ {

View File

@ -23,9 +23,7 @@ public:
~SerialPort(); ~SerialPort();
void sendData(const QByteArray &data) void sendData(const QByteArray &data)
{ {slotSendData(data); }
slotSendData(data);
}
void sendCmd(const E_CMD_TYPE e); void sendCmd(const E_CMD_TYPE e);
static void parserTest(); static void parserTest();
bool openSp(); bool openSp();

View File

@ -8,8 +8,15 @@
#include "global.h" #include "global.h"
#include "filemanager.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), ExperimentSettingForm::ExperimentSettingForm(QWidget *parent) : QWidget(parent),
ui(new Ui::ExperimentSettingForm) ui(new Ui::ExperimentSettingForm)
{ {
ui->setupUi(this); ui->setupUi(this);
uiReset(); uiReset();
@ -62,7 +69,7 @@ void ExperimentSettingForm::uiReset()
ui->phase_4_constant_temp->setText("0"); ui->phase_4_constant_temp->setText("0");
ui->phase_5_constant_temp->setText("0"); ui->phase_5_constant_temp->setText("0");
ui->phase_6_constant_temp->setText("0"); ui->phase_6_constant_temp->setText("0");
//
ui->phase_2_cutoff_temp->setEnabled(false); ui->phase_2_cutoff_temp->setEnabled(false);
ui->phase_3_cutoff_temp->setEnabled(false); ui->phase_3_cutoff_temp->setEnabled(false);
ui->phase_4_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_4->setTristate(false);
ui->checkBox_phase_5->setTristate(false); ui->checkBox_phase_5->setTristate(false);
ui->checkBox_phase_6->setTristate(false); ui->checkBox_phase_6->setTristate(false);
}
void ExperimentSettingForm::uiUpdatePhase(QVector<Phase> &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); //set objectName
QCheckBox *checkBox = ui->findChild<QCheckBox*>(checkBoxName); SET_OBJECT_NAME(ui->checkBox_phase_1);
if (checkBox) { SET_OBJECT_NAME(ui->checkBox_phase_2);
checkBox->setTristate(false); 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) void ExperimentSettingForm::slotPhase2StateChanged(int state)
{ {
qDebug() << "slotPhase2StateChanged:" << state; qDebug() << "slotPhase2StateChanged:" << state;
@ -358,14 +440,95 @@ void ExperimentSettingForm::on_pushButton_deliverData_clicked()
void ExperimentSettingForm::slotRecvPhaseInfo(const QByteArray &ba) void ExperimentSettingForm::slotRecvPhaseInfo(const QByteArray &ba)
{ {
QByteArray localba = ba; QByteArray localba = ba;
SerialPortProtocol *spp = localba.data(); SerialPortProtocol *spp = (SerialPortProtocol *)localba.data();
u8 *data = spp->data_buf; u8 *data = spp->data_buf;
vector<Phase> phaseVtr; QVector<Phase> phaseVtr;
for (int i = 0; i < 6; i++) 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); 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 :"<<i;
QString checkBoxName = QString("checkBox_phase_%1").arg(i);
QCheckBox *checkBox_phase = qobject_cast<QCheckBox*>(this->findChild<QObject*>(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<QLineEdit*>(this->findChild<QObject*>(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<QLineEdit*>(this->findChild<QObject*>(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<QLineEdit*>(this->findChild<QObject*>(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<QComboBox*>(this->findChild<QObject*>(atmoshpereComboBoxName));
if(atmoshpereComboBox){
qDebug()<<"gas:"<<phase.gas;
atmoshpereComboBox->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<QRadioButton*>(this->findChild<QObject*>(oitRadioButtonName));
if(oitRadioButton){
oitRadioButton->setChecked(true);
}
}else{
QString oitNotRadioButtonName("radioButton_OIT_not");
QRadioButton *oitNotRadioButton =
qobject_cast<QRadioButton*>(this->findChild<QObject*>(oitNotRadioButtonName));
if(oitNotRadioButton){
oitNotRadioButton->setChecked(true);
}
}
u8 *initAtmosphereValue = data + (ba.size() - 1);
QString initialAtmosphereComboBoxObjectName("comboBox_initial_atmosphere");
QComboBox *initialAtmosphereComboBox =
qobject_cast<QComboBox*>(this->findChild<QObject*>(initialAtmosphereComboBoxObjectName));
if(initialAtmosphereComboBox){
initialAtmosphereComboBox->setCurrentIndex(*initAtmosphereValue);
}
} }

View File

@ -29,7 +29,7 @@ private slots:
private: private:
//ui //ui
void uiReset(); void uiReset();
void uiUpdatePhase(QVector<Phase> &); void uiSetPhaseEnable(const int index);
// slot // slot
void slotPhaseCheck(); void slotPhaseCheck();
void slotPhase2StateChanged(int state); void slotPhase2StateChanged(int state);