#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
#include <QLibrary>
#include <QDebug>
#include <QMessageBox>
#include <QtCharts>
#include <QDateTime>

//驱动函数声明
typedef int(*MPS_OpenDeviceFunc)(int);                          //HANDLE MPS_OpenDevice(int DeviceNumber);
typedef int(*MPS_CloseDeviceFunc)(int);                         //int MPS_CloseDevice(HANDLE DeviceHandle);
typedef int(*MPS_ConfigureFunc)(int,int,int);                   //int MPS_Configure(int SampleRate, int DIOMode, HANDLE DeviceHandle);
typedef int(*MPS_StartFunc)(int);                               //int MPS_Start(HANDLE DeviceHandle);
typedef int(*MPS_StopFunc)(int);                                //int MPS_Stop(HANDLE DeviceHandle);
typedef int(*MPS_DataInFunc)(qint32*,int,int);                  //int MPS_DataIn(int *DataArray,int SampleNumber,HANDLE DeviceHandle);
typedef int(*MPS_TimeOutFunc)(int,int);                         //int MPS_TimeOut(int TimeOut_ms, HANDLE DeviceHandle);
typedef int(*MPS_GetDeviceDescriptorFunc)(char*,int,int);       //int MPS_GetDeviceDescriptor(char *DescriptorBuffer, int BufferSize, HANDLE DeviceHandle);
typedef int(*MPS_GetDeviceInformationFunc)(char*,int,int);      //int MPS_GetDeviceInformation(char *DeviceInformationBuffer, int BufferSize, HANDLE DeviceHandle);
typedef int(*MPS_SetUserInformationFunc)(char*,int,int);        //int MPS_SetUserInformation(char *UserInformationBuffer, int BufferSize, HANDLE DeviceHandle);
typedef int(*MPS_ResetDeviceFunc)(int);                         //int MPS_ResetDevice(HANDLE DeviceHandle);
typedef int(*MPS_Configure_GainFunc)(int, int, int);                //int MPS_Configure_Gain (int ChannelNumber, int GainIndex, HANDLEDeviceHandle)

//32位和64位的DLL文件名
//#define DLL_FileName "MPS Driver"
//32位
#define DLL_FileName "MPS Driver_x64"       //64位

#define r_Success 1
#define r_Error 0
#define r_Error_OpenDevice -1
#define r_Error_CloseDevice -2
#define r_Error_Configure -3
#define r_Error_Start -4
#define r_Error_Stop -5
#define r_Error_DataIn -6
#define r_Error_TimeOut -7
#define r_Error_GetDeviceDescriptor -8
#define r_Error_GetDeviceInformation -9
#define r_Error_SetUserInformation -10
#define r_Error_ResetDevice -11
#define r_Error_Configure_Gain -12
#define r_ChartView -13

#define r_Error_LoadDLL -99

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


        //创建波形图
        chartView = new QChartView(this);
        chartView->setFixedSize(800, 450);                      // 宽度800像素，高度600像素

        // 创建图表
        QChart *chart = new QChart();

        //设置标题
        chart->setTitle("MPS采集卡Qt例程");

        QFont titleFont;
        titleFont.setFamily("Arial");       // 字体家族
        titleFont.setPointSize(32);         // 字号
        titleFont.setBold(true);            // 加粗
        titleFont.setItalic(true);          // 斜体（可选）

        chart->setTitleFont(titleFont);

        //初始化显示
        QLineSeries *series = new QLineSeries();
        series->append(0, 0);
        chart->addSeries(series);

        //初始化坐标轴
        chart->createDefaultAxes();                             //创建默认坐标轴并自动缩放

        chart->axisX()->setTitleText("Time");                   //设置X轴名称
        chart->axisX()->setRange(0, 1);                         //设置初始显示范围

        chart->axisY()->setTitleText("Voltage");                //设置Y轴名称
        chart->axisY()->setRange(-5, 5);                        //设置初始显示范围

        //确认图表
        chartView->setChart(chart);
        chartView->setRenderHint(QPainter::Antialiasing);       //开启抗锯齿


        //设置定时器
        timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, &Widget::updateTime);
}

