Skip to content

Commit

Permalink
Adds the option to drop into zero mode for slaves
Browse files Browse the repository at this point in the history
  • Loading branch information
Galen Collins committed Jul 23, 2014
1 parent 46c9008 commit 741d6f0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
7 changes: 7 additions & 0 deletions examples/common/asynchronous-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
# 0x03: ModbusSlaveContext(...),
# }
# context = ModbusServerContext(slaves=slaves, single=False)
#
# The slave context can also be initialized in zero-mode which means that a
# request to address(0-7) will map to the address (0-7). The default is
# False which is based on section 4.4 of the specification, so address(0-7)
# will map to (1-8)::
#
# store = ModbusSlaveContext(..., zero-mode=True)
#---------------------------------------------------------------------------#
store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [17]*100),
Expand Down
7 changes: 7 additions & 0 deletions examples/common/synchronous-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
# 0x03: ModbusSlaveContext(...),
# }
# context = ModbusServerContext(slaves=slaves, single=False)
#
# The slave context can also be initialized in zero-mode which means that a
# request to address(0-7) will map to the address (0-7). The default is
# False which is based on section 4.4 of the specification, so address(0-7)
# will map to (1-8)::
#
# store = ModbusSlaveContext(..., zero-mode=True)
#---------------------------------------------------------------------------#
store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [17]*100),
Expand Down
6 changes: 6 additions & 0 deletions pymodbus/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class Defaults(Singleton):
The number of bits sent after each character in a message to
indicate the end of the byte. This defaults to 1.
.. attribute:: ZeroMode
Indicates if the slave datastore should use indexing at 0 or 1.
Mor about this can be read in section 4.4 of the modbus specification.
'''
Port = 502
Retries = 3
Expand All @@ -89,6 +94,7 @@ class Defaults(Singleton):
Parity = 'N'
Bytesize = 8
Stopbits = 1
ZeroMode = False


class ModbusStatus(Singleton):
Expand Down
7 changes: 4 additions & 3 deletions pymodbus/datastore/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, *args, **kwargs):
self.store['c'] = kwargs.get('co', ModbusSequentialDataBlock.create())
self.store['i'] = kwargs.get('ir', ModbusSequentialDataBlock.create())
self.store['h'] = kwargs.get('hr', ModbusSequentialDataBlock.create())
self.zero_mode = kwargs.get('zero-mode', Defaults.ZeroMode)

def __str__(self):
''' Returns a string representation of the context
Expand All @@ -56,7 +57,7 @@ def validate(self, fx, address, count=1):
:param count: The number of values to test
:returns: True if the request in within range, False otherwise
'''
address = address + 1 # section 4.4 of specification
if not self.zero_mode: address = address + 1
_logger.debug("validate[%d] %d:%d" % (fx, address, count))
return self.store[self.decode(fx)].validate(address, count)

Expand All @@ -68,7 +69,7 @@ def getValues(self, fx, address, count=1):
:param count: The number of values to retrieve
:returns: The requested values from a:a+c
'''
address = address + 1 # section 4.4 of specification
if not self.zero_mode: address = address + 1
_logger.debug("getValues[%d] %d:%d" % (fx, address, count))
return self.store[self.decode(fx)].getValues(address, count)

Expand All @@ -79,7 +80,7 @@ def setValues(self, fx, address, values):
:param address: The starting address
:param values: The new values to be set
'''
address = address + 1 # section 4.4 of specification
if not self.zero_mode: address = address + 1
_logger.debug("setValues[%d] %d:%d" % (fx, address, len(values)))
self.store[self.decode(fx)].setValues(address, values)

Expand Down

0 comments on commit 741d6f0

Please sign in to comment.