# `factory` — Request/Response Decoders

## API Documentation

### Modbus Request/Response Decoder Factories

The following factories make it easy to decode request/response messages. To add a new request/response pair to be decodeable by the library, simply add them to the respective function lookup table (order doesn’t matter, but it does help keep things organized).

Regardless of how many functions are added to the lookup, O(1) behavior is kept as a result of a pre-computed lookup dictionary.

class `pymodbus.factory.``ServerDecoder`

  Request Message Factory (Server)

  To add more implemented functions, simply add them to the list

  `__init__`()

    Initializes the client lookup tables

  `decode`(message)

    Wrapper to decode a request packet

    Parameters: | message – The raw modbus request packet
    Returns: | The decoded modbus message or None if error

  `lookupPduClass`(function_code)

    Use function_code to determine the class of the PDU.

    Parameters: | function_code – The function code specified in a frame.
    Returns: | The class of the PDU that has a matching function_code.

  `_helper`(data)

    This factory is used to generate the correct request object from a valid request packet. This decodes from a list of the currently implemented request types.

    Parameters: | data – The request packet to decode
    Returns: | The decoded request or illegal function request object

class `pymodbus.factory.``ClientDecoder`

  Response Message Factory (Client)

  To add more implemented functions, simply add them to the list

  `__init__`()

    Initializes the client lookup tables

  `lookupPduClass`(function_code)

    Use function_code to determine the class of the PDU.

    Parameters: | function_code – The function code specified in a frame.
    Returns: | The class of the PDU that has a matching function_code.

  `decode`(message)

    Wrapper to decode a response packet

    Parameters: | message – The raw packet to decode
    Returns: | The decoded modbus message or None if error

  `_helper`(data)

    This factory is used to generate the correct response object from a valid response packet. This decodes from a list of the currently implemented request types.

    Parameters: | data – The response packet to decode
    Returns: | The decoded request or an exception response object