Widget::~Widget()
{
    delete ui;
}


double Voltage[16][10240000];                                  //用于保存经过计算的电压的数据数组Voltage,定义的大小必须足够保存TimeLength对应的数据长度，定义为全局变量
int DeviceNumber = 0;                                         //操作系统给设备分配的序号，每次open函数都会用到，可以作为全局变量设置

int DataIn(int SampleRate, double TimeLength, int ChannalNumber, int DIOMode, double VoltageRange, int *GainIndexArray,int DeviceNumber)
{
    // 加载DLL（不需要.dll扩展名）
    QLibrary myLib("MPS Driver_x64");

    if(myLib.load())
    {
        MPS_OpenDeviceFunc MPS_OpenDevice = (MPS_OpenDeviceFunc)myLib.resolve("MPS_OpenDevice");
        MPS_CloseDeviceFunc MPS_CloseDevice = (MPS_CloseDeviceFunc)myLib.resolve("MPS_CloseDevice");
        MPS_ConfigureFunc MPS_Configure = (MPS_ConfigureFunc)myLib.resolve("MPS_Configure");
        MPS_StartFunc MPS_Start = (MPS_StartFunc)myLib.resolve("MPS_Start");
        MPS_StopFunc MPS_Stop = (MPS_StopFunc)myLib.resolve("MPS_Stop");
        MPS_DataInFunc MPS_DataIn = (MPS_DataInFunc)myLib.resolve("MPS_DataIn");
        MPS_TimeOutFunc MPS_TimeOut = (MPS_TimeOutFunc)myLib.resolve("MPS_TimeOut");
        MPS_Configure_GainFunc MPS_Configure_Gain = (MPS_Configure_GainFunc)myLib.resolve("MPS_Configure_Gain");


        int Flag;                                                                   //函数执行成功标志
        int DeviceHandle = 0;                                                       //设备句柄
//        int ChannalNumber = 4;                                                    //通道数目，MPS-HD2404为4通道，MPS-HD2408为8通道，MPS-HD2416为16通道
//        int SampleRate = 64000;                                                   //采样率，取值1000、2000、4000、8000、16000、32000、64000或128000
//        int DIOMode = 0;                                                          //DIO模式，取值0：闲置；1：输出5V；2：外启动模式
//        double TimeLength = 0.1;                                                  //每此采集的数据的时间长度，单位秒
//        double VoltageRange = 5.16;                                               //量程参数，MPS-HD2404、MPS-2408、MPS-HD2416、MPS-HD1608、MPS-HD1616等量程为5.16V；MPS-HD1604量程为5.2V；MPS-HD2402和MPS-HD1802量程为5.3V；
//        int GainIndexArray[16] = {0};                                             //保存每个通道PGA参数的数组。每个参数的值，GainIndex = 0对应1倍放大，GainIndex = 1对应2倍放大，GainIndex = 2对应4倍放大……GainIndex = 8对应256倍放大。

        DeviceHandle = MPS_OpenDevice(DeviceNumber);
        if (DeviceHandle == -1)                                                     //打开失败，返回错误值
        {
            return r_Error_OpenDevice;
        }

        else
        {
            //先发送停止采集的命令，确保设备进入待机状态
            Flag = MPS_Stop(DeviceHandle);                                          //采集任务结束后停止采集
            if (Flag == 0)return r_Error_Stop;

            //设置参数，设置超时，启动采集
            Flag = MPS_Configure(SampleRate, DIOMode, DeviceHandle);                //设置采样率和复用IO口
            if (Flag == 0)return r_Error_Configure;

            for(int i = 0; i < ChannalNumber; i++)                                  //设置Gain
            {
                Flag = MPS_Configure_Gain(i+1,GainIndexArray[i],DeviceHandle);
                if (Flag == 0)return r_Error_Configure_Gain;
            }

            Flag= MPS_TimeOut(500, DeviceHandle);                                   //设置等待超时为5s，如果用于外启动超时，可根据情况修改设置值
            if (Flag == 0)return r_Error_TimeOut;

            if (DIOMode != 2) Flag = MPS_Start(DeviceHandle);                       //启动设备采集；外部启动模式时不通过调用MPS_Start来启动
            if (Flag == 0)return r_Error_Start;

            //以下为循环调用采集函数的方式来读取数据
            qint32 DataBuffer[1024 * 16];                                           //用于保存读出的原始整形数据的缓存数组，请务必注意此变量一定定义为32位整形数

            Flag = MPS_DataIn(DataBuffer, 256, DeviceHandle);                       //预读一组数据，舍弃不用以确保清空缓存，这部分不是必须，但非连续不间断的分段式采集中，建议加上
            if (Flag == 0)return r_Error_DataIn;

            int Counter = 0;                                                        //用于计数的数据点数
            while (true)                                                            //采集一组数据，每通道读取SampleRate * TimeLength个样点
            {
                Flag = MPS_DataIn(DataBuffer, 1024 * ChannalNumber, DeviceHandle);  //读取数据，每次循环每个通道读取1024点

                if (Flag != 0)                                                      //如果采集成功
                {
                    for (int i = 0; i < 1024; i++)                                  //数据处理，计算电压值；4个通道的数据在缓存中循环排列；电压值 = (采集值/(65536*128))*量程
                    {
                        if (Counter >= SampleRate * TimeLength) break;              //如果样点达到SampleNumberPerCH，则跳出for循环
                        for (int j = 0; j < ChannalNumber; j++)
                        {
                            Voltage[j][Counter] = ((double)DataBuffer[i * ChannalNumber + j] / 8388608) * VoltageRange;         //计算电压值
                            //Voltage[j][Counter] = ((double)DataBuffer[i * ChannalNumber + j] / 8388608) * VoltageRange /(1 << GainIndexArray(i));         //计算放大前的电压值,即电压值除以2的GainIndex次方
                        }
                        Counter++;
                    }
                }
                else
                {
                    return r_Error_DataIn;                                          //DataIn函数执行失败，返回错误值
                }

                if (Counter >= SampleRate * TimeLength)                             //如果读出全部数据，则跳出while循环
                {
                    break;
                }

            }
            //至此处，采集过程完成，后面需要停止硬件采集并关闭设备
            Flag = MPS_Stop(DeviceHandle);                                          //采集任务结束后停止采集
            if (Flag == 0)return r_Error_Stop;

            QThread::msleep(5);
            MPS_CloseDevice(DeviceHandle);                                          //最后关闭设备
        }
    }
    else
    {
        return r_Error_LoadDLL;                                                     //导入DLL出错，返回错误值
    }
    return r_Success;
}

