Converting hex bytes to float

paulg
2024-08-02
2024-08-06
  • paulg - 2024-08-02

    I'll be capturing a hex stream over serial into a RasPi and I'd like to convert a group of bytes into a float. I see this is available (https://content.helpme-codesys.com/en/CODESYS%20Development%20System/cdsoperatorrealto.html) but not its inverse (i.e. BYTE_TO_REAL). The packets will be 18 bytes but I'll be selecting 4 byte segments from the middle, swapping byte endianness (i.e. changing 0x51847641 to 0x41768451), and converting to float; I believe they adhere to IEEE 754.

    Is there a pre-built function for some or all of this, or do I need to follow (https://forge.codesys.com/forge/talk/Engineering/thread/dc5829c7ea/)?

    Thanks!

     
  • robert-o - 2024-08-05

    Try OSCAT_BASIC and Function DW_TO_REAL. DWORD_OF_BYTE my although help.
    The Lib is open and free. It is not necessary to add this Lib, you can use/copy the source code just to get an idea.

     
  • dkugler - 2024-08-06

    have a look at UNIONs. With a union you are able to write single bytes to the same adress area which belongs to a float variable at the same time.
    I like to do a lot of convertions belonging to interfaces and fieldbus in this way, f.eg. word / byte swapping etc.

     

Log in to post a comment.