defadd_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 notin 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))
set_values(self, block_name, address, values)
If values is a number, only one value is written,对于这个函数, values是我已开始没清楚的
defset_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 safewith 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 = 1if 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 valuesif isinstance(values, list) or isinstance(values, tuple):
block[offset:offset+len(values)] = values
else:
block[offset] = values
@threadsafe_functiondefexecute(
self, slave, function_code, starting_address, quantity_of_x=0, output_value=0, data_format="", expected_length=-1):"""
Execute a modbus query and returns the data part of the answer as a tuple
The returned tuple depends on the query function code. see modbus protocol
specification for details
data_format makes possible to extract the data like defined in the
struct python module documentation
"""