int DataChart(QChartView *view,int SampleRate, double TimeLength, int ChannalNumber,char*DeviceDescriptor)
{
    //将采集到的数据绘制波形图

    //计算总样点数
    int Counter = SampleRate * TimeLength;

    //添加ChannalNumber条曲线到QLineSeries数组
    if (view)
    {
        QChart *chart = view->chart();

        chart->setTitle(QString(DeviceDescriptor) + "采集卡波形图");              //设置波形图标题

        chart->removeAllSeries();                                               //清除旧波形

        QList<QLineSeries*> seriesArray;                                        //建立多组波形
        for(int i = 0; i < ChannalNumber; i++)
        {
            QLineSeries *series = new QLineSeries();
            series->setName(QString("CH%1").arg(i+1));
            seriesArray.append(series);
        }


        //从全部数据中抽取要显示的点，写入波形图
        int interval = 1;

        for (int i = 0; i < ChannalNumber; i++)                                 //抽取要进行绘图的数据点
        {
            int PointsMumberMax = 1000;                                         //送入波形图的显示点数上限，点数越多CPU资源占用越大
            int PointsMumber = Counter;
            while (true)                                                        //确定送入波形图的样点，即间隔interval个点抽取一个点
            {
                if(Counter % interval  == 0)PointsMumber = (int)Counter / (int)interval;
                if (PointsMumber <= PointsMumberMax) break;
                interval++;
            }
            for (int j = 0; j < PointsMumber; j++)                              //将数据点写入QLineSeries数组
            {
                seriesArray[i]->append(j, Voltage[i][j * interval]);
            }
        }

        for (int i = 0; i < seriesArray.size(); i++)                            //绘制曲线
        {
            QLineSeries *series = seriesArray[i];
            chart->addSeries(series);
        }

        // 设置坐标轴
        chart->createDefaultAxes();                                             //创建默认坐标轴并自动缩放

        chart->removeAxis(chart->axisX());                                      //删除默认X轴并重新定义X轴
        QValueAxis *axisX = new QValueAxis;
        axisX->setRange(0, TimeLength);
        axisX->setTitleText("Time");
        chart->addAxis(axisX, Qt::AlignBottom);

        chart->axisY()->setTitleText("Voltage");                                //设置Y轴
        //chart->axisY()->setRange(-5, 5);                                      //此行可以设置固定显示范围
    }
    else
    {
        return r_ChartView;
    }
    return r_Success;
}

