Exemplo n.º 1
0
    def add_block(self, block_name, block_type, starting_address, size):
        """Add a new block identified by its name"""
        # thread-safe
        with self._data_lock:
            if size <= 0:
                raise InvalidArgumentError("size must be a positive number")

            if starting_address < 0:
                raise InvalidArgumentError("starting address must be zero or positive number")

            if block_name in self._blocks:
                raise DuplicatedKeyError("Block {0} already exists. ".format(block_name))

            if block_type not in self._memory:
                raise InvalidModbusBlockError("Invalid block type {0}".format(block_type))

            # check that the new block doesn't overlap an existing block
            # it means that only 1 block per type must correspond to a given address
            # for example: it must not have 2 holding registers at address 100
            index = 0
            for i in range(len(self._memory[block_type])):
                block = self._memory[block_type][i]
                if block.is_in(starting_address, size):
                    raise OverlapModbusBlockError(
                        "Overlap block at {0} size {1}".format(block.starting_address, block.size)
                    )
                if block.starting_address > starting_address:
                    index = i
                    break

            # if the block is ok: register it
            self._blocks[block_name] = (block_type, starting_address)
            # add it in the 'per type' shortcut
            self._memory[block_type].insert(index, ModbusBlock(starting_address, size, block_name))
Exemplo n.º 2
0
 def add_slave(self, slave_id, unsigned=True, memory=None):
     """Add a new slave with the given id"""
     with self._lock:
         if (slave_id <= 0) or (slave_id > 255):
             raise Exception("Invalid slave id {0}".format(slave_id))
         if slave_id not in self._slaves:
             self._slaves[slave_id] = Slave(slave_id, unsigned, memory)
             return self._slaves[slave_id]
         else:
             raise DuplicatedKeyError("Slave {0} already exists".format(slave_id))