API Templates

In order to set you up quickly, there are two API templates, which allow to create a Modbus client from a simple API definition. The API definition is assumed to be provided in terms of a dictionary, which contains a meaningful key (such as the numeric message ID or a string) as key and a Payload object as value.

Default API Template

class modbusclient.api_wrapper.ApiWrapper(api={}, host='', port=502, timeout=None, connect=False, unit=255)[source]

Default API implementation to quickly

Parameters:
  • api (dict) – Dictionary containing the API definition. Should contain a string with desired method name as key and a Payload object as value.
  • host (str) – IP address of the server. Passed verbatim to Client
  • port (int) – Port to connect to at the server. Passed verbatim to Client
  • timeout (int) – Client timeout. Passed verbatim to Client
  • connect (bool) – Connect to the client. Defaults to False. Passed verbatim to Client
unit

int – Modbus unit ID: Defaults to NO_UNIT.

__enter__()[source]

Context Manager support

Connects to the server if not already connected.

__exit__(type, value, traceback)[source]

Context Manager support

Logs out and disconnects from the server.

connect(**kwargs)[source]

Connect this client to a host

Parameters:
  • address (string) – IP Adress of the host
  • port (int) – Port to use. Defaults to 502
  • timeout (float) – Timeout in seconds. If not set, it will be set to the default timeout.
disconnect()[source]

Disconnect this client

If this client is connected, the socket will be shutdown and then closed. If the client is not connected, calling this method has no effect.

get(message)[source]

Get value of a single message

Parameters:message (Payload or str) – Message to read from remote device. If this is not a Payload instance, the api dictionary will be used with message as key to lookup the payload.
Returns:Value of message
Return type:value
is_connected()[source]

Check if this client is connected to a server

Returns:True if and only if this client is connected to a server
Return type:bool
is_logged_in()[source]

Check if this client is currently logged in

Intended to be implemented by derived class, if applicable

Returns:False.
Return type:bool
load(settings)[source]

Load settings from dictionary

Parameters:settings (dict) – Dictionary with settings as returned by save()
Returns:Successfully modified settings
Return type:list
logout()[source]

Log out client

This is a stub. Intended to be implemented by derived classes.

save(selection=None)[source]

Save current settings into dictionary

Parameters:selection (iterable) – Iterable of messages (API keys) to save. If None, all messages of the current API are backed up.
Returns:Dictionary containing API message key and setting as value
Return type:dict
set(message, value)[source]

Set value of a single message

Parameters:
  • message (Payload) – Message to write to remote device. If this is not a Payload instance, the api dictionary will be used with message as key to lookup the payload.
  • value (object) – Value to set for this message.
Returns:

Value of message

Return type:

value

Asynchronous API Template

class modbusclient.asyncio.api_wrapper.ApiWrapper(api={}, host='', port=502, timeout=None, max_transactions=3, unit=255)[source]

Asynchronous API implementation

Parameters:
  • api (dict) – Dictionary containing the API definition. Should contain a string with desired method name as key and a Payload object as value.
  • host (str) – IP address of the server. Passed verbatim to Client
  • port (int) – Port to connect to at the server. Passed verbatim to Client
  • timeout (int) – Client timeout. Passed verbatim to Client
  • max_transactions (int) – Maximum number of parallel transactions. Passed verbatim to Client.
  • unit (int) – Modbus unit ID to use. Defaults to NO_UNIT.
unit

int – Modbus unit ID: Defaults to NO_UNIT.

__aenter__()[source]

Context Manager support

Connects to the server if not already connected.

__aexit__(type, value, traceback)[source]

Context Manager support

Logs out and disconnects from the server.

connect(**kwargs)[source]

Connect this client to a host

Parameters:
  • address (string) – IP Adress of the host
  • port (int) – Port to use. Defaults to 502
  • timeout (float) – Timeout in seconds. If not set, it will be set to the default timeout.
disconnect()[source]

Disconnect this client

If this client is connected, the socket will be shutdown and then closed. If the client is not connected, calling this method has no effect.

get(message)[source]

Get value of a single message

Parameters:message (Payload or str) – Message to read from remote device. If this is not a Payload instance, the api dictionary will be used with message as key to lookup the payload.
Returns:Value of message
Return type:value
is_connected()[source]

Check if this client is connected to a server

Returns:True if and only if this client is connected to a server
Return type:bool
is_logged_in()[source]

Check if this client is currently logged in

Intended to be implemented by derived class, if applicable

Returns:False.
Return type:bool
load(settings)[source]

Load settings from dictionary

Parameters:settings (dict) – Dictionary with settings as returned by save()
Returns:Successfully modified settings
Return type:list
logout()[source]

Log out client

This is a stub. Intended to be implemented by derived classes.

save(selection=None)[source]

Save current settings into dictionary

Parameters:selection (iterable) – Iterable of messages (API keys) to save. If None, all messages of the current API are backed up.
Returns:Dictionary containing API message key and setting as value
Return type:dict
set(message, value)[source]

Set value of a single message

Parameters:
  • message (Payload or str) – Message to write to remote device. If this is not a Payload instance, the api dictionary will be used with message as key to lookup the payload.
  • value (object) – Value to set for this message.
Returns:

Value of message

Return type:

value