From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

convert a hex string to bytes/values

Solved!
Go to solution

Hi

Sorry for what it seems a basic question (I am an amateur in bytes and stuff).

I read some info from an instrument through a TCP Visa connection. Data is read either as Ascii or Bin as follows

 

To distinguish between ASCII command and binary data from the instrument each data block is transmitted in the following format:
0 1 2 3 4…Len+4
TypeHi TypeLo LenHi LenLo Data
LenHi, LenLo contain length of the Data part of the message, TypeHi, TypeLo determine the type:
• TypeHi = 55h, TypeLo = AAh – ASCII command, Data part of the message contains the ASCII string
(transferred string is not terminated by CR!).
• TypeHi = 66h, TypeLo = BBh – binary command, Data part of the message contains the binary
stream data.

 

I have difficulties in reading these. an exemple of what I tryin (without success) is attached here (not of much use but it gives an exemple of what the instrumet sends back).

Thanks for help

 

0 Kudos
Message 1 of 11
(1,630 Views)

Looks like the numbers are formatted as comma delimited decimal integers in the string, not flat binary. What values do you expect for your example string?

0 Kudos
Message 2 of 11
(1,623 Views)

The string received should be something like PRESNC:-412,-412 but includes a header marker like "55AA" for a ASCII and two bytes stating the length of the string submitted.

0 Kudos
Message 3 of 11
(1,615 Views)

In the doc I have this, but as I am noob, I don't know how to handle it :

nitad54448_0-1647364041395.png

 

0 Kudos
Message 4 of 11
(1,609 Views)
Solution
Accepted by topic author nitad54448

What you should do is read 4 bytes.  You can use the Unflatten From String to get the data type and the data length from those 4 bytes.  You then read the number of bytes stated in the data length.  You can then parse the data based on the data type.


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
Message 5 of 11
(1,606 Views)

Start with something like this (you can easily add more cases, one for each header type):

 

  1. See if the first 2 bytes are x55AA (Add cases for all other possibilities)
  2. unflatten 2 bytes to get the length of the message
  3. Clip the remaining string at length (not needed?)
  4. Scan two values as SGL, assuming a format of "PRESNC:%f,%f" You can easily cut the substring to the ":" and treat the remaining string depending on what it is.
  5. If there could be a variable amount if comma separated values, you could use spreadsheet string to array (not shown).

 

altenbach_0-1647364382549.png

 

 

 

Message 6 of 11
(1,599 Views)

Hi

Thanks, can you post it as LV 2020 or below ? (sirry, I am not up to date !)

regards

0 Kudos
Message 7 of 11
(1,598 Views)
Solution
Accepted by topic author nitad54448

Here's my version of Tim's above. I definitely would not make 55AA the default case, of course.

 

altenbach_0-1647365277292.png

 

 

As I mentioned:

  • if the "PRESNC:" header can vary, use another case structure.
  • If the number of values can vary, create an array instead of a cluster (using spreadsheet string to array" with comma as delimiter).
  • ...

 

Message 8 of 11
(1,593 Views)

Hi

thanks, it works. I just wonder how to read the other possibility with the 66BB in which the number of values is variable (I can not make a "variable" cluster...). anyway, thanks, I will try to figure it out.

Another problem I have : how to mark 2 solutions in this forum ?

regards

0 Kudos
Message 9 of 11
(1,579 Views)

 

  • If the number of values is variable, use a 1D array instead of a cluster.
  • If you want help with the 66BB situation, feel free to attach a typical received raw string for that option.
  • What is the dataype of the "binary stream data"??? (SGL array? I16 array? Byte order? etc.)
  • I think you can mark more than one solution.
Message 10 of 11
(1,574 Views)