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

  1. Create clients

  2. Create Tasks

  3. Start Tasks

The Clients that we suggest using can be found here

mbsim.core.client.start(loop=None)[source]

This is the function to start Tasks for client

Parameters:

loop – The event loop and if none will create new loop or use running loop

mbsim.core.client.stop()[source]

Function to stop Tacks

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.

  1. Create identity (Optional)

  2. Create Sever Context. Default is to full space with all values set to 0

  3. Create functions or coroutine functions and add tasks to run.

  4. 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:
  • name (str) – Vendor Name. Defaults to mbsim

  • code (str) – Product Code. Defaults to MB

  • url (str) – Vendor URL. Defaults to https://gitlab.com/nee2c/mbsim

  • product (str) – Product Name. Defaults to mbsim

  • model (str) – Model Name. Defaults to mbsim

  • version (str) – Version. Defaults to 1.0.0

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.server.stop()[source]

Function to stop modbus server

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: object

A 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
start()[source]

To start the task

classmethod startTasks(loop=None)[source]

Class method to start all tasks

stop()[source]

To stop a task

classmethod stopTasks()[source]

Class method to stop all tasks

tasks = []
async wrap(a, kw)[source]

A wrap for twisted to pass a and kw as args. This function will explode them into the function

Parameters:
mbsim.core.tasks.getloop()[source]

Function to return async loop

Module contents