探测器测试软件设计说明书

1.项目

安检仪探测器测试平台。

2.目的

对探测器采集输出的信息进行处理,测试探测器的性能。

3.功能需求

探测器测试平台框图如1所示。

1探测器测试平台框图

软件功能:求电压直流量(平均值)、噪声均方根值(标准差),样本数在不影响速度的情况下尽量大。通过直流量、噪声均方根值计算温度灵敏度,计算方法:把黑体温度为T1,探测器输出直流量(平均值)V1,噪声均方根值;把黑体温度为T2,探测器输出直流量(平均值)V2,噪声均方根值;利用以下公式计算各模块温度灵敏度

 

界面显示:软件显示界面如2所示,总共显示8个探测器。

点击开始采集按钮,开始连续采集数据,并计算平均值、标准差、峰峰值,波形图实时显示;点击暂停按钮,采集数据结束;点击计算按钮,计算温度灵敏度。(注:峰峰值只是用来观察是否出现大的噪声,不用来计算)

2软件显示界面

 

4.指令说明

上位机和FPGA通过网线连接,下发指令说明如表1所示

1 指令表

指令名称

指令格式

备注

开始采集

16 90 57 EB 00 00 30 00


暂停

16 90 57 EB 00 00 10 00


FPGA到上位机上传数据格式和之前系统上传格式一致,主要改变在太赫兹数据段中每个采样点数据中只有通道1AD0有数据,其他通道的AD数据均为0x00 00,采样点编码器值是人为产生的。

3 网口数据

4 数据格式

3为网口数据,其中黑色下划线为帧头数据,红色下滑线为一个编码器值对应的AD值,其中只有AD08个有数据(0x xx xx),其他AD数据均为0。对应于图4AD0有数据,而其他AD数据均为0

8个有效数据对应8个探测器输出,即数据在帧头aa aa bb bb cc cc dd dd 结束后第一个2字节03 23 为探测器1的值,第二个2字节03 65为探测器2的值,第32字节00 4b 为探测器器3的值……依次对应8个探测器值。(数据为图3中数据)。

运行结果:

8路探测器测试系统_#include

重要知识点:

  1. udp接收数据;
  2. 结构体排序:
  3. QChart绘图;
  4. 文件写入
  5. 代码耗时测算等


udpRecive.cpp
#include "udpRecive.h"
#include <QMessageBox>
#include <qdir.h>
#include <qfiledialog.h>
#include <qmessagebox.h>
#include <qtextstream.h>
#include<QTime>
#include <QNetworkDatagram>
#include <QUdpSocket>
#include <qmutex>//加锁
#include <qstringbuilder.h>
#include <qlabel.h>
#include <qwidget.h>

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

struct PackageHeader
{
////包头大小(sizeof(PackageHeader))
//unsigned int uTransPackageHdrSize;
////当前包头的大小(sizeof(PackageHeader)+当前数据包长度)
//unsigned int uTransPackageSize;
////数据的总大小
//unsigned int uDataSize;
////数据被分成包的个数
//unsigned int uDataPackageNum;
////数据包当前的帧号
//unsigned int uDataPackageCurrIndex;
////数据包在整个数据中的偏移
//unsigned int uDataPackageOffset;

unsigned int numberPackagePerFrame;//单帧业务数据帧需要传输包数M
unsigned int numberFrame;//本次启动后上传的第F帧业务数据帧
unsigned int numberPackage;//当前传送业务帧的第X包
unsigned long int sortNumber;//排序的标志位
unsigned int numberFlag;//0x05 0x06
QString dataStr;

}aaa;

struct FrameHeader
{
QString frameHeaderFlag;//帧头标识
unsigned int frameHeaderLenght;//帧头长度H
unsigned int frameTailLenght;//帧尾长度E
unsigned int ThzDataLenght;//太赫兹数据长度L
unsigned int frameCount;//帧计数器F
unsigned int channelCounts;//探测器通道数C
unsigned int sampleCounts;//采样点数N
double tempartureData1;//温度数据
double tempartureData2;//温度数据
double tempartureData3;//温度数据
double tempartureData4;//温度数据
//unsigned int bigSmallEnds;//大小端说明
}bbb;

