Esempio n. 1
0
    def set_values(self, block_name, address, values):
        """
        Set the values of the items at the given address
        If values is a list or a tuple, the value of every item is written
        If values is a number, only one value is written
        """
        # thread safe
        with self._data_lock:
            block = self._get_block(block_name)

            # the block has been found
            # check that it doesn't write out of the block
            offset = address-block.starting_address

            size = 1
            if isinstance(values, list) or isinstance(values, tuple):
                size = len(values)

            if (offset < 0) or ((offset + size) > block.size):
                raise OutOfModbusBlockError(
                    "address {0} size {1} is out of block {2}".format(address, size, block_name)
                )

            # if Ok: write the values
            if isinstance(values, list) or isinstance(values, tuple):
                block[offset:offset+len(values)] = values
            else:
                block[offset] = values
Esempio n. 2
0
    def get_values(self, block_name, address, size=1):
        """
        return the values of n items at the given address of the given block
        """
        # thread safe
        with self._data_lock:
            block = self._get_block(block_name)

            # the block has been found
            # check that it doesn't write out of the block
            offset = address - block.starting_address

            if (offset < 0) or ((offset + size) > block.size):
                raise OutOfModbusBlockError(
                    "address %s size {0} is out of block {1}".format(address, size, block_name)
                )

            # returns the values
            if size == 1:
                return tuple([block[offset], ])
            else:
                return tuple(block[offset:offset+size])