1.效果图
2. Plot控件用的是QCustomPlot
3. 源码
3.1 窗体头文件artusb3100.h
#ifndef ARTUSB3100_H
#define ARTUSB3100_H
#include <QWidget>
#include <iostream>
#include <QTimer>
#include <QQueue>
const int DATALEN = 1000;
//typedef struct{
// float data[DATALEN];
//} DATABUFF;
using namespace std;
namespace Ui {
class ArtUSB3100;
}
class ArtUSB3100 : public QWidget
{
Q_OBJECT
public:
explicit ArtUSB3100(QWidget *parent = nullptr);
~ArtUSB3100();
private slots:
void on_pushBtnExit_clicked();
void on_pushButtonStart_clicked();
void on_pushButtonStop_clicked();
void timerOutSlot();
void timerOutShowSlot();
void on_comboBoxModel_currentIndexChanged(int index);
void on_comboBoxChannelName_currentIndexChanged(int index);
void on_comboBoxInputCfg_currentIndexChanged(int index);
private:
Ui::ArtUSB3100 *ui;
vector<float> m_data;
// QQueue<DATABUFF> m_dataShow;
QTimer* m_timerGetData;
QTimer* m_timerShowData;
QString m_channelName;
void drawCurve();
void getData();
void initCustomPlot();
};
#endif // ARTUSB3100_H
3.2 窗体实现文件 artusb3100.cpp
#include "artusb3100.h"
#include "ui_artusb3100.h"
#include "conio.h"
#include "ArtDaq/Include/Art_DAQ.h"
#include <QDebug>
static int nshowcount = 0;
static int ncollectcount = 0;
#define ArtDAQErrChk(functionCall) if( ArtDAQFailed(error=(functionCall)) ) goto Error;
ArtUSB3100::ArtUSB3100(QWidget *parent) :
QWidget(parent),
ui(new Ui::ArtUSB3100)
{
ui->setupUi(this);
m_channelName = "Dev1/ai0";
m_timerGetData = new QTimer(this);
connect(m_timerGetData,SIGNAL(timeout()), this, SLOT(timerOutSlot()));
m_timerGetData->start(10000);
m_timerShowData = new QTimer(this);
connect(m_timerShowData,SIGNAL(timeout()), this, SLOT(timerOutShowSlot()));
m_timerShowData->start(500);
int datasize = ui->lineEditSamples->text().toInt();
m_data.resize(DATALEN * 2);
// m_dataShow.clear();
getData();
initCustomPlot();
}
ArtUSB3100::~ArtUSB3100()
{
delete ui;
delete m_timerGetData;
delete m_timerShowData;
}
void ArtUSB3100::timerOutSlot()
{
getData();
}
void ArtUSB3100::timerOutShowSlot()
{
drawCurve();
}
void ArtUSB3100::on_pushBtnExit_clicked()
{
this->close();
}
void ArtUSB3100::on_pushButtonStart_clicked()
{
m_timerGetData->start(10000);
m_timerShowData->start(500);
}
void ArtUSB3100::on_pushButtonStop_clicked()
{
m_timerGetData->stop();
m_timerShowData->stop();
}
void ArtUSB3100::initCustomPlot()
{
const int nsamples = DATALEN / 10;
// add one graphs to show channel data:
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
// ui->customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
// generate some points of data (y0 for first channel, y1 for second channel,....y7 for last channel):
QVector<double> x(nsamples), y(nsamples);
// configure right and top axis to show ticks but no labels:
// (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->xAxis2->setTickLabels(false);
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->yAxis2->setTickLabels(false);
// make left and bottom axes always transfer their ranges to right and top axes:
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
// // pass data points to graphs:
// ui->customPlot->graph(0)->setData(x, y);
// // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
// ui->customPlot->graph(0)->rescaleAxes();
// Note: we could have also just called customPlot->rescaleAxes(); instead
// Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}
void ArtUSB3100::drawCurve()
{
const int nsamples = DATALEN /10;
int offset = DATALEN / 20;
QVector<double> x(nsamples), y(nsamples);
for (int i=0; i<nsamples; ++i)
{
int index = (i + offset * nshowcount) % (DATALEN * 2);
x[i] = index;
y[i] = m_data[index];
}
nshowcount ++;
if (nshowcount == 21) nshowcount = 0;
ui->customPlot->graph(0)->setData(x, y);
ui->customPlot->graph(0)->rescaleAxes();
ui->customPlot->replot();
}
void ArtUSB3100::getData()
{
int32 error=0;
TaskHandle taskHandle=0;
int32 read;
float64 data[1000];
// DATABUFF tempBuff;
char errBuff[2048]={'\0'};
/*********************************************/
// ArtDAQ Configure Code
/*********************************************/
ArtDAQErrChk (ArtDAQ_CreateTask("",&taskHandle));
ArtDAQErrChk (ArtDAQ_CreateAIVoltageChan(taskHandle,m_channelName.toLatin1().data(),"",ArtDAQ_Val_Cfg_Default,-10.0,10.0,ArtDAQ_Val_Volts,NULL));
ArtDAQErrChk (ArtDAQ_CfgSampClkTiming(taskHandle,"",10000.0,ArtDAQ_Val_Rising,ArtDAQ_Val_FiniteSamps,1000));
/*********************************************/
// ArtDAQ Start Code
/*********************************************/
ArtDAQErrChk (ArtDAQ_StartTask(taskHandle));
/*********************************************/
// ArtDAQ Read Code
/*********************************************/
ArtDAQErrChk (ArtDAQ_ReadAnalogF64(taskHandle,1000,10.0,ArtDAQ_Val_GroupByChannel,data,1000,&read,NULL));
for (int i=0; i< 1000; ++i) {
// m_data[i] = data[i];
m_data[ncollectcount* 1000 +i] = data[i];
// tempBuff.data[i] = data[i];
}
ncollectcount ++;
if (ncollectcount == 2) ncollectcount = 0;
// if(m_dataShow.size() == 4)
// {
// m_dataShow.dequeue();
// }
// m_dataShow.enqueue(tempBuff);
Error:
if( ArtDAQFailed(error) )
ArtDAQ_GetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
/*********************************************/
// ArtDAQ Stop Code
/*********************************************/
ArtDAQ_StopTask(taskHandle);
ArtDAQ_ClearTask(taskHandle);
}
if( ArtDAQFailed(error) )
qDebug() << "ArtDAQ_ Error: " << errBuff << endl;
}
void ArtUSB3100::on_comboBoxModel_currentIndexChanged(int index)
{
}
void ArtUSB3100::on_comboBoxChannelName_currentIndexChanged(int index)
{
m_channelName = ui->comboBoxChannelName->itemText(index);
}
void ArtUSB3100::on_comboBoxInputCfg_currentIndexChanged(int index)
{
}
4. 主程序main.cpp
#include "artusb3100.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ArtUSB3100 w;
w.show();
return a.exec();
}
5. 工程文件
#-------------------------------------------------
#
# Project created by QtCreator 2022-10-14T08:56:56
#
#-------------------------------------------------
QT += core gui
QT += printsupport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ArtUSB3100
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
artusb3100.cpp \
qcustomplot.cpp
HEADERS += \
artusb3100.h \
qcustomplot.h
FORMS += \
artusb3100.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ArtDaq/Lib/x64/ -lArt_DAQ
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ArtDaq/Lib/x64/ -lArt_DAQd
INCLUDEPATH += $$PWD/ArtDaq/Include
DEPENDPATH += $$PWD/ArtDaq/Include