QList<PackageHeader> packageHeader;
QMutex mutex;
QString qStr;
QList<double> chartLis;
int numberFrame = 0;
QList<double> dataList[8], dataLis[8];

udpRecive::udpRecive(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//创建对象 初始化
mSocket = new QUdpSocket();

//绑定
mSocket->bind(QHostAddress::AnyIPv4, 8007);
//QMessageBox::information(this, "Warning", QString(QHostAddress::AnyIPv4));//不会乱码
//关联读数据信号readyread
connect(mSocket, SIGNAL(readyRead()), this, SLOT(read_data()));

//test2();
//chartDispaly();
//initChartView();

}


struct tes
{
unsigned int num1;
unsigned int num2;
unsigned int num3;
};
//排列判断
int compare(const PackageHeader &infoA, const PackageHeader &infoB)
{
return infoA.numberFrame < infoB.numberFrame;
}
int compare2(const PackageHeader &infoA, const PackageHeader &infoB)
{

return infoA.sortNumber < infoB.sortNumber;
}


QSplineSeries *series1;
QChart *chart1;
unsigned int tempCount;

void udpRecive::initChartView()
{
QValueAxis *axisX = new QValueAxis();
axisX->setGridLineVisible(false);
series1 = new QSplineSeries();
//for (float i = 0; i < dataList[0].count(); i++)
//{
// series1->append(i, dataList[0][i]);
//}
chart1 = new QChart();
chart1->addSeries(series1);
chart1->legend()->hide();
chart1->createDefaultAxes();

//改变坐标轴的范围
//chart1->axisX()->setMin(0);
//chart1->axisX()->setMax(100);
//chart1->createDefaultAxes();
chart1->setTheme(QChart::ChartThemeDark);
chart1->setTitle("通道1");//设置图标标题
//方法1
ui.widget1->setChart(chart1);
}

