Skip to content

Commit 8f2c6aa

Browse files
karlpdhoomakethu
authored andcommitted
datastore: sparse stores shouldn't need an initial list
Currently, to create a sparse data store, you are required to provide an initial list/dict. The first entry is then used as a "default" for the reset() call, which seems rather at odds with the "sparse" nature. Instead, use a supplied intial value if provided, and use that to properly reset the store to _that_ value. Similarly don't require an initial list at all, allowing addresses/registers to be filled in via follow up calls to .setValues() Example old code: (We want to register 20 addresses at 0x2000} # must provide at least one initial value! hr = ModbusSparseDataBlock({0x2000: 0x55aa}) # actually register what we want using flexible lists. hr.setValues(0x2000, [0]*20) Example new code: hr = ModbusSparseDataBlock() hr.setValues(0x2000, [0]*20) Example old code using reset: hr = ModbusSparseDataBlock({ 0x2000: 0x55, 0x3000: 0x66 }) hr.reset() At this point, the store contained two registers, 0x2000 and 0x2001, both containing the value 0x55. This hardly seems to have been the intention of .reset() on a Sparse store, but is obvious for the sequential store. Fixes: #566 Signed-off-by: Karl Palsson <[email protected]>
1 parent 2ada5c1 commit 8f2c6aa

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

pymodbus/datastore/store.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -192,31 +192,24 @@ def setValues(self, address, values):
192192
class ModbusSparseDataBlock(BaseModbusDataBlock):
193193
''' Creates a sparse modbus datastore '''
194194

195-
def __init__(self, values):
196-
''' Initializes the datastore
197-
198-
Using the input values we create the default
199-
datastore value and the starting address
195+
def __init__(self, values=None):
196+
''' Initializes a sparse datastore. Will only answer to addresses
197+
registered, either initially here, or later via setValues()
200198
201199
:param values: Either a list or a dictionary of values
202200
'''
203201
if isinstance(values, dict):
204202
self.values = values
205203
elif hasattr(values, '__iter__'):
206204
self.values = dict(enumerate(values))
207-
else: raise ParameterException(
208-
"Values for datastore must be a list or dictionary")
209-
self.default_value = get_next(itervalues(self.values)).__class__()
210-
self.address = get_next(iterkeys(self.values))
211-
212-
@classmethod
213-
def create(klass):
214-
''' Factory method to create a datastore with the
215-
full address space initialized to 0x00
205+
else:
206+
self.values = {} # Must make a new dict here per instance
207+
# We only need this to support .reset()
208+
self.default_value = self.values.copy()
216209

217-
:returns: An initialized datastore
218-
'''
219-
return klass([0x00] * 65536)
210+
def reset(self):
211+
''' Reset the store to the intially provided defaults'''
212+
self.values = self.default_value.copy()
220213

221214
def validate(self, address, count=1):
222215
''' Checks to see if the request is in range

0 commit comments

Comments
 (0)