PDA

View Full Version : Waiting for new values sent by UDP in Qt



Damiann
25th May 2016, 10:02
Hi guys!
I have quite easy, I guess ^^, problem with modification of my code. In general it reads incomming messeges (UDP) and print them in cmd. Instead of reading them all the time, I would like to modify it to wait for new value and then display. Thanks in advance for any help!

Code:


#include "myudp.h"

MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost, 25000);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}

void MyUDP::readyRead()
{
double a[3];
double prev_a[3];

QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());

QHostAddress sender;
quint16 senderPort;

socket->readDatagram(buffer.data(), buffer.size(),
&sender, &senderPort);

if(a[0] != prev_a[0] || a[1] != prev_a[1] || a[2] != prev_a[2])
{
memcpy(&a, buffer, 3*sizeof(double));
qDebug() << "\nMessage from: " << sender.toString();
qDebug() << "Message port: " << senderPort;
qDebug() << "X:" << a[0];
qDebug() << "Y:" << a[1];
qDebug() << "V:" << a[2];
}
prev_a[0] = a[0];
prev_a[1] = a[1];
prev_a[2] = a[2];

}

yeye_olive
25th May 2016, 10:42
Your program already waits for data and prints it when its value has changed. What is it you want to change, exactly?

anda_skoa
25th May 2016, 11:06
Your if condition is accessing uninitialized memory.
Your memcpy does not check if buffer is 3*sizeof(double) long before accessing it.

Your prev_a is a local variable, it is only valid to the end of the scope of your readyRead() function.
If you want it to be persistent over multiple calls, you need to have it as a member of the class.

Cheers,
_

Damiann
25th May 2016, 12:29
Your program already waits for data and prints it when its value has changed. What is it you want to change, exactly?

Before building I thought the same that it should works, but it doesn't. I am sending array of variables from Simulink and would like to make this code waiting for new values, for now when simulation gives the same values all the time, eg [1 2 3] and simulation works for 1 sec, then c++ print the same result 15-20 times. I would like to make it wait till new value appear, eg [1 2 5].


anda_skoa, could you explain this to me more clear? I am not software engineer, and my C++ skill is quite low, especially Qt, which it totally new for me. The best help would be to show some code or make some changes in mine. Many thanks.

I have solved this problem...
'memcpy(&a, buffer, 3*sizeof(double));' should be out of if.
Thank you for help!

anda_skoa
25th May 2016, 14:14
anda_skoa, could you explain this to me more clear? I am not software engineer, and my C++ skill is quite low, especially Qt, which it totally new for me. The best help would be to show some code or make some changes in mine. Many thanks.

When you declare variables, in your case for example the array named "a", they are usually not initialized to some known value.
Putting the memcpy before the if fixes that for "a".

For the problem of prev_a, this needs to be in the class declaration, e.g. like this


class MyUDP : public QObject
{
private:
double prev_a[3];
};

Again, remember that there the three values can be anything.

I would recommend using a vector instead, so that you can easily detect "first round of values"


class MyUDP : public QObject
{
private:
QVector<double> prev_a;
};

then you can check the size of prev_a to determine if this is the first time you receive data



bool gotUpdate = prev_a.isEmpty(); // no previous values, so we count this as an update
prev_a.resize(3);

memcpy(...);
gotUpdate = gotUpdate || (... value comparison...);
if (gotUpdate) {
// your code
}


Cheers,
_