void udpRecive::chartDispaly()
{
//最多显示100个
//int count = series1->points().size();
//if (count > 100)
//{
// series1->remove(0);
//}
//series1->clear();
//
////chart1->axisY()->setMin(2);
//// chart1->axisY()->setMax(3);
//for (float i = 0; i < dataList[0].count(); i++)
//{
// series1->append(i, dataList[0][i]);
//}
//chart1->addSeries(series1);
//chart1->legend()->hide();
//chart1->createDefaultAxes();
// //chart1->createDefaultAxes();
//;

//添加数据到serial
//series1->append(QPointF(tempCount, 1));
QSplineSeries *series1 = new QSplineSeries();
for (float i = 0; i < dataList[0].count(); i++)
{
series1->append(i, dataList[0][i]);

}

QChart *chart1 = new QChart();
chart1->addSeries(series1);
chart1->legend()->hide();
chart1->createDefaultAxes();
//QValueAxis *axisX = new QValueAxis();
//axisX->setGridLineVisible(false);
chart1->setTheme(QChart::ChartThemeDark);
//方法1
ui.widget1->setChart(chart1);

QSplineSeries *series2 = new QSplineSeries();
for (int i = 0; i < dataList[1].count(); i++)
{
series2->append(i, dataList[1][i]);

}

QChart *chart2 = new QChart();
chart2->addSeries(series2);
chart2->legend()->hide();
chart2->createDefaultAxes();
//QValueAxis *axisX = new QValueAxis();
//axisX->setGridLineVisible(false);
chart2->setTheme(QChart::ChartThemeDark);
//方法1
ui.widget2->setChart(chart2);

QSplineSeries *series3 = new QSplineSeries();
for (int i = 0; i < dataList[2].count(); i++)
{
series3->append(i, dataList[2][i]);

}

QChart *chart3 = new QChart();
chart3->addSeries(series3);
chart3->legend()->hide();
chart3->createDefaultAxes();
chart3->setTheme(QChart::ChartThemeDark);
//方法1
ui.widget3->setChart(chart3);

QSplineSeries *series4 = new QSplineSeries();
for (int i = 0; i < dataList[3].count(); i++)
{
series4->append(i, dataList[3][i]);

}

QChart *chart4 = new QChart();
chart4->addSeries(series4);
chart4->legend()->hide();
chart4->createDefaultAxes();
chart4->setTheme(QChart::ChartThemeDark);
//方法1
ui.widget4->setChart(chart4);

QSplineSeries *series5 = new QSplineSeries();
for (int i = 0; i < dataList[4].count(); i++)
{
series5->append(i, dataList[4][i]);

}

QChart *chart5 = new QChart();
chart5->addSeries(series5);
chart5->legend()->hide();
chart5->createDefaultAxes();
chart5->setTheme(QChart::ChartThemeDark);
//方法1
ui.widget5->setChart(chart5);

QSplineSeries *series6 = new QSplineSeries();
for (int i = 0; i < dataList[5].count(); i++)
{
series6->append(i, dataList[5][i]);

}

QChart *chart6 = new QChart();
chart6->addSeries(series6);
chart6->legend()->hide();
chart6->createDefaultAxes();
chart6->setTheme(QChart::ChartThemeDark);
//方法1
ui.widget6->setChart(chart6);

QSplineSeries *series7 = new QSplineSeries();
for (int i = 0; i < dataList[6].count(); i++)
{
series7->append(i, dataList[6][i]);

}

QChart *chart7 = new QChart();
chart7->addSeries(series7);
chart7->legend()->hide();
chart7->createDefaultAxes();
chart7->setTheme(QChart::ChartThemeDark);
//方法1
ui.widget7->setChart(chart7);

QSplineSeries *series8 = new QSplineSeries();
for (int i = 0; i < dataList[7].count(); i++)
{
series8->append(i, dataList[7][i]);

}

QChart *chart8 = new QChart();
chart8->addSeries(series8);
chart8->legend()->hide();
chart8->createDefaultAxes();
chart8->setTheme(QChart::ChartThemeDark);
//方法1
ui.widget8->setChart(chart8);


//方法2
//QChartView *chartView = new QChartView(chart);
//chartView->setRenderHint(QPainter::Antialiasing);
//this->setCentralWidget(chartView);
}

//{
// QChart *chart2 = new QChart();
// QLineSeries *series = new QLineSeries();
// *series << QPointF(1.0, 1.0) << QPointF(2.0, 73.0) << QPointF(3.0, 268.0) << QPointF(4.0, 17.0)
// << QPointF(5.0, 4325.0) << QPointF(6.0, 723.0);
// chart2->addSeries(series);
// chart2->legend()->hide();//隐藏图例
// chart2->setTitle("Logarithmic axis example");//设置图标标题
// QValueAxis *axisX = new QValueAxis();
// axisX->setTitleText("Data point");
// axisX->setLabelFormat("%im");
// axisX->setTickCount(series->count());
// chart2->addAxis(axisX, Qt::AlignBottom);
// series->attachAxis(axisX);
// QLogValueAxis *axisY = new QLogValueAxis();
// axisY->setTitleText("Values");
// axisY->setLabelFormat("%gkg");
// axisY->setBase(8);
// //axisY->setMinorTickCount(-1);
// chart2->addAxis(axisY, Qt::AlignLeft);
// series->attachAxis(axisY);
// QChartView *chartView = new QChartView(chart2);
// chartView->setRenderHint(QPainter::Antialiasing);
// //connect(series, &QLineSeries::hovered, this, &QtGuiApplication1::showValue);
// this->setCentralWidget(chartView);
//
//}



