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. UDP communication readyread
Forum Updated to NodeBB v4.3 + New Features

UDP communication readyread

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 393 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.
  • D Offline
    D Offline
    Damian7546
    wrote on last edited by Damian7546
    #1

    Hi,
    I try do UDP communication.

    Firstly I'm sending to broadcast addres 192.0.1.255 with frame to get IP addres device wihich I should exchange data.
    This recive properly IP addres, after them I send data to binded device sendData so in ready read I get only one datagram (Len=1).. But rest of data I do not have in my readyread... I should get other data (Len=28,Len=28, Len=74.....) Like below:
    2.jpg

    my code below:

    
    MyUdpCom::MyUdpCom(QObject *parent)
        : QObject{parent}
    {
        socket = new QUdpSocket(this);
        socket->bind(QHostAddress::LocalHost, 5010);
        connect(socket, &QUdpSocket::readyRead, this, &MyUdpCom::readyRead);
    
    }
    void MyUdpCom::bindWithDevice(QByteArray name, QByteArray ipAddr)
    {
        QByteArray buff;
        buff += name;
        buff += ipAddr; 
        socket->writeDatagram(buff, QHostAddress("192.0.1.255"), 5000);
    }
    
    void MyUdpCom::sendData(QString ipAddr)
    {
        socket->writeDatagram(buff, QHostAddress(ipAddr), 5010);
    
    }
    void MyUdpCom::readyRead()
    {
        QByteArray buff;
        buff.resize(socket->pendingDatagramSize());
    
        QHostAddress sender;
        quint16 senderPort;
    
        socket->readDatagram(buff.data(), buff.size(),&sender, &senderPort);
    
        qDebug() << "Message from sender: " << sender.toString();
        qDebug() << "Message port: " << senderPort;
        qDebug() << "Message" << buff;
    
        if (buff.at(0) == 0x2F){
            qDebug() << "We have Ip addres destination";
            sendData(sender.toString());
        }
    }
    
    
    
    

    main:

        MyUdpCom server;
        MyUdpCom client;
        client.bindWithDevice("devMy01","192.0.1.169");
    
    JonBJ 1 Reply Last reply
    0
    • D Damian7546

      @JonB Any idea?
      Maybe this is a problem with creating client and server UDP:

       MyUdpCom server;
       MyUdpCom client;
       client.bindWithDevice("devMy01","192.0.1.169");
      

      If I call function inside MyUdpComm, I do twice times ? and this generate problems ... Any idea how to do client and server in one object ?

      D Offline
      D Offline
      Damian7546
      wrote on last edited by Damian7546
      #7

      @JonB mayby the fact that remte device change UDP port when transmit next frames...? From 59185 (3row form log) to 12810(4,5,6...row from log) ?
      It can be a problem ?

      1 Reply Last reply
      0
      • D Damian7546

        Hi,
        I try do UDP communication.

        Firstly I'm sending to broadcast addres 192.0.1.255 with frame to get IP addres device wihich I should exchange data.
        This recive properly IP addres, after them I send data to binded device sendData so in ready read I get only one datagram (Len=1).. But rest of data I do not have in my readyread... I should get other data (Len=28,Len=28, Len=74.....) Like below:
        2.jpg

        my code below:

        
        MyUdpCom::MyUdpCom(QObject *parent)
            : QObject{parent}
        {
            socket = new QUdpSocket(this);
            socket->bind(QHostAddress::LocalHost, 5010);
            connect(socket, &QUdpSocket::readyRead, this, &MyUdpCom::readyRead);
        
        }
        void MyUdpCom::bindWithDevice(QByteArray name, QByteArray ipAddr)
        {
            QByteArray buff;
            buff += name;
            buff += ipAddr; 
            socket->writeDatagram(buff, QHostAddress("192.0.1.255"), 5000);
        }
        
        void MyUdpCom::sendData(QString ipAddr)
        {
            socket->writeDatagram(buff, QHostAddress(ipAddr), 5010);
        
        }
        void MyUdpCom::readyRead()
        {
            QByteArray buff;
            buff.resize(socket->pendingDatagramSize());
        
            QHostAddress sender;
            quint16 senderPort;
        
            socket->readDatagram(buff.data(), buff.size(),&sender, &senderPort);
        
            qDebug() << "Message from sender: " << sender.toString();
            qDebug() << "Message port: " << senderPort;
            qDebug() << "Message" << buff;
        
            if (buff.at(0) == 0x2F){
                qDebug() << "We have Ip addres destination";
                sendData(sender.toString());
            }
        }
        
        
        
        

        main:

            MyUdpCom server;
            MyUdpCom client;
            client.bindWithDevice("devMy01","192.0.1.169");
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @Damian7546
        In readyRead() you should loop for possible multiple datagrams received:

        while (socket->hasPendingDatagrams()) 
        {
              QByteArray buf(socket->pendingDatagramSize(), Qt::Uninitialized);
              socket->readDatagram(buf.data(), buf.size(), &sender, &senderPort);
        

        Does that resolve your issue?

        D 1 Reply Last reply
        1
        • JonBJ JonB

          @Damian7546
          In readyRead() you should loop for possible multiple datagrams received:

          while (socket->hasPendingDatagrams()) 
          {
                QByteArray buf(socket->pendingDatagramSize(), Qt::Uninitialized);
                socket->readDatagram(buf.data(), buf.size(), &sender, &senderPort);
          

          Does that resolve your issue?

          D Offline
          D Offline
          Damian7546
          wrote on last edited by Damian7546
          #3

          @JonB No, I still the same problem.. In my qt app I recived only first and third row form wireshark log published in my first post.

          Below updated readyRead() function:

          void MyUdpCom::readyRead()
          {
              QHostAddress sender;
              quint16 senderPort;
              
              while (socket->hasPendingDatagrams())
              {
                  QByteArray buff(socket->pendingDatagramSize(), Qt::Uninitialized);
                  socket->readDatagram(buff.data(), buff.size(), &sender, &senderPort);
                  
                  qDebug() << "Message from sender: " << sender.toString();
                  qDebug() << "Message port: " << senderPort;
                  qDebug() << "Message" << buff;
                  
                  if (buff.at(0) == 0x2F){
                      qDebug() << "We have Ip addres destination";
                      sendData(sender.toString());
                  }
              }
          }
          
          JonBJ 1 Reply Last reply
          0
          • D Damian7546

            @JonB No, I still the same problem.. In my qt app I recived only first and third row form wireshark log published in my first post.

            Below updated readyRead() function:

            void MyUdpCom::readyRead()
            {
                QHostAddress sender;
                quint16 senderPort;
                
                while (socket->hasPendingDatagrams())
                {
                    QByteArray buff(socket->pendingDatagramSize(), Qt::Uninitialized);
                    socket->readDatagram(buff.data(), buff.size(), &sender, &senderPort);
                    
                    qDebug() << "Message from sender: " << sender.toString();
                    qDebug() << "Message port: " << senderPort;
                    qDebug() << "Message" << buff;
                    
                    if (buff.at(0) == 0x2F){
                        qDebug() << "We have Ip addres destination";
                        sendData(sender.toString());
                    }
                }
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Damian7546
            Well that's already an improvement since you said you only received one datagram and now you receive two.

            Does your software still work if try commenting out the sendData(sender.toString()); just to see whether that affects reception?

            You say " I recived only first and third row form wireshark log" but row #2 has different source/destination addresses from the others. If you mean you also did not receive rows #4 onward then this does not apply.

            Other than this I know that UDP does not guarantee delivery so normally missing datagrams can happen. I do not know whether the fact that wireshark shows various packets/datagrams means that no loss should occur.

            D 1 Reply Last reply
            0
            • JonBJ JonB

              @Damian7546
              Well that's already an improvement since you said you only received one datagram and now you receive two.

              Does your software still work if try commenting out the sendData(sender.toString()); just to see whether that affects reception?

              You say " I recived only first and third row form wireshark log" but row #2 has different source/destination addresses from the others. If you mean you also did not receive rows #4 onward then this does not apply.

              Other than this I know that UDP does not guarantee delivery so normally missing datagrams can happen. I do not know whether the fact that wireshark shows various packets/datagrams means that no loss should occur.

              D Offline
              D Offline
              Damian7546
              wrote on last edited by Damian7546
              #5

              @JonB
              My host IP: 192.0.1.169
              From my host I write UDP frame to broadcast address: 192.0.1.255
              and I get recive from 192.0.1.167... row 1 from log

              Next I write from my host to 192.0.1.167 - sendData(sender.toString()); , where sender.toString() is 192.0.1.167 ... row 2 from log

              Next I get data from 192.0.1.167 - row 3 from log...
              and after that silence....
              Other data 4,5,6,7..rows from log doesn't receive to my app....

              I can't not call sendData(sender.toString()); because the device will not send data to my host (3 to 6 rows from log).
              In this function I only send data like below:

              void MyUdpCom::sendData(QString ipAddr)
              {
                  QByteArray buff;
                  buff += "01202032403420445550"
                  buff += crc(buff);
                  socket->writeDatagram(buff, QHostAddress(ipAddr), 5010);
                  
              }
              
              D 1 Reply Last reply
              0
              • D Damian7546

                @JonB
                My host IP: 192.0.1.169
                From my host I write UDP frame to broadcast address: 192.0.1.255
                and I get recive from 192.0.1.167... row 1 from log

                Next I write from my host to 192.0.1.167 - sendData(sender.toString()); , where sender.toString() is 192.0.1.167 ... row 2 from log

                Next I get data from 192.0.1.167 - row 3 from log...
                and after that silence....
                Other data 4,5,6,7..rows from log doesn't receive to my app....

                I can't not call sendData(sender.toString()); because the device will not send data to my host (3 to 6 rows from log).
                In this function I only send data like below:

                void MyUdpCom::sendData(QString ipAddr)
                {
                    QByteArray buff;
                    buff += "01202032403420445550"
                    buff += crc(buff);
                    socket->writeDatagram(buff, QHostAddress(ipAddr), 5010);
                    
                }
                
                D Offline
                D Offline
                Damian7546
                wrote on last edited by Damian7546
                #6

                @JonB Any idea?
                Maybe this is a problem with creating client and server UDP:

                 MyUdpCom server;
                 MyUdpCom client;
                 client.bindWithDevice("devMy01","192.0.1.169");
                

                If I call function inside MyUdpComm, I do twice times ? and this generate problems ... Any idea how to do client and server in one object ?

                D 1 Reply Last reply
                0
                • D Damian7546

                  @JonB Any idea?
                  Maybe this is a problem with creating client and server UDP:

                   MyUdpCom server;
                   MyUdpCom client;
                   client.bindWithDevice("devMy01","192.0.1.169");
                  

                  If I call function inside MyUdpComm, I do twice times ? and this generate problems ... Any idea how to do client and server in one object ?

                  D Offline
                  D Offline
                  Damian7546
                  wrote on last edited by Damian7546
                  #7

                  @JonB mayby the fact that remte device change UDP port when transmit next frames...? From 59185 (3row form log) to 12810(4,5,6...row from log) ?
                  It can be a problem ?

                  1 Reply Last reply
                  0
                  • D Damian7546 has marked this topic as solved on

                  • Login

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