Results 1 to 9 of 9

Thread: UDP datagram receive

  1. #1
    Join Date
    Oct 2007
    Posts
    5
    Thanks
    1

    Default UDP datagram receive

    Hi,
    I am using QT3 on Fedora8 and need to receive an UDP datagram which contains a temperature reading from a remote device. The received UDP datagram payload data is in the following ASCII form: 3032332E38313235 which is a character string representing a temperature like 023.8125 and that's it. I'm currently able to receive the data and display the four characters after the decimal point and am thinking this is a simple "type" problem.

    Does anyone have a simple QT3 example to receive this data and display in a GUI and save the same data to a file?

    Thanks for any help getting me around this problem.
    Mark

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UDP datagram receive

    Is the problem related to receiving the datagram or decoding the data within?

  3. #3
    Join Date
    Oct 2007
    Posts
    5
    Thanks
    1

    Default Re: UDP datagram receive

    Wysota,

    Thanks for your help. I believe it is the decoding part since in the GUI I can watch the last four digits after the decimal point change in real time. Also, if I uncomment the line: // temperature_string = "023.1234"; then the the GUI shows 23.1234 which is the format I'd like to see.

    Here's the function which I am having trouble with:

    double WeatherBalloon::temperature()
    {
    double temperature;
    QString temperature_string; //("123.1234")
    QByteArray sensogram(socketDevice.bytesAvailable());
    QDataStream out(sensogram, IO_WriteOnly);
    out.setVersion(1);
    socketDevice.writeBlock(sensogram, sensogram.size(), 0x0A640005, 80);
    // out << temperature; //out anything to 10.100.0.5
    socketDevice.readBlock(sensogram.data(), sensogram.size());
    QDataStream in(sensogram, IO_ReadOnly);
    in.setVersion(1);
    in >> temperature_string;
    // temperature_string = "023.1234";
    temperature = temperature_string.toDouble();
    return temperature;
    Last edited by mdannenb; 26th July 2008 at 13:34. Reason: updated contents

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UDP datagram receive

    Where does the temperature originate? Do you transmit it between two applications of yours or do you get it from an external source?

  5. #5
    Join Date
    Oct 2007
    Posts
    5
    Thanks
    1

    Default Re: UDP datagram receive

    Wysota,

    It originates from a Microchip PIC microcontroller with an Ethernet interface. If I send the PIC any UDP packet, either broadcast or to its IP address it responds with the ASCII data shown in my first post above.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UDP datagram receive

    So why don't you do it like this?
    Qt Code:
    1. QByteArray ba(sock->pendingDatagramSize(), 0);
    2. sock->readDatagram(ba.data(), ba.size(), ...);
    3. double temp = QString(ba).toDouble();
    4. qDebug("temperature: %f", temp);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2007
    Posts
    5
    Thanks
    1

    Default Re: UDP datagram receive

    Wysota,
    Thanks again for your responses and suggestions. I must apologize for my ignorance as I am a beginner but I'm not sure how to use that. My original program was based on example code supplied with QT3 and I made very small modifications to that code to get this far. Thats why I was asking if someone had a simple program to receive a UDP datagram which could display and save the data to a file. I replaced my temperature function with your code and get the following compile errors:

    weatherballoon.cpp:38: error: ‘sock’ was not declared in this scope
    weatherballoon.cpp:39: error: expected primary-expression before ‘...’ token
    weatherballoon.cpp:42: error: argument of type ‘double (WeatherBalloon:()’ does not match ‘double’
    weatherballoon.cpp: At global scope:
    weatherballoon.cpp:54: error: expected unqualified-id before numeric constant
    weatherballoon.cpp:56: error: expected unqualified-id before numeric constant
    weatherballoon.cpp:58: error: expected unqualified-id before numeric constant
    weatherballoon.cpp:60: error: expected unqualified-id before numeric constant
    make: *** [weatherballoon.o] Error 1

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UDP datagram receive

    You can't replace the code directly. I was using different variable names and constructs than you. I only pointed you towards a possible solution. You have to use your C++ skills to convert my code into yours.

  9. The following user says thank you to wysota for this useful post:

    mdannenb (27th July 2008)

  10. #9
    Join Date
    Oct 2007
    Posts
    5
    Thanks
    1

    Default Re: UDP datagram receive

    Ok, I plan to do just that. Thanks for your help and pointing me in the right direction.

Similar Threads

  1. Receive file over TCP
    By winarko in forum Qt Programming
    Replies: 18
    Last Post: 29th May 2008, 17:25

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.