# Modbus Payload Server Example

    #!/usr/bin/env python
    """
    Pymodbus Server Payload Example
    --------------------------------------------------------------------------

    If you want to initialize a server context with a complicated memory
    layout, you can actually use the payload builder.
    """
    # --------------------------------------------------------------------------- #
    # import the various server implementations
    # --------------------------------------------------------------------------- #
    from pymodbus.server.sync import StartTcpServer

    from pymodbus.device import ModbusDeviceIdentification
    from pymodbus.datastore import ModbusSequentialDataBlock
    from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext

    # --------------------------------------------------------------------------- #
    # import the payload builder
    # --------------------------------------------------------------------------- #

    from pymodbus.constants import Endian
    from pymodbus.payload import BinaryPayloadDecoder
    from pymodbus.payload import BinaryPayloadBuilder

    # --------------------------------------------------------------------------- #
    # configure the service logging
    # --------------------------------------------------------------------------- #
    import logging
    FORMAT = ('%(asctime)-15s %(threadName)-15s'
              ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
    logging.basicConfig(format=FORMAT)
    log = logging.getLogger()
    log.setLevel(logging.DEBUG)

    def run_payload_server():
        # ----------------------------------------------------------------------- #
        # build your payload
        # ----------------------------------------------------------------------- #
        builder = BinaryPayloadBuilder(byteorder=Endian.Little,
                                       wordorder=Endian.Little)
        builder.add_string('abcdefgh')
        builder.add_bits([0, 1, 0, 1, 1, 0, 1, 0])
        builder.add_8bit_int(-0x12)
        builder.add_8bit_uint(0x12)
        builder.add_16bit_int(-0x5678)
        builder.add_16bit_uint(0x1234)
        builder.add_32bit_int(-0x1234)
        builder.add_32bit_uint(0x12345678)
        builder.add_32bit_float(22.34)
        builder.add_32bit_float(-22.34)
        builder.add_64bit_int(-0xDEADBEEF)
        builder.add_64bit_uint(0x12345678DEADBEEF)
        builder.add_64bit_uint(0xDEADBEEFDEADBEED)
        builder.add_64bit_float(123.45)
        builder.add_64bit_float(-123.45)

        # ----------------------------------------------------------------------- #
        # use that payload in the data store
        # ----------------------------------------------------------------------- #
        # Here we use the same reference block for each underlying store.
        # ----------------------------------------------------------------------- #

        block = ModbusSequentialDataBlock(1, builder.to_registers())
        store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block)
        context = ModbusServerContext(slaves=store, single=True)

        # ----------------------------------------------------------------------- #
        # initialize the server information
        # ----------------------------------------------------------------------- #
        # If you don't set this or any fields, they are defaulted to empty strings.
        # ----------------------------------------------------------------------- #
        identity = ModbusDeviceIdentification()
        identity.VendorName = 'Pymodbus'
        identity.ProductCode = 'PM'
        identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
        identity.ProductName = 'Pymodbus Server'
        identity.ModelName = 'Pymodbus Server'
        identity.MajorMinorRevision = '1.5'
        # ----------------------------------------------------------------------- #
        # run the server you want
        # ----------------------------------------------------------------------- #
        StartTcpServer(context, identity=identity, address=("localhost", 5020))

    if __name__ == "__main__":
        run_payload_server()