From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Any good examples of constructing and parsing byte data?

Solved!
Go to solution

Hello all,

 

Pretty soon, I'm going to have to figure out how to construct byte messages to send to a serial port, and receive from a serial port, and parse out to base events off of....

 

However, before I delve into the complexities of the serial port communications and VISA, etc....I'd like to simply see some examples of basically working with byte data.

 

I've found how to get a numeric  constant set to byte...the signed and unsigned binaries representations, etc.

 

However, if I could find a simple example or tutorial just to see how to assemble byte code....and how to parse it, it would prove invaluable to me to study.  I've not ever worked really on a byte/binary level before...and some examples with LV would be very handy.

 

Anyone know of any good links out there for something like this?

 

I'm going to be eventually sending 5 bytes out that I need to construct...and will be receiving 9 bytes back in, each of which I'll need to parse out and react to......

 

Any links or information GREATLY appreciated.  I'm pretty decent at parsing strings, real expressions, etc...but have never worked with constructing or parsing byte data.

 

 

Thank you in advance,

 

cayenne

0 Kudos
Message 1 of 6
(4,158 Views)

I don't know all the details of what you will need to be sending but look into the byte array to string and string to byte array primatives. These will allow you to convert the string you read from the serial port and convert it to individual u8 integers or the bytes you will be sending into a string so the VISA write function can accept them.

Message 2 of 6
(4,155 Views)

cayenne,

 

When you talk about parsing "bytes," do you mean manipulating individual bits within a byte?  Clearly the meaning or interpretation of a bit or a byte in a binary system is not universal; it is defined by whoever designed the system.  That information we cannot help with.

 

To manipulate data at the byte level there are two sets of functions to learn. Look at the subpalette "Data Manipulation" of the Numeric palette. Read the help and try some things to see which ones will do the things you need.

 

The other key is that boolean functions work on integers.  So if you have a byte represented as U8 and use the boolean function AND, you can separate and test individual bits quite easily.  For example a binary constant of b00000010 ANDed with an unknown byte will produce zero if the second bit from the right is zero and 1 if it is a one, regardless of the values of the other 7 bits. Appropriate combinations of AND and OR and suitable "masks" allow you to set individual bits to 1 or 0 as needed.

 

Lynn

Message 3 of 6
(4,150 Views)

@johnsold wrote:

cayenne,

 

When you talk about parsing "bytes," do you mean manipulating individual bits within a byte?  Clearly the meaning or interpretation of a bit or a byte in a binary system is not universal; it is defined by whoever designed the system.  That information we cannot help with.

 

To manipulate data at the byte level there are two sets of functions to learn. Look at the subpalette "Data Manipulation" of the Numeric palette. Read the help and try some things to see which ones will do the things you need.

 

The other key is that boolean functions work on integers.  So if you have a byte represented as U8 and use the boolean function AND, you can separate and test individual bits quite easily.  For example a binary constant of b00000010 ANDed with an unknown byte will produce zero if the second bit from the right is zero and 1 if it is a one, regardless of the values of the other 7 bits. Appropriate combinations of AND and OR and suitable "masks" allow you to set individual bits to 1 or 0 as needed.

 

Lynn


Thank you.

 

From what I can tell I need to do (I't still trying to hash out the requirements somewhat)....

 

I need to construct bytes, bit by bit...to send.

 

Example, I might send something like the following 5 bytes...each bit in each byte has meaning to the unit I'm sending to:

 

 

00010011  11101000 10000001 00000000 00001000

 

I'll need to construct these 5 bytes and send them.

 

I may get in something like

 

00000100 11100000 00000100 00000000 10000000 11101111 10101000 00000001  11100010

 

And I'll need to parse each byte received, bit by bit to know what to do next....

 

I'm guessing I'll be using I8 or U8 as my data type......

 

I do have some documentation that tells me what bits to set in each byte....and what each bit means on the receiving message....but not sure how to construct the bytes nor how to parse one out in order for me to use my logic to translate the message.

 

Thank you for the advice given so far...I'm startring to look at the Data Manipulation section of the Numeric Pallette as you suggested....

 

Ok...I think I've heard of the 'math' you suggested...just not familiar with it.

 

I was hoping I could manipulate a byte much like I can with strings....to look at something like:

 

00010011 

 

And go through starting with the first 3 bits....see that it was 000, and now that meant status was 'x'...look at the 4 bit of 1, and know the power was on..look at bits 6 and 7 seeing they were both 0...and know that it was dark....etc.

 

Just random examples here, but I have a code chart for setting true meanings bit by bit on each of these....and was hoping that there was as straightforward a way to construct and deconstruct and parse a bit into its bits as easily as it is to parse through or construct a string.

 

Thank you, and please keep suggestions or links to examples coming in.

 

I"m back to reading up on what you've given me so far,

 

cayenne

 

 

 

0 Kudos
Message 4 of 6
(4,144 Views)

You are on the right track.  Working with the binary tools is not unlike regular expressions for strings.  It is not quite as cryptic. It can be used on single bytes or arrays of bytes of any integer representation.  Of course you could type cast to strings and use regex if you want.  I think the issue would be that most of the combinations you want will not be printable characters and translating everything from your code charts could be a big job.  It will also be difficult to separate out parts of bytes.

 

I usually use U8 for byte manipulations so that you do not get into sign interpretations that you would have with I8.  The exception would be if the data actaully represented a signed integer, but then you probably would not be talking about status and power bits.

 

The code chart will be your key. You can copy it to byte constants and build the masks you need.

 

I have attached a very simple example showing how the boolean functions work with integers.

 

Lynn

Message 5 of 6
(4,137 Views)
Solution
Accepted by cayenne

If you are uncomfortable with binary data, you can use Boolean arrays. Look at the Number to Boolean Array and Boolean Array to Number functions. Instead of masking with an AND, you can simple use the index array function to check a particular bit.

Message 6 of 6
(4,123 Views)