Created
January 16, 2024 04:40
-
-
Save optio50/8f993896157ca5d4fcd4d3be401a01ff to your computer and use it in GitHub Desktop.
ModBus Battery SOC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from pymodbus.constants import Defaults | |
from pymodbus.constants import Endian | |
from pymodbus.client.sync import ModbusTcpClient as ModbusClient | |
from pymodbus.payload import BinaryPayloadDecoder | |
BmvID = 223 | |
ip = "localhost" # ip address of GX device or if on venus local try localhost | |
# Local network ip address of Cerbo GX. Default port 502 | |
client = ModbusClient(ip, port='502') | |
# All modbus variable requests are sent to this function and it return's the requested value | |
def modbus_register(address, unit): | |
msg = client.read_holding_registers(address, unit=unit) | |
decoder = BinaryPayloadDecoder.fromRegisters(msg.registers, byteorder=Endian.Big) | |
msg = decoder.decode_16bit_int() | |
return msg | |
BatterySOC = modbus_register(266, BmvID) / 10 | |
print(f"BatterySOC {BatterySOC}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment