| 12345678910111213141516171819 |
- """Encode point values to Modbus registers."""
- import struct
- def encode_registers(value: object, data_type: str) -> list[int]:
- if value is None:
- value = 0
- if data_type == "int16":
- packed = struct.pack(">h", int(value))
- elif data_type == "int32":
- packed = struct.pack(">i", int(value))
- elif data_type == "float32":
- packed = struct.pack(">f", float(value))
- else:
- raise ValueError(f"unsupported data_type: {data_type}")
- return [int.from_bytes(packed[index:index + 2], "big") for index in range(0, len(packed), 2)]
|