int GetDeviceDescriptor(char* Descriptor, int StringLength,int DeviceNumber)
{
    // 加载DLL（不需要.dll扩展名）
    QLibrary myLib(DLL_FileName);

    if(myLib.load())
    {
        MPS_OpenDeviceFunc MPS_OpenDevice = (MPS_OpenDeviceFunc)myLib.resolve("MPS_OpenDevice");
        MPS_CloseDeviceFunc MPS_CloseDevice = (MPS_CloseDeviceFunc)myLib.resolve("MPS_CloseDevice");
        MPS_GetDeviceDescriptorFunc MPS_GetDeviceDescriptor = (MPS_GetDeviceDescriptorFunc)myLib.resolve("MPS_GetDeviceDescriptor");

        int Flag;                                           //函数执行成功标志
        int DeviceHandle = 0;                               //设备句柄

        DeviceHandle = MPS_OpenDevice(DeviceNumber);        //先打开设备
        if (DeviceHandle == -1)                             //打开失败，返回错误值
        {
            return r_Error_OpenDevice;
        }
        else
        {
            //获取设备的型号
            char DeviceDescriptor[64];                      //保存设备型号信息的缓存数组
            Flag = MPS_GetDeviceDescriptor(DeviceDescriptor, 64, DeviceHandle);     //获取设备型号
            DeviceDescriptor[10] = 0;                       //只保留前十位

            if(Flag == 0)
            {
                return r_Error_GetDeviceDescriptor;         //获取设备描述符函数执行失败，返回错误值
            }
            else
            {
                for(int i = 0; i < StringLength;i++)
                    Descriptor[i] = DeviceDescriptor[i];    //将信息写入Descriptor
            }

            MPS_CloseDevice(DeviceHandle);                  //关闭设备
        }
    }
    else
    {
        return r_Error_LoadDLL;                             //导入DLL出错，返回错误值
    }

    return r_Success;                                       //函数执行成功，返回成功值
}