double average(double *value, int dataCount)
{
double average, sum;
for (int i = 0; i < dataCount; i++) {
sum += value[i];
}
average = sum / dataCount;

return average;
}


void udpRecive::dataProcess(QString str)
{
QTime startTime = QTime::currentTime();
bbb.frameHeaderFlag = str.mid(0, 8);
bbb.frameHeaderLenght = str.mid(8, 4).toInt(NULL, 16);
bbb.frameTailLenght = str.mid(12, 4).toInt(NULL, 16);
bbb.ThzDataLenght = str.mid(16, 8).toInt(NULL, 16);
bbb.frameCount = str.mid(24, 8).toInt(NULL, 16);
bbb.channelCounts = str.mid(32, 4).toInt(NULL, 16);
bbb.sampleCounts = str.mid(36, 4).toInt(NULL, 16);
bbb.tempartureData1 = str.mid(40, 4).toInt(NULL,16)/16.0;//温度是负数的问题先留下来,以后处理
bbb.tempartureData2 = str.mid(44, 4).toInt(NULL, 16)/16.0;
bbb.tempartureData3 = str.mid(48, 4).toInt(NULL, 16)/16.0;
bbb.tempartureData4 = str.mid(52, 4).toInt(NULL, 16)/16.0;
double *channel, *channel1;
QList<double> Lis;
double variance = 0.0, average_original = 0.0, average_converted[8], stdDev = 0.0, maxNum = 0.0, minNum = 0.0;
QChart *ch = new QChart[8]{};
channel = new double[bbb.sampleCounts];
channel1 = new double[bbb.sampleCounts];
//ui.listWidget->addItem(QString::number(bbb.tempartureData1) +"***" + QString::number(bbb.tempartureData2)+ "***"+
// QString::number(bbb.tempartureData3)+"***"+ QString::number(bbb.tempartureData4));//显示数据

ui.tempartureLabel->setText("设备温度:\r\nT1:" + QString::number(bbb.tempartureData1,'f',2) + "°\r\nT2:" + QString::number(bbb.tempartureData2, 'f', 2) + "°\r\nT3:" +
QString::number(bbb.tempartureData3, 'f', 2)+ "°\r\nT4:" + QString::number(bbb.tempartureData4, 'f', 2)+ "°");
//ui.tempartureLabel->setText("设备温度:" + QString::number(bbb.tempartureData1) );
//for (int i = 0; i < bbb.channelCounts; i++)// 所有通道全部计算
for (int i = 0; i < 8; i++)// 只计算要用的 8个通道
{
for (int j = 0; j < bbb.sampleCounts; j++)
//for (int j = 0; j < 1; j++)
{
channel[j] = str.mid(j * 74*2+ i * 2*2 + 80, 4).toInt(NULL, 16) / 8192.0 * 10;
//channel1[j] = str.mid(j * 74*2 + i * 2*2 + 80, 4).toInt(NULL, 16);
//if(i==0)
//Lis.append(channel[j]);
}
average_converted[i] = average(channel, bbb.sampleCounts);
//average_converted[i] = average(channel, 1);
dataList[i].append(average_converted[i]);
//dataLis[i].append(average_converted[i]);
if (dataList[i].count() > 10)
{
dataList[i].removeAt(0);

}
//qSort(dataLis[i].begin(), dataLis[i].end());
//if (i == 0)
//{
// series1->append(QPointF(tempCount, 1));
// //最多显示100个
// if (series1->points().size() > 100)
// {
// series1->remove(0);
// //tempCount = 99;
// }
// //if (dataList[i].count() > 100)
// // dataList[i].removeAt(0);
//}
}


chartDispaly();
//initChartView();

tempCount++;
QTime stopTime = QTime::currentTime();
int elapsed = startTime.msecsTo(stopTime);
ui.listWidget->addItem("QTime.currentTime =" + QString::number(elapsed) + "ms");//显示数据
}
void udpRecive::test1()
{
//aaa.uTransPackageHdrSize = 10;
//aaa.uTransPackageHdrSize = 20;
//aaa.uTransPackageHdrSize = 30;
//aaa.uTransPackageHdrSize = 40;
//aaa.uTransPackageHdrSize = 50;

//aaa[5].uTransPackageSize = 20;
//aaa[5].uDataSize = 30;
//aaa[5].uDataPackageNum = 40;
//aaa[5].uDataPackageCurrIndex = 50;
//aaa[5].uDataPackageOffset = 60;

//QMessageBox::information(this, "Warning", QString::number(aaa.uTransPackageHdrSize));
QString curPath = QDir::currentPath();//获取系统当前目录
//qSort(packageHeader.begin(), packageHeader.end(), compare);

//QMessageBox::information(this, "Warning", QString::number(packageHeader.count()));//不会乱码


if (packageHeader.count() ==0 )
return;

//QMessageBox::information(this, "Warning", QString::number(packageHeader.at(0).numberPackagePerFrame));//不会乱码
//ui.listWidget->addItem(QString::number(packageHeader.count()));//显示数据
if ( packageHeader.count() >= aaa.numberPackagePerFrame )
//if (packageHeader.count() >= 44)
{
qSort(packageHeader.begin(), packageHeader.end(), compare2);
if (packageHeader.at(0).numberPackage == 1)
{
//QTime startTime = QTime::currentTime();
//for (int i = 0; i < packageHeader.at(0).numberPackagePerFrame; i++)
for (int i = 0; i < aaa.numberPackagePerFrame; i++)
{
//saveTextByIODevice(curPath + "\\" + QString::number((numberFrame) % 10000) + ".txt", QString::number(packageHeader.at(i).numberPackagePerFrame) + "*"+ QString::number(packageHeader.at(i).numberFrame) +"**"
// + QString::number(packageHeader.at(i).numberPackage) + "***"+ QString::number(packageHeader.at(i).numberFlag) + "\r\n");
if (numberFrame > 0)
{
//saveTextByIODevice(curPath + "\\" + QString::number((numberFrame) % 10000) + ".txt",
// QString::number(packageHeader.at(0).numberPackagePerFrame) + "*" + QString::number(packageHeader.at(0).numberFrame) + "**"
// + QString::number(packageHeader.at(0).numberPackage) + "***" + QString::number(packageHeader.at(0).numberFlag) + "\r\n" +
// packageHeader.at(0).dataStr + "\r\n");
qStr.append(packageHeader.at(0).dataStr);
}
packageHeader.removeAt(0);

}
//QTime startTime = QTime::currentTime();
if (qStr != "")
{
saveTextByIODevice(curPath + "\\log\\" + QString::number((numberFrame) % 10000) + ".txt", qStr);
dataProcess(qStr);
}
qStr.clear();

//QTime stopTime = QTime::currentTime();
//int elapsed = startTime.msecsTo(stopTime);

//qDebug() << "QTime.currentTime =" << elapsed << "ms";
//ui.listWidget->addItem("QTime.currentTime =" + QString::number(elapsed) + "ms");//显示数据

numberFrame++;
//packageHeader.clear();
}
else
{
//for (int i = 0; i < packageHeader.count(); i++)
{
while (packageHeader.at(0).numberPackage != 1)
{
packageHeader.removeAt(0);
};
}

}
}
}



