[Python] Native Modbus Register (Signed) to 32-bit Float with Little-endian Byte Swap

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to convert data from a Native Modbus Register (Signed) to a 32-bit float with a Little-endian byte swap in Python, but I can't seem to get the transformation right. I've tried a few different approaches, but none have worked so far, and I’m unsure if the issue lies in my method or the data itself.

    In my reference, the initial data type is "Native Modbus Register - Signed." The transformation I'm trying to achieve is from this type to "32-bit float - Little-endian byte swap." Here's a sample of the input and expected output values:

    SlewEncoder_input_and_output = [
    (2100, -1533, -601.2821),
    (2100, -1533, -3395.887),
    (1592, -1502, 19834.04),
    (-305, 1807, -106.4646),
    (-463, -1566, -1045.991)
    ]


    I also have a different reference containing some sample data that seems to be in the 32-bit float - Little-endian byte swap format (the result). However, I'm not entirely sure about the initial data type; it appears to be similar to the previous one (Native Modbus Register - Signed) but merged into a single signed register value:

    SlewEncoder_input_and_output = [
    (1505910957, -0.963)
    ]


    Here's the last thing I've tried (without success, the output is just a really huge value):

    upper = 2100
    lower = -1533
    int_value = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF)

    # Convert the integer to bytes
    bytes_value = int_value.to_bytes(4, byteorder='little', signed=True)

    # Interpret the bytes as a float (little-endian)
    import struct
    reading_value = struct.unpack('<f', bytes_value)[0]

    print(reading_value) # 147925204992.0


    Any help is very welcome, thanks.

    Continue reading...

Compartilhe esta Página