Skip to content

Commit

Permalink
Fix scaling when writing holding registers (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
LavermanJJ committed Oct 18, 2022
1 parent 1f7d980 commit 62e84f2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pysolarfocus"
version = "2.0.3"
version = "2.0.4"
description = "Unofficial, local Solarfocus client"
authors = ["Jeroen Laverman <jjlaverman@web.de>"]
license = "Apache-2.0"
Expand Down
7 changes: 4 additions & 3 deletions pysolarfocus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Python client lib for Solarfocus"""
__version__ = "2.0.3"
__version__ = "2.0.4"

import logging
from enum import Enum
Expand Down Expand Up @@ -435,13 +435,14 @@ def pv_set_grid_im_export(self, value: int) -> bool:
"""Set Photovoltaic"""
return self.__write_register(self.photovoltaic.grid_im_export,value)

def __write_register(self,data_value:DataValue, value:int, check_connection:bool = True) -> bool:
def __write_register(self,data_value:DataValue, value:float, check_connection:bool = True) -> bool:
"""Internal methode to write a value to the modbus server"""
if check_connection and not self.is_connected:
logging.error("Connection to modbus is not established!")
return False
try:
scaled = int(data_value.reverse_scale(value))
scaled = int(data_value.scale(value))
logging.info(f"Scaled Value={scaled}")
response = self._conn.write_registers(data_value.get_absolute_address(), [scaled], unit=self._slave_id)
if response.isError():
logging.error(f"Error writing value={value} to register: {data_value.get_absolute_address()}: {response}")
Expand Down
9 changes: 9 additions & 0 deletions pysolarfocus/components/base/data_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def reverse_scale(self,value:float)->float:
else:
return value

def scale(self,value:float)->float:
"""
Applies the scaler in the reverse direction
"""
if self.has_scaler:
return value * self.multiplier
else:
return value

def set_unscaled_value(self,value:float)->None:
"""
Applies the scaler to the value and sets the value
Expand Down

0 comments on commit 62e84f2

Please sign in to comment.