int compare1(const tes &infoA, const tes &infoB)
{

//、return (QString::number(infoA.num1) + QString::number(infoA.num2)).toInt(NULL, 10) < (QString::number(infoA.num1) + QString::number(infoA.num2)).toInt(NULL, 10);
return infoA.num3 < infoB.num3;


}
void udpRecive::test2()
{
QTime startTime = QTime::currentTime();
QList<tes> s;
tes aa;
tes bb;
tes cc;
tes dd;
tes ee;
aa.num1 = 3;
bb.num1 = 2;
cc.num1 = 2;
dd.num1 = 3;
ee.num1 = 1;

aa.num2 = 3;
bb.num2 = 5;
cc.num2 = 4;
dd.num2 = 8;
ee.num2 = 6;

aa.num3 = aa.num1*10 + aa.num2;
bb.num3 = bb.num1*10 + bb.num2;
cc.num3 = cc.num1*10 + cc.num2;
dd.num3 = dd.num1*10 + dd.num2;
ee.num3 = ee.num1*10 + ee.num2;

s.append(aa);
s.append(bb);
s.append(cc);
s.append(dd);
s.append(ee);
qSort(s.begin(), s.end(), compare1);

for (int i = 0; i < s.count(); i++)
{
qDebug() << s.at(i).num1;
QMessageBox::information(this, "Warning", QString::number(s.at(i).num1) + "***" + QString::number(s.at(i).num2));//不会乱码
}
QTime stopTime = QTime::currentTime();
int elapsed = startTime.msecsTo(stopTime);

qDebug() << "QTime.currentTime =" << elapsed << "ms";
}

