API
Modbus Constants
- umodbus.const.ACKNOWLEDGE = <Mock name='mock.const()' id='140345175704576'>
Response is returned to prevent a timeout error from occurring in the client
- umodbus.const.CRC16_TABLE = (0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448)
CRC16 lookup table
- umodbus.const.CRC_LENGTH = <Mock name='mock.const()' id='140345175704576'>
CRC length
- umodbus.const.DEVICE_FAILED_TO_RESPOND = <Mock name='mock.const()' id='140345175704576'>
No response was obtained from the target device
- umodbus.const.DIAGNOSTICS = <Mock name='mock.const()' id='140345175704576'>
Provide series of tests for checking the communication system (serial only)
- umodbus.const.ERROR_BIAS = <Mock name='mock.const()' id='140345175704576'>
Error code offset
- umodbus.const.ERROR_RESP_LEN = <Mock name='mock.const()' id='140345175704576'>
Error response length
- umodbus.const.FIXED_RESP_LEN = <Mock name='mock.const()' id='140345175704576'>
Fixed response length
- umodbus.const.GET_COM_EVENT_COUNTER = <Mock name='mock.const()' id='140345175704576'>
Get status word and an event count from the remote device com event counter
- umodbus.const.GET_COM_EVENT_LOG = <Mock name='mock.const()' id='140345175704576'>
Get a status word, event count, message count, and a field of event bytes
- umodbus.const.ILLEGAL_DATA_ADDRESS = <Mock name='mock.const()' id='140345175704576'>
Data address received in query is not an allowable address for the server
- umodbus.const.ILLEGAL_DATA_VALUE = <Mock name='mock.const()' id='140345175704576'>
An unrecoverable error occurred while the server was attempting to perform the requested action
- umodbus.const.ILLEGAL_FUNCTION = <Mock name='mock.const()' id='140345175704576'>
Function code received in query is not an allowable action for the server
- umodbus.const.MASK_WRITE_REGISTER = <Mock name='mock.const()' id='140345175704576'>
Perform a combination of one read operation and one write operation in a single MODBUS transaction
- umodbus.const.MBAP_HDR_LENGTH = <Mock name='mock.const()' id='140345175704576'>
Modbus Application Protocol High Data Response length
- umodbus.const.MEMORY_PARITY_ERROR = <Mock name='mock.const()' id='140345175704576'>
Gateway was unable to allocate an internal communication path from the input port to the output port for processing the request
- umodbus.const.READ_COILS = <Mock name='mock.const()' id='140345175704576'>
Read contiguous status of coils
- umodbus.const.READ_DEVICE_IDENTIFICATION = <Mock name='mock.const()' id='140345175704576'>
Encapsulated Interface Transport
- umodbus.const.READ_DISCRETE_INPUTS = <Mock name='mock.const()' id='140345175704576'>
Read contiguous status of discrete inputs
- umodbus.const.READ_EXCEPTION_STATUS = <Mock name='mock.const()' id='140345175704576'>
Read the contents of eight Exception Status outputs
- umodbus.const.READ_FIFO_QUEUE = <Mock name='mock.const()' id='140345175704576'>
Read the contents of a First-In-First-Out (FIFO) queue of register
- umodbus.const.READ_FILE_RECORD = <Mock name='mock.const()' id='140345175704576'>
Perform a file record read
- umodbus.const.READ_HOLDING_REGISTERS = <Mock name='mock.const()' id='140345175704576'>
Read the contents of a contiguous block of holding registers
- umodbus.const.READ_INPUT_REGISTER = <Mock name='mock.const()' id='140345175704576'>
Read contiguous input registers
- umodbus.const.REPORT_SERVER_ID = <Mock name='mock.const()' id='140345175704576'>
Read the description of the type, the current status, and other informations
- umodbus.const.RESPONSE_HDR_LENGTH = <Mock name='mock.const()' id='140345175704576'>
High Data Response length
- umodbus.const.SERVER_DEVICE_BUSY = <Mock name='mock.const()' id='140345175704576'>
Server is engaged in processing a long duration program command
- umodbus.const.WRITE_FILE_RECORD = <Mock name='mock.const()' id='140345175704576'>
Perform a file record write
- umodbus.const.WRITE_MULTIPLE_COILS = <Mock name='mock.const()' id='140345175704576'>
Force each coil in a sequence of coils to either ON or OFF
- umodbus.const.WRITE_MULTIPLE_REGISTERS = <Mock name='mock.const()' id='140345175704576'>
Modify the contents of a specified holding register using a combination of an AND mask, an OR mask, and the register’s current contents
- umodbus.const.WRITE_SINGLE_COIL = <Mock name='mock.const()' id='140345175704576'>
Write a single coil output status to ON or OFF
- umodbus.const.WRITE_SINGLE_REGISTER = <Mock name='mock.const()' id='140345175704576'>
Write a single holding register
Common module
- class umodbus.common.CommonModbusFunctions[source]
Bases:
object
Common Modbus functions
- read_discrete_inputs(slave_addr: int, starting_addr: int, input_qty: int) None [source]
Read discrete inputs (ISTS).
- read_holding_registers(slave_addr: int, starting_addr: int, register_qty: int, signed: bool = True) None [source]
Read holding registers (HREGS).
- read_input_registers(slave_addr: int, starting_addr: int, register_qty: int, signed: bool = True) None [source]
Read input registers (IREGS).
- write_multiple_coils(slave_addr: int, starting_address: int, output_values: None) bool [source]
Update multiple coils.
- write_multiple_registers(slave_addr: int, starting_address: int, register_values: None, signed: bool = True) bool [source]
Update multiple registers.
- write_single_coil(slave_addr: int, output_address: int, output_value: None) bool [source]
Update a single coil.
- exception umodbus.common.ModbusException(function_code: int, exception_code: int)[source]
Bases:
Exception
Exception for signaling modbus errors
Common functions
- umodbus.functions.bin_to_float(binary: str) float [source]
Convert binary string to floating point value
- umodbus.functions.bytes_to_bool(byte_list: bytes, bit_qty: None = 1) None [source]
Convert bytes to list of boolean values
- umodbus.functions.exception_response(function_code: int, exception_code: int) bytes [source]
Create Modbus exception response
- umodbus.functions.float_to_bin(num: float) bin [source]
Convert floating point value to binary
See IEEE 754
- Parameters
num (float) – The number
- Returns
Binary representation
- Return type
bin
- umodbus.functions.read_coils(starting_address: int, quantity: int) bytes [source]
Create Modbus Protocol Data Unit for reading coils.
- umodbus.functions.read_discrete_inputs(starting_address: int, quantity: int) bytes [source]
Create Modbus Protocol Data Unit for reading discrete inputs.
- umodbus.functions.read_holding_registers(starting_address: int, quantity: int) bytes [source]
Create Modbus Protocol Data Unit for reading holding registers.
- umodbus.functions.read_input_registers(starting_address: int, quantity: int) bytes [source]
Create Modbus Protocol Data Unit for reading input registers.
- umodbus.functions.response(function_code: int, request_register_addr: int, request_register_qty: int, request_data: list, value_list: None = None, signed: bool = True) bytes [source]
Construct a Modbus response Protocol Data Unit
- Parameters
- Returns
Protocol data unit
- Return type
- umodbus.functions.to_short(byte_array: bytes, signed: bool = True) bytes [source]
Convert bytes to tuple of integer values
- umodbus.functions.validate_resp_data(data: bytes, function_code: int, address: int, value: Optional[int] = None, quantity: Optional[int] = None, signed: bool = True) bool [source]
Validate the response data.
- umodbus.functions.write_multiple_coils(starting_address: int, value_list: None) bytes [source]
Create Modbus message to update multiple coils
- umodbus.functions.write_multiple_registers(starting_address: int, register_values: None, signed: bool = True) bytes [source]
Create Modbus message to update multiple coils
- umodbus.functions.write_single_coil(output_address: int, output_value: None) bytes [source]
Create Modbus message to update single coil
Modbus client module
Modbus register abstraction class
Used to add, remove, set and get values or states of a register or coil. Additional helper properties and functions like getters for changed registers are available as well.
This class is inherited by the Modbus client implementations
umodbus.serial.ModbusRTU
and umodbus.tcp.ModbusTCP
- class umodbus.modbus.Modbus(itf, addr_list: None)[source]
Bases:
object
Modbus register abstraction
- Parameters
itf (Callable) – Abstraction interface
addr_list (List[int]) – List of addresses
- _check_valid_register(reg_type: str) bool [source]
Check register type to be a valid modbus register
- _get_reg_in_dict(reg_type: str, address: int) None [source]
Get the register value from the dictionary of registers.
- _get_regs_of_dict(reg_type: str) dict_keys [source]
Get all configured registers of specified register type.
- _process_read_access(request: Request, reg_type: str) None [source]
Process read access to register
- _process_write_access(request: Request, reg_type: str) None [source]
Process write access to register
- _remove_changed_register(reg_type: str, address: int, timestamp: int) bool [source]
Remove the register from the dictionary of changed registers.
- _remove_reg_from_dict(reg_type: str, address: int) None [source]
Remove the register from the dictionary of registers.
- _set_changed_register(reg_type: str, address: int, value: None) None [source]
Set the register value in the dictionary of changed registers.
- _set_reg_in_dict(reg_type: str, address: int, value: None, on_set_cb: None = None, on_get_cb: None = None) None [source]
Set the register value in the dictionary of registers.
- Parameters
reg_type (str) – The register type
address (int) – The address (ID) of the register
value (Union[bool, int, List[bool], List[int]]) – The value(s) of the register(s)
on_set_cb – Callback on setting the register
on_get_cb (Callable[ [str, int, Union[List[bool], List[int]]], None ]) – Callback on getting the register
- Raises
KeyError – No register at specified address found
- _set_single_reg_in_dict(reg_type: str, address: int, value: None, on_set_cb: None = None, on_get_cb: None = None) None [source]
Set a register value in the dictionary of registers.
- add_coil(address: int, value: None = False, on_set_cb: None = None, on_get_cb: None = None) None [source]
Add a coil to the modbus register dictionary.
- Parameters
address (int) – The address (ID) of the register
value (Union[bool, List[bool]], optional) – The default value
on_set_cb (Callable[ [str, int, Union[List[bool], List[int]]], None ]) – Callback on setting the coil
on_get_cb (Callable[ [str, int, Union[List[bool], List[int]]], None ]) – Callback on getting the coil
- add_hreg(address: int, value: None = 0, on_set_cb: None = None, on_get_cb: None = None) None [source]
Add a holding register to the modbus register dictionary.
- Parameters
- add_ireg(address: int, value: None = 0, on_get_cb: None = None) None [source]
Add an input register to the modbus register dictionary.
- add_ist(address: int, value: None = False, on_get_cb: None = None) None [source]
Add a discrete input register to the modbus register dictionary.
- property changed_coils: dict
Get the changed coil registers.
- Returns
The changed coil registers.
- Return type
- property changed_hregs: dict
Get the changed holding registers.
- Returns
The changed holding registers.
- Return type
- property changed_registers: dict
Get the changed registers.
- Returns
The changed registers.
- Return type
- property coils: dict_keys
Get the configured coils.
- Returns
The dictionary keys.
- Return type
dict_keys
- property hregs: dict_keys
Get the configured holding registers.
- Returns
The dictionary keys.
- Return type
dict_keys
- property iregs: dict_keys
Get the configured input registers.
- Returns
The dictionary keys.
- Return type
dict_keys
- property ists: dict_keys
Get the configured discrete input registers.
- Returns
The dictionary keys.
- Return type
dict_keys
- process() bool [source]
Process the Modbus requests.
- Returns
Result of processing, True on success, False otherwise
- Return type
- remove_hreg(address: int) None [source]
Remove a holding register from the modbus register dictionary.
- remove_ireg(address: int) None [source]
Remove an input register from the modbus register dictionary.
- remove_ist(address: int) None [source]
Remove a discrete input register from the modbus register dictionary.
Serial
- class umodbus.serial.ModbusRTU(addr: int, baudrate: int = 9600, data_bits: int = 8, stop_bits: int = 1, parity: None = None, pins: None = None, ctrl_pin: Optional[int] = None, uart_id: int = 1)[source]
Bases:
Modbus
Modbus RTU client class
- Parameters
addr (int) – The address of this device on the bus
baudrate (int) – The baudrate, default 9600
data_bits (int) – The data bits, default 8
stop_bits (int) – The stop bits, default 1
parity (Optional[int]) – The parity, default None
pins (List[Union[int, Pin], Union[int, Pin]]) – The pins as list [TX, RX]
ctrl_pin (int) – The control pin
uart_id (int) – The ID of the used UART
- class umodbus.serial.Serial(uart_id: int = 1, baudrate: int = 9600, data_bits: int = 8, stop_bits: int = 1, parity=None, pins: None = None, ctrl_pin: Optional[int] = None)[source]
Bases:
CommonModbusFunctions
- _send(modbus_pdu: bytes, slave_addr: int) None [source]
Send Modbus frame via UART
If a flow control pin has been setup, it will be controlled accordingly
- _send_receive(modbus_pdu: bytes, slave_addr: int, count: bool) bytes [source]
Send a modbus message and receive the reponse.
- _uart_read() bytearray [source]
Read incoming slave response from UART
- Returns
Read content
- Return type
- _validate_resp_hdr(response: bytearray, slave_addr: int, function_code: int, count: bool) bytes [source]
Validate the response header.
- get_request(unit_addr_list: None, timeout: None = None) None [source]
Check for request within the specified timeout
- send_exception_response(slave_addr: int, function_code: int, exception_code: int) None [source]
Send an exception response to a client.
- send_response(slave_addr: int, function_code: int, request_register_addr: int, request_register_qty: int, request_data: list, values: None = None, signed: bool = True) None [source]
Send a response to a client.
- Parameters
slave_addr (int) – The slave address
function_code (int) – The function code
request_register_addr (int) – The request register address
request_register_qty (int) – The request register qty
request_data (list) – The request data
values (Optional[list]) – The values
signed (bool) – Indicates if signed
TCP
- class umodbus.tcp.ModbusTCP[source]
Bases:
Modbus
Modbus TCP client class
- class umodbus.tcp.TCP(slave_ip: str, slave_port: int = 502, timeout: float = 5.0)[source]
Bases:
CommonModbusFunctions
TCP class handling socket connections and parsing the Modbus data
- Parameters
- _send_receive(slave_addr: int, modbus_pdu: bytes, count: bool) bytes [source]
Send a modbus message and receive the reponse.
- class umodbus.tcp.TCPServer[source]
Bases:
object
Modbus TCP host class
- _accept_request(accept_timeout: float, unit_addr_list: list) None [source]
Accept, read and decode a socket based request
- bind(local_ip: str, local_port: int = 502, max_connections: int = 10)[source]
Bind IP and port for incomming requests
- get_is_bound() bool [source]
Get the IP and port binding status, legacy support.
- Returns
True if bound to IP and port, False otherwise
- Return type
- get_request(unit_addr_list: None = None, timeout: Optional[int] = None) None [source]
Check for request within the specified timeout
- property is_bound: bool
Get the IP and port binding status
- Returns
True if bound to IP and port, False otherwise
- Return type
- send_exception_response(slave_addr: int, function_code: int, exception_code: int) None [source]
Send an exception response to a client.
- send_response(slave_addr: int, function_code: int, request_register_addr: int, request_register_qty: int, request_data: list, values: None = None, signed: bool = True) None [source]
Send a response to a client.
- Parameters
slave_addr (int) – The slave address
function_code (int) – The function code
request_register_addr (int) – The request register address
request_register_qty (int) – The request register qty
request_data (list) – The request data
values (Optional[list]) – The values
signed (bool) – Indicates if signed