Results 1 to 7 of 7

Thread: Sending over UDP - Binary Manipulation

  1. #1
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Sending over UDP - Binary Manipulation

    Hi everyone,

    I am communicating with a microcontroller through UDP, this microcontroller in turn sends data to an ASIC. The ASIC receives SPI strings of length 50bits and 904bits in two different modes. I am in the process of generating a user interface with Qt to allow for the handling of settings/data to be sent and now I need to find a decent way to package my numbers into an acceptable format.

    For instance, I have 5 x 10bit numbers creating the 50bit stream (I also need to append 6 zeros to the front for the uController handling). Each of these numbers is currently stored in Qt as an int.

    How would I go about packaging this data to send as a datagram over UDP?

    I was considering using either a QByteArray or a QBitArray. The byte array seems more useful for strings (I think it is also '/0' terminated, which may get sent over UDP -- ??) If I construct a QBitArray, is there an easy was to convert my int numbers to 10bits?

    Despite the fact that I have not solved this problem, it still seems rather trivial... I would really appreciate any help or insight into this matter, thanks!

    - Cotlone

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sending over UDP - Binary Manipulation

    A bit array is more like a series of switches. It's not meant to be used to represent 10 bit numbers for example, although you can do that.
    A byte array stores bytes, and a byte is 8 bits.

    You can use fromRawData with the byte array, then it is not /0 terminated.
    But here too, your computer uses 8 bits for a byte.

    What you need to do is create a converter. You say that you need to append (or prepend) 6 bits? So that means you need to receive and send 56 bits? That would be easy then, as this is a multitude of 8 bits. So in essence you need to send and receive 56/8 = 7 bytes. The same with 904 bits, which is 113 bytes.

  3. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending over UDP - Binary Manipulation

    sprintf() function with the %d modifier ?
    - just a thought!
    Last edited by JD2000; 7th June 2010 at 15:43.
    Got to keep the loonies on the path ...

  4. #4
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending over UDP - Binary Manipulation

    Thanks for your replies guys. Yeah, the extra 6 bits prepended (I wrote appended before sorry) is to chock the length to the next multiple of 8 (56) to give the 7 bytes for transmission.

    I had set up a bit of code here to load a bit array. My values are 10bit (as defined by "unsigned varname:10;") and taken from the settings object in each respective getX function. I'm then loading the individual bits in the array as I go, this seems to work fine, but would using fromRawData be more appropriate here, as I still need to convert to a QByteArray for sending with writeDatagram (from the UDP socket class)?

    Is it generally easier to convert this QBitArray to a QByteArray or just figure a way to generate the QByteArray in the first place?

    Qt Code:
    1. QBitArray *bitArray = new QBitArray(56);
    2.  
    3. for(int i=0;i<10;i++)
    4. {
    5. bitArray->setBit(i+40, (settings->getVH2()&(2^i))==(2^i)); // AND the value with a mask and see if it equals the mask
    6. bitArray->setBit(i+30, (settings->getVL2()&(2^i))==(2^i));
    7. bitArray->setBit(i+20, (settings->getVH1()&(2^i))==(2^i));
    8. bitArray->setBit(i+10, (settings->getVL1()&(2^i))==(2^i));
    9. bitArray->setBit(i+00, (settings->getVL0()&(2^i))==(2^i));
    10. }
    To copy to clipboard, switch view to plain text mode 

    Thanks again for your help guys!

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sending over UDP - Binary Manipulation

    Here's what I would do

    Qt Code:
    1. 0: 000000
    2. 1: 0000011111 (decimal = 31)
    3. 2: 1100110011 (decimal = 819)
    4. 3: 0011100111 (decimal = 231)
    5. 4: 1110000111 (decimal = 903)
    6. 5: 1010101010 (decimal = 682)
    7.  
    8. In total:
    9.  
    10. 00000000000111111100110011001110011111100001111010101010
    11.  
    12. In 8 bit sections:
    13.  
    14. 00000000 00011111 11001100 11001110 01111110 00011110 10101010
    15. Values: 0 63 204 206 63 30 170
    To copy to clipboard, switch view to plain text mode 

    Turn those decimal values into chars. Then use a QByteArray to append them.
    http://doc.qt.nokia.com/4.6/qbytearray.html#fromRawData

    Do the same in reverse when reading the data.

  6. #6
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending over UDP - Binary Manipulation

    Thanks for your reply tbscope, I appreciate it ;-)
    As for the data manipulation and packaging, I was thinking the same thing as you illustrated. This is what I have implemented at present, though on a second look I think I may need to shuffle the order of my variables/bits around. Using QByteArray this seems to be the easiest way to go, I can also send this QByteArray directly into the writeDatagram function of QUdpSocket.

    Qt Code:
    1. /*
    2.   50 bit input:
    3.   P0:P9(VH2), P0:P9(VL2), P0:P9(VH1), P0:P9(VL1), P0:P9(VL0)
    4.   first bit of data stream is P9(VL0), last bit of data stream P0(VH2)
    5.  
    6.   bbbbbbbbBBBBBBBBbbbbbbbbBBBBBBBBbbbbbbbbBBBBBBBBbbbbbbbb
    7.   MSB 00000055555555554444444444333333333322222222221111111111 LSB
    8.   */
    9.  
    10. QByteArray *thresholdData = new QByteArray();
    11. thresholdData->append((unsigned char)(settings->getVH2() >> 8));
    12. thresholdData->append((unsigned char)(settings->getVH2()));
    13. thresholdData->append((unsigned char)(settings->getVL2() >> 2));
    14. thresholdData->append((unsigned char)((settings->getVL2() << 6) | (settings->getVH1() >> 4)));
    15. thresholdData->append((unsigned char)((settings->getVH1() << 4) | (settings->getVL1() >> 6)));
    16. thresholdData->append((unsigned char)((settings->getVL1() << 2) | (settings->getVL0() >> 8)));
    17. thresholdData->append((unsigned char)(settings->getVL0()));
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help!

  7. #7
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending over UDP - Binary Manipulation

    Its been a long time since I did any bit twiddling and I forgot about bit fields, you can use one of these:
    Qt Code:
    1. struct asicdata{
    2. unsigned int padding :6
    3. unsigned int a :10
    4. unsigned int b :10
    5. unsigned int c :10
    6. unsigned int d :10
    7. unsigned int e :10
    8. }
    9.  
    10. //using tbscope's data
    11. struct asicdata dta = { 0, 31, 819, 231, 903, 682 }
    To copy to clipboard, switch view to plain text mode 
    I hope this helps.
    Last edited by JD2000; 9th June 2010 at 15:00.
    Got to keep the loonies on the path ...

Similar Threads

  1. String manipulation methods?
    By PaladinKnight in forum Newbie
    Replies: 5
    Last Post: 17th March 2010, 00:44
  2. binary file sending using qt4.3
    By omprakash in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2008, 21:56
  3. sending Text or binary file over network
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2008, 01:42
  4. QString manipulation - QRegExp
    By mattia in forum Newbie
    Replies: 1
    Last Post: 18th March 2008, 12:21
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 19:10

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.