LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Build/join a number out of an array

Solved!
Go to solution

Hi!

 

I believe it is a very simple problem, but I can't find a proper simple solution.

The example attached (in addtion to a screenshot below) illustrates a simulated array of bytes I am getting from a device (it corresponds to a velocity in LSB that I have to translate in RPM at the very end, 1 LSB corresponds to 0,25 RPM).

My array will always be made of 4 bytes (6 if I enable the checksum function) and the method I have to extract the velocity out of it doesn't convince me:

 

First method works fine, but it's kinda uggly and if ever tomorrow a new device uses more than 4Bytes, I'll have to change my code.

Second one just doesn't work, translating my lack of knowledge and skills in using Join Numbers.

Third method seems to work, but is it the right way to proceed?

 

Thanks in advance for any tips and advices.

Vinny

 

VinnyLaTaupe_1-1594369442139.png

0 Kudos
Message 1 of 9
(1,641 Views)

You forgot to save your simulated bytes as the default data in that array.

 

Also, the top method is a Rube Goldberg.  Index Array is expandable.  Drag the bottom border downwards until you have 4 outputs, then you'll have the 4 elements without splitting wires and duplicating the array.  You won't even need to wire up the 0 through 3 constants.

 

Have you tried typecast on the array to convert the 4 U8 elements to a U32?

Message 2 of 9
(1,624 Views)
Solution
Accepted by topic author VinnyAstro

Hi Vinny,

 

use UnflattenFromString:

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 9
(1,615 Views)

@RavensFan wrote:

You forgot to save your simulated bytes as the default data in that array.


Youps sorry.

Changed it. Otherwise the array is [0, 0, F, A0] and can go from [0, 0, 4E, 20] (+5000 RPM) to ..... -5000RPM I just realized I've only tested positive values ... gonna have to check out how it works with negatives...

 


@RavensFan wrote:

Also, the top method is a Rube Goldberg.  Index Array is expandable.  Drag the bottom border downwards until you have 4 outputs, then you'll have the 4 elements without splitting wires and duplicating the array.  You won't even need to wire up the 0 through 3 constants.


Ah yeah of course... It's more pleasant to read. But technically still not great for future changes.

 


@RavensFan wrote:

Have you tried typecast on the array to convert the 4 U8 elements to a U32?


Hmmm No. what would be the changes here ? So using the typecast directly after the array?

0 Kudos
Message 4 of 9
(1,612 Views)

Hi Gerd!

 

Nice I'll try that out.

I suppose that if I have more than 4Bytes in my array then I'd have to cast not to U32 but then U64?

Also my array is big endian but that a Me problem 🙂

 

 

0 Kudos
Message 5 of 9
(1,610 Views)

Hi Vinny,

 


@VinnyAstro wrote:

Otherwise the array is [0, 0, F, A0] and can go from [0, 0, 4E, 20] (+5000 RPM) to ..... -5000RPM 


When you are expecting negative values too then you should convert into an I32 value.

Simply change the representation of that constant in my snippet…

 


@VinnyAstro wrote:
I suppose that if I have more than 4Bytes in my array then I'd have to cast not to U32 but then U64?

It depends on the type of data you want to decode. When you expect an I32 value then you need to decode an I32 value!

One advantage of UnflattenFromString is its output of the remaining string - just read its LabVIEW help entry!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 6 of 9
(1,607 Views)

@GerdW wrote:

Hi Vinny,

 


@VinnyAstro wrote:

Otherwise the array is [0, 0, F, A0] and can go from [0, 0, 4E, 20] (+5000 RPM) to ..... -5000RPM 


When you are expecting negative values too then you should convert into an I32 value.

Simply change the representation of that constant in my snippet…



Yup will do.

I just have, for now, now clue how my velocity read table will look like if my wheel goes in the negative speed (and what command I should send for that) ... 

If I remember well, I need to look at the MSB? If it is 0 then my velocity is positive, if it is 1 then negative? 

0 Kudos
Message 7 of 9
(1,590 Views)

Hi Vinny,

 


@VinnyAstro wrote:

If I remember well, I need to look at the MSB? If it is 0 then my velocity is positive, if it is 1 then negative? 


Well, -5000 should be encoded by the bytes FF.FF.EC.78…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 9
(1,583 Views)

Hi!

I've came accross this discussion about 2's complements in which you participated Gerd, I've used some code proposed by Carlod80 here which I adapted for myself... And it works fine ! 🙂

 


@GerdW wrote:

Hi Vinny,

 


@VinnyAstro wrote:

If I remember well, I need to look at the MSB? If it is 0 then my velocity is positive, if it is 1 then negative? 


Well, -5000 should be encoded by the bytes FF.FF.EC.78…



I've forgot to mention an important point: first byte (MSB) is a dummy byte, so while I'm reading 4B, I'm only interested in the last 3.

 

I'm attaching my final solution if anyone coming accross is interested. I'll just add some error management later on.

 

Thanks for your help Gerd.

 

Best,

Vinny.

0 Kudos
Message 9 of 9
(1,460 Views)