Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unpack UDP Message
Forum Updated to NodeBB v4.3 + New Features

Unpack UDP Message

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 455 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chanchan
    wrote on last edited by
    #1

    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();
    
    KillerSmathK 1 Reply Last reply
    0
    • C Chanchan

      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();
      
      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      @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
      }
      

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      2
      • C Offline
        C Offline
        Chanchan
        wrote on last edited by
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • C Chanchan

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved