PDA

View Full Version : UDP datagram receive



mdannenb
25th July 2008, 17:23
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

wysota
26th July 2008, 11:53
Is the problem related to receiving the datagram or decoding the data within?

mdannenb
26th July 2008, 14:29
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;

wysota
26th July 2008, 14:52
Where does the temperature originate? Do you transmit it between two applications of yours or do you get it from an external source?

mdannenb
26th July 2008, 15:01
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.

wysota
26th July 2008, 15:16
So why don't you do it like this?

QByteArray ba(sock->pendingDatagramSize(), 0);
sock->readDatagram(ba.data(), ba.size(), ...);
double temp = QString(ba).toDouble();
qDebug("temperature: %f", temp);

mdannenb
26th July 2008, 15:47
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

wysota
26th July 2008, 20:14
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.

mdannenb
27th July 2008, 04:30
Ok, I plan to do just that. Thanks for your help and pointing me in the right direction.