Results 1 to 10 of 10

Thread: udp receive data problem

  1. #1
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question udp receive data problem

    hi ,folks,i have a problem in qbytearray to float array,i received float array in udp,but return value is 0,what's wrong with me?can u help me?tks.i have searched in google.
    the sender program is write by labwindows cvi.
    cvi code:
    Qt Code:
    1. float AIdat[344];
    2. UDPWrite(writerChannel, AI_PORT, MULTICAST_ADDRESS, AIdat, sizeof(AIdat));
    To copy to clipboard, switch view to plain text mode 

    qt code:
    Qt Code:
    1. const udpsize=1376;
    2. void Widget::processPendingDatagrams()
    3. {
    4. qint16 packetsize;
    5. while (udpSocket->hasPendingDatagrams()) {
    6. packetsize=udpSocket->pendingDatagramSize();
    7. ba.resize(packetsize);
    8. udpSocket->readDatagram(ba.data(), ba.size());
    9. if (packetsize!=udpsize)
    10. {
    11. return;
    12. }
    13. // bool ok;
    14. // ba = QByteArray::number(ba.toLongLong(&ok,16),2);
    15. QDataStream datin(&ba,QIODevice::ReadOnly);
    16. float firstdat;
    17. datin>>firstdat; //first float data
    18. if (!datin.status())
    19. {
    20. qDebug()<<"ok" ;
    21. }
    22. qDebug()<<firstdat; //it return 0 or inf;
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: udp receive data problem

    You are assuming that the transmitted array is transmitted as a single datagram and arrives as a single datagram etc. If it arrives in pieces you do nothing to accumulate the pieces (ba is discarded between while loops).

    Are you sure that QDataStream's serialisation format for float is the sames as the sender's?
    Is a LabView CVI float the same size and format as a C++ float?
    Is there actually data in your byte array?
    Is the data block just a pure list of float values, or is there an envelope structure/checksum/block size?
    Are you sure the zero or inf are not the values being sent?

  3. #3
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: udp receive data problem

    hi,chrisw67,thanks your reply.
    the udp client(delphi/indy ) can received correct values,i want to use qt to replace my delphi program.

    Are you sure that QDataStream's serialisation format for float is the sames as the sender's?
    so i think the problem is data format .
    But I can't think of a better idea,
    how to ?somebody can help me?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: udp receive data problem

    As Chris says, you may receive your datagram in multiple chunks.

    Secondly, UDP is not meant to be reliable, so you may receive some data in duplicate and some data not at all.

    If you want to use QDataStream, then you should use the same Qt version for both sender and receiver, to ensure compatibility.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: udp receive data problem

    You have the data in a QByteArray. You can get a char* to the internal data buffer. You can cast that to float* if you are certain that the byte ordering is the same, and the 'float' data sent is the same format as a C++ float (not a double).

  6. #6
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Cool Re: udp receive data problem

    float* ft=ba.data();
    float dat=*(ft+x);

    but andbody have a Perfect solution use qt?

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: udp receive data problem

    We don't know how the data is internally formatted, so we can't tell you the best way.

  8. #8
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: udp receive data problem

    hi,squidge,thanks your reply,but my english is bad.
    qbytearray's data type is char* ,i want to convert from char* to float array,because udp received data is float array.
    if I run
    Qt Code:
    1. float* ft=ba.data();
    2. float dat=*(ft+x);
    To copy to clipboard, switch view to plain text mode 
    then
    result is correct,but this is c,not c++,not qt,I believe a best way to do it with qt.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: udp receive data problem

    What you have there won't compile without error as C or C++. This has nothing to do with Qt once you have a char* to the data in memory:
    Qt Code:
    1. // Create some dummy data
    2. ba.fill(0, 10 * sizeof(float));
    3. unsigned int size = ba.size();
    4. char* c = ba.data();
    5. // OR const char* cc = ba.constData();
    6. // Make sure that ba stays in scope while you do this
    7. // Nothing Qt below here
    8.  
    9.  
    10. float* ft = reinterpret_cast<float*>(c);
    11. // OR const float* ft = reinterpret_cast<const float*>(cc);
    12. for (unsigned int i = 0; i < size/sizeof(float); ++i) {
    13. std::cout << *ft++ << std:endl;
    14. }
    To copy to clipboard, switch view to plain text mode 

    The caveats regarding byte order, float/double etc still apply. If it breaks you get to keep the pieces

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: udp receive data problem

    Quote Originally Posted by zxwmail View Post
    I believe a best way to do it with qt.
    A better way in Qt is to use the streaming operators, but we don't know if these will be compatible with what you are creating at the "other end", so you may have to do something like that (or like what Chris states) to get the values you want

Similar Threads

  1. receive data from client via QTcpServer
    By Fallen_ in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2010, 16:08
  2. Replies: 4
    Last Post: 18th August 2010, 08:13
  3. Qextserialport receive
    By Max123 in forum Newbie
    Replies: 2
    Last Post: 28th March 2010, 22:40
  4. QTCPsocket data corrupt when receive
    By gabizzz in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2010, 17:29
  5. Replies: 6
    Last Post: 29th April 2009, 18:17

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.