LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Combine MSB and LSB

Hello to all,

 

In the picture you can see a can message i am reading from a usb to can device i am using.The 0 byte and byte 1  corresponce to a sensor we are reading, Byte 0 is the LSB of  the sensor 1 and Byte 1 is the MSB of the same sensor,so i try to combine those two bytes and MSB must come first.For example if LSB is 94 and MSB 3 the combination of these two bytes must be 0394 in hex format, and if we transpose this value to decimal we have 916 and i divide by 1024 and multiply by 5 because i want to transpose it to voltage.

 

The Vi i am using is working fine but some times its seems to lose the conversation,some times instead of 4.5 voltage it gives 0,so i begun to wonder if my vi working properly or not?or this is a bug?Can some one provide another solution to combine those two bytes?

 

Thank you in advance.

Download All
0 Kudos
Message 1 of 6
(6,203 Views)

Since your data seems to be stored in bytes you should use U8 datatype for each byte. Then you can just use "Join numbers" to create a U16 value from the low and high bytes. See snippet.

Lo-Hi.png

0 Kudos
Message 2 of 6
(6,193 Views)

dan_u I  just test your solution and the problem remains instead of having 4.5 voltage my first value is always 0.I believe this is a wrong conversion instead of a bug because this wrong value i notice not in the beginning but in random positions 

0 Kudos
Message 3 of 6
(6,182 Views)

If you're getting a 0 that's not a conversion problem (there can't be an error when joining 2 8-bit numbers into a 16-bit number), but a problem of your source data. Probably there are communication problems so you sometimes get 0's from the USB device. Do you have error handling implemented?

 

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

Yes i have implemented an error handling in my main program and i am not starting taking measurements if i don't have a valid can message.I also checking the can message if is valid or not during the measurement process and i always have a valid can message and of course no error.

0 Kudos
Message 5 of 6
(6,172 Views)

Well, the fact of the matter is that the only way the Join Numbers function can return a zero is if both inputs are zero. Since the inputs come from your read data, then that's where you have to look. I would suggest you do some debugging on your own rather than us guessing. One simple thing you can do is to use a conditional probe. Put a probe on the output of the Join Number functions, and have it set so that it halts the program execution when the output is zero. Put some other probes farther up the chain, such as the input wires, and where you're reading the data.

Message 6 of 6
(6,169 Views)