diff --git a/pymodbus/datastore/context.py b/pymodbus/datastore/context.py index a5e311a34..b364f1051 100644 --- a/pymodbus/datastore/context.py +++ b/pymodbus/datastore/context.py @@ -100,10 +100,10 @@ def __init__(self, slaves=None, single=True): :param slaves: A dictionary of client contexts :param single: Set to true to treat this as a single context ''' - self.single = single - self.__slaves = slaves or {} + self.single = single + self._slaves = slaves or {} if self.single: - self.__slaves = {Defaults.UnitId: self.__slaves} + self._slaves = {Defaults.UnitId: self._slaves} def __iter__(self): ''' Iterater over the current collection of slave @@ -111,7 +111,7 @@ def __iter__(self): :returns: An iterator over the slave contexts ''' - return iteritems(self.__slaves) + return iteritems(self._slaves) def __contains__(self, slave): ''' Check if the given slave is in this list @@ -119,7 +119,10 @@ def __contains__(self, slave): :param slave: slave The slave to check for existance :returns: True if the slave exists, False otherwise ''' - return slave in self.__slaves + if self.single and self._slaves: + return True + else: + return slave in self._slaves def __setitem__(self, slave, context): ''' Used to set a new slave context @@ -127,11 +130,13 @@ def __setitem__(self, slave, context): :param slave: The slave context to set :param context: The new context to set for this slave ''' - if self.single: slave = Defaults.UnitId + if self.single: + slave = Defaults.UnitId if 0xf7 >= slave >= 0x00: - self.__slaves[slave] = context + self._slaves[slave] = context else: - raise NoSuchSlaveException('slave index :{} out of range'.format(slave)) + raise NoSuchSlaveException('slave index :{} ' + 'out of range'.format(slave)) def __delitem__(self, slave): ''' Wrapper used to access the slave context @@ -139,9 +144,10 @@ def __delitem__(self, slave): :param slave: The slave context to remove ''' if not self.single and (0xf7 >= slave >= 0x00): - del self.__slaves[slave] + del self._slaves[slave] else: - raise NoSuchSlaveException('slave index: {} out of range'.format(slave)) + raise NoSuchSlaveException('slave index: {} ' + 'out of range'.format(slave)) def __getitem__(self, slave): ''' Used to get access to a slave context @@ -149,8 +155,10 @@ def __getitem__(self, slave): :param slave: The slave context to get :returns: The requested slave context ''' - if self.single: slave = Defaults.UnitId - if slave in self.__slaves: - return self.__slaves.get(slave) + if self.single: + slave = Defaults.UnitId + if slave in self._slaves: + return self._slaves.get(slave) else: - raise NoSuchSlaveException("slave - {} does not exist, or is out of range".format(slave)) + raise NoSuchSlaveException("slave - {} does not exist, " + "or is out of range".format(slave)) diff --git a/pymodbus/server/async.py b/pymodbus/server/async.py index 5dfdc1f65..8d412174c 100644 --- a/pymodbus/server/async.py +++ b/pymodbus/server/async.py @@ -59,7 +59,7 @@ def dataReceived(self, data): if _logger.isEnabledFor(logging.DEBUG): _logger.debug(' '.join([hex(byte2int(x)) for x in data])) if not self.factory.control.ListenOnly: - unit_address = byte2int(data[0]) + unit_address = byte2int(data[6]) if unit_address in self.factory.store: self.framer.processIncomingPacket(data, self._execute) diff --git a/pymodbus/server/sync.py b/pymodbus/server/sync.py index 7941701d6..253a18ee6 100644 --- a/pymodbus/server/sync.py +++ b/pymodbus/server/sync.py @@ -106,9 +106,14 @@ def handle(self): unit_address = int(data[1:3], 16) elif isinstance(self.framer, ModbusBinaryFramer): unit_address = byte2int(data[1]) - else: + elif isinstance(self.framer, ModbusRtuFramer): unit_address = byte2int(data[0]) - + elif isinstance(self.framer, ModbusSocketFramer): + unit_address = byte2int(data[6]) + else: + _logger.error("Unknown" + " framer - {}".format(type(self.framer))) + unit_address = -1 if unit_address in self.server.context: self.framer.processIncomingPacket(data, self.execute) except Exception as msg: