LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

dword HEX string to number

Solved!
Go to solution

Hi all

It looks like my little VI works OK with word HEX as input but it fails when it has to deal with double word HEX numbers.

E9B2 --> -5710 OK

ADD22 --> 32767 NO! --> I want 711970

Can anyone explain to me why and how can I solve this problem?

Thanks

 

Download All
0 Kudos
Message 1 of 8
(1,484 Views)

The representation is not ok but even so it will depends of your need.

 

If representation is i16 (as you have it now) you have a good value (up to your words) for E9B2 but ADD22 iw wrong as going to overflow.

If representation is i32, E9B2 will give 59826 (wrong if I follow well) but ADD22 will give 711970 as you expect.

 

It is confusing. What are the expected limits values of your input ?

Message 2 of 8
(1,473 Views)

You are right: I have skipped a part of the story...

I receive ASCII string from COM port of my PC and I have to interpret it.

In some cases it's OK for me to use word HEX.

In others I have to use double words HEX. How can I do in this last case? Do I just have to change the representation?

Thanks

 

0 Kudos
Message 3 of 8
(1,456 Views)

You must have the communications protocol somewhere.  The general steps would be:

  • Determine what message you are receiving.
  • Break up the byte array into chunks representing the number of bytes for each data type you are expecting.
  • Decode each chunk as needed.

If you are trying to adapt common code to each data type on the fly, that's making it a lot more difficult than it needs to be.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 4 of 8
(1,433 Views)

@Lucky-Luka wrote:

I receive ASCII string from COM port of my PC and I have to interpret it.

In some cases it's OK for me to use word HEX.

In others I have to use double words HEX. How can I do in this last case? Do I just have to change the representation?


The word "HEX" alone is very ambiguous, often misused, and really does not tell us anything.

 

When attaching code, it would be best to have a few default values for the inputs, exactly as received from the com port. Or at least diagram constant for your typical scenarios.

 

(Create an indicator where you receive the string. Run your communication code so the indicator shows the received string. Stop the VI, right-click the terminal, and "create constant". repeat a few times until you have a few typical strings of various length. Save the VI under a new name an attach it here. You might add a label for each constant to tell us what value you expect from it, if known)

 

For troubleshooting, you should also create an indicator for "remaining string".

 

Do you have a link to the manual of the device?

0 Kudos
Message 5 of 8
(1,422 Views)

Thanks for the suggestion.

Let's say that I just have to deal with double word HEX...how can I adapt my VI?

Thanks

0 Kudos
Message 6 of 8
(1,417 Views)
Solution
Accepted by topic author Lucky-Luka

@Lucky-Luka wrote:

Let's say that I just have to deal with double word HEX...how can I adapt my VI?

Thanks


While there are many definitions of the term word, you probably mean two bytes and thus four bytes per double word. Formatted in hex, each character (0..F) represents a nibble (4 bits), so it seems you want to parse 8 characters into a 4 byte integer. Do you want signed or unsigned? Where (left or right) do you want to pad with zeroes if there are fewer than 8 characters?

 

The first adaptation would be to change the representation of the type constant to either I32 or U32, depending on your needs and change the indicator accordingly.

 

so. many. question! It would help if you could do some of my suggestions above.

Message 7 of 8
(1,380 Views)
Solution
Accepted by topic author Lucky-Luka

6e5a3362-e455-4375-967e-308dc37036b1.jpegYou can do it like this

Message 8 of 8
(1,293 Views)