LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting Two Hexadecimal Bytes to a number from Serial Port

Solved!
Go to solution

Attached is my VI that I have been working on for some time.  

 

For whatever reason, I am unable to convert two hexadecimal bytes to a number.   I have tried using the functions "Hexadecimal String To Number", "Type Cast", and "Scan Value".   I receive the correct two-byte hexadecimal string data.  However, it does not convert the string to a number, allowing me to perform the necessary math to convert it to a voltage value. 

 

Example:

The hexadecimal string, Byte 1 = 0A and byte 2 = B2  (0AB2), should return the number  2738.  I would then perform a conversion to 2.738V.

 

0 Kudos
Message 1 of 5
(200 Views)

Typecasting to U16 would be correct:

 

altenbach_0-1756838387884.png

 

(Your attached code makes very little sense, for example if your array size is >3, you take a subset of three elements starting at three, so that subset could be anywhere from 0 to a huge number of elements)

 

0 Kudos
Message 2 of 5
(172 Views)
Solution
Accepted by topic author David_Troike

Your attached VI takes an array of u8 numbers, not a string. From your example 0AB2 being "returning" the number 2738 it sounds like you are getting a 16 bit number (or maybe bigger) returned as an array of 8 bit numbers. You can take a 2 element array of u8 [0A/10] [B2/178] and type cast it to u16 to get 2738.

 

I suspect you want something like this.

 

u8-array-to-u16.png

 

This assumes unsigned numbers and only 2 bytes in the string. If you really are looking at three bytes you will need to use a bigger data type than u16.

 

 

0 Kudos
Message 3 of 5
(163 Views)

Thanks!  It was a matter of where I was taking the value from and not using a string to a byte array.     

0 Kudos
Message 4 of 5
(152 Views)

Sorry my code does not make sense.  I have not used LabVIEW in the last 10 years.  Little rusty with my code.  You solution did work!  Thanks!!!!

0 Kudos
Message 5 of 5
(150 Views)