实验环境:Ubuntu14.04,qt5.5.0,dsp板。
实验描述:dsp板会不断发送udp数据包,数据包是float类型的数据。
#ifndef UDPRECEIVER
#define UDPRECEIVER
#include
#include"radardata.h"
class QUdpSocket;
class UdpReceiver:public QObject
{
Q_OBJECT
public:
UdpReceiver(QObject *parent = 0);
~UdpReceiver();
private slots:
void processPendingDatagrams();
private:
QUdpSocket* udpSocket;
QByteArray udppak1;
int pNum;
RadarData rd;
RadarData::Trace tra;
};
#endif // UDPRECEIVER
#include"udpreceiver.h"
#include
#include
#include
UdpReceiver::UdpReceiver(QObject* parent)
:QObject(parent)
{
pNum = 1;
udpSocket = new QUdpSocket(this);
udpSocket->bind(50000);
connect(udpSocket,SIGNAL(readyRead()),
this,SLOT(processPendingDatagrams()));
}
UdpReceiver::~UdpReceiver()
{
}
void UdpReceiver::processPendingDatagrams()
{
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
if(datagram.size()!=1204)
{
pNum = 1;
continue;
}
qDebug()<<"receive data:
";
if(pNum == 1)
{
if(datagram.at(0)!=1)
{
continue;
}
udppak1 = datagram;
pNum = 2;
}
else if(pNum == 2)
{
if(datagram.at(0)!=2)
{
pNum=1;
continue;
}
QTime time;
time.start();
datagram.remove(0,4);
udppak1.remove(0,4);
udppak1.append(datagram);
pNum = 1;
QDataStream in(&udppak1,QIODevice::ReadOnly);
in.setByteOrder(QDataStream::LittleEndian);//重要!读取设置小端模式
in.setFloatingPointPrecision(QDataStream::SinglePrecision);//重要!默认double,float需要设置
in>>rd.targets_num>>rd.type>>rd.version>>rd.curtime;
qDebug()<<"tracedata:"<>tra.targetId>>tra.target_time>>tra.r>>tra.ah>>tra.av>>tra. v>>tra. x>>tra. y>>tra. z>>tra. vx>>tra. vy>>tra. vz>>tra. ax>>tra. ay>>tra. az;
qDebug()<<"trace:"<