Results 1 to 5 of 5

Thread: Waiting for new values sent by UDP in Qt

  1. #1
    Join Date
    May 2016
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Smile Waiting for new values sent by UDP in Qt

    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:
    Qt Code:
    1. #include "myudp.h"
    2.  
    3. MyUDP::MyUDP(QObject *parent) :
    4. QObject(parent)
    5. {
    6. socket = new QUdpSocket(this);
    7. socket->bind(QHostAddress::LocalHost, 25000);
    8. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    9. }
    10.  
    11. void MyUDP::readyRead()
    12. {
    13. double a[3];
    14. double prev_a[3];
    15.  
    16. QByteArray buffer;
    17. buffer.resize(socket->pendingDatagramSize());
    18.  
    19. QHostAddress sender;
    20. quint16 senderPort;
    21.  
    22. socket->readDatagram(buffer.data(), buffer.size(),
    23. &sender, &senderPort);
    24.  
    25. if(a[0] != prev_a[0] || a[1] != prev_a[1] || a[2] != prev_a[2])
    26. {
    27. memcpy(&a, buffer, 3*sizeof(double));
    28. qDebug() << "\nMessage from: " << sender.toString();
    29. qDebug() << "Message port: " << senderPort;
    30. qDebug() << "X:" << a[0];
    31. qDebug() << "Y:" << a[1];
    32. qDebug() << "V:" << a[2];
    33. }
    34. prev_a[0] = a[0];
    35. prev_a[1] = a[1];
    36. prev_a[2] = a[2];
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 25th May 2016 at 11:02. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Waiting for new values sent by UDP in Qt

    Your program already waits for data and prints it when its value has changed. What is it you want to change, exactly?

  3. The following user says thank you to yeye_olive for this useful post:

    Damiann (25th May 2016)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Waiting for new values sent by UDP in Qt

    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,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Damiann (25th May 2016)

  6. #4
    Join Date
    May 2016
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Waiting for new values sent by UDP in Qt

    Quote Originally Posted by yeye_olive View Post
    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!
    Last edited by Damiann; 25th May 2016 at 13:57.

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Waiting for new values sent by UDP in Qt

    Quote Originally Posted by Damiann View Post
    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
    Qt Code:
    1. class MyUDP : public QObject
    2. {
    3. private:
    4. double prev_a[3];
    5. };
    To copy to clipboard, switch view to plain text mode 
    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"
    Qt Code:
    1. class MyUDP : public QObject
    2. {
    3. private:
    4. QVector<double> prev_a;
    5. };
    To copy to clipboard, switch view to plain text mode 
    then you can check the size of prev_a to determine if this is the first time you receive data

    Qt Code:
    1. bool gotUpdate = prev_a.isEmpty(); // no previous values, so we count this as an update
    2. prev_a.resize(3);
    3.  
    4. memcpy(...);
    5. gotUpdate = gotUpdate || (... value comparison...);
    6. if (gotUpdate) {
    7. // your code
    8. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 27th November 2014, 10:15
  2. How to convert string hex values to its real binary values?
    By AttilaPethe in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 22:47
  3. Waiting WebKit
    By giusepped in forum Qt Programming
    Replies: 0
    Last Post: 13th May 2009, 03:40
  4. Waiting on a thread?
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2007, 16:13
  5. Waiting for something
    By JonathanForQT4 in forum Newbie
    Replies: 20
    Last Post: 2nd May 2007, 17:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.