Button Status Updation on UDP packet reception
-
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 -
So how do you know when it's stopped?
-
So how do you know when it's stopped?
@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. -
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?
-
@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. -
@JonB
but the button colour remains the same.i want to change it back to white when reception stops. -
@reshu
You haven't answered the question. How do you expect to detect "when reception stops"? -
@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.
-
@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
QTimerticking. 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.
-
@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
QTimerticking. 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.
@JonB
actually this is a simple thing which i am not able to solve. My Button has three states or colours.- it should be white before i receive any data(udp pkt) from my data acquisition PCB (DAQ)
- should turn green when DAQ UDP packets are received
- should turn red if leak detected
- 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.
-
@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!
-
@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
QTimerticking. 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.
-
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.
-
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.
