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. Button Status Updation on UDP packet reception
Forum Updated to NodeBB v4.3 + New Features

Button Status Updation on UDP packet reception

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 1.4k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    reshu
    wrote on last edited by
    #1

    Hi
    i am writing the code to update the colours of a button whose default colour is set to white. on receiving UDP data it updates the colour to green. when i set a field in the UDP packet to '1' it turns red. when the UDP packets are stopped, it should turn white again which is not happening to me. the status is not getting updated when i stop receiving. how to solve this. plz suggest.
    i have attached the code
    ~```
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QUdpSocket>
    #include <QtEndian>

    static int pktsize1;

    //************************************************************
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->leak->setStyleSheet("background-color:white");// button initailly white
    msocket = new QUdpSocket(this);
    msocket-> bind(QHostAddress("192.168.10.11"),55500);
    connect(msocket,SIGNAL(readyRead()),this,SLOT(readyRead_DAQ()));

    }

    void MainWindow:: readyRead_DAQ() // datagram read function
    {
    while (msocket->hasPendingDatagrams())
    {

     QByteArray Buffer;                                     // QBytearray to store the read data. 
     Buffer.resize(msocket->pendingDatagramSize());         
     QHostAddress sender;                                   // address of transmitter (192.168.10.100)
     quint16 senderPort;                                    // port of transmitter(55000)
     msocket->readDatagram(Buffer.data(),Buffer.size(),& sender,& senderPort);  
    
    if (QByteArray().append(Buffer[10]).toHex()=="01")
        {
            ui->leak->setStyleSheet("background-color:red");// turns red if leak detected
        }
    else
        {
            ui->leak->setStyleSheet("background-color:green");// green if no leak
        }
    

    }

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    Reshu
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      So how do you know when it's stopped?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply
      2
      • R Offline
        R Offline
        reshu
        wrote on last edited by
        #3

        i am sending the udp test packet from another PC. I stop that.

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          So how do you know when it's stopped?

          R Offline
          R Offline
          reshu
          wrote on last edited by
          #4

          @Christian-Ehrlicher
          I checked the status of
          connect(msocket,SIGNAL(readyRead()),this,SLOT(readyRead_DAQ()));
          it remains the same even after i stop the test UDP packets. i transmit UDP packet from another PC at 100ms interval to test the code.

          1 Reply Last reply
          0
          • R reshu

            i am sending the udp test packet from another PC. I stop that.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @reshu

            i am sending the udp test packet from another PC. I stop that.

            :) So do you mean it has stopped when nothing arrives for a while?

            R 1 Reply Last reply
            0
            • JonBJ JonB

              @reshu

              i am sending the udp test packet from another PC. I stop that.

              :) So do you mean it has stopped when nothing arrives for a while?

              R Offline
              R Offline
              reshu
              wrote on last edited by
              #6

              @JonB
              i can stop transmission from the other PC. i checked if udp packets are received using wireshark in host PC.
              My doubt is why the status of connect(msocket,SIGNAL(readyRead()),this,SLOT(readyRead_DAQ())) is not changing when no packet is received.

              JonBJ 1 Reply Last reply
              0
              • R reshu

                @JonB
                i can stop transmission from the other PC. i checked if udp packets are received using wireshark in host PC.
                My doubt is why the status of connect(msocket,SIGNAL(readyRead()),this,SLOT(readyRead_DAQ())) is not changing when no packet is received.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @reshu
                Why should it change? If nothing is ready to read the signal won't fire.

                R 1 Reply Last reply
                0
                • JonBJ JonB

                  @reshu
                  Why should it change? If nothing is ready to read the signal won't fire.

                  R Offline
                  R Offline
                  reshu
                  wrote on last edited by
                  #8

                  @JonB
                  but the button colour remains the same.i want to change it back to white when reception stops.

                  JonBJ 1 Reply Last reply
                  -1
                  • R reshu

                    @JonB
                    but the button colour remains the same.i want to change it back to white when reception stops.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @reshu
                    You haven't answered the question. How do you expect to detect "when reception stops"?

                    R 1 Reply Last reply
                    2
                    • JonBJ JonB

                      @reshu
                      You haven't answered the question. How do you expect to detect "when reception stops"?

                      R Offline
                      R Offline
                      reshu
                      wrote on last edited by
                      #10

                      @JonB
                      there should be a signal like readyRead that would fire when reception stops

                      jsulmJ JonBJ 2 Replies Last reply
                      0
                      • R reshu

                        @JonB
                        there should be a signal like readyRead that would fire when reception stops

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

                        @reshu No. readyRead is fired when there is something to read. You have to implement a protocol to let the receiver know when the data transmission is finished. The network layer simply transmits data from a to b, it does not know when the data you're sending ends. It is your job to let the receiver know when all data was send.

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

                        1 Reply Last reply
                        2
                        • R reshu

                          @JonB
                          there should be a signal like readyRead that would fire when reception stops

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @reshu said in Button Status Updation on UDP packet reception:

                          @JonB
                          there should be a signal like readyRead that would fire when reception stops

                          :) Computers are good at doing something when things happen, but not when things do not happen! There is no event to slot onto here.

                          I have already asked several times what you mean by/how you intend to detect when "[UDP] reception stops".

                          i transmit UDP packet from another PC at 100ms interval to test the code.

                          Given that, the best you can do is have a QTimer ticking. Note when the last message arrived, or restart the timer when a message arrives. When 100ms has elapsed with no message, that is apparently your definition of "stopped", and you can act on that. However, apart from the vagaries of timings, don't forget that UDP is a "lossy" protocol: there is no guarantee that a packet will arrive at all, or at the right time. You might want to wait for several packets to be missed. If you want reliable, guaranteed, TCP gives that rather than UDP.

                          Or, you might change your protocol to @jsulm's, where a "terminating" packet gets sent. Though you might still have the issue of missing it.

                          R 2 Replies Last reply
                          2
                          • JonBJ JonB

                            @reshu said in Button Status Updation on UDP packet reception:

                            @JonB
                            there should be a signal like readyRead that would fire when reception stops

                            :) Computers are good at doing something when things happen, but not when things do not happen! There is no event to slot onto here.

                            I have already asked several times what you mean by/how you intend to detect when "[UDP] reception stops".

                            i transmit UDP packet from another PC at 100ms interval to test the code.

                            Given that, the best you can do is have a QTimer ticking. Note when the last message arrived, or restart the timer when a message arrives. When 100ms has elapsed with no message, that is apparently your definition of "stopped", and you can act on that. However, apart from the vagaries of timings, don't forget that UDP is a "lossy" protocol: there is no guarantee that a packet will arrive at all, or at the right time. You might want to wait for several packets to be missed. If you want reliable, guaranteed, TCP gives that rather than UDP.

                            Or, you might change your protocol to @jsulm's, where a "terminating" packet gets sent. Though you might still have the issue of missing it.

                            R Offline
                            R Offline
                            reshu
                            wrote on last edited by
                            #13

                            @JonB
                            actually this is a simple thing which i am not able to solve. My Button has three states or colours.

                            1. it should be white before i receive any data(udp pkt) from my data acquisition PCB (DAQ)
                            2. should turn green when DAQ UDP packets are received
                            3. should turn red if leak detected
                            4. should turn back to white if i disconnect DAQ( this is not happening). if i disconnect DAQ it is still red.
                              this is my issue. when i printed the status of 'connect' it is remaining the same even after disconnecting. I don't know what is wrong.I am not a full time user of QT. only a starter. My requirement is that whenever i disconnect DAQ it should return back to the initial status and not retain the existing condition. pls help.
                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @reshu said in Button Status Updation on UDP packet reception:

                              if i disconnect DAQ it is still red.

                              Again (third time or so): UDP is stateless - there is no connect/disconnect. The one and only s“thing is that you don't receive packets anymore!

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              2
                              • JonBJ JonB

                                @reshu said in Button Status Updation on UDP packet reception:

                                @JonB
                                there should be a signal like readyRead that would fire when reception stops

                                :) Computers are good at doing something when things happen, but not when things do not happen! There is no event to slot onto here.

                                I have already asked several times what you mean by/how you intend to detect when "[UDP] reception stops".

                                i transmit UDP packet from another PC at 100ms interval to test the code.

                                Given that, the best you can do is have a QTimer ticking. Note when the last message arrived, or restart the timer when a message arrives. When 100ms has elapsed with no message, that is apparently your definition of "stopped", and you can act on that. However, apart from the vagaries of timings, don't forget that UDP is a "lossy" protocol: there is no guarantee that a packet will arrive at all, or at the right time. You might want to wait for several packets to be missed. If you want reliable, guaranteed, TCP gives that rather than UDP.

                                Or, you might change your protocol to @jsulm's, where a "terminating" packet gets sent. Though you might still have the issue of missing it.

                                R Offline
                                R Offline
                                reshu
                                wrote on last edited by
                                #15

                                i understood what you are saying.
                                The readyRead() signal is emitted whenever datagrams arrive. so when datagram is not there( physically detached) the status of this connect function has to change right.
                                6155bb1b-7c71-4b51-8ae4-51017c219509-image.png

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Hi,

                                  Everybody answering here has understood your case. What you seem to not understand is that this detection you want so badly is something that you need to implement yourself. @JonB already gave you good suggestions to achieve that.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  R 1 Reply Last reply
                                  1
                                  • SGaistS SGaist

                                    Hi,

                                    Everybody answering here has understood your case. What you seem to not understand is that this detection you want so badly is something that you need to implement yourself. @JonB already gave you good suggestions to achieve that.

                                    R Offline
                                    R Offline
                                    reshu
                                    wrote on last edited by
                                    #17

                                    thanks for spending your valuable time. let me try to sort it out.

                                    1 Reply Last reply
                                    0

                                    • Login

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