码迷,mamicode.com
首页 > 其他好文 > 详细

数据解析-20191121

时间:2019-11-21 18:39:16      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:rev   定位   his   signal   include   space   在线   关心   缺省   

NetClient.h

#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <stdint.h>
#include <iostream>
using namespace std;

#pragma   pack(1)

//msg_type 为基站时,仅需关心Precision和ma_id
typedef struct
{
quint8 msg_type; //数据类型 1-签定,2-基站
quint8 Precision; //类型为标签时,表示电量;为基站时,表示状态:1-在线,0-离线
quint8 prevsion;  //暂时无效
quint8 seq_num; //数据序列号 每次定位数据会增加1,范围0-255
quint16 ma_id; //类型为标签时,此字段无效;类型为基站时,为基站ID
quint16 TAG_ID; //类型为标签时,为标签ID;类型为基站时,无效
qint32 pos[3]; //坐标值,仅在类型为基站时有效。 单位mm pos[0]为x坐标,pos[1]为y坐标,pos[2]为z坐标
}stuDevData;
#pragma pack () /*取消指定对齐,恢复缺省对齐*/

class NetClient: public QObject
{
    Q_OBJECT
public:
    NetClient(QObject* parent = 0);
    ~NetClient();
    void StartConnect(QString DestIp,int DestPort);
private:
      QTcpSocket *m_pTcpSocket;
      bool flag;
public slots:
     void ReadTcp();
     void displayError(QAbstractSocket::SocketError socketError);
};

#endif // CLIENT_H

NetClient.cpp

#include "NetClient.h"
#include <QtCore/QCoreApplication>
#include <QHostAddress>
#include <QByteArray>
#include <qdebug.h>

NetClient::NetClient(QObject* parent): QObject(parent)
{
    m_pTcpSocket = new QTcpSocket(this);
    m_pTcpSocket->bind(9000);
    connect(m_pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
    connect(m_pTcpSocket, SIGNAL(readyRead()),this, SLOT(ReadTcp()));
}

NetClient:: ~NetClient()
{
    m_pTcpSocket->close();
}

void NetClient::StartConnect(QString DestIp,int DestPort)
{
    cout<<"开始连接..."<<endl;
    QHostAddress addr(DestIp);
    m_pTcpSocket->connectToHost(addr, DestPort);
    cout<<"\r\n";
}

void NetClient::ReadTcp()
{
    while(m_pTcpSocket->bytesAvailable()>0)
    {
        QByteArray baRevData;
        baRevData.resize(m_pTcpSocket->bytesAvailable());
        m_pTcpSocket->read(baRevData.data(),baRevData.size());

        int Datalen = sizeof(stuDevData);
        int Revlen = baRevData.size();
        int num = 0;

        int remainder = Revlen%Datalen;//求余
        if(remainder != 0)
        {
            cout<< "数据错误"<<endl;
            return;
        }
        num = Revlen/Datalen;
        while (num > 0)
        {
            //收到的16进制数先转成字符串
            QString Hexstr = baRevData.toHex().toUpper();
            QByteArray ba = Hexstr.toLatin1();

            uint8_t *pbuf = (uint8_t *)baRevData.data();
            stuDevData *stuData = (stuDevData*)pbuf;

            if(stuData->msg_type == 1)
            {
                 cout<< "收到标签数据:"<<ba.data()<<endl;
                 cout<<"标签ID:"<<stuData->TAG_ID<<endl;
                 int num = stuData->seq_num;
                 cout<<"序列号:"<<num<<endl;
                 cout<<"坐  标:"<<"X-"<<stuData->pos[0]<<" "<<"Y-"<<stuData->pos[1]<<" "<<"Z-"<<stuData->pos[2]<<endl;
                 int Elct = stuData->Precision;
                 cout<<"电  量:"<<Elct<<endl;
                 cout<<"\r\n";
                 continue;
            }
            else if(stuData->msg_type == 2)
            {
                 cout<< "收到基站数据:"<<ba.data()<<endl;
                 cout<<"基站ID:"<<stuData->ma_id<<endl;
                 int num = stuData->seq_num;
                 cout<<"序列号:"<<num<<endl;                 
                 if(stuData->Precision == 1)
                 {
                     cout<<"状  态:在线"<<endl;
                 }
                 else
                 {
                      cout<<"状  态:离线"<<endl;
                 }
                 cout<<"\r\n";
                 continue;
            }
            else
            {
                cout<< "数据错误"<<endl;
                continue;
            }
            num--;
        }
    }

}

void NetClient::displayError(QAbstractSocket::SocketError socketError)
{
    cout<<"连接断开"<<endl;
    return;
}

 

数据解析-20191121

标签:rev   定位   his   signal   include   space   在线   关心   缺省   

原文地址:https://www.cnblogs.com/herd/p/11907341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!