05-05-2020 11:03 AM
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
Solved! Go to Solution.
05-05-2020 11:22 AM - edited 05-05-2020 11:25 AM
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.
05-05-2020 11:24 AM
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.
05-05-2020 11:31 AM
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.
05-05-2020 11:50 AM
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
05-05-2020 11:57 AM
Also the unpacked data need to have 3 Boolean and 5 integer values with the specific allocated bit size.
05-05-2020 12:01 PM
@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".
05-05-2020 12:03 PM - edited 05-05-2020 12:03 PM
Do you actually have the bitmap of the message so we can see what you are trying to send?
05-05-2020 12:14 PM
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.
05-05-2020 12:15 PM
I can only provide a description as shown below
Byte no of Bits Data Value Data Type
0 2 Data1 0-2 Enumeration