Skip to content

Commit

Permalink
#256 Modbus TCP async server fix transactions > 255
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoomakethu committed Jan 5, 2018
1 parent 019bdde commit b77d4a6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
36 changes: 22 additions & 14 deletions pymodbus/datastore/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,57 +100,65 @@ 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
contexts.
: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
: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
: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
: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
: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))
2 changes: 1 addition & 1 deletion pymodbus/server/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

This comment has been minimized.

Copy link
@karlp

karlp Feb 9, 2018

Contributor

This is not the same change as you made in sync.py, and by all appearances you're now just breaking RTU mode to fix TCP mode. Why are you not just leaving this to the framer.processIncomingPacket to decide itself?

This comment has been minimized.

Copy link
@dhoomakethu

dhoomakethu Feb 9, 2018

Author Contributor

Good catch, I am in the process of re-writing the framer class and will try to cover your suggestion there. Lot of clutter to clean. I will add you as a reviewer.

if unit_address in self.factory.store:
self.framer.processIncomingPacket(data, self._execute)

Expand Down
9 changes: 7 additions & 2 deletions pymodbus/server/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b77d4a6

Please sign in to comment.