class tinymodbusrtu.TinyModbusRtu(serial_connection: Serial = None, crc_enabled: bool = True, timeout: float = 0.5)[source]

TinyModbusRtu Class for communicating with Modbus RTU

Supports Modbus RTU Protocol over Serial ONLY.

NOT SUPPORTED: ASCII Protocol, Modbus over TCP

Allows the following messages:

Standard Modbus RTU Read requests, Standard Modbus RTU Write requests, Fully custom Modbus RTU requests, Custom function code trigger requests

Allows Optionally Disabling the CRC16 Validity check for Hobbyist Development

Note: Disables CRC16 Check for both requests and responses

See Official Modbus Documentation for RTU request formatting:

https://www.modbustools.com/modbus.html

send(message: RtuMessage) None[source]

Send Byte Message on underlying serial connection

Parameters:

message – message to send

listen() RtuMessage[source]

Listen for incoming messages Terminates upon receiving a complete message, must call listen() again to receive another message

Returns:

message received

property crc_enabled: bool
class tinymodbusrtu.TinyModbusClient(serial_connection: Serial = None, crc_enabled: bool = True, timeout: float = 0.5)[source]

Client Object for communicating with one or more MODBUS RTU Servers

read_coils(server_id: int, address: int, count: int) bytes[source]

Sends Modbus RTU request to read server coils

Parameters:
  • server_id – Intended recipient

  • address – Starting coil address

  • count – Count of coils to read

Returns:

Data of response

read_discrete_inputs(server_id: int, address: int, count: int) bytes[source]

Sends Modbus RTU request to read server discrete inputs

Parameters:
  • server_id – Intended recipient

  • address – Starting discrete input address

  • count – Count of disrete inputs to read

Returns:

Data of response

read_holding_registers(server_id: int, address: int, count: int) bytes[source]

Sends Modbus RTU request to read server holding registers

Parameters:
  • server_id – Intended recipient

  • address – Starting holding register address

  • count – Count of holding registers to read

Returns:

Data of response

read_input_registers(server_id: int, address: int, count: int) bytes[source]

Sends Modbus RTU request to read server input registers

Parameters:
  • server_id – Intended recipient

  • address – Starting input register address

  • count – Count of input registers to read

Returns:

Data of response

write_single_coil(server_id: int, address: int, coil_status: int) bool[source]

Sends Modbus RTU request to write a single coil to “ON”(65280) or “OFF”(0) Recommend using module attributes COIL_ON and COIL_OFF

Parameters:
  • server_id – Intended recipient

  • address – Coil Address to be written

  • coil_status – Coil Status to be set

Returns:

True if coil written successfully, False otherwise

write_single_register(server_id: int, address: int, value: int) bool[source]

Sends Modbus RTU request to write a single register to a given value

Parameters:
  • server_id – Intended recipient

  • address – Register Address to Write

  • value – Value to be written to Register

Returns:

True if register written successfully, False otherwise

write_multiple_coils(server_id: int, starting_address: int, values: list[bool]) bool[source]

Sends Modbus RTU request to write multiple coils to ‘ON’ or ‘OFF from a given list

Not Supported in this version of TinyModbusRtu

Parameters:
  • server_id – Intended recipient

  • starting_address – Starting Address of Coils to Write

  • values – List of values to write to Coils in Order

Returns:

True if write operation is successful, False otherwise

write_multiple_registers(server_id: int, starting_address: int, values: list[int]) bool[source]

Sends Modbus RTU request to write multiple registers to values in a given list

Parameters:
  • server_id – Intended recipient

  • starting_address – Starting Address of Registers to Write

  • values – List of values to write to Registers in Order

Returns:

True if write operation is successful, False otherwise

send_function_code(server_id: int, function_code: int) bytes[source]

Send only a custom function code to a server

Parameters:
  • server_id – Intended recipient

  • function_code – Custom function code

send_custom_message(server_id: int, function_code: int, data_bytes: bytes) bytes[source]

Send a fully custom message to a server

Parameters:
  • server_id – Intended recipient

  • function_code – Custom function code

  • data_bytes – Custom message contents

property crc_enabled: bool
listen() RtuMessage

Listen for incoming messages Terminates upon receiving a complete message, must call listen() again to receive another message

Returns:

message received

send(message: RtuMessage) None

Send Byte Message on underlying serial connection

Parameters:

message – message to send

class tinymodbusrtu.TinyModbusServer(serial_connection=None, server_id=0, crc_enabled=True)[source]
listen() RtuMessage[source]

Listen for incoming messages Terminates upon receiving a complete message, must call listen() again to receive another message

Returns:

message received

send(message: RtuMessage) None

Send Byte Message on underlying serial connection

Parameters:

message – message to send

class tinymodbusrtu.RtuMessage(server_id: int = None, function_code: int = None, data_bytes: bytes = b'', crc_bytes: bytes = b'')[source]

Initiate an RtuMessage object

Parameters:
  • server_id – Server Id of intended message recipient

  • function_code – Function Code of action to be performed

  • data_bytes – Raw data bytes of message

  • crc_bytes – Bytes for crc16 integrity check

decode(message: bytes, crc_enabled: bool) None[source]

Decode a raw message string to the applicable parts

Parameters:
  • message – Bytes message to decode

  • crc_enabled – Whether the message contains crc_bytes

encode(crc_enabled: bool) bytes[source]

Encodes Message object into serial writable bytes

Returns:

Serial writable message

set_crc(crc_bytes: bytes) None[source]

Set the CRC16 byte value

Parameters:

crc_bytes – new crc bytes to append to message

property server_id: int
property function_code: int
property data_bytes: bytes
property crc_bytes: bytes
property length: int
class tinymodbusrtu.RtuRequest(server_id: int = None, function_code: int = None, address: int = None, count: int = None, value: int = None, crc_bytes: bytes = b'')[source]

Rtu Request Object

Parameters:
  • server_id – Intended Recipient’s Server Id

  • function_code – Function Code of Request

  • address – Address to Start Reading OR Address to Write

  • count – Count of Registers to Read, Do Not Provide for Write Requests

  • value – Value to Write to Given Address, Do Not Provide for Read Requests

  • crc_bytes – crc16 Value of Request

property address: int
property count: int
property crc_bytes: bytes
property data_bytes: bytes
decode(message: bytes, crc_enabled: bool) None

Decode a raw message string to the applicable parts

Parameters:
  • message – Bytes message to decode

  • crc_enabled – Whether the message contains crc_bytes

encode(crc_enabled: bool) bytes

Encodes Message object into serial writable bytes

Returns:

Serial writable message

property function_code: int
property length: int
property server_id: int
set_crc(crc_bytes: bytes) None

Set the CRC16 byte value

Parameters:

crc_bytes – new crc bytes to append to message

property value: int
class tinymodbusrtu.RtuResponse(server_id: int = None, function_code: int = None, byte_count: int = None, response_data: list[int] = None, crc_bytes: bytes = b'')[source]

Rtu Response Object

Parameters:
  • server_id

  • function_code

  • byte_count

  • response_data

Crc_bytes:

property crc_bytes: bytes
property data_bytes: bytes
decode(message: bytes, crc_enabled: bool) None

Decode a raw message string to the applicable parts

Parameters:
  • message – Bytes message to decode

  • crc_enabled – Whether the message contains crc_bytes

encode(crc_enabled: bool) bytes

Encodes Message object into serial writable bytes

Returns:

Serial writable message

property function_code: int
property length: int
property server_id: int
set_crc(crc_bytes: bytes) None

Set the CRC16 byte value

Parameters:

crc_bytes – new crc bytes to append to message