LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial data Hex string to I16 number

Hi all

I have a demo from hex string to I16 number converted(Hex -> decimal->ascill->hex string->decimal),I have did it but it's not good in the way,could someone help me fixed it or point out,it's a appreciated,thanks!!

Download All
0 Kudos
Message 1 of 10
(3,302 Views)

You just need to unflatten your data from string.

 

Your description doesn't make much sense.  An I16 number is 16 bits, two bytes, and will be 4 hexadecimal characters.

Yet you say 66 63 is "low bit" and "66 66" is high bit.

You can only have high or low bytes first (in a 16-bit number), or high or low words first (in a 32-bit number).

 

If you use Unflatten from string, then you can designate the endianism.

 

I'm not going to even try to understand what your original loop is doing.  Hopefully this snippet will show you the easier way.

 

 

Edit:  Looking at this closer, I see AABB 04 is repeated a lot.  What is generating this data?  What do the AA BB bytes mean?

 

You may just need to modify the loop so that you grab X number of bytes every iteration, discard the AA BB 04, and then proceed to unflatten the next 4 bytes into two I16 values.

0 Kudos
Message 2 of 10
(3,260 Views)

Thanks for you comments,the hex string AABB is identifier,04 is byte length,66 63 66 66 is data,I need to rotated it as 66 66 66 63 .

 

convert rule as below:

 

HEX     DEC       ASCII

0x66  ->102     ->F

0x66  ->102     ->F

0x66  ->102     ->F

0x63   ->99      ->C

 

FFFC=-4

How to use easy way to handle this convert,thanks!!

 

 

0 Kudos
Message 3 of 10
(3,240 Views)

Hi Sam,

 

who invented this kind of data formatting? Writing ASCII chars in the wrong ordering…

 

Try this:

Btw. you should ALWAYS make the display style indicator visible for strings when you don't use the default display style! (The same applies to numeric values…)

 


@Sam.Huang wrote:

HEX     DEC       ASCII

0x66  ->102     ->F

0x63   ->99      ->C


Those are the ASCII values for lower-case chars like "f" and "c". Uppercase would use values like 0x46 or 0x43…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 10
(3,237 Views)

Similar to GerdW, but using the Swap Bytes instead of indexing and building back up.


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 10
(3,219 Views)

I'm still confused as to whether we are talking about I16 numbers like the subject says which consist of 2 bytes (4 hex characters) or if we are talking about 4 bytes (8 hex characters) which would be a 32 bit number  !!!

0 Kudos
Message 6 of 10
(3,210 Views)

Hi RavensFan,

 

we are talking about a weird format of I16 values saved as 4 bytes each, representing the ASCII code of the 4 (hex) chars needed to represent that I16 value… (And even byte-swapped the I16 before converting to lowercase ASCII chars.)

 

Someone thought it would be a good idea to use twice the space needed to save those values, and even pepper it with lots of additional filler bytes (3 additional byte per I16 value saved in 4 bytes). In total 7 bytes are used to save a 2 byte value: they could have used human-readable decimal numbers formatted as %+05d with a space or comma in between: this would also need 7 bytes per element…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 10
(3,207 Views)

Thanks a lot,It's very useful to solve this issue,actually received data has irregularity as snapshot

occasionally get AABB AABB 0463 6266 65 or AABB 0363 6266,how to judged received byte length get data convert and skip repeated data,thanks!!

 

 

Download All
0 Kudos
Message 8 of 10
(3,201 Views)

Hi Sam,

 


@Sam.Huang wrote:

received data has irregularity as snapshot occasionally get AABB AABB 0463 6266 65


Oh boy, beside that weird format it also shows variations?

Suggestion:

  1. Search for "AABB".
  2. When found check the next byte.
  3. When BYTE<>03 AND BYTE<>04 THEN back to step 1
  4. Read the next bytes using BYTE…
  5. back to step 1 until end of message

When you get a 3-byte value: is there also the order of LSB/MSB swapped?

 

It seems it would be easier (and more safe) to change the software on the sender of those messages! Do you have control over that other software?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 9 of 10
(3,184 Views)

Where is this data coming from?  I think it would be easier to take care of this where the data is read.


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 10 of 10
(3,181 Views)