From 7e38fa61d1a189eb6a67eff8a6b50d9f1e366594 Mon Sep 17 00:00:00 2001 From: Dan Shinde <112338485+DanShinde@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:44:21 +0000 Subject: [PATCH] Answer ILLEGAL_VALUE for Read Device Identification code 0 Read Device ID code 0x00 is not defined (valid codes are 01-04), but the range check let it through, so the lookup raised KeyError and the server logged a traceback and answered DEVICE_FAILURE. Reject it with ILLEGAL_VALUE, like codes above 04. --- pymodbus/pdu/mei_message.py | 2 +- test/pdu/test_mei_messages.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pymodbus/pdu/mei_message.py b/pymodbus/pdu/mei_message.py index fed229f32..08504c9cc 100644 --- a/pymodbus/pdu/mei_message.py +++ b/pymodbus/pdu/mei_message.py @@ -89,7 +89,7 @@ async def datastore_update( _ = context if not 0x00 <= self.object_id <= 0xFF: return ExceptionResponse(self.function_code, ExcCodes.ILLEGAL_VALUE) - if not 0x00 <= self.read_code <= 0x04: + if not 0x01 <= self.read_code <= 0x04: return ExceptionResponse(self.function_code, ExcCodes.ILLEGAL_VALUE) information = DeviceInformationFactory.get(_MCB, self.read_code, self.object_id) diff --git a/test/pdu/test_mei_messages.py b/test/pdu/test_mei_messages.py index a4d4bf2e5..9f86ca9cf 100644 --- a/test/pdu/test_mei_messages.py +++ b/test/pdu/test_mei_messages.py @@ -92,6 +92,9 @@ async def test_read_device_information_request_error(self, mock_server_context): assert (await handle.datastore_update(context, 0)).function_code == 0xAB handle.read_code = 0x05 assert (await handle.datastore_update(context, 0)).function_code == 0xAB + handle.read_code = 0x00 + result = await handle.datastore_update(context, 0) + assert result.exception_code == ExcCodes.ILLEGAL_VALUE handle.object_id = -1 assert (await handle.datastore_update(context, 0)).function_code == 0xAB handle.object_id = 0x100