LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Hexadecimal String To Number conversion

Solved!
Go to solution

Hi,

I'm reading hex strings from serial port. Then i wat to convert the string into number so i can operate with it.

I tried to use the Hexadecimal String To Number conversion block but it doesn't work. I wired an indicator and it doesn't show me any number. I can read the string but not the corresponding number.

Any advise?Cattura.PNG

0 Kudos
Message 1 of 42
(33,052 Views)

Do you actually get an answer with the text e.g. "1A5B" or do you get the pure hex 0x1A5B? If it's the latter you can do a type cast. Place a string indicator with the resulting string and compare the result if you look at it in Hex or String form.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 2 of 42
(33,045 Views)

@giomarinna wrote:

Hi,

I'm reading hex strings from serial port. Then i wat to convert the string into number so i can operate with it.

I tried to use the Hexadecimal String To Number conversion block but it doesn't work. I wired an indicator and it doesn't show me any number. I can read the string but not the corresponding number.

Any advise?Cattura.PNG


Be careful here.  You have an uninitialized shift register (commonly shortened to "USR").  That means as long as the VI is in memory, every time you run it, it will contain the contents of whatever was inside it last.  I'm pretty sure that was not your intent.  Initialize it to a known value - usually the default value for that datatype - so that you will always start with that value.  Your VI probably needs more work than that, but you'll likely not know whether changes you made fixed the issue or not if you don't take care of this issue first.  Or if you did fix it, we will get a post saying, "The VI only works the first time after I open it, but after that it mostly gives strange results."

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.
Message 3 of 42
(33,036 Views)

I get the first answer type e.g. "1A5B".

0 Kudos
Message 4 of 42
(33,023 Views)

@giomarinna wrote:

I get the first answer type e.g. "1A5B".


Is that 2 bytes or 4?

Hex Strings.png

Message 5 of 42
(33,005 Views)

@giomarinna wrote:

I tried to use the Hexadecimal String To Number conversion block but it doesn't work. I wired an indicator and it doesn't show me any number.


That's just not possible. It probably shows 0, but that is a number...

 

A Type Cast would does what you seem to be trying, but you have to cast to a U16. Possibly (that depends on the input, and reversing the string changes this) with swapped bytes:

Hex to Value 3.png

 

0 Kudos
Message 6 of 42
(32,995 Views)

I'm actually more concerned about how you are reading the data.  Do you have a good definition of how the data is transmitted?  You really should have some type of frame structure in the message to make sure you are converting the right bytes into whatever data type you need.


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 7 of 42
(32,986 Views)

@crossrulz wrote:

I'm actually more concerned about how you are reading the data.  Do you have a good definition of how the data is transmitted?  You really should have some type of frame structure in the message to make sure you are converting the right bytes into whatever data type you need.


And the "Bytes at Port". What if you run that when all data is not available, or when more than expected data is available? Most often, the data will have a termination character which you should use instead.

And the loop timer. Is the source sending data at a specific intervals? How do you sync that? Do you expect to read every written data or just the last...

Certified LabVIEW Architect
Message 8 of 42
(32,953 Views)

@crossrulz wrote:

I'm actually more concerned about how you are reading the data.  Do you have a good definition of how the data is transmitted?  You really should have some type of frame structure in the message to make sure you are converting the right bytes into whatever data type you need.


I concur.

 

I was assuming a WIP, with the hex to number conversion as a showstopper. Once this is out of the way, there are other areas to work on for sure.

0 Kudos
Message 9 of 42
(32,947 Views)

I try to answer to everyone in this message because I have only two possibilities in 24h, I'm beginner. Thanks for help.

I'm sending unsigned int 16 bit HEX by my nucleo board in debug mode, so at the moment i can establish when the message is sent. The message is received but with swapped bytes (e.g. i send 1CFF and I receive FF1C).
For this reason I use the Reverse String Function. In the "substring" display I can read the correnct number in string format. Now, I want to convert it into a 16 bit number.

I use the timer just to hold the return count display value for 2 sec.

I tried to use the Type Cast but it returns me a value different from what I expect.

I tried to use the String To Byte Array Function. The conversion is correct but the numer become two 8 bit numbers (e.g. 1CFF become 1C and FF separately stored into the array). This would complicate the post processing.

My objective is to achieve the original 16 bit HEX.

Probably the problem could be the way I'm using the Hexadecimal String To Number Function. The numer display show always 0.
Is possible to set the "default" pin as U16?

Cattura1.PNGCattura.PNG

0 Kudos
Message 10 of 42
(32,934 Views)