LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert 3 Bytes to 16 Bit Decimal Number

Solved!
Go to solution

Hello, I have the following problem:
From a RS-232 I recieve three Bytes (each consists of 8 bits and the first one is a Start Byte, it will be repeated) per second:
1. Byte (Start Byte):  01000000
2. Byte (Random): B8 B9 B10 B11 B12 B13 B14 B15
3. Byte: (Random): B0 B1 B2 B3 B4 B5 B6 B7

I want these two Bytes (2. and 3.) to be converted to 16 bit decimal number. Can you please help me?
I know that I need to use Visa Read and so on, but I don't know how to extract these 3 Bytes and combine two of them.
(Iam really new to Labview)
Thank you, Regards mattes

0 Kudos
Message 1 of 17
(6,861 Views)
Solution
Accepted by topic author mattespotter

Several questions.

  • Is the VISA device sending bytes at a constant rate, or are you getting 3 bytes at a time, pause, 3 bytes, pause, etc.?
  • Am I correct that the only thing this device is sending is "byte-triplets", at least one of which is 01000000?
  • Is there any provision to handle all of the bytes being 01000000?
  • I'm confused by your Byte naming convention.  Is the third Byte, B0 B1 B2 B3 B4 B5 B6 B7 have the most significant or the least significant bits?  I'm used to calling the least significant bit Bit0, and writing it on the right end, e.g. my low Byte I'd identify as B7 B6 B5 B4 B3 B2 B1 B0.  I'm going to assume this convention, so that you are describing the order Start, High Byte, Low Byte.

Let's assume that you've managed to get yourself at a "nice place" where you know that if you read exactly three bytes from your VISA device, you will get a Start Byte, a High Byte, and a Low Byte.  Here's what you do:

  • Be sure to configure VISA to not use Termination Characters -- you are reading U8 (binary) Bytes, not Ascii text (although that is what VISA will return to you).
  • Ask VISA to read three bytes.  Pass the String through a String to U8 Array function to get an Array of 3 U8 Bytes.
  • Combine the last two using Join Numbers.

Here's an example, with the high Byte = 1 (or 256) and the low Byte = 2, so the result should be (and is) 258:

3-Byte Read.png

 

 

 

 

 

 

Bob Schor

Message 2 of 17
(6,839 Views)

Here is another option assuming you start with a string.  I like using the Unflatten From String since I can specify the Endianness.  You can get there with the Join Number as well by swapping the inputs as needed.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 17
(6,835 Views)
Solution
Accepted by topic author mattespotter

Assuming that you might want to check the presence of a valid start byte for verification, I would go one step further and parse the entire three bytes.

 

Most convenient would be a cluster of a U8 and U16 (or I16 if it is signed, who knows?).

Give the cluster elements intuitive names and now things are completely self-documenting and easy to understand. You can also easily pick the byte order if needed.

 

Here's a picture of what I would suggest (make sure to pick the correct representation for all cluster elements!).

 

ThreeBytes.png

Note that the 3 byte string is displayed in hexadecimal for clarity. Also note that your request for a "decimal number" is pointless. You can display the resulting numeric in any form (decimal, hex, binary) as needed. This is just a cosmetic property of the indicator and does not change the underlying data.

 

 

Message 4 of 17
(6,817 Views)

Hello guys,

thank you a lot for your fast answers!
@Bob_Schor: Sorry, for my short (bad) explanation, I try to make things clear:

Yes, the RS-232 is sending me any second these three bytes without any pause. The Start byte is always the same and always the first byte each second. My byte naming convention should be clear in the figure I am posting now.
Now, my problem is that I don't know how to build or connect the VISA read with your short program. Moreover, my byte convention is a little bit different.
I am really sorry for asking these stupid questions, but I have no clue yet.
Thank you 🙂

Download All
0 Kudos
Message 5 of 17
(6,805 Views)

@mattespotter wrote:

Now, my problem is that I don't know how to build or connect the VISA read with your short program.


If you are going with Bob's code, then you just need the String To Byte Array to convert your read data into an array of bytes.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 17
(6,794 Views)

I would strongly recommend to use my code and simply wire the string from the reading instead of the "three bytes" string control that is currently there. Also throw an error if the start byte is not as expected and then discard the two others whenever that happens.

0 Kudos
Message 7 of 17
(6,781 Views)

I like Altenbach's method!

 

I'm not sure why you are having trouble with your VISA Read, but it probably comes because you didn't tell the Serial Device to start sending you stuff.  There was a post I saw earlier today (which I thought was yours, as it dealt with VISA, but I'm just tired enough to be confused) that talked about sending a command and then getting multiple responses.  Look over recent Forum posts for VISA stuff and look at the code, see if you "get the idea".  Or check out an Example of using VISA.

 

Bob Schor

0 Kudos
Message 8 of 17
(6,755 Views)

Hello guys,

I solved my problem, the solution is attached.

 

My program only starts if the start byte is seen as start byte. Moreover, I had to change the bit order in the data bytes to get the correct number.

 

Thank you very much! Great support!

 

Regards mattes

0 Kudos
Message 9 of 17
(6,725 Views)

@mattespotter wrote:

 

My program only starts if the start byte is seen as start byte. Moreover, I had to change the bit order in the data bytes to get the correct number.

 


I can't see where you "start" the program if the start byte is correct.

 

Changing the bit order is highly unusual (typically it is the byte order), but changing the bit oder can be done with a 256 byte lookup table. No need to temporarily go through a boolean array with 8x more data. Of course the bit reversal can also easily be done in the 16bit data, no need to do all these duplicate operations.

 

Are you sure your conversion is correct? Can you attach your code? Do you have some examples of 3 byte strings and the expected result?

 

(Also note that you can omit the "Voltage" indicator by simply showing the digital display of the chart. Less diagram clutter. When only unflattening 8bit values, the byte order is irrelevant. Your case structure is pointless. What's in the TRUE case? Just  a TRUE diagram constant? What do you think would happen if you would delete the case structure and just keep the green wire? Look at your coercion dots. If you would wire the U8 indicators after the "To U8", they would be eliminated. Instead of using free labels inside the cluster, show and set the labels of the cluster elements (see my code!). Now you can unbundle by name for much better documentation). 

0 Kudos
Message 10 of 17
(6,702 Views)