int SetUserInformation(char* UserInformation, int StringLength,int DeviceNumber)
{
    // 加载DLL（不需要.dll扩展名）
    QLibrary myLib(DLL_FileName);

    if(myLib.load())
    {
        MPS_OpenDeviceFunc MPS_OpenDevice = (MPS_OpenDeviceFunc)myLib.resolve("MPS_OpenDevice");
        MPS_CloseDeviceFunc MPS_CloseDevice = (MPS_CloseDeviceFunc)myLib.resolve("MPS_CloseDevice");
        MPS_SetUserInformationFunc MPS_SetUserInformation = (MPS_SetUserInformationFunc)myLib.resolve("MPS_SetUserInformation");

        int Flag;                                           //函数执行成功标志
        int DeviceHandle = 0;                               //设备句柄

        DeviceHandle = MPS_OpenDevice(DeviceNumber);        //先打开设备
        if (DeviceHandle == -1)                             //打开失败，返回错误值
        {
            return r_Error_OpenDevice;
        }

        //写入用户自定义信息
        Flag = MPS_SetUserInformation(UserInformation, 500, DeviceHandle);          //写入自定义信息
        if (Flag == 0)
        {
            return r_Error_SetUserInformation;              //写入信息函数执行失败，返回错误值
        }

        MPS_CloseDevice(DeviceHandle);                      //关闭设备

    }
    else
    {
        return r_Error_LoadDLL;                             //导入DLL出错，返回错误值
    }

    return r_Success;                                       //函数执行成功，返回成功值
}

int GetDeviceInformation(char* Information, int StringLength,int DeviceNumber)
{
    // 加载DLL（不需要.dll扩展名）
    QLibrary myLib(DLL_FileName);

    if(myLib.load())
    {
        MPS_OpenDeviceFunc MPS_OpenDevice = (MPS_OpenDeviceFunc)myLib.resolve("MPS_OpenDevice");
        MPS_CloseDeviceFunc MPS_CloseDevice = (MPS_CloseDeviceFunc)myLib.resolve("MPS_CloseDevice");
        MPS_GetDeviceInformationFunc MPS_GetDeviceInformation = (MPS_GetDeviceInformationFunc)myLib.resolve("MPS_GetDeviceInformation");

        int Flag;                                           //函数执行成功标志
        int DeviceHandle = 0;                               //设备句柄

        DeviceHandle = MPS_OpenDevice(DeviceNumber);        //先打开设备
        if (DeviceHandle == -1)                             //打开失败，返回错误值
        {
            return r_Error_OpenDevice;
        }

        else
        {
            //获取设备的硬件信息
            char DeviceInformation[2048]={0};               //用于保存设备硬件信息的缓存数组

            Flag = MPS_GetDeviceInformation(DeviceInformation, 2048, DeviceHandle);     //获取设备硬件信

            if (Flag == 0)
            {
                return r_Error_GetDeviceInformation;         //获取信息执行失败，返回错误值
            }
            else
            {
                for(int i = 0; i < StringLength;i++)
                    Information[i] = DeviceInformation[i];                              //将信息写入Information
            }

            MPS_CloseDevice(DeviceHandle);                  //关闭设备
        }
    }
    else
    {
        return r_Error_LoadDLL;                             //载入DLL出错，返回错误值
    }

    return r_Success;                                       //函数执行成功，返回成功值
}

int M_Reset(int DeviceNumber)
{
    // 加载DLL（不需要.dll扩展名）
    QLibrary myLib(DLL_FileName);

    if(myLib.load())
    {
        MPS_OpenDeviceFunc MPS_OpenDevice = (MPS_OpenDeviceFunc)myLib.resolve("MPS_OpenDevice");
        MPS_CloseDeviceFunc MPS_CloseDevice = (MPS_CloseDeviceFunc)myLib.resolve("MPS_CloseDevice");
        MPS_ResetDeviceFunc MPS_ResetDevice = (MPS_ResetDeviceFunc)myLib.resolve("MPS_ResetDevice");

        int Flag;                                           //函数执行成功标志
        int DeviceHandle = 0;                               //设备句柄

        DeviceHandle = MPS_OpenDevice(DeviceNumber);        //先打开设备
        if (DeviceHandle == -1)                             //打开失败，返回错误值
        {
            return r_Error_OpenDevice;
        }
        else
        {
            Flag = MPS_ResetDevice(DeviceHandle);           //重启设备
            if(Flag == 0)
            {
                return r_Error_ResetDevice;                 //重启函数执行失败，返回错误值
            }

            MPS_CloseDevice(DeviceHandle);                  //关闭设备
        }
    }
    else
    {
        return r_Error_LoadDLL;                             //导入DLL出错，返回错误值
    }

    return r_Success;                                       //函数执行成功，返回成功值
}

