01-14-2021 11:11 AM
Hi!
I have a device giving me its orientation (X Y Z angles) in a hexadecimal string of 20 bytes as followed:
[Start of message] 2bytes
[X DATA (3bytes) | X Status (1bytes)]
[Y DATA (3bytes) | Y Status (1bytes)]
[Z DATA (3bytes) | Z Status (1bytes)]
[other data] 6 Bytes
All attached without spaces, that looks like : AAAAFF67BE02AFE3BD027F60BA02AABBCCDD
I'd like to convert for instance the X data (FF 67 BE 02, 02 being the status - ok/notok) into a real angle value : FF67BE into -38 978° (if conversion LSB/number is 1.)
So far I have done what you see below as an example, but I am not sure that I am not overdoing things a bit.
Especially, I am not sure when I should do the conversion to I32...
If I could have your honest opinion on wether what I'm doing is ok or not, that'd be great, thanks 🙂
Vinny.
Solved! Go to Solution.
01-14-2021 11:17 AM - edited 01-14-2021 11:30 AM
Your string has 40bytes and X is not a valid hex character. Please give an example with valid inputs!
There is no 3 byte datatype. What is the datatype of the angle? SGL? DBL? FXP? (FF67BE into -38 978°)
Are you sure you are getting a hex formatted plain string? Typically I would have expected a 20 character binary string. Can you link us to a website or manual that describes the format?
01-14-2021 11:38 AM
Don't know if you are getting raw hex values or ascii values, either way I would probably not do what you are doing.
01-14-2021 11:44 AM - edited 01-14-2021 11:47 AM
I think this is what you are trying to do. Notice that I have "Display Style" and "Radix" visible for most of my constants and controls/indicators. This is because I am showing the values in Hexidecimal.
EDIT: I really like Darin's idea of using the Fixed Point conversion. That would be much simpler.
01-14-2021 11:58 AM
Here's what I would probably do. Note that "scale by power of two" is an arithmetic shift, thus retaining the sign.
(And no, I probably would not cast a 3 byte string to a four byte datatype. Just does not feel right ;))
01-14-2021 12:14 PM - edited 01-14-2021 12:23 PM
And here's how you could expand it to get xyz.
You could even include the header (U16) and footer (U32) as cluster elements in the right order so you don't need to strip.
01-14-2021 12:22 PM
@altenbach wrote:
You could even include the header (U16) and footer (U32) as cluster elements in the right order so you don't need to strip.
Here's how that would look like:
01-14-2021 12:53 PM - edited 01-14-2021 12:55 PM
Hi,
Thanks for all your quick answers.
I'll integrate Darin's idea tomorrow and see how it is doing, thanks 🙂
EDIT, the picture didn't upload for some reason:
01-14-2021 01:38 PM - edited 01-14-2021 01:40 PM
@VinnyAstro wrote:
- I suppose I am getting ascii character and not raw hex data (picture below). n:
You are getting a 20byte binary string (independently on how you choose to display it! The display style does NOT change the data. Hex display is more suitable if you expect non-displayable characters to be part of the string. "Normal display" is useful if you only have printable characters, and you don't).
(ASCII is a not a datatype, just a convention to assign "meaning" to bytes, such as printable or control characters, e.g. which bit pattern should be displayed as the letter Q in normal display)
01-14-2021 01:55 PM - edited 01-14-2021 02:43 PM
Also note that your example string (AAAAFF67BE02AFE3BD027F60BA02AABBCCDD) only has 18 bytes. I assume that you are also counting two termination characters. If you use the 18byte string (i.e. without the termination characters), you can try to use my example, then use "unbundle by name" to get the desired values as needed. (You can expand the cluster to also include a field for the termination characters, etc.)
Unflatten from string even gives you error information, e.g. if the string is too short. (Note that typecast does not provide that luxury!)
Maybe you should configure your serial I/O to use defined termination characters instead of fixed size reads. I am not a "serial" expert, so others might have more specific advice here. (See below instead)