LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

converting hex string to number (not working)

Solved!
Go to solution

2018-08-15.pnghi.  i am trying to convert the response from a VISA request from whatever form it's in to an integer.  my display reads the correct hex code when display is set to hex but i cannot seem to make the same output that gives me the display into any usable number so that i can manipulate it.  cam someone please help?  this has got to be something simple.  if it matters, i am supposed to get an output of 2 hex numbers in sequence.  i have tried to read only one byte and do the conversion.  still nothing works.  always zero.

0 Kudos
Message 1 of 14
(4,545 Views)

well, for anyone that needs it, here's what i did:  i changed the string to a binary array and then read out the elements of the array and recombined them in the way i needed.  this works well.  i cannot imagine, though, that this is best way to do it.  however if you have a micro-epsilon CT thermometer, this will do it.  snippet2.png

0 Kudos
Message 2 of 14
(4,530 Views)

I suspect there may be a much simpler (and more robust) way to read temperature.  Can you supply a reference to a data sheet that describes how the Thermometer works?  I specifically want to know the command structure and the format(s) of the data being returned.  I'm concerned that you read exactly two bytes, and that you have a built-in "wait" between your VISA Write and Read.  If the device uses a VISA Termination character, there is a much better (and faster) way to read the data.  Similarly, if we know what data are being sent and in what form, we can probably help you get the number out in a way that "makes sense".

 

Bob Schor

0 Kudos
Message 3 of 14
(4,521 Views)
Solution
Accepted by topic author prhs

Hi prhs,

 

apart from the communication problems mentioned by Bob you don't need StringToU8Array, two IndexArray functions, followed by multiply and add to get that U16 value:

check.png(use DeserializeString)

In case that trasmitted data is encoded as signed value: change the numeric constant to I16 representation!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 14
(4,517 Views)

Since you are dealing with binary/hex data, do make sure you turn off the termination character (Boolean on the top of the VISA Configure Serial Port, it defaults to TRUE).

 

Since you are just reading the 2 bytes, no need for the wait at all.  That is what the VISA timeout is for.

 

And finally, you want to use the Unflatten From String to convert your data to a U16 or I16.  There is also an input on that function for Endianness (ie byte order).  If your data does not look right, that would be the first thing to look at.


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 5 of 14
(4,506 Views)

sure!  this is the command list from the manufacturer (it's pasted as a file.)  any help is very much appreciated.  

peter

0 Kudos
Message 6 of 14
(4,466 Views)

@GerdW wrote:

Hi prhs,

 

apart from the communication problems mentioned by Bob you don't need StringToU8Array, two IndexArray functions, followed by multiply and add to get that U16 value:

check.png(use DeserializeString)

In case that trasmitted data is encoded as signed value: change the numeric constant to I16 representation!


thanks Gerd, but that doesn't work; i end up getting zero from the unflatten command.  i had tried this earlier.  i really don't understand why i am getting zero with this method and sort of threw up my hands in frustration before doing the method of converting to array and then indexing.  

0 Kudos
Message 7 of 14
(4,460 Views)

@prhs wrote:

@GerdW wrote:

Hi prhs,

 

apart from the communication problems mentioned by Bob you don't need StringToU8Array, two IndexArray functions, followed by multiply and add to get that U16 value:

check.png(use DeserializeString)

In case that trasmitted data is encoded as signed value: change the numeric constant to I16 representation!


thanks Gerd, but that doesn't work; i end up getting zero from the unflatten command.  i had tried this earlier.  i really don't understand why i am getting zero with this method and sort of threw up my hands in frustration before doing the method of converting to array and then indexing.  


Ddi you try creating a constant for the "byte order" input. The data may be little endian.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 8 of 14
(4,457 Views)

The "Endian" matters, but shouldn't give you 0 if you get it wrong.  I'm too simple-minded for Unflatten routines, so I just coded up something that takes a Dbl, transforms it back to the U16 by adding 100 and multiplying by 10 (and rounding), splits it into bytes, combines the bytes into an array, converts the Byte Array to a String, then "undoes" the process (String to Byte Array, Index Array, Join Numbers, Divide by 10, Subtract 100).  I also do the same with the Reversed Byte Array (to  mimic "wrong Endian".  I always get numbers out the other side (the "correct" number when I don't reverse the array, a "silly" number when I do).

 

I'll bet you aren't getting the two bytes out of the Thermometer.  Time to write some simple code to put a byte of 0x1 out, read two bytes back, and see what they are.  I'm betting they are 0x0, 0x0.  Check such things as Baud Rate and other RS232 parameters (or better yet, fire up MAX and test with MAX until you see that you are getting a reasonable set of byte data).  Incidentally, 0x0, 0x0 corresponds to a temperature of -100.
Temp Conversion.png

Bob Schor

0 Kudos
Message 9 of 14
(4,452 Views)

@Bob_Schor wrote:

The "Endian" matters, but shouldn't give you 0 if you get it wrong.  I'm too simple-minded for Unflatten routines, so I just coded up something that takes a Dbl, transforms it back to the U16 by adding 100 and multiplying by 10 (and rounding), splits it into bytes, combines the bytes into an array, converts the Byte Array to a String, then "undoes" the process (String to Byte Array, Index Array, Join Numbers, Divide by 10, Subtract 100).  I also do the same with the Reversed Byte Array (to  mimic "wrong Endian".  I always get numbers out the other side (the "correct" number when I don't reverse the array, a "silly" number when I do).

 

I'll bet you aren't getting the two bytes out of the Thermometer.  Time to write some simple code to put a byte of 0x1 out, read two bytes back, and see what they are.  I'm betting they are 0x0, 0x0.  Check such things as Baud Rate and other RS232 parameters (or better yet, fire up MAX and test with MAX until you see that you are getting a reasonable set of byte data).  Incidentally, 0x0, 0x0 corresponds to a temperature of -100.
Temp Conversion.png

Bob Schor


thanks Bob,

i AM getting two bytes out.  here's my code including Gerd's unflattening routine.  As you can see, the intermediate is zero.  so temp is going to be -100.  yet, my method run at the same time indicates the proper temperature and shows two bytes read. also, the response window gives me the correct hex answer.  but conversion of that to decimal doesn't work.  it's just weird!  🙂  

with_unflattening.png2018-08-16.png

0 Kudos
Message 10 of 14
(4,443 Views)