Skip to content

Commit

Permalink
fix CRC to be compliant for new firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
britkat1980 committed Jun 27, 2023
1 parent 12e56e1 commit ab5c96d
Show file tree
Hide file tree
Showing 22 changed files with 253 additions and 193 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ buildx.sh
.venv
.vscode
GivTCP/__pycache__
./givenergy_modbus/__pycache__
GivTCP/givenergy_modbus_2
GivTCP/asyncclient.py
GivTCP/testdata.json
Expand Down
2 changes: 1 addition & 1 deletion GivTCP/mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def isfloat(num):

def on_message(client, userdata, message):
payload={}
logger.critical("MQTT Message Recieved: "+str(message.topic)+"= "+str(message.payload.decode("utf-8")))
logger.debug("MQTT Message Recieved: "+str(message.topic)+"= "+str(message.payload.decode("utf-8")))
writecommand={}
command=str(message.topic).split("/")[-1]
if command=="setDischargeRate":
Expand Down
2 changes: 1 addition & 1 deletion buildx.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6 -t britkat/giv_tcp-dev:2.2.0 --push .
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6 -t britkat/giv_tcp-dev:2.2.1 --push .
::docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6 -t britkat/giv_tcp-ma:latest -t britkat/giv_tcp-ma:2.2.0 --push .
Binary file modified givenergy_modbus/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/__pycache__/client.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/__pycache__/decoder.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/__pycache__/framer.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/__pycache__/modbus.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/__pycache__/pdu.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/__pycache__/transaction.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/__pycache__/util.cpython-310.pyc
Binary file not shown.
10 changes: 5 additions & 5 deletions givenergy_modbus/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import click

from givenergy_modbus.client import GivEnergyClient
from givenergy_modbus.model.battery import Battery
from givenergy_modbus.model.inverter import Inverter # type: ignore
from givenergy_modbus.model.plant import Plant
from givenergy_modbus.util import InterceptHandler
from client import GivEnergyClient
from model.battery import Battery
from model.inverter import Inverter # type: ignore
from model.plant import Plant
from util import InterceptHandler

_logger = logging.getLogger(__package__)

Expand Down
9 changes: 4 additions & 5 deletions givenergy_modbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def fetch_register_pages(
self,
pages: Mapping[type[HoldingRegister | InputRegister], Sequence[int]],
register_cache: RegisterCache,
slave_address: int = 0x32,
slave_address: int = 0x31,
sleep_between_queries: float = DEFAULT_SLEEP,
) -> None:
"""Reload all inverter data from the device."""
Expand All @@ -54,13 +54,13 @@ def refresh_plant(self, plant: Plant, full_refresh: bool, sleep_between_queries=
inverter_registers[HoldingRegister] = [0, 60, 120]

self.fetch_register_pages(
inverter_registers, plant.inverter_rc, slave_address=0x32, sleep_between_queries=sleep_between_queries
inverter_registers, plant.inverter_rc, slave_address=0x11, sleep_between_queries=sleep_between_queries
)
for i, battery_rc in enumerate(plant.batteries_rcs):
self.fetch_register_pages(
{InputRegister: [60]},
battery_rc,
slave_address=0x32 + i,
slave_address=0x11 + i,
sleep_between_queries=sleep_between_queries,
)

Expand Down Expand Up @@ -194,8 +194,7 @@ def set_mode_dynamic(self):
self-consumption of renewable generation and minimise the amount of energy drawn from the grid.
"""
self.set_battery_discharge_mode_demand() # r27=1
#self.set_shallow_charge(4) # r110=4 Commented as this has nothing to do with dynamic mode. This should be handled
# in either setBatteryMode() or by calling setBatteryReserve()
self.set_shallow_charge(4) # r110=4
self.disable_discharge() # r59=0

def set_mode_storage(
Expand Down
Binary file modified givenergy_modbus/model/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/model/__pycache__/battery.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/model/__pycache__/inverter.cpython-310.pyc
Binary file not shown.
Binary file modified givenergy_modbus/model/__pycache__/plant.cpython-310.pyc
Binary file not shown.
Binary file not shown.
13 changes: 9 additions & 4 deletions givenergy_modbus/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class ModbusPDU(ABC):

builder: BinaryPayloadBuilder
function_code: int
data_adapter_serial_number: str = 'AB1234G567'
data_adapter_serial_number: str = ''
#data_adapter_serial_number: int = 0x0000000000
padding: int = 0x00000008
slave_address: int = 0x31 # 0x11 is the inverter but the cloud systems interfere, 0x32+ are the batteries
slave_address: int = 0x32 # 0x11 is the inverter but the cloud systems interfere, 0x32+ are the batteries
check: int = 0x0000
error: bool = False

Expand Down Expand Up @@ -201,10 +202,12 @@ def _decode_function_data(self, decoder):

def _update_check_code(self):
crc_builder = BinaryPayloadBuilder(byteorder=Endian.Big)
crc_builder.add_8bit_uint(self.slave_address)
crc_builder.add_8bit_uint(self.function_code)
crc_builder.add_16bit_uint(self.base_register)
crc_builder.add_16bit_uint(self.register_count)
self.check = CrcModbus().process(crc_builder.to_string()).final()
self.check=int.from_bytes(self.check.to_bytes(2,'little'),'big')
self.builder.add_16bit_uint(self.check)

def _calculate_function_data_size(self):
Expand Down Expand Up @@ -381,10 +384,12 @@ def _decode_function_data(self, decoder):

def _update_check_code(self):
crc_builder = BinaryPayloadBuilder(byteorder=Endian.Big)
crc_builder.add_8bit_uint(self.slave_address)
crc_builder.add_8bit_uint(self.function_code)
crc_builder.add_16bit_uint(self.register)
crc_builder.add_16bit_uint(self.value)
crc_builder.add_16bit_uint(self.base_register)
crc_builder.add_16bit_uint(self.register_count)
self.check = CrcModbus().process(crc_builder.to_string()).final()
self.check=int.from_bytes(self.check.to_bytes(2,'little'),'big')
self.builder.add_16bit_uint(self.check)

def _calculate_function_data_size(self):
Expand Down
1 change: 1 addition & 0 deletions givenergy_modbus/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def _transact(
self.client.connect()
tx_data = self.client.framer.buildPacket(request)
_logger.debug(f"SEND raw frame: {hexlify(tx_data)}")
# print(f"SEND raw frame: {hexlify(tx_data)}")
_logger.info(f'Sending request {request}')
tx_size = self._send(tx_data)

Expand Down
17 changes: 10 additions & 7 deletions startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def palm_job():
hasMQTT=False
logger.critical("No HA MQTT service has been found")


#Get Host Details
url="http://supervisor/network/info"
result = requests.get(url,
Expand All @@ -69,12 +68,16 @@ def palm_job():
hostDetails=result.json()

if hostDetails['result']=="ok":
# For each interface scan for inverters
for interface in hostDetails['data']['interfaces']:
subnet=interface['ipv4']['gateway']
invList=findInvertor(subnet)
logger.critical ("We have found the following invertors: "+str(invList))

# For each interface scan for inverters
try:
for interface in hostDetails['data']['interfaces']:
subnet=interface['ipv4']['gateway']
invList=findInvertor(subnet)
logger.critical ("We have found the following invertors: "+str(invList))
except:
logger.error("Error scanning for Inverters")

logger.critical("GivTCP isAddon: "+str(isAddon))

if not os.path.exists(str(os.getenv("CACHELOCATION"))):
os.makedirs(str(os.getenv("CACHELOCATION")))
Expand Down
Loading

0 comments on commit ab5c96d

Please sign in to comment.