pymodbus.datastore package
Define datastore.
- class pymodbus.datastore.ModbusSequentialDataBlock(address, values)
Bases:
BaseModbusDataBlock
Creates a sequential modbus datastore.
- classmethod create()
Create a datastore.
With the full address space initialized to 0x00
- Returns
An initialized datastore
- getValues(address, count=1)
Return the requested values of the datastore.
- Parameters
address – The starting address
count – The number of values to retrieve
- Returns
The requested values from a:a+c
- setValues(address, values)
Set the requested values of the datastore.
- Parameters
address – The starting address
values – The new values to be set
- validate(address, count=1)
Check to see if the request is in range.
- Parameters
address – The starting address
count – The number of values to test for
- Returns
True if the request in within range, False otherwise
- class pymodbus.datastore.ModbusServerContext(slaves=None, single=True)
Bases:
object
This represents a master collection of slave contexts.
If single is set to true, it will be treated as a single context so every unit-id returns the same context. If single is set to false, it will be interpreted as a collection of slave contexts.
- slaves()
Define slaves.
- class pymodbus.datastore.ModbusSlaveContext(*args, **kwargs)
Bases:
IModbusSlaveContext
This creates a modbus data model with each data access stored in a block.
- getValues(fc_as_hex, address, count=1)
Get count values from datastore.
- Parameters
fc_as_hex – The function we are working with
address – The starting address
count – The number of values to retrieve
- Returns
The requested values from a:a+c
- register(function_code, fc_as_hex, datablock=None)
Register a datablock with the slave context.
- Parameters
function_code – function code (int)
fc_as_hex – string representation of function code (e.g “cf” )
datablock – datablock to associate with this function code
- reset()
Reset all the datastores to their default values.
- setValues(fc_as_hex, address, values)
Set the datastore with the supplied values.
- Parameters
fc_as_hex – The function we are working with
address – The starting address
values – The new values to be set
- validate(fc_as_hex, address, count=1)
Validate the request to make sure it is in range.
- Parameters
fc_as_hex – The function we are working with
address – The starting address
count – The number of values to test
- Returns
True if the request in within range, False otherwise
- class pymodbus.datastore.ModbusSparseDataBlock(values=None, mutable=True)
Bases:
BaseModbusDataBlock
Create a sparse modbus datastore.
E.g Usage. sparse = ModbusSparseDataBlock({10: [3, 5, 6, 8], 30: 1, 40: [0]*20})
This would create a datablock with 3 blocks starting at offset 10 with length 4 , 30 with length 1 and 40 with length 20
sparse = ModbusSparseDataBlock([10]*100) Creates a sparse datablock of length 100 starting at offset 0 and default value of 10
sparse = ModbusSparseDataBlock() –> Create Empty datablock sparse.setValues(0, [10]*10) –> Add block 1 at offset 0 with length 10 (default value 10) sparse.setValues(30, [20]*5) –> Add block 2 at offset 30 with length 5 (default value 20)
if mutable is set to True during initialization, the datablock can not be altered with setValues (new datablocks can not be added)
- classmethod create(values=None)
Create sparse datastore.
Use setValues to initialize registers.
- Parameters
values – Either a list or a dictionary of values
- Returns
An initialized datastore
- getValues(address, count=1)
Return the requested values of the datastore.
- Parameters
address – The starting address
count – The number of values to retrieve
- Returns
The requested values from a:a+c
- reset()
Reset the store to the initially provided defaults.
- setValues(address, values, use_as_default=False)
Set the requested values of the datastore.
- Parameters
address – The starting address
values – The new values to be set
use_as_default – Use the values as default
- Raises
- validate(address, count=1)
Check to see if the request is in range.
- Parameters
address – The starting address
count – The number of values to test for
- Returns
True if the request in within range, False otherwise
Subpackages
Submodules
pymodbus.datastore.context module
Context for datastore.
- class pymodbus.datastore.context.ModbusServerContext(slaves=None, single=True)
Bases:
object
This represents a master collection of slave contexts.
If single is set to true, it will be treated as a single context so every unit-id returns the same context. If single is set to false, it will be interpreted as a collection of slave contexts.
- slaves()
Define slaves.
- class pymodbus.datastore.context.ModbusSlaveContext(*args, **kwargs)
Bases:
IModbusSlaveContext
This creates a modbus data model with each data access stored in a block.
- getValues(fc_as_hex, address, count=1)
Get count values from datastore.
- Parameters
fc_as_hex – The function we are working with
address – The starting address
count – The number of values to retrieve
- Returns
The requested values from a:a+c
- register(function_code, fc_as_hex, datablock=None)
Register a datablock with the slave context.
- Parameters
function_code – function code (int)
fc_as_hex – string representation of function code (e.g “cf” )
datablock – datablock to associate with this function code
- reset()
Reset all the datastores to their default values.
- setValues(fc_as_hex, address, values)
Set the datastore with the supplied values.
- Parameters
fc_as_hex – The function we are working with
address – The starting address
values – The new values to be set
- validate(fc_as_hex, address, count=1)
Validate the request to make sure it is in range.
- Parameters
fc_as_hex – The function we are working with
address – The starting address
count – The number of values to test
- Returns
True if the request in within range, False otherwise
pymodbus.datastore.remote module
Remote datastore.
- class pymodbus.datastore.remote.RemoteSlaveContext(client, unit=None)
Bases:
IModbusSlaveContext
TODO.
This creates a modbus data model that connects to a remote device (depending on the client used)
- getValues(fc_as_hex, address, count=1)
Get values from real call in validate
- reset()
Reset all the datastores to their default values.
- setValues(fc_as_hex, address, values)
Set the datastore with the supplied values.
Already done in validate
- validate(fc_as_hex, address, count=1)
Validate the request to make sure it is in range.
- Parameters
fc_as_hex – The function we are working with
address – The starting address
count – The number of values to test
- Returns
True if the request in within range, False otherwise
pymodbus.datastore.store module
Modbus Server Datastore.
For each server, you will create a ModbusServerContext and pass in the default address space for each data access. The class will create and manage the data.
Further modification of said data accesses should be performed with [get,set][access]Values(address, count)
Datastore Implementation
There are two ways that the server datastore can be implemented. The first is a complete range from “address” start to “count” number of indices. This can be thought of as a straight array:
data = range(1, 1 + count)
[1,2,3,...,count]
The other way that the datastore can be implemented (and how many devices implement it) is a associate-array:
data = {1:"1", 3:"3", ..., count:"count"}
[1,3,...,count]
The difference between the two is that the latter will allow arbitrary gaps in its datastore while the former will not. This is seen quite commonly in some modbus implementations. What follows is a clear example from the field:
Say a company makes two devices to monitor power usage on a rack. One works with three-phase and the other with a single phase. The company will dictate a modbus data mapping such that registers:
n: phase 1 power
n+1: phase 2 power
n+2: phase 3 power
Using this, layout, the first device will implement n, n+1, and n+2, however, the second device may set the latter two values to 0 or will simply not implemented the registers thus causing a single read or a range read to fail.
I have both methods implemented, and leave it up to the user to change based on their preference.
- class pymodbus.datastore.store.BaseModbusDataBlock
Bases:
object
Base class for a modbus datastore
- Derived classes must create the following fields:
@address The starting address point @defult_value The default value of the datastore @values The actual datastore values
- Derived classes must implemented the following methods:
validate(self, address, count=1) getValues(self, address, count=1) setValues(self, address, values)
- default(count, value=False)
Use to initialize a store to one value.
- Parameters
count – The number of fields to set
value – The default value to set to the fields
- getValues(address, count=1)
Return the requested values from the datastore.
- Parameters
address – The starting address
count – The number of values to retrieve
- Raises
- reset()
Reset the datastore to the initialized default value.
- setValues(address, values)
Return the requested values from the datastore.
- Parameters
address – The starting address
values – The values to store
- Raises
- validate(address, count=1)
Check to see if the request is in range.
- Parameters
address – The starting address
count – The number of values to test for
- Raises
- class pymodbus.datastore.store.ModbusSequentialDataBlock(address, values)
Bases:
BaseModbusDataBlock
Creates a sequential modbus datastore.
- classmethod create()
Create a datastore.
With the full address space initialized to 0x00
- Returns
An initialized datastore
- getValues(address, count=1)
Return the requested values of the datastore.
- Parameters
address – The starting address
count – The number of values to retrieve
- Returns
The requested values from a:a+c
- setValues(address, values)
Set the requested values of the datastore.
- Parameters
address – The starting address
values – The new values to be set
- validate(address, count=1)
Check to see if the request is in range.
- Parameters
address – The starting address
count – The number of values to test for
- Returns
True if the request in within range, False otherwise
- class pymodbus.datastore.store.ModbusSparseDataBlock(values=None, mutable=True)
Bases:
BaseModbusDataBlock
Create a sparse modbus datastore.
E.g Usage. sparse = ModbusSparseDataBlock({10: [3, 5, 6, 8], 30: 1, 40: [0]*20})
This would create a datablock with 3 blocks starting at offset 10 with length 4 , 30 with length 1 and 40 with length 20
sparse = ModbusSparseDataBlock([10]*100) Creates a sparse datablock of length 100 starting at offset 0 and default value of 10
sparse = ModbusSparseDataBlock() –> Create Empty datablock sparse.setValues(0, [10]*10) –> Add block 1 at offset 0 with length 10 (default value 10) sparse.setValues(30, [20]*5) –> Add block 2 at offset 30 with length 5 (default value 20)
if mutable is set to True during initialization, the datablock can not be altered with setValues (new datablocks can not be added)
- classmethod create(values=None)
Create sparse datastore.
Use setValues to initialize registers.
- Parameters
values – Either a list or a dictionary of values
- Returns
An initialized datastore
- getValues(address, count=1)
Return the requested values of the datastore.
- Parameters
address – The starting address
count – The number of values to retrieve
- Returns
The requested values from a:a+c
- reset()
Reset the store to the initially provided defaults.
- setValues(address, values, use_as_default=False)
Set the requested values of the datastore.
- Parameters
address – The starting address
values – The new values to be set
use_as_default – Use the values as default
- Raises
- validate(address, count=1)
Check to see if the request is in range.
- Parameters
address – The starting address
count – The number of values to test for
- Returns
True if the request in within range, False otherwise