代码拉取完成,页面将自动刷新
#include "MainWindow.h"
#include <QDoubleSpinBox>
#include <QApplication>
#include "SingleMassDialog.h"
#include "AcquireSaveDialog.h"
//
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
initControls();
createSubWindow();
m_needUpdate = false;
connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimerOut()));
connect(&m_gather, &GatherThread::sigData, this, &MainWindow::onHRF9828Data);
connect(&m_gather, &GatherThread::sigError, this, &MainWindow::onHRF9828Error);
#if SIM_9982
connect(&m_acqGather, &DataGatherUSB9982Sim::sigData, this, &MainWindow::onAcquireData);
connect(&m_acqGather, &DataGatherUSB9982Sim::sigError, this, &MainWindow::onAcquireError);
connect(&m_acqGather, &DataGatherUSB9982Sim::finished, this, &MainWindow::onAcquireFinish);
#else
connect(&m_acqGather, &DataGatherUSB9982::sigData, this, &MainWindow::onAcquireData);
connect(&m_acqGather, &DataGatherUSB9982::sigError, this, &MainWindow::onAcquireError);
connect(&m_acqGather, &DataGatherUSB9982::finished, this, &MainWindow::onAcquireFinish);
#endif
m_acqGather.setGather(&m_file);
ui.comboRange->addItem("-5V - +5V");
ui.comboRange->addItem("-10V - +10V");
ui.actionNew->setIcon(style()->standardIcon(QStyle::SP_FileDialogNewFolder));
ui.actionOpen->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
ui.actionExit->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
ui.splitter->setStretchFactor(1, 1);
m_timer.start(1000);
updateAcqStatus();
m_config.load();
initProject();
}
MainWindow::~MainWindow()
{
m_file.close();
m_gather.stopGather();
m_acqGather.stopGather();
m_config.save();
}
void MainWindow::initControls()
{
{
QStringList items1, items2, items3, items4, items5;
for (int i = 0; i <= 12; i++)
items1 << QString::asprintf("AO%02d", i);
for (int i = 13; i <= 17; i++)
items2 << QString::asprintf("AO%02d", i);
for (int i = 18; i <= 21; i++)
items3 << QString::asprintf("AO%02d", i);
for (int i = 22; i <= 23; i++)
items4 << QString::asprintf("AO%02d", i);
for (int i = 0; i < 14; i++)
items5 << QString::asprintf("DO%02d", i);
m_setRange << 0;
QWidget *widget_di = new QWidget();
QVBoxLayout *layout_id = new QVBoxLayout();
widget_di->setLayout(layout_id);
layout_id->addWidget(createGroupSpin("低压", items1, m_AOLabelList, m_AOList));
layout_id->addWidget(createGroupSpin("高压", items2, m_AOLabelList, m_AOList));
layout_id->addWidget(createGroupSpin("脉冲", items3, m_AOLabelList, m_AOList));
layout_id->addWidget(createGroupSpin("射频", items4, m_AOLabelList, m_AOList));
layout_id->addWidget(createGroupBtn("DO", items5, m_DOList));
ui.scrollArea1->setWidget(widget_di);
for (int i = 0; i < m_DOList.size(); i++)
connect(m_DOList[i], SIGNAL(clicked()), this, SLOT(onBtnDOClicked()));
QSize barSize = ui.scrollArea1->verticalScrollBar()->size();
int n = qMin(barSize.width(), barSize.height());
QSize size = widget_di->sizeHint();
ui.tabWidget->setMinimumWidth(size.width() + n);
}
{
QStringList items1, items2, items3, items4, items5;
for (int i = 0; i <= 12; i++)
items1 << QString::asprintf("AI%02d", i);
for (int i = 13; i <= 17; i++)
items2 << QString::asprintf("AI%02d", i);
for (int i = 18; i <= 21; i++)
items3 << QString::asprintf("AI%02d", i);
for (int i = 22; i <= 23; i++)
items4 << QString::asprintf("AI%02d", i);
for (int i = 0; i < 14; i++)
items5 << QString::asprintf("DI%02d", i);
QWidget *widget_di = new QWidget();
QVBoxLayout *layout_id = new QVBoxLayout();
widget_di->setLayout(layout_id);
layout_id->addWidget(createGroupSpin("低压", items1, m_AILabelList, m_AIList));
layout_id->addWidget(createGroupSpin("高压", items2, m_AILabelList, m_AIList));
layout_id->addWidget(createGroupSpin("脉冲", items3, m_AILabelList, m_AIList));
layout_id->addWidget(createGroupSpin("射频", items4, m_AILabelList, m_AIList));
layout_id->addWidget(createGroupBtn("DI", items5, m_DIList));
ui.scrollArea2->setWidget(widget_di);
for (int i = 0; i < m_AIList.size(); i++)
m_AIList[i]->setReadOnly(true);
}
{
auto createSetting = [this](bool ai)->QGridLayout*
{
QStringList inList;
QString pre = ai ? "I" : "O";
for (int i = 0; i < 24; i++)
inList << "A" + pre + QString::asprintf("%02d", i);
for (int i = 0; i < 14; i++)
inList << "D" + pre + QString::asprintf("%02d", i);
QGridLayout *layout = new QGridLayout();
layout->setMargin(6);
layout->setSpacing(6);
auto l1 = new QLabel("名称");
auto l2 = new QLabel("系数");
l1->setAlignment(Qt::AlignCenter);
l2->setAlignment(Qt::AlignCenter);
layout->addWidget(l1, 0, 1);
layout->addWidget(l2, 0, 2);
for (int i = 0; i < inList.size(); i++)
{
QLabel *lb = new QLabel(inList[i]);
QLineEdit *line = new QLineEdit();
layout->addWidget(lb, i + 1, 0);
layout->addWidget(line, i + 1, 1);
m_SetLineList << line;
if (i < 24)
{
QDoubleSpinBox *spin = new QDoubleSpinBox();
spin->setRange(0, 100000);
layout->addWidget(spin, i + 1, 2);
m_SetSpinList << spin;
}
}
return layout;
};
auto layoutOutput = createSetting(false);
auto layoutInput = createSetting(true);
ui.groupBoxOutput->setLayout(layoutOutput);
ui.groupBoxInput->setLayout(layoutInput);
}
ui.tabWidget->setCurrentIndex(0);
}
QGroupBox *MainWindow::createGroupSpin(QString groupname, QStringList items, QList<QLabel*> &labelList,QList<QDoubleSpinBox*> &spinBoxList)
{
QGroupBox *box = new QGroupBox();
QGridLayout *layout = new QGridLayout();
box->setLayout(layout);
box->setTitle(groupname);
for (int i = 0; i < items.size(); i++)
{
auto spin = new QDoubleSpinBox();
auto label = new QLabel(items[i]);
spin->setRange(-1000000, 1000000);
spin->setSingleStep(0.5);
layout->addWidget(label, i, 0);
layout->addWidget(spin, i, 1);
labelList << label;
spinBoxList << spin;
}
if (&labelList == &m_AOLabelList)
{
QPushButton *btn1 = new QPushButton("撤销");
QPushButton *btn2 = new QPushButton("下发");
layout->addWidget(btn1, items.size(), 0);
layout->addWidget(btn2, items.size(), 1);
m_setBtnList << btn2;
m_backBtnList << btn1;
connect(btn1, SIGNAL(clicked()), this, SLOT(onBtnHRF9828Back()));
connect(btn2, SIGNAL(clicked()), this, SLOT(onBtnHRF9828Set()));
m_setRange << m_AOLabelList.size();
}
return box;
}
QGroupBox *MainWindow::createGroupBtn(QString groupname, QStringList items, QList<QPushButton*> &btnList)
{
QGroupBox *box = new QGroupBox();
QGridLayout *layout = new QGridLayout();
box->setLayout(layout);
box->setTitle(groupname);
for (int i = 0; i < items.size(); i++)
{
QString style = "padding:4px; padding-right:10px; text-align: center;";
QIcon icon(":/DataGather/Resources/comclosed.png");
auto btn = new QPushButton(items[i]);
btn->setIcon(icon);
btn->setIconSize(QSize(24, 24));
btn->setStyleSheet(style);
btn->setLayoutDirection(Qt::LayoutDirection::RightToLeft);
btn->setCheckable(true);
layout->addWidget(btn, i / 2, i % 2);
btnList << btn;
}
return box;
}
void MainWindow::updateButtonIcon(QPushButton *btn)
{
bool flag = btn->isChecked();
QIcon icon(flag ? ":/DataGather/Resources/comopened.png" : ":/DataGather/Resources/comclosed.png");
btn->setIcon(icon);
}
void MainWindow::initProject()
{
ParamHRF9828ToUi();
m_gather.setParam(m_config.hrf9828);
updateHRF9828Name();
if(!m_gather.isRunning())
m_gather.startGather();
}
void MainWindow::createSubWindow()
{
QMdiSubWindow *w1 = new QMdiSubWindow();
w1->setWindowTitle("累加窗口");
m_sum = new PlotWidget(this,0);
w1->setWidget(m_sum);
QMdiSubWindow *w2 = new QMdiSubWindow();
w2->setWindowTitle("单次窗口");
m_tic = new PlotWidget(this,1);
w2->setWidget(m_tic);
connect(m_sum, &PlotWidget::sigFrameSelect, this, &MainWindow::onFrameSelect);
ui.mdiArea->addSubWindow(w1);
ui.mdiArea->addSubWindow(w2);
for (int i = 0; i < 4; i++)
{
auto w = new PlotWidget(this,0);
QMdiSubWindow *sub = new QMdiSubWindow();
sub->setWidget(w);
sub->setVisible(false);
ui.mdiArea->addSubWindow(sub);
m_singleWindow << w;
}
QRect rc = ui.mdiArea->rect();
int w = rc.width()/2;
int h = rc.height()/2;
w1->setGeometry(0, 0, w, h);
w2->setGeometry(0, h, w, rc.height() - h);
}
void MainWindow::layoutSubWindow()
{
QRect rc = ui.mdiArea->rect();
int w = rc.width();
int h = rc.height() / 2;
auto list = ui.mdiArea->subWindowList();
auto w1 = list[0];
auto w2 = list[1];
w1->setGeometry(0, 0, w, h);
w2->setGeometry(0, h, w, rc.height() - h);
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
QTimer::singleShot(100, [this] {layoutSubWindow(); });
}
void MainWindow::updateAcqStatus()
{
bool run = m_acqGather.isRunning();
ui.actionAcqStart->setEnabled(!run);
ui.actionAcqSingle->setEnabled(!run);
ui.actionAcqStop->setEnabled(run);
}
void MainWindow::uiToParamHRF9828()
{
for (int i = 0; i < m_AOList.size(); i++)
m_config.hrf9828.AO[i] = m_AOList[i]->value();
for (int i = 0; i < m_DOList.size(); i++)
m_config.hrf9828.DO[i] = m_DOList[i]->isChecked();
for (int i = 0; i < m_SetLineList.size(); i++)
m_config.hrf9828.Names[i] = m_SetLineList[i]->text();
for (int i = 0; i < m_SetSpinList.size(); i++)
m_config.hrf9828.Coefs[i] = m_SetSpinList[i]->value();
m_config.hrf9828.DAQFreq = ui.spinDAQFreq->value();
m_config.hrf9828.RangeIndex = ui.comboRange->currentIndex();
m_config.hrf9828.T0_Freq = ui.spinFreqT0->value();
m_config.hrf9828.T0_Duty = ui.spinRadioT0->value();
m_config.hrf9828.T1_Freq = ui.spinFreqT1->value();
m_config.hrf9828.T1_Duty = ui.spinRadioT1->value();
m_config.hrf9828.T1_Freq = ui.spinFreqT2->value();
m_config.hrf9828.T1_Duty = ui.spinRadioT2->value();
}
void MainWindow::ParamHRF9828ToUi()
{
for (int i = 0; i < m_AOList.size(); i++)
m_AOList[i]->setValue(m_config.hrf9828.AO[i]);
for (int i = 0; i < m_DOList.size(); i++)
m_DOList[i]->setChecked(m_config.hrf9828.DO[i]);
for (int i = 0; i < m_SetLineList.size(); i++)
m_SetLineList[i]->setText(m_config.hrf9828.Names[i]);
for (int i = 0; i < m_SetSpinList.size(); i++)
m_SetSpinList[i]->setValue(m_config.hrf9828.Coefs[i]);
ui.spinDAQFreq->setValue(m_config.hrf9828.DAQFreq);
ui.comboRange->setCurrentIndex(m_config.hrf9828.RangeIndex);
ui.spinFreqT0->setValue(m_config.hrf9828.T0_Freq);
ui.spinRadioT0->setValue(m_config.hrf9828.T0_Duty);
ui.spinFreqT1->setValue(m_config.hrf9828.T1_Freq);
ui.spinRadioT1->setValue(m_config.hrf9828.T1_Duty);
ui.spinFreqT2->setValue(m_config.hrf9828.T1_Freq);
ui.spinRadioT2->setValue(m_config.hrf9828.T1_Duty);
}
void MainWindow::updateHRF9828Name()
{
for (int i = 0; i < m_AOLabelList.size(); i++)
m_AOLabelList[i]->setText(m_config.hrf9828.Names[i]);
for (int i = 0; i < m_AILabelList.size(); i++)
m_AILabelList[i]->setText(m_config.hrf9828.Names[24 + 14 + i]);
for (int i = 0; i < m_DOList.size(); i++)
m_DOList[i]->setText(m_config.hrf9828.Names[24 + i]);
for(int i = 0; i < m_DIList.size(); i++)
m_DIList[i]->setText(m_config.hrf9828.Names[24 * 2 + 14 + i]);
}
void MainWindow::syncGatherParam()
{
m_gather.setParam(m_config.hrf9828);
}
void MainWindow::onBtnDOClicked()
{
QPushButton *btn = (QPushButton*)sender();
int idx = m_DOList.indexOf(btn);
m_config.hrf9828.DO[idx] = btn->isChecked();
updateButtonIcon(m_DOList[idx]);
syncGatherParam();
}
void MainWindow::on_actionNew_triggered()
{
m_file.close();
m_sum->clear();
m_tic->clear();
}
void MainWindow::on_actionOpen_triggered()
{
QString filepath = QFileDialog::getOpenFileName(this, "选择数据文件", m_config.saveInfo.savePath, "*.dat");
if (filepath.isEmpty())
return;
m_file.close();
m_file.load(filepath);
m_sum->clear();
m_tic->clear();
for (int i = 0; i < m_file.frameInfo.size(); i++)
{
m_sum->addData(m_file.frameInfo[i].time, m_file.frameInfo[i].sum);
}
}
void MainWindow::on_actionExit_triggered()
{
close();
}
void MainWindow::beginSave()
{
if (m_config.saveInfo.saveFile && !m_file.isSave())
{
m_file.close();
QDir dir;
if (!dir.exists(m_config.saveInfo.savePath))
dir.mkpath(m_config.saveInfo.savePath);
QString path = m_config.saveInfo.savePath + "/" + m_config.saveInfo.savePre + "_"
+ QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss") + ".dat";
m_file.maxSize = qint64(m_config.saveInfo.maxSize) * 1024 * 1024;
m_file.beignSave(path, m_config.usb9982);
m_sum->clear();
m_tic->clear();
}
}
void MainWindow::beginGather(bool signle)
{
beginSave();
m_sum->setRealTime(true);
m_tic->setRealTime(true);
m_updateTime = QDateTime::currentDateTime();
m_acqGather.setParam(m_config.usb9982);
if(signle)
m_acqGather.singleGather();
else
m_acqGather.startGather();
updateAcqStatus();
}
void MainWindow::on_actionAcqStart_triggered()
{
beginGather(false);
}
void MainWindow::on_actionAcqSingle_triggered()
{
beginGather(true);
}
void MainWindow::on_actionAcqStop_triggered()
{
m_file.close();
m_acqGather.stopGather();
updateAcqStatus();
}
void MainWindow::on_actionAcqReset_triggered()
{
m_file.close();
m_sum->clear();
m_tic->clear();
}
void MainWindow::on_actionAcquire_triggered()
{
if (m_acqGather.isRunning())
m_acqGather.stopGather();
AcquireDialog dlg(this);
dlg.setUSB9982(m_config.usb9982);
if (dlg.exec() != QDialog::Accepted)
return;
m_config.usb9982 = dlg.USB9982();
m_file.endSave();
}
void MainWindow::on_actionAcquireSave_triggered()
{
if (m_acqGather.isRunning())
m_acqGather.stopGather();
AcquireSaveDialog dlg(this);
dlg.setConfig(m_config.saveInfo);
if (dlg.exec() != QDialog::Accepted)
return;
m_config.saveInfo = dlg.config();
m_file.endSave();
}
void MainWindow::on_actionSumWindow_triggered()
{
auto list = ui.mdiArea->subWindowList();
list[0]->setVisible(!list[0]->isVisible());
}
void MainWindow::on_actionEachWindow_triggered()
{
auto list = ui.mdiArea->subWindowList();
list[1]->setVisible(!list[1]->isVisible());
}
void MainWindow::on_actionSingleWindow_triggered()
{
SingleMassDialog dlg(this);
if (dlg.exec() != QDialog::Accepted)
return;
auto list = ui.mdiArea->subWindowList();
list[2]->show();
list[3]->show();
}
void MainWindow::on_actionCabli_triggered()
{
CalibDialog dlg(this);
dlg.setGap(m_tic->getGap());
if (dlg.exec() != QDialog::Accepted)
return;
m_tic->setTrans(dlg.scale);
}
void MainWindow::parseHRF9828Data(QByteArray &buffer)
{
byte *OutputData = (byte *)buffer.data();
byte dat[100 * 50] = { 0 };
double volt[100 * 24] = { 0 };
int RangeIndex = m_config.hrf9828.RangeIndex;
QVector<double> ret;
ret.resize(24);
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 50; j++)
{
dat[i * 50 + j] = OutputData[i * 50 + j];
}
for (int j = 0; j < 24; j++)
{
if (RangeIndex > 0)
{
volt[i * 24 + j] = dat[i * 50 + j + j] * 256 + dat[i * 50 + j + j + 1];
volt[i * 24 + j] = volt[i * 24 + j] > 32767 ? ((volt[i * 24 + j] - 65535) / 32767 * 10)
: volt[i * 24 + j] / 32767 * 10;
}
else
{
volt[i * 24 + j] = dat[i * 50 + j + j] * 256 + dat[i * 50 + j + j + 1];
volt[i * 24 + j] = volt[i * 24 + j] > 32767 ? ((volt[i * 24 + j] - 65535) / 32767 * 5)
: volt[i * 24 + j] / 32767 * 5;
}
}
for (int idx = 0; idx < 24; idx++)
ret[idx] += volt[i * 24 + idx];
}
//AI
for (int i = 0; i < 24; i++)
{
ret[i] /= 100;
m_AIList[i]->setValue(ret[i] * m_config.hrf9828.Coefs[i + 24]);
}
//DI
int DI0 = dat[0 * 50 + 48];
int DI1 = dat[0 * 50 + 49];
bool chk[14] = {0};
chk[0] = ((DI0 & 0x01) == 0x00) ? 0 : 1;
chk[1] = ((DI0 & 0x02) == 0x00) ? 0 : 1;
chk[2] = ((DI0 & 0x04) == 0x00) ? 0 : 1;
chk[3] = ((DI0 & 0x08) == 0x00) ? 0 : 1;
chk[4] = ((DI0 & 0x10) == 0x00) ? 0 : 1;
chk[5] = ((DI0 & 0x20) == 0x00) ? 0 : 1;
chk[6] = ((DI0 & 0x40) == 0x00) ? 0 : 1;
chk[7] = ((DI0 & 0x80) == 0x00) ? 0 : 1;
chk[8] = ((DI1 & 0x01) == 0x00) ? 0 : 1;
chk[9] = ((DI1 & 0x02) == 0x00) ? 0 : 1;
chk[10] = ((DI1 & 0x04) == 0x00) ? 0 : 1;
chk[11] = ((DI1 & 0x08) == 0x00) ? 0 : 1;
chk[12] = ((DI1 & 0x10) == 0x00) ? 0 : 1;
chk[13] = ((DI1 & 0x20) == 0x00) ? 0 : 1;
for (int i = 0; i < 14; i++)
m_DIList[i]->setChecked(chk[i]);
}
void MainWindow::onBtnHRF9828Set()
{
auto btn = (QPushButton*)sender();
int idx = m_setBtnList.indexOf(btn);
int s = m_setRange[idx];
int e = m_setRange[idx + 1];
for (int i = s; i < e; i++)
m_config.hrf9828.AO[i] = m_AOList[i]->value();
syncGatherParam();
}
void MainWindow::onBtnHRF9828Back()
{
auto btn = (QPushButton*)sender();
int idx = m_backBtnList.indexOf(btn);
int s = m_setRange[idx];
int e = m_setRange[idx + 1];
for (int i = s; i < e; i++)
m_AOList[i]->setValue(m_config.hrf9828.AO[i]);
}
void MainWindow::onHRF9828Data(QByteArray data)
{
parseHRF9828Data(data);
}
void MainWindow::onHRF9828Error(QString error)
{
}
void MainWindow::onAcquireData(QDateTime time,GatherDataBuffer *data)
{
auto ¶m = m_config.usb9982;
QElapsedTimer t;
t.start();
int data_size = data->size();
m_dataBuff.resize(data_size);
memcpy(m_dataBuff.data(), data->data(), data->size() * sizeof(float));
m_dataTime = time;
m_sum->addData(time,data->sum);
QDateTime cur = QDateTime::currentDateTime();
if (m_updateTime.msecsTo(cur) > 150)
{
m_tic->setData(time,m_dataBuff);
m_updateTime = time;
m_needUpdate = false;
}
else
{
m_needUpdate = true;
}
data->detach();
qDebug() << "deal cost" << t.elapsed() << "size" << data_size;
}
void MainWindow::onAcquireError(QString error)
{
QMessageBox::information(this, "", "采集错误: " + error);
}
void MainWindow::onAcquireFinish()
{
updateAcqStatus();
m_sum->setRealTime(false);
m_tic->setRealTime(false);
if (m_needUpdate)
{
m_tic->setData(m_dataTime,m_dataBuff);
m_needUpdate = false;
}
}
void MainWindow::onFrameSelect(int index)
{
if (!m_file.isOpen())
return;
if(m_file.getFrame(index, m_dataTime, m_dataBuff))
m_tic->setData(m_dataTime, m_dataBuff);
}
void MainWindow::on_actionHelp_triggered()
{
}
void MainWindow::on_btnCancelSetting_clicked()
{
ParamHRF9828ToUi();
}
void MainWindow::on_btnInitSetting_clicked()
{
m_config.hrf9828 = Param_HRF9828();
ParamHRF9828ToUi();
}
void MainWindow::on_btnSaveSetting_clicked()
{
uiToParamHRF9828();
updateHRF9828Name();
}
void MainWindow::onTimerOut()
{
//check gather
if (m_timeIndex % 5 == 0)
{
if (!m_gather.isRunning())
m_gather.startGather();
}
m_timeIndex++;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。