mbsim.core package
Submodules
mbsim.core.client module
Client
This module manages loading the modbus Client or Master.
Examples of severs can be seen in example/client
To start prototyping
Create clients
Create Tasks
Start Tasks
The Clients that we suggest using can be found here
mbsim.core.server module
Server
This module manages loading the modbus Server or Slave.
Examples of severs can be seen in example/server
When creating a prototype.
Create identity (Optional)
Create Sever Context. Default is to full space with all values set to 0
Create functions or coroutine functions and add tasks to run.
Start the server and tasks. See pymodbus server to see keyword arguments to pass to the start function.
- mbsim.core.server.genContext(context=None, single=True)[source]
A Function to return Context for Server.
The context can be None. This will generate a context that all slavesid will use. All values set to 0
If context is an instance of ModbusServerContext, function returns the context
Else dict of slaves with dict of address space(“di”, “co”, “hr”, “ir”) with list or dict {offset: [reg0, reg1, …]}
dict Context example
{0: {"di": [0, 1, 0, 1, 0], "co": {0: [1, 0, 1], 100: [1]}, "hr": [123, 10], "ir": {123: [123, 0, 123]}}, ...}
- Parameters:
context – Server Context or dict of Slaves context or dict
single (bool) – This makes all slave id maps to one Slave context
- mbsim.core.server.genDevice(name='mbsim', code='MB', url='https://gitlab.com/nee2c/mbsim', product='mbsim', model='mbsim', version='1.0.0')[source]
A function to create a identity. All parameters are key word arguments.
- Parameters:
- Returns:
Returns an identity for server
- mbsim.core.server.start(server, loop=None, **kwargs)[source]
This is the function to start modbus server. Passes all kwargs though to server, if missing uses default.
To see full list of keyword arguments for supported protocols see.
- Parameters:
server (str) – The protocol to run modbus server.
loop – The event loop and if none will use running loop or create new loop
mbsim.core.tasks module
Task
A module to manage classes of Task objects.
Task are a way to run a function or coroutinefunction periodically. This is useful for things like updating a values or sending a message to a client every so often.
@mbsim.core.server.Task(1, args=("Hello", "world"))
def hw(*args):
print(*args)
def main():
task = mbsim.core.server.Task(1, func=hw, args=("foo", "bar"))
mb.start()
This will print “Hello” and “world” every second. I also prints “foo” and “bar” every second.
- class mbsim.core.tasks.Task(inter, func=None, args=(), kwargs=None, now=False, block=False)[source]
Bases:
objectA Class to manage all the task to run periodically alongside the server/client
This can be called as a decorators. The following example will print “Hellonworld” every second. The decorator can be used multiple times to call more than once.
@mbsim.core.server.Task(1, args=("Hello", "world")) def hw(*args): for arg in args: print(arg) or mbsim.core.server.Task(1, func=hw, args=("Hello", "world"))
Both example are equivalent
This will take the arguments for the task to be run
- Parameters:
inter (float) – Interval of time to wait before calling the function or coroutinefunction in seconds
func (function, coroutinefunction, optional) – Function to call
args (tuple, optional) – arguments to be past to task
kwargs (dict, optional) – key word arguments to be passed to task, defaults to {}
now (bool, optional) – To run immediately or wait the interval, defaults to False
- loop = None
- tasks = []