void udpRecive::read_data()
{
QString curPath = QDir::currentPath();//获取系统当前目录
QString dataStr;
QByteArray array;
QHostAddress address;
quint16 port;


while (mSocket->hasPendingDatagrams()) {
//array.resize(mSocket->bytesAvailable());//根据可读数据来设置空间大小
array.resize(1420);//根据可读数据来设置空间大小
mSocket->readDatagram(array.data(), array.size(), &address, &port); //读取数据
//ui.listWidget->addItem(array.toHex() + "******" + QString::number(array.size(), 10) + "!!!!");//显示数据
QString qstrTemp = QString(array.toHex());
//aaa.uTransPackageHdrSize = qstrTemp.mid(0).toLocal8Bit();
aaa.numberPackagePerFrame = (qstrTemp.mid(0, 4)).toInt(NULL, 16);
aaa.numberFrame = (qstrTemp.mid(4, 8)).toInt(NULL, 16);
aaa.numberPackage = (qstrTemp.mid(12, 4)).toInt(NULL, 16);
aaa.numberFlag = (qstrTemp.mid(16, 4)).toInt(NULL, 16);
aaa.dataStr = qstrTemp.mid(20);
aaa.sortNumber = aaa.numberFrame * 1000 + aaa.numberPackage;

packageHeader.append(aaa);

//QMessageBox::information(this, "Warning", QString::number(aaa.numberFlag));//不会乱码
//发送反馈数据
//saveTextByIODevice(curPath + "\\1.txt", aaa.dataStr + "\r\n");
//saveTextByIODevice(curPath + "\\1.txt", array.toHex() + "\r\n");
}
//if (numberPackage == 0)
//{
// numberPackage = 1;
//mutex.lock();
test1();
//mutex.unlock();
//}

}

void udpRecive::read_data1()
{
QString curPath = QDir::currentPath();//获取系统当前目录
QString dataStr;
QByteArray array;
QHostAddress address;
quint16 port;

array.resize(20);//根据可读数据来设置空间大小

//while (mSocket->hasPendingDatagrams()) {
// QNetworkDatagram datagram = mSocket->receiveDatagram();
// ui.listWidget->addItem(datagram.data().toHex() + "*****" + QString::number(numberPackage));//显示数据
//}

while (mSocket->hasPendingDatagrams()) {
mSocket->readDatagram(array.data(), array.size(), &address, &port); //读取数据
ui.listWidget->addItem(array.toHex() + "******" + QString::number(numberFrame) + "!!!!");//显示数据
}
numberFrame++;

}
void udpRecive::saveTextByIODevice(const QString &aFileName, QString st)
{ //用IODevice方式保存文本文件
QFile aFile(aFileName);
//aFile.setFileName(aFileName);
if (!aFile.open(QIODevice::ReadWrite | QIODevice::Append))
return ;
QString str = st;//整个内容作为字符串
QByteArray strBytes = str.toUtf8();//转换为字节数组
//QByteArray strBytes=str.toLocal8Bit();
aFile.write(strBytes, strBytes.length()); //写入文件
aFile.close();
//ui->tabWidget->setCurrentIndex(0);

}

