01-03-2022 06:31 AM
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?
Solved! Go to Solution.
01-03-2022 07:00 AM
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.
01-03-2022 07:15 AM
@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?
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."
01-03-2022 07:42 AM
I get the first answer type e.g. "1A5B".
01-03-2022 08:56 AM
01-03-2022 09:01 AM
@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:
01-03-2022 10:03 AM
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.
01-04-2022 02:32 AM
@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...
01-04-2022 03:23 AM
@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.
01-04-2022 06:29 AM
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?