From 8681db9b7acdfdfd43035167ab056f5e37552b38 Mon Sep 17 00:00:00 2001 From: dhoomakethu Date: Thu, 26 Oct 2017 09:53:01 +0530 Subject: [PATCH] #60 Check for slave unit id before processing the request for serial servers --- pymodbus/server/async.py | 4 +++- pymodbus/server/sync.py | 9 ++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pymodbus/server/async.py b/pymodbus/server/async.py index 0e1649e1a3..b72eb3b534 100644 --- a/pymodbus/server/async.py +++ b/pymodbus/server/async.py @@ -58,7 +58,9 @@ 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: - self.framer.processIncomingPacket(data, self._execute) + unit_address = byte2int(data[0]) + if unit_address in self.factory.store: + self.framer.processIncomingPacket(data, self._execute) def _execute(self, request): ''' Executes the request and returns the result diff --git a/pymodbus/server/sync.py b/pymodbus/server/sync.py index 4559513f26..f4beeea912 100644 --- a/pymodbus/server/sync.py +++ b/pymodbus/server/sync.py @@ -102,7 +102,9 @@ def handle(self): if data: if _logger.isEnabledFor(logging.DEBUG): _logger.debug(" ".join([hex(byte2int(x)) for x in data])) - self.framer.processIncomingPacket(data, self.execute) + unit_address = byte2int(data[0]) + if unit_address in self.server.context: + self.framer.processIncomingPacket(data, self.execute) except Exception as msg: # since we only have a single socket, we cannot exit # Clear frame buffer @@ -198,6 +200,7 @@ class ModbusDisconnectedRequestHandler(ModbusBaseRequestHandler): only difference is that we have to specify who to send the resulting packet data to. ''' + socket = None def handle(self): ''' Callback when we receive any data @@ -205,7 +208,7 @@ def handle(self): reset_frame = False while self.running: try: - data, self.request = self.request + data, self.socket = self.request if not data: self.running = False if _logger.isEnabledFor(logging.DEBUG): @@ -236,7 +239,7 @@ def send(self, message): pdu = self.framer.buildPacket(message) if _logger.isEnabledFor(logging.DEBUG): _logger.debug('send: %s' % b2a_hex(pdu)) - return self.request.sendto(pdu, self.client_address) + return self.socket.sendto(pdu, self.client_address) #---------------------------------------------------------------------------#