QString udpRecive::ByteArrayToHexString(QByteArray data) {

QString ret(data.toHex().toUpper());

int len = ret.length() / 2;

qDebug() << len;
for (int i = 1; i < len; i++)
{
qDebug() << i;
ret.insert(2 * i + i - 1, " ");
}
return ret;

}
void udpRecive::test()
{
//quint8 str[8]={ 0x41,0x43,0x45,0x46,0x47,0x48,0x49,0x50 };
//QByteArray array = QByteArray::fromHex(str);
//char* str = new char[]{ 0x41,0x43,0x45,0x46,0x47,0x48,0x49,0x50 };
//QChar str[3];
//str[0] = '0x01';
//str[1] = '0x03';
//str[2] = '0x05';
//QByteArray array(str);
//qDebug() << sizeof(str) << str.toHex();
//QMessageBox::information(this, "Warning", QString(array.size()));
//QMessageBox::information(this, "Warning", QString::sizeof(str));//不会乱码
//QByteArray array("0x01");
//QHostAddress address;
//quint16 port;
//ui.listWidget->addItem(ByteArrayToHexString(array));//显示数据
//发送反馈数据

//QByteArray array(9, 'Q');
//char buf[10];//数组
//int len_array = array.size();
//int len_buf = sizeof(buf);
//int len = qMin(len_array, len_buf);

//// 转化
//memcpy(buf, array, len);

//char buf[10];
////给buf赋值
//for (int i = 0; i < 10; i++)
//{
// buf[i] = i ;//其中存在'\0'元素
//}

//// 转化
//QByteArray array;
//array.resize(sizeof(buf));//重置数据大小
//memcpy(array.data(), buf, sizeof(buf));//copy数据
//ui.listWidget->addItem(array.toHex());//显示数据
////qDebug() << array.size() << array.toHex()

QByteArray bb("Helloewdecerfrefvrrtvbrtvbrgtevbrt"); //创建字节数组
int n = bb.size(); //返回字节数组的大小
//不包括终止符 \0
QByteArray ba;//创建一个空字节数组
ba.resize(6);//设置字节数组的大小
// \0 为终止符
ba[0] = 0x3c; //赋值
ba[1] = 0xb8;
ba[2] = 0x64;
ba[3] = 0x18;
ba[4] = 0xca;

QByteArray cc=bb.data();//创建一个空字节数组

qDebug() << cc.size() << cc.toHex();
QMessageBox::information(this, "Warning", cc);//不会乱码
}

void udpRecive::on_checkBox_clicked(bool checked)
{
if (checked)
{
//加入组播
mSocket->joinMulticastGroup(QHostAddress("192.168.1.30"));
}

else
{
//退出组播
mSocket->leaveMulticastGroup(QHostAddress("192.168.1.30"));

}

}

udpRecive.h


#include <QtWidgets/QMainWindow>
#include <QtCharts>
#include "ui_udpRecive.h"
#include<QUdpSocket>

class udpRecive : public QMainWindow
{
Q_OBJECT

public:
udpRecive(QWidget *parent = Q_NULLPTR);
private:
Ui::udpReciveClass ui;
QUdpSocket *mSocket;


private slots:
void on_checkBox_clicked(bool);
void saveTextByIODevice(const QString &aFileName, QString st);
void read_data(); void read_data1();
void test(); void test1(); void test2();
void chartDispaly();
void initChartView();
//struct PackageHeader aaa;
void udpRecive::dataProcess(QString str);
QString udpRecive::ByteArrayToHexString(QByteArray data);
};