Skip to content

Commit

Permalink
Merge pull request #928 from ivan73/develop_mudbus_tcp
Browse files Browse the repository at this point in the history
modbus_tcp: Fixed error writing registers, conversion byte/word to endian
  • Loading branch information
msinn committed May 5, 2024
2 parents 25935a2 + 98d3651 commit ad1365b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
38 changes: 18 additions & 20 deletions modbus_tcp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class modbus_tcp(SmartPlugin):
devices.
"""

PLUGIN_VERSION = '1.0.10'
PLUGIN_VERSION = '1.0.11'

def __init__(self, sh, *args, **kwargs):
"""
Expand Down Expand Up @@ -133,8 +133,8 @@ def parse_item(self, item):
value = item()
dataType = 'uint16'
factor = 1
byteOrder = 'Endian.Big'
wordOrder = 'Endian.Big'
byteOrderStr = 'Endian.BIG'
wordOrderStr = 'Endian.BIG'
slaveUnit = self._slaveUnit
dataDirection = 'read'

Expand All @@ -158,24 +158,22 @@ def parse_item(self, item):
if self.has_iattr(item.conf, AttrFactor):
factor = float(self.get_iattr_value(item.conf, AttrFactor))
if self.has_iattr(item.conf, AttrByteOrder):
byteOrder = self.get_iattr_value(item.conf, AttrByteOrder)
byteOrderStr = self.get_iattr_value(item.conf, AttrByteOrder)
if self.has_iattr(item.conf, AttrWordOrder):
wordOrder = self.get_iattr_value(item.conf, AttrWordOrder)
if byteOrder == 'Endian.Big': # Von String in Endian-Konstante "umwandeln"
byteOrder = Endian.BIG
elif byteOrder == 'Endian.Little':
byteOrder = Endian.LITTLE
else:
wordOrderStr = self.get_iattr_value(item.conf, AttrWordOrder)

try: # den letzten Teil des Strings extrahieren, in Großbuchstaben und in Endian-Konstante wandeln
byteOrder = Endian[(str(byteOrderStr).split('.')[-1]).upper()]
except Exception as e:
self.logger.warning(f"Invalid byteOrder -> default(Endian.BIG) is used. Error:{e}")
byteOrder = Endian.BIG
self.logger.warning("Invalid byte order -> default(Endian.Big) is used")
if wordOrder == 'Endian.Big': # Von String in Endian-Konstante "umwandeln"
wordOrder = Endian.BIG
elif wordOrder == 'Endian.Little':
wordOrder = Endian.LITTLE
else:

try: # den letzten Teil des Strings extrahieren, in Großbuchstaben und in Endian-Konstante wandeln
wordOrder = Endian[(str(wordOrderStr).split('.')[-1]).upper()]
except Exception as e:
self.logger.warning(f"Invalid byteOrder -> default(Endian.BIG) is used. Error:{e}")
wordOrder = Endian.BIG
self.logger.warning("Invalid byte order -> default(Endian.Big) is used")


regPara = {'regAddr': regAddr, 'slaveUnit': slaveUnit, 'dataType': dataType, 'factor': factor,
'byteOrder': byteOrder,
'wordOrder': wordOrder, 'item': item, 'value': value, 'objectType': objectType,
Expand Down Expand Up @@ -391,10 +389,10 @@ def __write_Registers(self, regPara, value):
return None

if objectType == 'Coil':
result = self._Mclient.write_coil(address, value, unit=slaveUnit)
result = self._Mclient.write_coil(address, value, slave=slaveUnit)
elif objectType == 'HoldingRegister':
registers = builder.to_registers()
result = self._Mclient.write_registers(address, registers, unit=slaveUnit)
result = self._Mclient.write_registers(address, registers, slave=slaveUnit)
elif objectType == 'DiscreteInput':
self.logger.warning(f"this object type cannot be written {objectType}:{address} slaveUnit:{slaveUnit}")
return
Expand Down
22 changes: 11 additions & 11 deletions modbus_tcp/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugin:
keywords: modbus_tcp modbus smartmeter inverter heatpump
#documentation: http://smarthomeng.de/user/plugins/modbus_tcp/user_doc.html
support: https://knx-user-forum.de/forum/supportforen/smarthome-py/1154368-einbindung-von-modbus-tcp
version: 1.0.10 # Plugin version
version: 1.0.11 # Plugin version
sh_minversion: 1.8 # minimum shNG version to use this plugin
#sh_maxversion: # maximum shNG version to use this plugin (leave empty if latest)
py_minversion: 3.6
Expand Down Expand Up @@ -102,23 +102,23 @@ item_attributes:

modBusByteOrder:
type: str
default: 'Endian.Big'
default: 'Endian.BIG'
description:
de: 'Endian.Big oder Endian.Little'
en: 'Endian.Big or Endian.Little'
de: 'Endian.BIG oder Endian.LITTLE'
en: 'Endian.BIG or Endian.LITTLE'
valid_list:
- 'Endian.Big'
- 'Endian.Little'
- 'Endian.BIG'
- 'Endian.LITTLE'

modBusWordOrder:
type: str
default: 'Endian.Big'
default: 'Endian.BIG'
description:
de: 'Endian.Big oder Endian.Little'
en: 'Endian.Big or Endian.Little'
de: 'Endian.BIG oder Endian.LITTLE'
en: 'Endian.BIG or Endian.LITTLE'
valid_list:
- 'Endian.Big'
- 'Endian.Little'
- 'Endian.BIG'
- 'Endian.LITTLE'

modBusFactor:
type: num
Expand Down
3 changes: 3 additions & 0 deletions modbus_tcp/user_doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ siehe auch example.yaml
Changelog
---------
V1.0.11 Verbesserung Umwandlung Byte/Wordorder in Endian-Konstante
Fehler beim Schreiben von Register behoben

V1.0.10 Mindestversion für pymodbus ist nun 3.5.2

V1.0.9
Expand Down

0 comments on commit ad1365b

Please sign in to comment.