#include "UDPRecData.h" #include #include UDPRecData::UDPRecData(int nPort, QObject *parent) : QObject(parent) { receiver = new QUdpSocket(this); receiver->bind(nPort, QUdpSocket::ShareAddress); connect(receiver, SIGNAL(readyRead()), this, SLOT(processPendingDatagram())); } UDPRecData::~UDPRecData() { } void UDPRecData::ParaStruct(QByteArray &byte) { ParamStruct2 stru; int nSize = 0; memcpy(&(stru.label), byte.constData() + nSize, sizeof(stru.label)); nSize += sizeof(stru.label); memcpy(&(stru.qby), byte.constData() + nSize, sizeof(stru.qby)); nSize += sizeof(stru.qby); memcpy(&(stru.twh), byte.constData() + nSize, sizeof(stru.twh)); nSize += sizeof(stru.twh); memcpy(&(stru.xmbh), byte.constData() + nSize, sizeof(stru.xmbh)); nSize += sizeof(stru.xmbh); memcpy(&(stru.hqsj), byte.constData() + nSize, sizeof(stru.hqsj)); nSize += sizeof(stru.hqsj); memcpy(&(stru.mbsj), byte.constData() + nSize, sizeof(stru.mbsj)); nSize += sizeof(stru.mbsj); memcpy(&(stru.mbph), byte.constData() + nSize, sizeof(stru.mbph)); nSize += sizeof(stru.mbph); memcpy(&(stru.mbbh), byte.constData() + nSize, sizeof(stru.mbbh)); nSize += sizeof(stru.mbbh); memcpy(&(stru.task_code), byte.constData() + nSize, sizeof(stru.task_code)); nSize += sizeof(stru.task_code); memcpy(&(stru.jd), byte.constData() + nSize, sizeof(stru.jd)); nSize += sizeof(stru.jd); memcpy(&(stru.wd), byte.constData() + nSize, sizeof(stru.wd)); nSize += sizeof(stru.wd); memcpy(&(stru.high), byte.constData() + nSize, sizeof(stru.high)); nSize += sizeof(stru.high); memcpy(&(stru.direction), byte.constData() + nSize, sizeof(stru.direction)); nSize += sizeof(stru.direction); memcpy(&(stru.speed), byte.constData() + nSize, sizeof(stru.speed)); nSize += sizeof(stru.speed); memcpy(&(stru.shipname), byte.constData() + nSize, sizeof(stru.shipname)); nSize += sizeof(stru.shipname); memcpy(&(stru.xianhao), byte.constData() + nSize, sizeof(stru.xianhao)); nSize += sizeof(stru.xianhao); memcpy(&(stru.property), byte.constData() + nSize, sizeof(stru.property)); nSize += sizeof(stru.property); memcpy(&(stru.Object_Class), byte.constData() + nSize, sizeof(stru.Object_Class)); nSize += sizeof(stru.Object_Class); //memcpy(&(stru.leixing), byte.constData() + nSize, sizeof(stru.leixing)); //nSize += sizeof(stru.leixing); //memcpy(&(stru.xinghao), byte.constData() + nSize, sizeof(stru.xinghao)); //nSize += sizeof(stru.xinghao); //memcpy(&(stru.country), byte.constData() + nSize, sizeof(stru.country)); //nSize += sizeof(stru.country); //memcpy(&(stru.use), byte.constData() + nSize, sizeof(stru.use)); //nSize += sizeof(stru.use); //memcpy(&(stru.import), byte.constData() + nSize, sizeof(stru.import)); //nSize += sizeof(stru.import); //memcpy(&(stru.JudgeProperty), byte.constData() + nSize, sizeof(stru.JudgeProperty)); //nSize += sizeof(stru.JudgeProperty); //memcpy(&(stru.BeliefMass), byte.constData() + nSize, sizeof(stru.BeliefMass)); //nSize += sizeof(stru.BeliefMass); //memcpy(&(stru.ThreatenRank), byte.constData() + nSize, sizeof(stru.ThreatenRank)); //nSize += sizeof(stru.ThreatenRank); //memcpy(&(stru.FusionMode), byte.constData() + nSize, sizeof(stru.FusionMode)); //nSize += sizeof(stru.FusionMode); //memcpy(&(stru.ObjectState), byte.constData() + nSize, sizeof(stru.ObjectState)); //nSize += sizeof(stru.ObjectState); //memcpy(&(stru.RelatingRatio), byte.constData() + nSize, sizeof(stru.RelatingRatio)); //nSize += sizeof(stru.RelatingRatio); //memcpy(&(stru.SuperiorUnit), byte.constData() + nSize, sizeof(stru.SuperiorUnit)); //nSize += sizeof(stru.SuperiorUnit); //memcpy(&(stru.Designation), byte.constData() + nSize, sizeof(stru.Designation)); //nSize += sizeof(stru.Designation); //memcpy(&(stru.SendBs), byte.constData() + nSize, sizeof(stru.SendBs)); //nSize += sizeof(stru.SendBs); //memcpy(&(stru.SourceInf), byte.constData() + nSize, sizeof(stru.SourceInf)); //nSize += sizeof(stru.SourceInf); //memcpy(&(stru.by1), byte.constData() + nSize, sizeof(stru.by1)); //nSize += sizeof(stru.by1); //memcpy(&(stru.by2), byte.constData() + nSize, sizeof(stru.by2)); //nSize += sizeof(stru.by2); CreateMsg(stru); } QVariant UDPRecData::CreateMsg(ParamStruct2 &stru) { QString strMsg = QString("%1 %2 %3 %4 %5 %6 %7 %8 %9") .arg(QString::number(stru.hqsj)) .arg(QString::number(stru.mbsj)) .arg(QString::number(stru.mbph)) .arg(QString::number(stru.jd, 'f', 6)) .arg(QString::number(stru.wd, 'f', 6)) .arg(QString::number(stru.high, 'f', 2)) .arg(QString::number(stru.direction)) .arg(QString::number(stru.speed)) .arg(QString::fromLocal8Bit(stru.shipname)); emit signalUpdateMsg(strMsg); return QVariant(strMsg); } void UDPRecData::processPendingDatagram() { while (receiver->hasPendingDatagrams()) { QByteArray byte; byte.resize(receiver->pendingDatagramSize()); receiver->readDatagram(byte.data(), receiver->pendingDatagramSize()); ParaStruct(byte); } }