Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unpack UDP Message



  • Here is my problem, I have a Sockt recovering UDP data. These data are multitude and have an identificator byte.
    In all these data I want to recover GPS informations : longitude and lattitude. The ID of GPS is 0x1900.

    I dont know how to proceed to separate them from my reception socket and then display it on my screen on a basic Main widow

    Can someone could help me ?

    Here is my socket reception code :

    #ifndef MYUDP_H
    #define MYUDP_H
    #include <QObject>
    #include <QUdpSocket>
    
    class MyUDP : public QObject
    {
        Q_OBJECT
    public:
        explicit MyUDP(QObject *parent = 0);
        void HelloUDP();
    signals:
    
    public slots:
        void readyRead();
    
    private:
        QUdpSocket *socket;
    
    };
    #endif // MYUDP_H
    
    
    #include "myudp.h"
    
    MyUDP::MyUDP(QObject *parent) :
        QObject(parent)
    {
        // create a QUDP socket
        socket = new QUdpSocket(this);
    
    
        // The most common way to use QUdpSocket class is
        // to bind to an address and port using bind()
        // bool QAbstractSocket::bind(const QHostAddress & address,
        //     quint16 port = 0, BindMode mode = DefaultForPlatform)
        socket->bind(QHostAddress("192.168.2.x"), 14500);
    
        connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    }
    
    void MyUDP::HelloUDP()
    {
        QByteArray Data;
        Data.append("En ecoute");
    
        // Sends the datagram datagram
        // to the host address and at port.
        // qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
        //                      const QHostAddress & host, quint16 port)
        socket->writeDatagram(Data, QHostAddress("192.168.2.x"), 14500);
    }
    
    void MyUDP::readyRead()
    {
        // when data comes in
        const auto SIZE_BUF = 500000;
        QByteArray buffer;
        buffer.reserve(SIZE_BUF);
        buffer.resize(socket->pendingDatagramSize());
    
        QHostAddress sender;
        quint16 senderPort;
    
        // qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize,
        //                 QHostAddress * address = 0, quint16 * port = 0)
        // Receives a datagram no larger than maxSize bytes and stores it in data.
        // The sender's host address and port is stored in *address and *port
        // (unless the pointers are 0).
    
        socket->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);
    
        qDebug() << "Message from: " << sender.toString();
        qDebug() << "Message port: " << senderPort;
        qDebug() << "Message: " << buffer;
    }
    
    

    and my main :

        QCoreApplication a(argc, argv);
    
        MyUDP client;
    
        client.HelloUDP();
    
        return a.exec();
    


  • @Chanchan

    Create a MyUDP variable as a MainWindow's Member variable, then process the datagram and emit a signal to forward post-processed gps data from MyUdp to MainWindow.

    Below is an example to guide you.

    class MyUDP : public QObject
    {
        Q_OBJECT
        
       ...
    
    signals:
        void gpsDataReceived(double lat, double lng, int uid) // signal to forward the data to MainWindow
    };
    #endif // MYUDP_H
    
    void MyUDP::readyRead()
    {
    
        ...
    
        socket->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);
    
       // Processing buffer Data. Note: The processing step depend of your GPS transmission protocol
       // Assuming you will create gps_lat, gps_lng and gps_uid variables to store the processed gps data
        emit gpsDataReceived(gps_lat, gps_lng, gps_uid); 
    }
    
    class YourMainWindow : public QMainWindow{
       Q_OBJECT
       // Your MainWindow implementation
    public: 
       YourMainWindow(); 
    
    public slots:
       void processGpsData(double lat, double lng, int uid);
    
    private:
       MyUDP _client;
    }
    
    YourMainWindow::YourMainWindow() : QMainWindow()
    {
       // your mainwindow constructor implementation
    
       connect(&_client, &MyUDP::gpsDataReceived, this, &YourMainWindow:: processGpsData); // connect client signal to window slot
    }
    
    void YourMainWindow::processGpsData(double lat, double lng, int uid)
    {
       // implement how you intend to show the data on mainwindow
    }
    


  • @KillerSmath Thank you for your example !
    But how the program knows if the package he is receiving is a GPS protocol or not ?


  • Lifetime Qt Champion

    @Chanchan said in Unpack UDP Message:

    But how the program knows if the package he is receiving is a GPS protocol or not ?

    That depends on the protocol you're using to communicate. You should send that information together with actual GPS data.


Log in to reply