Beispiel #1
0
    def run_server(self):

        # ----------------------------------------------------------------------- #
        # initialize your data store
        # ----------------------------------------------------------------------- #
        # The datastores only respond to the addresses that they are initialized to
        # Therefore, if you initialize a DataBlock to addresses of 0x00 to 0xFF, a
        # request to 0x100 will respond with an invalid address exception. This is
        # because many devices exhibit this kind of behavior (but not all)::
        #
        #     block = ModbusSequentialDataBlock(0x00, [0]*0xff)
        #
        # Continuing, you can choose to use a sequential or a sparse DataBlock in
        # your data context.  The difference is that the sequential has no gaps in
        # the data while the sparse can. Once again, there are devices that exhibit
        # both forms of behavior::
        #
        #     block = ModbusSparseDataBlock({0x00: 0, 0x05: 1})
        #     block = ModbusSequentialDataBlock(0x00, [0]*5)
        #
        # Alternately, you can use the factory methods to initialize the DataBlocks
        # or simply do not pass them to have them initialized to 0x00 on the full
        # address range::
        #
        #     store = ModbusSlaveContext(di = ModbusSequentialDataBlock.create())
        #     store = ModbusSlaveContext()
        #
        # Finally, you are allowed to use the same DataBlock reference for every
        # table or you may use a separate DataBlock for each table.
        # This depends if you would like functions to be able to access and modify
        # the same data or not::
        #
        #     block = ModbusSequentialDataBlock(0x00, [0]*0xff)
        #     store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block)
        #
        # The server then makes use of a server context that allows the server to
        # respond with different slave contexts for different unit ids. By default
        # it will return the same context for every unit id supplied (broadcast
        # mode).
        # However, this can be overloaded by setting the single flag to False and
        # then supplying a dictionary of unit id to context mapping::
        #
        #     slaves  = {
        #         0x01: ModbusSlaveContext(...),
        #         0x02: ModbusSlaveContext(...),
        #         0x03: ModbusSlaveContext(...),
        #     }
        #     context = ModbusServerContext(slaves=slaves, single=False)
        #
        # The slave context can also be initialized in zero_mode which means that a
        # request to address(0-7) will map to the address (0-7). The default is
        # False which is based on section 4.4 of the specification, so address(0-7)
        # will map to (1-8)::
        #
        #     store = ModbusSlaveContext(..., zero_mode=True)
        # ----------------------------------------------------------------------- #

        slaves = {
            0x00:
            ModbusSlaveContext(
                di=CustomModbusSequentialDataBlock(0, [17] * 100),
                co=CustomModbusSequentialDataBlock(0, [1] * 100),
                hr=CustomModbusSequentialDataBlock(0, [17] * 100),
                ir=CustomModbusSequentialDataBlock(0, [17] * 100)),
            0x01:
            ModbusSlaveContext(
                di=CustomModbusSequentialDataBlock(0, [17] * 100),
                co=CustomModbusSequentialDataBlock(0, [0] * 100),
                hr=CustomModbusSequentialDataBlock(0, [17] * 100),
                ir=CustomModbusSequentialDataBlock(0, [17] * 100)),
            0x02:
            ModbusSlaveContext(
                di=CustomModbusSequentialDataBlock(0, [30] * 100),
                co=CustomModbusSparseDataBlock({
                    0x00: 0,
                    0x05: 1
                }),
                hr=CustomModbusSequentialDataBlock(0, [17] * 100),
                ir=CustomModbusSequentialDataBlock.create()),
            0x03:
            ModbusSlaveContext(
                di=CustomModbusSequentialDataBlock(0, [13] * 1),
                co=CustomModbusSequentialDataBlock(0, [1] * 23),
                hr=CustomModbusSequentialDataBlock(0, [65] * 18),
                ir=CustomModbusSequentialDataBlock(0, [178] * 56)),
            0x04:
            ModbusSlaveContext(
                di=CustomModbusSequentialDataBlock(6, [17] * 100),
                co=CustomModbusSequentialDataBlock(2, [0] * 100),
                hr=CustomModbusSequentialDataBlock(45, [17] * 100),
                ir=CustomModbusSequentialDataBlock(33, [17] * 100)),
            0x05:
            ModbusSlaveContext(
                di=CustomModbusSequentialDataBlock(0, [42] * 100),
                co=CustomModbusSequentialDataBlock(0, [1] * 100),
                hr=CustomModbusSequentialDataBlock(0, [76] * 100),
                ir=CustomModbusSequentialDataBlock(0, [32] * 100)),
            0xFF:
            ModbusSlaveContext(
                di=CustomModbusSequentialDataBlock(0, [17] * 100),
                co=CustomModbusSequentialDataBlock(0, [1] * 100),
                hr=CustomModbusSequentialDataBlock(0, [17] * 100),
                ir=CustomModbusSequentialDataBlock(0, [17] * 100))
        }

        context = ModbusServerContext(slaves=slaves, single=False)

        # ----------------------------------------------------------------------- #
        # initialize the server information
        # ----------------------------------------------------------------------- #
        # If you don't set this or any fields, they are defaulted to empty strings.
        # ----------------------------------------------------------------------- #
        identity = ModbusDeviceIdentification()
        identity.VendorName = Config.modbus.vendor_name
        identity.ProductCode = Config.modbus.product_code
        identity.VendorUrl = Config.modbus.vendor_url
        identity.ProductName = Config.modbus.product_name
        identity.ModelName = Config.modbus.model_name
        identity.MajorMinorRevision = Config.modbus.major_minor_revision
        identity.UserApplicationName = Config.modbus.user_application_name

        # ----------------------------------------------------------------------- #
        # run the server
        # ----------------------------------------------------------------------- #
        StartTcpServer(self,
                       context,
                       identity=identity,
                       address=(self._address, self._port),
                       transport=self._transport)