Beispiel #1
0
    def add_block(self, block_name, block_type, starting_address, size):
        """Add a new block identified by its name"""
        with self._data_lock:  # thread-safe
            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 %s already exists. " %
                                         block_name)

            if block_type not in self._memory:
                raise InvalidModbusBlockError("Invalid block type %d" %
                                              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 xrange(len(self._memory[block_type])):
                block = self._memory[block_type][i]
                if block.is_in(starting_address, size):
                    raise OverlapModbusBlockError, "Overlap block at %d size %d" % (
                        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, ModbusBlockDatabusMediator(block_name,
                                                  starting_address))
Beispiel #2
0
 def add_slave(self, slave_id):
     """
     Add a new slave with the given id
     """
     if (slave_id < 0) or (slave_id > 255):
         raise Exception("Invalid slave id %d" % slave_id)
     if slave_id not in self._slaves:
         self._slaves[slave_id] = MBSlave(slave_id, self.dom)
         return self._slaves[slave_id]
     else:
         raise DuplicatedKeyError("Slave %d already exists" % slave_id)