QString  ErrorString (int ErrorCode)
{
    switch(ErrorCode)
    {
        case r_Success: return "NoError";
        case r_Error_OpenDevice: return "OpenDeviceError";
        case r_Error_CloseDevice: return "CloseDeviceError";
        case r_Error_Configure: return "ConfigureError";
        case r_Error_Start: return "StartError";
        case r_Error_Stop: return "StartError";
        case r_Error_DataIn: return "DataInError";
        case r_Error_TimeOut: return "TimeOutError";
        case r_Error_GetDeviceDescriptor: return "GetDeviceDescriptorError";
        case r_Error_GetDeviceInformation: return "GetDeviceInformationError";
        case r_Error_SetUserInformation: return "SetUserInformationError";
        case r_Error_ResetDevice: return "ResetDeviceError";
        case r_Error_Configure_Gain: return "GainError";
        case r_ChartView: return "ChartViewError";
        case r_Error_LoadDLL: return "LoadDLLError";
    }
    return "NoThisErrorCode";
}



//定时器采集数据
bool TimerEnable = false;   //定时器开关
void Widget::updateTime()
{
    int ChannalNumber = 4;                                                    //通道数目，MPS-HD2404为4通道，MPS-HD2408为8通道，MPS-HD2416为16通道
    int SampleRate = 64000;                                                   //采样率，取值1000、2000、4000、8000、16000、32000、64000或128000
    int DIOMode = 0;                                                          //DIO模式，取值0：闲置；1：输出5V；2：外启动模式
    double TimeLength = 0.1;                                                  //每此采集的数据的时间长度，单位秒
    double VoltageRange = 5.16;                                               //量程参数，MPS-HD2404、MPS-2408、MPS-HD2416、MPS-HD1608、MPS-HD1616等量程为5.16V；MPS-HD1604量程为5.2V；MPS-HD2402和MPS-HD1802量程为5.3V；
    int GainIndexArray[16] = {0};                                             //保存每个通道PGA参数的数组。每个参数的值，GainIndex = 0对应1倍放大，GainIndex = 1对应2倍放大，GainIndex = 2对应4倍放大……GainIndex = 8对应256倍放大。


    //获取DeviceDescriptor和ChannalNumber
    char DeviceDescriptor[64] = {0};
    int Flag = GetDeviceDescriptor(DeviceDescriptor,64,DeviceNumber);
    if(Flag != 1)
    {
        QMessageBox::information(nullptr, "Error", ErrorString(Flag));                               //报告打开设备出错
        timer->stop();              // 停止定时器
        TimerEnable = false;
        ui->DataAcquisition->setText("DataAcquisition");
        return;
    }
    DeviceDescriptor[10] = 0;                                           //只保留前十个字符，在第11给字符插入终止符
    if (DeviceDescriptor[9] == '2') ChannalNumber = 2;                  //判断通道个数，根据型号的最后一位来判断通道个数，最后一位为2时通道数为2，最后一位为4时通道数为4，最后一位为8时通道数为8，最后一位为6（即16）时通道数为16；例如型号为MPS-HD2404，最后一位为4，此时通道数为4；
    else if (DeviceDescriptor[9] == '4') ChannalNumber = 4;
    else if (DeviceDescriptor[9] == '8') ChannalNumber = 8;
    else if (DeviceDescriptor[9] == '6') ChannalNumber = 16;

    //设置每个通道的Gain值，可以用循环统一设置，也可以逐个分别设置
    int GainIndexAllCH = 0;
    for(int i = 0; i < ChannalNumber; i++) GainIndexArray[i] = GainIndexAllCH;

    //采集数据
    Flag = DataIn(SampleRate, TimeLength, ChannalNumber, DIOMode, VoltageRange, GainIndexArray,DeviceNumber); //数据数组Voltage[16][10240000]在外部定义位全局变量
    if(Flag != 1)
    {
        QMessageBox::information(nullptr, "Error", ErrorString(Flag));                               //报告打开设备出错
        timer->stop();              // 停止定时器
        TimerEnable = false;
        ui->DataAcquisition->setText("DataAcquisition");
        return;
    }

    //绘制波形图
    DataChart(chartView,SampleRate, TimeLength, ChannalNumber,DeviceDescriptor);

    ui->InfLabel->setText("采样率 = " + QString::number(SampleRate) + "\n" + "采集时长 = " + QString::number(TimeLength) + "秒" + "\n" + "放大倍数 = " + QString::number(1<<GainIndexAllCH));

}

