pylablib.devices.Modbus package

Submodules

pylablib.devices.Modbus.modbus module

exception pylablib.devices.Modbus.modbus.ModbusError[source]

Bases: DeviceError

Generic Modbus device error

add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pylablib.devices.Modbus.modbus.ModbusBackendError(exc)[source]

Bases: ModbusError, DeviceBackendError

Generic Modbus backend communication error

add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class pylablib.devices.Modbus.modbus.TModbusFrame(address, function, data)

Bases: tuple

address
data
function
class pylablib.devices.Modbus.modbus.GenericModbusRTUDevice(conn, daddr=1)[source]

Bases: ICommBackendWrapper

Generic Modbus-connected RTU protocol device.

Parameters:
  • conn – serial connection parameters (usually port, a tuple containing port and baudrate, or a tuple with full specification such as ("COM1", 9600, 8, 'N', 1))

  • daddr – default device address

Error

alias of ModbusError

mb_get_default_address()[source]

Get device address used by default in Modbus methods

mb_set_default_address(daddr)[source]

Set device address used by default in Modbus methods

mb_using_address(daddr)[source]

Context manager for temporary using a different default device address

mb_read_coils(address, quantity=1, daddr=None)[source]

Read Modbus one-bit discrete coils with the given starting address and quantity

mb_read_discrete_inputs(address, quantity, daddr=None)[source]

Read Modbus one-bit discrete inputs with the given starting address and quantity

mb_read_holding_registers(address, quantity, daddr=None)[source]

Read Modbus two-byte holding registers with the given starting address and quantity

mb_read_input_registers(address, quantity, daddr=None)[source]

Read Modbus two-byte input registers with the given starting address and quantity

mb_write_single_coil(address, value, daddr=None)[source]

Write a single Modbus one-bit discrete coil at the given address

mb_write_single_holding_register(address, value, daddr=None)[source]

Write a single Modbus two-byte holding register at the given address

mb_write_multiple_coils(address, value, quantity=None, daddr=None)[source]

Write multiple Modbus one-bit discrete coils with the given starting address and quantity.

value is a bytes object with the bit values listed LSB first.

mb_write_multiple_holding_registers(address, value, daddr=None)[source]

Write a multiple Modbus two-byte holding registers at the given address.

value is a bytes object with the values listed LSB first.

mb_get_device_id(daddr=None)[source]

Get Modbus device ID (function 17)

mb_scan_devices(daddrs='all', timeout=0.1, func=1, payload=b'')[source]

Scan for devices on the bus by sending a specified command and waiting for the reply.

daddrs is a list of addresses to check ("all" means all addresses from 1 to 247 inclusive) timeout is the timeout to wait for each device reply. func and payload specify the message to send (by default, ‘read coil’ command with no arguments, which should always return and error) Since the addresses are polled consecutively, this function can take a long time (~25s for the default settings).

apply_settings(settings)

Apply the settings.

settings is the dict {name: value} of the device available settings. Non-applicable settings are ignored.

close()

Close the backend

get_device_variable(key)

Get the value of a settings, status, or full info parameter

get_full_info(include=0)

Get dict {name: value} containing full device information (including status and settings).

include specifies either a list of variables (only these variables are returned), a priority threshold (only values with the priority equal or higher are returned), or "all" (all available variables). Since the lowest priority is -10, setting include=-10 queries all available variables, which is equivalent to include="all".

get_full_status(include=0)

Get dict {name: value} containing the device status (including settings).

include specifies either a list of variables (only these variables are returned), a priority threshold (only values with the priority equal or higher are returned), or "all" (all available variables). Since the lowest priority is -10, setting include=-10 queries all available variables, which is equivalent to include="all".

get_settings(include=0)

Get dict {name: value} containing all the device settings.

include specifies either a list of variables (only these variables are returned), a priority threshold (only values with the priority equal or higher are returned), or "all" (all available variables). Since the lowest priority is -10, setting include=-10 queries all available variables, which is equivalent to include="all".

is_opened()

Check if the device is connected

lock(timeout=None)

Lock the access to the device from other threads/processes (isn’t necessarily implemented)

locking(timeout=None)

Context manager for lock & unlock

open()

Open the backend

set_device_variable(key, value)

Set the value of a settings parameter

unlock()

Unlock the access to the device from other threads/processes (isn’t necessarily implemented)

Module contents