# `server.sync` — Twisted Synchronous Modbus Server

## API Documentation

### Implementation of a Threaded Modbus Server

class `pymodbus.server.sync.``ModbusBaseRequestHandler`(request, client_address, server)

  Implements the modbus server protocol

  This uses the socketserver.BaseRequestHandler to implement the client handler.

  `setup`()

    Callback for when a client connects

  `finish`()

    Callback for when a client disconnects

  `execute`(request)

    The callback to call with the resulting message

    Parameters: | request – The decoded request message

  `handle`()

    Callback when we receive any data

  `send`(message)

    Send a request (string) to the network

    Parameters: | message – The unencoded modbus response

class `pymodbus.server.sync.``ModbusSingleRequestHandler`(request, client_address, server)

  Implements the modbus server protocol

  This uses the socketserver.BaseRequestHandler to implement the client handler for a single client(serial clients)

  `handle`()

    Callback when we receive any data

  `send`(message)

    Send a request (string) to the network

    Parameters: | message – The unencoded modbus response

class `pymodbus.server.sync.``ModbusConnectedRequestHandler`(request, client_address, server)

  Implements the modbus server protocol

  This uses the socketserver.BaseRequestHandler to implement the client handler for a connected protocol (TCP).

  `handle`()

    Callback when we receive any data, until self.running becomes not True. Blocks indefinitely awaiting data. If shutdown is required, then the global socket.settimeout(<seconds>) may be used, to allow timely checking of self.running. However, since this also affects socket connects, if there are outgoing socket connections used in the same program, then these will be prevented, if the specfied timeout is too short. Hence, this is unreliable.

    To respond to Modbus…Server.server_close() (which clears each handler’s self.running), derive from this class to provide an alternative handler that awakens from time to time when no input is available and checks self.running. Use Modbus…Server( handler=… ) keyword to supply the alternative request handler class.

  `send`(message)

    Send a request (string) to the network

    Parameters: | message – The unencoded modbus response

class `pymodbus.server.sync.``ModbusDisconnectedRequestHandler`(request, client_address, server)

  Implements the modbus server protocol

  This uses the socketserver.BaseRequestHandler to implement the client handler for a disconnected protocol (UDP). The only difference is that we have to specify who to send the resulting packet data to.

  `handle`()

    Callback when we receive any data

  `send`(message)

    Send a request (string) to the network

    Parameters: | message – The unencoded modbus response

class `pymodbus.server.sync.``ModbusTcpServer`(context, framer=None, identity=None, address=None, handler=None, **kwargs)

  A modbus threaded tcp socket server

  We inherit and overload the socket server so that we can control the client threads as well as have a single server context instance.

  `__init__`(context, framer=None, identity=None, address=None, handler=None, **kwargs)

    Overloaded initializer for the socket server

    If the identify structure is not passed in, the ModbusControlBlock uses its own empty structure.

    Parameters:

    - context – The ModbusServerContext datastore
    - framer – The framer strategy to use
    - identity – An optional identify structure
    - address – An optional (interface, port) to bind to.
    - handler – A handler for each client session; default is ModbusConnectedRequestHandler
    - ignore_missing_slaves – True to not send errors on a request to a missing slave

  `process_request`(request, client)

    Callback for connecting a new client thread

    Parameters:

    - request – The request to handle
    - client – The address of the client

  `shutdown`()

    Stops the serve_forever loop.

    Overridden to signal handlers to stop.

  `server_close`()

    Callback for stopping the running server

class `pymodbus.server.sync.``ModbusUdpServer`(context, framer=None, identity=None, address=None, handler=None, **kwargs)

  A modbus threaded udp socket server

  We inherit and overload the socket server so that we can control the client threads as well as have a single server context instance.

  `__init__`(context, framer=None, identity=None, address=None, handler=None, **kwargs)

    Overloaded initializer for the socket server

    If the identify structure is not passed in, the ModbusControlBlock uses its own empty structure.

    Parameters:

    - context – The ModbusServerContext datastore
    - framer – The framer strategy to use
    - identity – An optional identify structure
    - address – An optional (interface, port) to bind to.
    - handler – A handler for each client session; default is ModbusDisonnectedRequestHandler
    - ignore_missing_slaves – True to not send errors on a request to a missing slave

  `process_request`(request, client)

    Callback for connecting a new client thread

    Parameters:

    - request – The request to handle
    - client – The address of the client

  `server_close`()

    Callback for stopping the running server

class `pymodbus.server.sync.``ModbusSerialServer`(context, framer=None, identity=None, **kwargs)

  A modbus threaded serial socket server

  We inherit and overload the socket server so that we can control the client threads as well as have a single server context instance.

  `__init__`(context, framer=None, identity=None, **kwargs)

    Overloaded initializer for the socket server

    If the identify structure is not passed in, the ModbusControlBlock uses its own empty structure.

    Parameters:

    - context – The ModbusServerContext datastore
    - framer – The framer strategy to use
    - identity – An optional identify structure
    - port – The serial port to attach to
    - stopbits – The number of stop bits to use
    - bytesize – The bytesize of the serial messages
    - parity – Which kind of parity to use
    - baudrate – The baud rate to use for the serial device
    - timeout – The timeout to use for the serial device
    - ignore_missing_slaves – True to not send errors on a request to a missing slave

  `_connect`()

    Connect to the serial server

    Returns: | True if connection succeeded, False otherwise

  `_build_handler`()
    A helper method to create and monkeypatch
      a serial handler.

    Returns: | A patched handler

  `serve_forever`()

    Callback for connecting a new client thread

    Parameters:

    - request – The request to handle
    - client – The address of the client

  `server_close`()

    Callback for stopping the running server

`pymodbus.server.sync.``StartTcpServer`(context=None, identity=None, address=None, **kwargs)

  A factory to start and run a tcp modbus server

  Parameters:

  - context – The ModbusServerContext datastore
  - identity – An optional identify structure
  - address – An optional (interface, port) to bind to.
  - ignore_missing_slaves – True to not send errors on a request to a missing slave

`pymodbus.server.sync.``StartUdpServer`(context=None, identity=None, address=None, **kwargs)

  A factory to start and run a udp modbus server

  Parameters:

  - context – The ModbusServerContext datastore
  - identity – An optional identify structure
  - address – An optional (interface, port) to bind to.
  - framer – The framer to operate with (default ModbusSocketFramer)
  - ignore_missing_slaves – True to not send errors on a request to a missing slave

`pymodbus.server.sync.``StartSerialServer`(context=None, identity=None, **kwargs)

  A factory to start and run a serial modbus server

  Parameters:

  - context – The ModbusServerContext datastore
  - identity – An optional identify structure
  - framer – The framer to operate with (default ModbusAsciiFramer)
  - port – The serial port to attach to
  - stopbits – The number of stop bits to use
  - bytesize – The bytesize of the serial messages
  - parity – Which kind of parity to use
  - baudrate – The baud rate to use for the serial device
  - timeout – The timeout to use for the serial device
  - ignore_missing_slaves – True to not send errors on a request to a missing slave