//打开设备并获取设备型号描述符
void Widget::on_OpenDevice_clicked()
{
    char DeviceDescriptor[64] = {0};

    int Flag = GetDeviceDescriptor(DeviceDescriptor,64,DeviceNumber);

    if(Flag == 1)
    {
        QMessageBox::information(nullptr, "Success", "The device has been successfully opened!");     //报告打开设备成功
        ui->InfLabel->setText(DeviceDescriptor);                                                      //显示描述符内容
    }
    else
    {
        QMessageBox::information(nullptr, "Error", ErrorString(Flag));                               //报告打开设备出错
    }
}

//开始或停止定时器采集数据
int DAQTimer_ms = 200;      //此处设置多少ms采集一次
void Widget::on_DataAcquisition_clicked()
{
    if(TimerEnable == false)
    {
        timer->start(DAQTimer_ms);         // 启动定时器（DAQTimer_ms间隔）
        TimerEnable = true;
        ui->DataAcquisition->setText("Stop");
    }
    else
    {
        timer->stop();              // 停止定时器
        TimerEnable = false;
        ui->DataAcquisition->setText("DataAcquisition");
    }
}

//设置用户自定义信息
void Widget::on_SetUserInformation_clicked()
{
    char UserInformation[500] = {0};                    //保存用户自定义信息的缓存数组

    QString qtString = "This is a Qt test!" + QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");         //准备写入的字符串内容
    strncpy(UserInformation, qtString.toUtf8().constData(), qtString.toUtf8().size() + 1);              //将字符串装入自定义信息数组

    int Flag = SetUserInformation(UserInformation,500,DeviceNumber);

    if(Flag == 1)
    {
        //回读信息并显示
        char DeviceInformation[2048] = {0};
        int Flag = GetDeviceInformation(DeviceInformation,2048,DeviceNumber);
        if(Flag == 1)
        {
            QMessageBox::information(nullptr, "Success", "Writing user information has been successful!!");         //报告写入用户信息成功
            ui->InfLabel->setText(DeviceInformation);                                                               //显示信息内容
        }
        else
        {
            QMessageBox::information(nullptr, "Error", ErrorString(Flag));                   //报告获取信息出错
        }

    }
    else
    {
        QMessageBox::information(nullptr, "Error", ErrorString(Flag));                         //报告写入用户信息出错
    }
}

//获取设备硬件信息
void Widget::on_GetDeviceInformation_clicked()
{
    char DeviceInformation[2048] = {0};

    int Flag = GetDeviceInformation(DeviceInformation,2048,DeviceNumber);

    if(Flag == 1)
    {
        QMessageBox::information(nullptr, "Success", "Device information has been successfully obtained!");     //报告获取信息成功
        ui->InfLabel->setText(DeviceInformation);                                                               //显示信息内容
    }
    else
    {
        QMessageBox::information(nullptr, "Error", ErrorString(Flag));                               //报告获取信息出错
    }
}

//重启设备
void Widget::on_ResetDevice_clicked()
{
    int Flag = M_Reset(DeviceNumber);

    if(Flag == 1)
    {
        QMessageBox::information(nullptr, "Success", "The device has been successfully reset!");                //报告重启成功
    }
    else
    {
        QMessageBox::information(nullptr, "Error", ErrorString(Flag));                                         //报告重启出错
    }
}

