LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Packing and unpacking bits

Solved!
Go to solution

Hi,

I'm trying to perform bit manipulation for my udp communication protocol where I'm packing different bits into an unsigned integer and then retrieving the individual bits. The issue is that the data doesn't seem to map into an 8 bit integer but rather coming out as separate bytes when I view the manipulated data on wireshark. Please see attached sample code to see what I might be doing wrong.

Thanks

0 Kudos
Message 1 of 22
(4,559 Views)

Let's say you have a byte where the first two bits are a number and the second two bits are also a number.  Since one byte is as small as you can get on a computer, you have both represented as U8.

 

The way that most describes what you are doing is to convert each number to an array of Booleans, take the bottom four bits from each number and stuff them into your byte to send.  You can do this with bitwise logic, too - but I really suck at that.  Someone else will have to show you how to do it that way.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 2 of 22
(4,550 Views)

To unstuff literally, take the byte out and convert it to an array of Booleans.  Then split them up correctly and do a conversion to U8 for each.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 3 of 22
(4,546 Views)

Does the code you attached basically work?  It seems like it does.  I would do the integer to numbers a bit differently, like  Bill says.  I would do bit manipulation in pretty much the reverse situation as you did to turn the original values into a single byte.

 

Where does TCP come into this?  I would assume the TCP communication would be happening in the middle of your VI where you have the single U8 blue wire.  So I don;t why a single byte would be showing up as multiple bytes in your wireshark view unless you are sending different parts of your data then what I can tell from your attached VI.

0 Kudos
Message 4 of 22
(4,538 Views)

RavensFan

 

The u8 wire is the packed bits that are now split into different components bits. The code is supposed to send the extracted bits ie 1,1,1,2 and 3bits respectively via tcp. I'm not sending the u8 single byte but rather the individual components which includes the 3 Booleans and the 2 u8 values. 

Obviously I'm not doing it the right way and that is most likely why the data came out as separate bytes. 

I'm trying to send the individual bits and not the stuffed byte. Is that possible?

 

Thanks

0 Kudos
Message 5 of 22
(4,521 Views)

Also the unpacked data need to have 3 Boolean and 5 integer values with the specific allocated bit size.

0 Kudos
Message 6 of 22
(4,513 Views)

@SolPS wrote:

RavensFan

 

The u8 wire is the packed bits that are now split into different components bits. The code is supposed to send the extracted bits ie 1,1,1,2 and 3bits respectively via tcp. I'm not sending the u8 single byte but rather the individual components which includes the 3 Booleans and the 2 u8 values. 

Obviously I'm not doing it the right way and that is most likely why the data came out as separate bytes. 

I'm trying to send the individual bits and not the stuffed byte. Is that possible?

 

Thanks


The smallest packet you can send/receive is one byte.  So if you wanted to send the bits separately, you would have to send each one as a single byte.  Terribly wasteful.  Which is why bit stuffing is even a "thing".

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 7 of 22
(4,507 Views)

Do you actually have the bitmap of the message so we can see what you are trying to send?

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 8 of 22
(4,505 Views)

Message1

Byte Number

Bit 7

Bit 6

Bit 5

Bit 4

Bit 3

Bit 2

Bit 1

Bit 0 (LSB)

0

 

 

 

 

 

 

 

F/R

1

 

 

 

File Selects

 

FS

LMI

FMI

2

Spare

3

Offset(31…24)

4

Offset(23…16)

5

Offset(15…8)

6

Offset(7…0)

7

Length(31…24)

8

Length(23…16)

9

Length(15…8)

10

Length(7…0)

11

Spare

probably looks something like that.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 9 of 22
(4,500 Views)

I can only provide a description as shown below

Byte     no of Bits     Data       Value         Data Type

 0             2              Data1      0-2            Enumeration

0 Kudos
Message 10 of 22
(4,498 Views)