# `pdu` — Base Structures

## API Documentation

Contains base classes for modbus request/response/error packets

class `pymodbus.pdu.``ModbusPDU`(**kwargs)

  Base class for all Modbus mesages

  `transaction_id`

    This value is used to uniquely identify a request response pair. It can be implemented as a simple counter

  `protocol_id`

    This is a constant set at 0 to indicate Modbus. It is put here for ease of expansion.

  `unit_id`

    This is used to route the request to the correct child. In the TCP modbus, it is used for routing (or not used at all. However, for the serial versions, it is used to specify which child to perform the requests against. The value 0x00 represents the broadcast address (also 0xff).

  `check`

    This is used for LRC/CRC in the serial modbus protocols

  `skip_encode`

    This is used when the message payload has already been encoded. Generally this will occur when the PayloadBuilder is being used to create a complicated message. By setting this to True, the request will pass the currently encoded message through instead of encoding it again.

  `__init__`(**kwargs)

    Initializes the base data for a modbus request

  `encode`()

    Encodes the message

    Raises: | A not implemented exception

  `decode`(data)

    Decodes data part of the message.

    Parameters: | data – is a string object
    Raises: | A not implemented exception

  classmethod `calculateRtuFrameSize`(buffer)

    Calculates the size of a PDU.

    Parameters: | buffer – A buffer containing the data that have been received.
    Returns: | The number of bytes in the PDU.

class `pymodbus.pdu.``ModbusRequest`(**kwargs)

  Base class for a modbus request PDU

  `__init__`(**kwargs)

    Proxy to the lower level initializer

  `doException`(exception)

    Builds an error response based on the function

    Parameters: | exception – The exception to return
    Raises: | An exception response

class `pymodbus.pdu.``ModbusResponse`(**kwargs)

  Base class for a modbus response PDU

  `should_respond`

    A flag that indicates if this response returns a result back to the client issuing the request

  `_rtu_frame_size`

    Indicates the size of the modbus rtu response used for calculating how much to read.

  `__init__`(**kwargs)

    Proxy to the lower level initializer

class `pymodbus.pdu.``ModbusExceptions`

  An enumeration of the valid modbus exceptions

  classmethod `decode`(code)

    Given an error code, translate it to a string error name.

    Parameters: | code – The code number to translate

class `pymodbus.pdu.``ExceptionResponse`(function_code, exception_code=None, **kwargs)

  Base class for a modbus exception PDU

  `__init__`(function_code, exception_code=None, **kwargs)

    Initializes the modbus exception response

    Parameters:

    - function_code – The function to build an exception response for
    - exception_code – The specific modbus exception to return

  `encode`()

    Encodes a modbus exception response

    Returns: | The encoded exception packet

  `decode`(data)

    Decodes a modbus exception response

    Parameters: | data – The packet data to decode

  `__str__`()

    Builds a representation of an exception response

    Returns: | The string representation of an exception response

class `pymodbus.pdu.``IllegalFunctionRequest`(function_code, **kwargs)

  Defines the Modbus slave exception type ‘Illegal Function’ This exception code is returned if the slave:

      - does not implement the function code **or**
      - is not in a state that allows it to process the function

  `__init__`(function_code, **kwargs)

    Initializes a IllegalFunctionRequest

    Parameters: | function_code – The function we are erroring on

  `decode`(data)

    This is here so this failure will run correctly

    Parameters: | data – Not used

  `execute`(context)

    Builds an illegal function request error response

    Parameters: | context – The current context for the message
    Returns: | The error response packet