From 9eb6e650e4df71ab11c072e8d4a7623b1bc98039 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 4 Nov 2024 18:10:14 +0100 Subject: [PATCH 1/9] PDU base strongly typed.. --- pymodbus/client/mixin.py | 2 +- pymodbus/pdu/pdu.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pymodbus/client/mixin.py b/pymodbus/client/mixin.py index 8829c10a2..1431a88ef 100644 --- a/pymodbus/client/mixin.py +++ b/pymodbus/client/mixin.py @@ -416,7 +416,7 @@ def write_coils( def write_registers( self, address: int, - values: list[bytes] | list[int], + values: list[bytes | int], slave: int = 1, no_response_expected: bool = False ) -> T: diff --git a/pymodbus/pdu/pdu.py b/pymodbus/pdu/pdu.py index 4ec7f62d7..14bb504ad 100644 --- a/pymodbus/pdu/pdu.py +++ b/pymodbus/pdu/pdu.py @@ -19,13 +19,13 @@ class ModbusPDU: rtu_byte_count_pos: int = 0 def __init__(self, - slave_id = 0, - transaction_id = 0, - address = 0, - count = 0, + slave_id: int = 0, + transaction_id: int = 0, + address: int = 0, + count: int = 0, bits = None, registers = None, - status = 1, + status: int = 1, ) -> None: """Initialize the base data for a modbus request.""" if not registers: From 45515c28a95172b542266ff2be91fefbdd2a935d Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 4 Nov 2024 19:02:49 +0100 Subject: [PATCH 2/9] Step. --- pymodbus/datastore/context.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pymodbus/datastore/context.py b/pymodbus/datastore/context.py index 2783705eb..4ee096317 100644 --- a/pymodbus/datastore/context.py +++ b/pymodbus/datastore/context.py @@ -32,7 +32,7 @@ def decode(self, fx): """ return self._fx_mapper[fx] - async def async_getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int | bool | None]: + async def async_getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int | bool]: """Get `count` values from datastore. :param fc_as_hex: The function we are working with @@ -51,7 +51,7 @@ async def async_setValues(self, fc_as_hex: int, address: int, values: Sequence[i """ self.setValues(fc_as_hex, address, values) - def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int | bool | None]: + def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int | bool]: """Get `count` values from datastore. :param fc_as_hex: The function we are working with From a8af82fff7f7fc93e9f9c7ab726722bab29d1874 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 4 Nov 2024 19:10:40 +0100 Subject: [PATCH 3/9] Step. --- pymodbus/datastore/context.py | 10 ++++------ pymodbus/pdu/register_message.py | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/pymodbus/datastore/context.py b/pymodbus/datastore/context.py index 4ee096317..9e912f8b7 100644 --- a/pymodbus/datastore/context.py +++ b/pymodbus/datastore/context.py @@ -2,8 +2,6 @@ from __future__ import annotations -from collections.abc import Sequence - # pylint: disable=missing-type-doc from pymodbus.datastore.store import ModbusSequentialDataBlock from pymodbus.exceptions import NoSuchSlaveException @@ -32,7 +30,7 @@ def decode(self, fx): """ return self._fx_mapper[fx] - async def async_getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int | bool]: + async def async_getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int] | list[bool]: """Get `count` values from datastore. :param fc_as_hex: The function we are working with @@ -42,7 +40,7 @@ async def async_getValues(self, fc_as_hex: int, address: int, count: int = 1) -> """ return self.getValues(fc_as_hex, address, count) - async def async_setValues(self, fc_as_hex: int, address: int, values: Sequence[int | bool]) -> None: + async def async_setValues(self, fc_as_hex: int, address: int, values: list[int] | list[bool]) -> None: """Set the datastore with the supplied values. :param fc_as_hex: The function we are working with @@ -51,7 +49,7 @@ async def async_setValues(self, fc_as_hex: int, address: int, values: Sequence[i """ self.setValues(fc_as_hex, address, values) - def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int | bool]: + def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int] | list[bool]: """Get `count` values from datastore. :param fc_as_hex: The function we are working with @@ -62,7 +60,7 @@ def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int | Log.error("getValues({},{},{}) not implemented!", fc_as_hex, address, count) return [] - def setValues(self, fc_as_hex: int, address: int, values: Sequence[int | bool]) -> None: + def setValues(self, fc_as_hex: int, address: int, values: list[int] | list[bool]) -> None: """Set the datastore with the supplied values. :param fc_as_hex: The function we are working with diff --git a/pymodbus/pdu/register_message.py b/pymodbus/pdu/register_message.py index c8bc96ac7..c9ae01b9b 100644 --- a/pymodbus/pdu/register_message.py +++ b/pymodbus/pdu/register_message.py @@ -1,6 +1,5 @@ """Register Reading Request/Response.""" import struct -from typing import cast from pymodbus.datastore import ModbusSlaveContext from pymodbus.exceptions import ModbusIOException @@ -308,7 +307,7 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: return self.doException(merror.IllegalValue) if not context.validate(self.function_code, self.address, 1): return self.doException(merror.IllegalAddress) - values = cast(int, (await context.async_getValues(self.function_code, self.address, 1))[0]) + values = (await context.async_getValues(self.function_code, self.address, 1))[0] values = (values & self.and_mask) | (self.or_mask & ~self.and_mask) await context.async_setValues( self.function_code, self.address, [values] From 0f2b9fadd1a2b2d6cfb70ba17e126190c272d1ff Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 4 Nov 2024 19:12:48 +0100 Subject: [PATCH 4/9] Step. --- pymodbus/pdu/pdu.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pymodbus/pdu/pdu.py b/pymodbus/pdu/pdu.py index 14bb504ad..5bfbbb0cb 100644 --- a/pymodbus/pdu/pdu.py +++ b/pymodbus/pdu/pdu.py @@ -23,21 +23,22 @@ def __init__(self, transaction_id: int = 0, address: int = 0, count: int = 0, - bits = None, + bits: list[bool] | None = None, registers = None, status: int = 1, ) -> None: """Initialize the base data for a modbus request.""" + self.slave_id: int = slave_id + self.transaction_id: int = transaction_id + self.address: int = address + self.bits: list[bool] = bits if bits else [] + if not registers: registers = [] for i, value in enumerate(registers): if isinstance(value, bytes): registers[i] = int.from_bytes(value, byteorder="big") - self.transaction_id: int = transaction_id - self.slave_id: int = slave_id - self.address: int = address self.count: int = count if count else len(registers) - self.bits: list[bool] = bits if bits else [] self.registers: list[int] = registers if registers else [] self.status: int = status self.fut: asyncio.Future From 86f3a8cf3689f5c00fd70cee41ab138268ff8a0b Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 4 Nov 2024 20:12:32 +0100 Subject: [PATCH 5/9] Step. --- pymodbus/datastore/context.py | 10 ++++++---- pymodbus/pdu/pdu.py | 9 +++++---- pymodbus/pdu/register_message.py | 7 +++++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pymodbus/datastore/context.py b/pymodbus/datastore/context.py index 9e912f8b7..4282c40e3 100644 --- a/pymodbus/datastore/context.py +++ b/pymodbus/datastore/context.py @@ -2,6 +2,8 @@ from __future__ import annotations +from collections.abc import Sequence + # pylint: disable=missing-type-doc from pymodbus.datastore.store import ModbusSequentialDataBlock from pymodbus.exceptions import NoSuchSlaveException @@ -30,7 +32,7 @@ def decode(self, fx): """ return self._fx_mapper[fx] - async def async_getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int] | list[bool]: + async def async_getValues(self, fc_as_hex: int, address: int, count: int = 1) -> Sequence[int | bool]: """Get `count` values from datastore. :param fc_as_hex: The function we are working with @@ -40,7 +42,7 @@ async def async_getValues(self, fc_as_hex: int, address: int, count: int = 1) -> """ return self.getValues(fc_as_hex, address, count) - async def async_setValues(self, fc_as_hex: int, address: int, values: list[int] | list[bool]) -> None: + async def async_setValues(self, fc_as_hex: int, address: int, values: Sequence[int | bool]) -> None: """Set the datastore with the supplied values. :param fc_as_hex: The function we are working with @@ -49,7 +51,7 @@ async def async_setValues(self, fc_as_hex: int, address: int, values: list[int] """ self.setValues(fc_as_hex, address, values) - def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int] | list[bool]: + def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> Sequence[int | bool]: """Get `count` values from datastore. :param fc_as_hex: The function we are working with @@ -60,7 +62,7 @@ def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int] | Log.error("getValues({},{},{}) not implemented!", fc_as_hex, address, count) return [] - def setValues(self, fc_as_hex: int, address: int, values: list[int] | list[bool]) -> None: + def setValues(self, fc_as_hex: int, address: int, values: Sequence[int | bool]) -> None: """Set the datastore with the supplied values. :param fc_as_hex: The function we are working with diff --git a/pymodbus/pdu/pdu.py b/pymodbus/pdu/pdu.py index 5bfbbb0cb..38a8e6c05 100644 --- a/pymodbus/pdu/pdu.py +++ b/pymodbus/pdu/pdu.py @@ -4,7 +4,9 @@ import asyncio import struct from abc import abstractmethod +from collections.abc import Sequence from enum import Enum +from typing import cast from pymodbus.exceptions import NotImplementedException from pymodbus.logging import Log @@ -24,7 +26,7 @@ def __init__(self, address: int = 0, count: int = 0, bits: list[bool] | None = None, - registers = None, + registers: Sequence[int | bytes] | None = None, status: int = 1, ) -> None: """Initialize the base data for a modbus request.""" @@ -32,14 +34,13 @@ def __init__(self, self.transaction_id: int = transaction_id self.address: int = address self.bits: list[bool] = bits if bits else [] - if not registers: registers = [] + self.registers: list[int] = cast(list[int], registers) for i, value in enumerate(registers): if isinstance(value, bytes): - registers[i] = int.from_bytes(value, byteorder="big") + self.registers[i] = int.from_bytes(value, byteorder="big") self.count: int = count if count else len(registers) - self.registers: list[int] = registers if registers else [] self.status: int = status self.fut: asyncio.Future diff --git a/pymodbus/pdu/register_message.py b/pymodbus/pdu/register_message.py index c9ae01b9b..0745ed591 100644 --- a/pymodbus/pdu/register_message.py +++ b/pymodbus/pdu/register_message.py @@ -1,5 +1,8 @@ """Register Reading Request/Response.""" +from __future__ import annotations + import struct +from typing import cast from pymodbus.datastore import ModbusSlaveContext from pymodbus.exceptions import ModbusIOException @@ -62,9 +65,9 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: return self.doException(merror.IllegalValue) if not context.validate(self.function_code, self.address, self.count): return self.doException(merror.IllegalAddress) - values = await context.async_getValues( + values = cast(list[int], await context.async_getValues( self.function_code, self.address, self.count - ) + )) return ReadHoldingRegistersResponse(registers=values, slave_id=self.slave_id, transaction_id=self.transaction_id) From cbae130277af1ec4f52af3a727ab402145faf917 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 4 Nov 2024 20:22:47 +0100 Subject: [PATCH 6/9] Step. --- doc/source/library/simulator/calls_response.rst | 4 ++-- examples/client_custom_msg.py | 2 +- pymodbus/pdu/bit_message.py | 6 +++--- pymodbus/pdu/pdu.py | 4 ++-- pymodbus/pdu/register_message.py | 14 +++++++------- pymodbus/server/async_io.py | 2 +- pymodbus/server/simulator/http_server.py | 8 ++++---- test/framer/generator.py | 2 +- test/pdu/test_bit.py | 4 ++-- test/pdu/test_register_read_messages.py | 6 +++--- test/pdu/test_register_write_messages.py | 6 +++--- 11 files changed, 29 insertions(+), 29 deletions(-) diff --git a/doc/source/library/simulator/calls_response.rst b/doc/source/library/simulator/calls_response.rst index f3cb303fd..47ff64597 100644 --- a/doc/source/library/simulator/calls_response.rst +++ b/doc/source/library/simulator/calls_response.rst @@ -116,12 +116,12 @@ "function_error": [ { "value": 1, - "text": "IllegalFunction", + "text": "ILLEGAL_FUNCTION", "selected": false }, { "value": 2, - "text": "IllegalAddress", + "text": "ILLEGAL_ADDRESS", "selected": false }, { diff --git a/examples/client_custom_msg.py b/examples/client_custom_msg.py index c57e6aefc..1f8ba0495 100755 --- a/examples/client_custom_msg.py +++ b/examples/client_custom_msg.py @@ -87,7 +87,7 @@ def execute(self, context): if not 1 <= self.count <= 0x7D0: return self.doException(ModbusExceptions.IllegalValue) if not context.validate(self.function_code, self.address, self.count): - return self.doException(ModbusExceptions.IllegalAddress) + return self.doException(ModbusExceptions.ILLEGAL_ADDRESS) values = context.getValues(self.function_code, self.address, self.count) return CustomModbusPDU(values) diff --git a/pymodbus/pdu/bit_message.py b/pymodbus/pdu/bit_message.py index be971e380..35fe5cc5e 100644 --- a/pymodbus/pdu/bit_message.py +++ b/pymodbus/pdu/bit_message.py @@ -37,7 +37,7 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: if not (1 <= self.count <= 0x7D0): return self.doException(merror.IllegalValue) if not context.validate(self.function_code, self.address, self.count): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) values = cast(list[bool], await context.async_getValues( self.function_code, self.address, self.count )) @@ -98,7 +98,7 @@ class WriteSingleCoilRequest(WriteSingleCoilResponse): async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: """Run a request against a datastore.""" if not context.validate(self.function_code, self.address, 1): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) await context.async_setValues(self.function_code, self.address, self.bits) values = cast(list[bool], await context.async_getValues(self.function_code, self.address, 1)) @@ -138,7 +138,7 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: if not 1 <= count <= 0x07B0: return self.doException(merror.IllegalValue) if not context.validate(self.function_code, self.address, count): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) await context.async_setValues( self.function_code, self.address, self.bits diff --git a/pymodbus/pdu/pdu.py b/pymodbus/pdu/pdu.py index 38a8e6c05..942cad73e 100644 --- a/pymodbus/pdu/pdu.py +++ b/pymodbus/pdu/pdu.py @@ -84,8 +84,8 @@ def calculateRtuFrameSize(cls, data: bytes) -> int: class ModbusExceptions(int, Enum): """An enumeration of the valid modbus exceptions.""" - IllegalFunction = 0x01 # pylint: disable=invalid-name - IllegalAddress = 0x02 # pylint: disable=invalid-name + ILLEGAL_FUNCTION = 0x01 + ILLEGAL_ADDRESS = 0x02 IllegalValue = 0x03 # pylint: disable=invalid-name SlaveFailure = 0x04 # pylint: disable=invalid-name Acknowledge = 0x05 # pylint: disable=invalid-name diff --git a/pymodbus/pdu/register_message.py b/pymodbus/pdu/register_message.py index 0745ed591..e644551e9 100644 --- a/pymodbus/pdu/register_message.py +++ b/pymodbus/pdu/register_message.py @@ -64,7 +64,7 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: if not (1 <= self.count <= 0x7D): return self.doException(merror.IllegalValue) if not context.validate(self.function_code, self.address, self.count): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) values = cast(list[int], await context.async_getValues( self.function_code, self.address, self.count )) @@ -87,7 +87,7 @@ async def update_datastore(self, context) -> ModbusPDU: if not (1 <= self.count <= 0x7D): return self.doException(merror.IllegalValue) if not context.validate(self.function_code, self.address, self.count): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) values = await context.async_getValues( self.function_code, self.address, self.count ) @@ -161,9 +161,9 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: if not context.validate( self.function_code, self.write_address, self.write_count ): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) if not context.validate(self.function_code, self.read_address, self.read_count): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) await context.async_setValues( self.function_code, self.write_address, self.write_registers ) @@ -210,7 +210,7 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: if not 0 <= self.registers[0] <= 0xFFFF: return self.doException(merror.IllegalValue) if not context.validate(self.function_code, self.address, 1): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) await context.async_setValues( self.function_code, self.address, self.registers @@ -252,7 +252,7 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: if not 1 <= self.count <= 0x07B: return self.doException(merror.IllegalValue) if not context.validate(self.function_code, self.address, self.count): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) await context.async_setValues( self.function_code, self.address, self.registers @@ -309,7 +309,7 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: if not 0x0000 <= self.or_mask <= 0xFFFF: return self.doException(merror.IllegalValue) if not context.validate(self.function_code, self.address, 1): - return self.doException(merror.IllegalAddress) + return self.doException(merror.ILLEGAL_ADDRESS) values = (await context.async_getValues(self.function_code, self.address, 1))[0] values = (values & self.and_mask) | (self.or_mask & ~self.and_mask) await context.async_setValues( diff --git a/pymodbus/server/async_io.py b/pymodbus/server/async_io.py index e8c507fae..286d61f49 100644 --- a/pymodbus/server/async_io.py +++ b/pymodbus/server/async_io.py @@ -127,7 +127,7 @@ async def inner_handle(self): except ModbusException: pdu = ExceptionResponse( 40, - exception_code=merror.IllegalFunction + exception_code=merror.ILLEGAL_FUNCTION ) self.server_send(pdu, 0) pdu = None diff --git a/pymodbus/server/simulator/http_server.py b/pymodbus/server/simulator/http_server.py index ed6371ef2..4f506b075 100644 --- a/pymodbus/server/simulator/http_server.py +++ b/pymodbus/server/simulator/http_server.py @@ -364,8 +364,8 @@ def build_html_calls(self, params: dict, html: str) -> str: function_error = "" for i, txt in ( - (1, "IllegalFunction"), - (2, "IllegalAddress"), + (1, "ILLEGAL_FUNCTION"), + (2, "ILLEGAL_ADDRESS"), (3, "IllegalValue"), (4, "SlaveFailure"), (5, "Acknowledge"), @@ -526,8 +526,8 @@ def build_json_calls(self, params: dict) -> dict: function_error = [] for i, txt in ( - (1, "IllegalFunction"), - (2, "IllegalAddress"), + (1, "ILLEGAL_FUNCTION"), + (2, "ILLEGAL_ADDRESS"), (3, "IllegalValue"), (4, "SlaveFailure"), (5, "Acknowledge"), diff --git a/test/framer/generator.py b/test/framer/generator.py index 9c709cc51..ef1e9f2b9 100755 --- a/test/framer/generator.py +++ b/test/framer/generator.py @@ -36,7 +36,7 @@ def set_calls(): result = server.buildFrame(response) print(f" response --> {result}") print(f" response --> {result.hex()}") - exception = request.doException(merror.IllegalAddress) + exception = request.doException(merror.ILLEGAL_ADDRESS) exception.transaction_id = tid exception.slave_id = dev_id result = server.buildFrame(exception) diff --git a/test/pdu/test_bit.py b/test/pdu/test_bit.py index d47c2ef2f..f2fff36cd 100644 --- a/test/pdu/test_bit.py +++ b/test/pdu/test_bit.py @@ -110,7 +110,7 @@ async def test_write_single_coil_update_datastore(self, mock_context): context = mock_context(False, default=True) request = bit_msg.WriteSingleCoilRequest(address=2, bits=[True]) result = await request.update_datastore(context) - assert result.exception_code == ModbusExceptions.IllegalAddress + assert result.exception_code == ModbusExceptions.ILLEGAL_ADDRESS context.valid = True result = await request.update_datastore(context) @@ -133,7 +133,7 @@ async def test_write_multiple_coils_update_datastore(self, mock_context): context.valid = False request = bit_msg.WriteMultipleCoilsRequest(address=2, bits=[False] * 4) result = await request.update_datastore(context) - assert result.exception_code == ModbusExceptions.IllegalAddress + assert result.exception_code == ModbusExceptions.ILLEGAL_ADDRESS # validated request context.valid = True diff --git a/test/pdu/test_register_read_messages.py b/test/pdu/test_register_read_messages.py index 52f8f1e1d..4f8e16780 100644 --- a/test/pdu/test_register_read_messages.py +++ b/test/pdu/test_register_read_messages.py @@ -112,7 +112,7 @@ async def test_register_read_requests_validate_errors(self, mock_context): ] for request in requests: result = await request.update_datastore(context) - assert ModbusExceptions.IllegalAddress == result.exception_code + assert result.exception_code == ModbusExceptions.ILLEGAL_ADDRESS async def test_register_read_requests_update_datastore(self, mock_context): """This tests that the register request messages. @@ -145,11 +145,11 @@ async def test_read_write_multiple_registers_validate(self, mock_context): read_address=1, read_count=10, write_address=2, write_registers=[0x00] ) response = await request.update_datastore(context) - assert response.exception_code == ModbusExceptions.IllegalAddress + assert response.exception_code == ModbusExceptions.ILLEGAL_ADDRESS context.validate = lambda f, a, c: a == 2 response = await request.update_datastore(context) - assert response.exception_code == ModbusExceptions.IllegalAddress + assert response.exception_code == ModbusExceptions.ILLEGAL_ADDRESS request.write_byte_count = 0x100 response = await request.update_datastore(context) diff --git a/test/pdu/test_register_write_messages.py b/test/pdu/test_register_write_messages.py index 5475d2f9b..af548be43 100644 --- a/test/pdu/test_register_write_messages.py +++ b/test/pdu/test_register_write_messages.py @@ -87,7 +87,7 @@ async def test_write_single_register_request(self, mock_context): request.registers[0] = 0x00FF result = await request.update_datastore(context) - assert result.exception_code == ModbusExceptions.IllegalAddress + assert result.exception_code == ModbusExceptions.ILLEGAL_ADDRESS context.valid = True result = await request.update_datastore(context) @@ -98,7 +98,7 @@ async def test_write_multiple_register_request(self, mock_context): context = mock_context() request = WriteMultipleRegistersRequest(address=0x00, registers=[0x00] * 10) result = await request.update_datastore(context) - assert result.exception_code == ModbusExceptions.IllegalAddress + assert result.exception_code == ModbusExceptions.ILLEGAL_ADDRESS request.count = 0x800 # outside of range result = await request.update_datastore(context) @@ -159,7 +159,7 @@ async def test_mask_write_register_request_invalid_update_datastore(self, mock_c handle = MaskWriteRegisterRequest(0x0000, 0x0101, 0x1010) result = await handle.update_datastore(context) - assert ModbusExceptions.IllegalAddress == result.exception_code + assert result.exception_code == ModbusExceptions.ILLEGAL_ADDRESS # -----------------------------------------------------------------------# # Mask Write Register Response From d0c9b18934d62de17ba0a199b6f3c5ea2bfa3603 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 4 Nov 2024 20:29:39 +0100 Subject: [PATCH 7/9] Step. --- .../library/simulator/calls_response.rst | 2 +- examples/client_custom_msg.py | 2 +- pymodbus/pdu/bit_message.py | 4 ++-- pymodbus/pdu/file_message.py | 4 ++-- pymodbus/pdu/mei_message.py | 4 ++-- pymodbus/pdu/pdu.py | 2 +- pymodbus/pdu/register_message.py | 18 +++++++++--------- pymodbus/server/simulator/http_server.py | 4 ++-- test/pdu/test_bit.py | 6 +++--- test/pdu/test_file_message.py | 4 ++-- test/pdu/test_register_read_messages.py | 4 ++-- test/pdu/test_register_write_messages.py | 8 ++++---- 12 files changed, 31 insertions(+), 31 deletions(-) diff --git a/doc/source/library/simulator/calls_response.rst b/doc/source/library/simulator/calls_response.rst index 47ff64597..029527611 100644 --- a/doc/source/library/simulator/calls_response.rst +++ b/doc/source/library/simulator/calls_response.rst @@ -126,7 +126,7 @@ }, { "value": 3, - "text": "IllegalValue", + "text": "ILLEGAL_VALUE", "selected": false }, { diff --git a/examples/client_custom_msg.py b/examples/client_custom_msg.py index 1f8ba0495..4ed0b39dc 100755 --- a/examples/client_custom_msg.py +++ b/examples/client_custom_msg.py @@ -85,7 +85,7 @@ def decode(self, data): def execute(self, context): """Execute.""" if not 1 <= self.count <= 0x7D0: - return self.doException(ModbusExceptions.IllegalValue) + return self.doException(ModbusExceptions.ILLEGAL_VALUE) if not context.validate(self.function_code, self.address, self.count): return self.doException(ModbusExceptions.ILLEGAL_ADDRESS) values = context.getValues(self.function_code, self.address, self.count) diff --git a/pymodbus/pdu/bit_message.py b/pymodbus/pdu/bit_message.py index 35fe5cc5e..a208c4a17 100644 --- a/pymodbus/pdu/bit_message.py +++ b/pymodbus/pdu/bit_message.py @@ -35,7 +35,7 @@ def get_response_pdu_size(self) -> int: async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: """Run request against a datastore.""" if not (1 <= self.count <= 0x7D0): - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not context.validate(self.function_code, self.address, self.count): return self.doException(merror.ILLEGAL_ADDRESS) values = cast(list[bool], await context.async_getValues( @@ -136,7 +136,7 @@ async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: """Run a request against a datastore.""" count = len(self.bits) if not 1 <= count <= 0x07B0: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not context.validate(self.function_code, self.address, count): return self.doException(merror.ILLEGAL_ADDRESS) diff --git a/pymodbus/pdu/file_message.py b/pymodbus/pdu/file_message.py index d0f7b9351..29554d31e 100644 --- a/pymodbus/pdu/file_message.py +++ b/pymodbus/pdu/file_message.py @@ -239,9 +239,9 @@ def get_response_pdu_size(self) -> int: async def update_datastore(self, _context: ModbusSlaveContext) -> ModbusPDU: """Run a read exception status request against the store.""" if not 0x0000 <= self.address <= 0xFFFF: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if len(self.values) > 31: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) return ReadFifoQueueResponse(values=self.values, slave_id=self.slave_id, transaction_id=self.transaction_id) diff --git a/pymodbus/pdu/mei_message.py b/pymodbus/pdu/mei_message.py index 3b22d3057..bd2f08af5 100644 --- a/pymodbus/pdu/mei_message.py +++ b/pymodbus/pdu/mei_message.py @@ -61,9 +61,9 @@ def decode(self, data: bytes) -> None: async def update_datastore(self, _context: ModbusSlaveContext) -> ModbusPDU: """Run a read exception status request against the store.""" if not 0x00 <= self.object_id <= 0xFF: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not 0x00 <= self.read_code <= 0x04: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) information = DeviceInformationFactory.get(_MCB, self.read_code, self.object_id) return ReadDeviceInformationResponse(read_code=self.read_code, information=information, slave_id=self.slave_id, transaction_id=self.transaction_id) diff --git a/pymodbus/pdu/pdu.py b/pymodbus/pdu/pdu.py index 942cad73e..f923e888f 100644 --- a/pymodbus/pdu/pdu.py +++ b/pymodbus/pdu/pdu.py @@ -86,7 +86,7 @@ class ModbusExceptions(int, Enum): ILLEGAL_FUNCTION = 0x01 ILLEGAL_ADDRESS = 0x02 - IllegalValue = 0x03 # pylint: disable=invalid-name + ILLEGAL_VALUE = 0x03 # pylint: disable=invalid-name SlaveFailure = 0x04 # pylint: disable=invalid-name Acknowledge = 0x05 # pylint: disable=invalid-name SlaveBusy = 0x06 # pylint: disable=invalid-name diff --git a/pymodbus/pdu/register_message.py b/pymodbus/pdu/register_message.py index e644551e9..5b805496a 100644 --- a/pymodbus/pdu/register_message.py +++ b/pymodbus/pdu/register_message.py @@ -62,7 +62,7 @@ class ReadHoldingRegistersRequest(ReadRegistersRequestBase): async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: """Run a read holding request against a datastore.""" if not (1 <= self.count <= 0x7D): - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not context.validate(self.function_code, self.address, self.count): return self.doException(merror.ILLEGAL_ADDRESS) values = cast(list[int], await context.async_getValues( @@ -85,7 +85,7 @@ class ReadInputRegistersRequest(ReadRegistersRequestBase): async def update_datastore(self, context) -> ModbusPDU: """Run a read input request against a datastore.""" if not (1 <= self.count <= 0x7D): - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not context.validate(self.function_code, self.address, self.count): return self.doException(merror.ILLEGAL_ADDRESS) values = await context.async_getValues( @@ -153,11 +153,11 @@ def decode(self, data: bytes) -> None: async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: """Run a write single register request against a datastore.""" if not (1 <= self.read_count <= 0x07D): - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not 1 <= self.write_count <= 0x079: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if self.write_byte_count != self.write_count * 2: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not context.validate( self.function_code, self.write_address, self.write_count ): @@ -208,7 +208,7 @@ class WriteSingleRegisterRequest(WriteSingleRegisterResponse): async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: """Run a write single register request against a datastore.""" if not 0 <= self.registers[0] <= 0xFFFF: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not context.validate(self.function_code, self.address, 1): return self.doException(merror.ILLEGAL_ADDRESS) @@ -250,7 +250,7 @@ def decode(self, data: bytes) -> None: async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: """Run a write single register request against a datastore.""" if not 1 <= self.count <= 0x07B: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not context.validate(self.function_code, self.address, self.count): return self.doException(merror.ILLEGAL_ADDRESS) @@ -305,9 +305,9 @@ def decode(self, data: bytes) -> None: async def update_datastore(self, context: ModbusSlaveContext) -> ModbusPDU: """Run a mask write register request against the store.""" if not 0x0000 <= self.and_mask <= 0xFFFF: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not 0x0000 <= self.or_mask <= 0xFFFF: - return self.doException(merror.IllegalValue) + return self.doException(merror.ILLEGAL_VALUE) if not context.validate(self.function_code, self.address, 1): return self.doException(merror.ILLEGAL_ADDRESS) values = (await context.async_getValues(self.function_code, self.address, 1))[0] diff --git a/pymodbus/server/simulator/http_server.py b/pymodbus/server/simulator/http_server.py index 4f506b075..2c237eba1 100644 --- a/pymodbus/server/simulator/http_server.py +++ b/pymodbus/server/simulator/http_server.py @@ -366,7 +366,7 @@ def build_html_calls(self, params: dict, html: str) -> str: for i, txt in ( (1, "ILLEGAL_FUNCTION"), (2, "ILLEGAL_ADDRESS"), - (3, "IllegalValue"), + (3, "ILLEGAL_VALUE"), (4, "SlaveFailure"), (5, "Acknowledge"), (6, "SlaveBusy"), @@ -528,7 +528,7 @@ def build_json_calls(self, params: dict) -> dict: for i, txt in ( (1, "ILLEGAL_FUNCTION"), (2, "ILLEGAL_ADDRESS"), - (3, "IllegalValue"), + (3, "ILLEGAL_VALUE"), (4, "SlaveFailure"), (5, "Acknowledge"), (6, "SlaveBusy"), diff --git a/test/pdu/test_bit.py b/test/pdu/test_bit.py index f2fff36cd..444927a24 100644 --- a/test/pdu/test_bit.py +++ b/test/pdu/test_bit.py @@ -33,7 +33,7 @@ async def test_bit_read_update_datastore_value_errors(self, mock_context): (bit_msg.ReadDiscreteInputsRequest(address=1, count=0x800)), ): result = await pdu.update_datastore(context) - assert ModbusExceptions.IllegalValue == result.exception_code + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE async def test_bit_read_update_datastore_address_errors(self, mock_context): """Test bit read request encoding.""" @@ -43,7 +43,7 @@ async def test_bit_read_update_datastore_address_errors(self, mock_context): (bit_msg.ReadDiscreteInputsRequest(address=1, count=0x800)), ): result = await pdu.update_datastore(context) - assert ModbusExceptions.IllegalValue == result.exception_code + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE async def test_bit_read_update_datastore_success(self, mock_context): """Test bit read request encoding.""" @@ -127,7 +127,7 @@ async def test_write_multiple_coils_update_datastore(self, mock_context): # too many values request = bit_msg.WriteMultipleCoilsRequest(address=2, bits=[]) result = await request.update_datastore(context) - assert result.exception_code == ModbusExceptions.IllegalValue + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE # does not validate context.valid = False diff --git a/test/pdu/test_file_message.py b/test/pdu/test_file_message.py index 84eed5383..871986e21 100644 --- a/test/pdu/test_file_message.py +++ b/test/pdu/test_file_message.py @@ -44,11 +44,11 @@ async def test_read_fifo_queue_request(self, mock_context): handle.address = -1 result = await handle.update_datastore(context) - assert ModbusExceptions.IllegalValue == result.exception_code + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE handle.values = [0x00] * 33 result = await handle.update_datastore(context) - assert ModbusExceptions.IllegalValue == result.exception_code + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE async def test_read_fifo_queue_request_error(self, mock_context): """Test basic bit message encoding/decoding.""" diff --git a/test/pdu/test_register_read_messages.py b/test/pdu/test_register_read_messages.py index 4f8e16780..97129cc49 100644 --- a/test/pdu/test_register_read_messages.py +++ b/test/pdu/test_register_read_messages.py @@ -96,7 +96,7 @@ async def test_register_read_requests_count_errors(self): ] for request in requests: result = await request.update_datastore(None) - assert ModbusExceptions.IllegalValue == result.exception_code + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE async def test_register_read_requests_validate_errors(self, mock_context): """This tests that the register request messages. @@ -153,7 +153,7 @@ async def test_read_write_multiple_registers_validate(self, mock_context): request.write_byte_count = 0x100 response = await request.update_datastore(context) - assert response.exception_code == ModbusExceptions.IllegalValue + assert response.exception_code == ModbusExceptions.ILLEGAL_VALUE def test_read_write_multiple_registers_request_decode(self): """Test read/write multiple registers.""" diff --git a/test/pdu/test_register_write_messages.py b/test/pdu/test_register_write_messages.py index af548be43..d54d8af1a 100644 --- a/test/pdu/test_register_write_messages.py +++ b/test/pdu/test_register_write_messages.py @@ -83,7 +83,7 @@ async def test_write_single_register_request(self, mock_context): context = mock_context() request = WriteSingleRegisterRequest(address=0x00, registers=[0xF0000]) result = await request.update_datastore(context) - assert result.exception_code == ModbusExceptions.IllegalValue + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE request.registers[0] = 0x00FF result = await request.update_datastore(context) @@ -102,7 +102,7 @@ async def test_write_multiple_register_request(self, mock_context): request.count = 0x800 # outside of range result = await request.update_datastore(context) - assert result.exception_code == ModbusExceptions.IllegalValue + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE context.valid = True request = WriteMultipleRegistersRequest(address=0x00, registers=[0x00] * 10) @@ -151,11 +151,11 @@ async def test_mask_write_register_request_invalid_update_datastore(self, mock_c context = mock_context(valid=False, default=0x0000) handle = MaskWriteRegisterRequest(0x0000, -1, 0x1010) result = await handle.update_datastore(context) - assert ModbusExceptions.IllegalValue == result.exception_code + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE handle = MaskWriteRegisterRequest(0x0000, 0x0101, -1) result = await handle.update_datastore(context) - assert ModbusExceptions.IllegalValue == result.exception_code + assert result.exception_code == ModbusExceptions.ILLEGAL_VALUE handle = MaskWriteRegisterRequest(0x0000, 0x0101, 0x1010) result = await handle.update_datastore(context) From 4b7244a9730981729807a751d7b9d9d3ac6672d3 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 4 Nov 2024 20:42:49 +0100 Subject: [PATCH 8/9] Step. --- API_changes.rst | 1 + doc/source/library/simulator/calls_response.rst | 6 +++--- pymodbus/device.py | 6 +++--- pymodbus/pdu/diag_message.py | 6 +++--- pymodbus/pdu/pdu.py | 10 +++++----- pymodbus/server/async_io.py | 2 +- pymodbus/server/simulator/http_server.py | 12 ++++++------ 7 files changed, 22 insertions(+), 21 deletions(-) diff --git a/API_changes.rst b/API_changes.rst index fbe2010a8..639cf111f 100644 --- a/API_changes.rst +++ b/API_changes.rst @@ -6,6 +6,7 @@ API changes 3.8.0 ----------------- - ModbusSlaveContext, remove zero_mode parameter. - Remove skip_encode parameter. +- rename ModbusExceptions enums to legal constants. API changes 3.7.0 diff --git a/doc/source/library/simulator/calls_response.rst b/doc/source/library/simulator/calls_response.rst index 029527611..7d11c10de 100644 --- a/doc/source/library/simulator/calls_response.rst +++ b/doc/source/library/simulator/calls_response.rst @@ -131,17 +131,17 @@ }, { "value": 4, - "text": "SlaveFailure", + "text": "SLAVE_FAILURE", "selected": false }, { "value": 5, - "text": "Acknowledge", + "text": "ACKNOWLEDGE", "selected": false }, { "value": 6, - "text": "SlaveBusy", + "text": "SLAVE_BUSY", "selected": false }, { diff --git a/pymodbus/device.py b/pymodbus/device.py index 136ac0c82..7187b54ee 100644 --- a/pymodbus/device.py +++ b/pymodbus/device.py @@ -343,7 +343,7 @@ class ModbusCountersHandler: 0x10 6 Return Slave NAK Count Quantity of messages addressed to the remote device for which it - returned a Negative Acknowledge (NAK) exception response, since + returned a Negative ACKNOWLEDGE (NAK) exception response, since its last restart, clear counters operation, or power-up. Exception responses are described and listed in "MODBUS Application Protocol Specification" document. @@ -375,7 +375,7 @@ class ModbusCountersHandler: "SlaveMessage", "SlaveNoResponse", "SlaveNAK", - "SlaveBusy", + "SLAVE_BUSY", "BusCharacterOverrun", ] @@ -425,7 +425,7 @@ def summary(self): SlaveMessage = dict_property(lambda s: s.__data, 3) # pylint: disable=protected-access SlaveNoResponse = dict_property(lambda s: s.__data, 4) # pylint: disable=protected-access SlaveNAK = dict_property(lambda s: s.__data, 5) # pylint: disable=protected-access - SlaveBusy = dict_property(lambda s: s.__data, 6) # pylint: disable=protected-access + SLAVE_BUSY = dict_property(lambda s: s.__data, 6) # pylint: disable=protected-access BusCharacterOverrun = dict_property(lambda s: s.__data, 7) # pylint: disable=protected-access Event = dict_property(lambda s: s.__data, 8) # pylint: disable=protected-access # fmt: on diff --git a/pymodbus/pdu/diag_message.py b/pymodbus/pdu/diag_message.py index 7946bc247..9723a39dc 100644 --- a/pymodbus/pdu/diag_message.py +++ b/pymodbus/pdu/diag_message.py @@ -540,7 +540,7 @@ class ReturnSlaveNAKCountRequest(DiagnosticStatusSimpleRequest): """Return slave NAK count. The response data field returns the quantity of messages addressed to the - remote device for which it returned a Negative Acknowledge (NAK) exception + remote device for which it returned a Negative ACKNOWLEDGE (NAK) exception response, since its last restart, clear counters operation, or power-up. Exception responses are described and listed in section 7 . """ @@ -560,7 +560,7 @@ class ReturnSlaveNAKCountResponse(DiagnosticStatusSimpleResponse): """Return slave NAK. The response data field returns the quantity of messages addressed to the - remote device for which it returned a Negative Acknowledge (NAK) exception + remote device for which it returned a Negative ACKNOWLEDGE (NAK) exception response, since its last restart, clear counters operation, or power-up. Exception responses are described and listed in section 7. """ @@ -583,7 +583,7 @@ async def update_datastore(self, *args): :returns: The initialized response message """ - count = _MCB.Counter.SlaveBusy + count = _MCB.Counter.SLAVE_BUSY return ReturnSlaveBusyCountResponse(count) diff --git a/pymodbus/pdu/pdu.py b/pymodbus/pdu/pdu.py index f923e888f..2022961ea 100644 --- a/pymodbus/pdu/pdu.py +++ b/pymodbus/pdu/pdu.py @@ -86,11 +86,11 @@ class ModbusExceptions(int, Enum): ILLEGAL_FUNCTION = 0x01 ILLEGAL_ADDRESS = 0x02 - ILLEGAL_VALUE = 0x03 # pylint: disable=invalid-name - SlaveFailure = 0x04 # pylint: disable=invalid-name - Acknowledge = 0x05 # pylint: disable=invalid-name - SlaveBusy = 0x06 # pylint: disable=invalid-name - NegativeAcknowledge = 0x07 # pylint: disable=invalid-name + ILLEGAL_VALUE = 0x03 + SLAVE_FAILURE = 0x04 + ACKNOWLEDGE = 0x05 + SLAVE_BUSY = 0x06 + NegativeACKNOWLEDGE = 0x07 # pylint: disable=invalid-name MemoryParityError = 0x08 # pylint: disable=invalid-name GatewayPathUnavailable = 0x0A # pylint: disable=invalid-name GatewayNoResponse = 0x0B # pylint: disable=invalid-name diff --git a/pymodbus/server/async_io.py b/pymodbus/server/async_io.py index 286d61f49..f68db6136 100644 --- a/pymodbus/server/async_io.py +++ b/pymodbus/server/async_io.py @@ -205,7 +205,7 @@ async def _async_execute(self, request, *addr): exc, traceback.format_exc(), ) - response = request.doException(merror.SlaveFailure) + response = request.doException(merror.SLAVE_FAILURE) # no response when broadcasting if not broadcast: response.transaction_id = request.transaction_id diff --git a/pymodbus/server/simulator/http_server.py b/pymodbus/server/simulator/http_server.py index 2c237eba1..6cc1a119a 100644 --- a/pymodbus/server/simulator/http_server.py +++ b/pymodbus/server/simulator/http_server.py @@ -367,9 +367,9 @@ def build_html_calls(self, params: dict, html: str) -> str: (1, "ILLEGAL_FUNCTION"), (2, "ILLEGAL_ADDRESS"), (3, "ILLEGAL_VALUE"), - (4, "SlaveFailure"), - (5, "Acknowledge"), - (6, "SlaveBusy"), + (4, "SLAVE_FAILURE"), + (5, "ACKNOWLEDGE"), + (6, "SLAVE_BUSY"), (7, "MemoryParityError"), (10, "GatewayPathUnavailable"), (11, "GatewayNoResponse"), @@ -529,9 +529,9 @@ def build_json_calls(self, params: dict) -> dict: (1, "ILLEGAL_FUNCTION"), (2, "ILLEGAL_ADDRESS"), (3, "ILLEGAL_VALUE"), - (4, "SlaveFailure"), - (5, "Acknowledge"), - (6, "SlaveBusy"), + (4, "SLAVE_FAILURE"), + (5, "ACKNOWLEDGE"), + (6, "SLAVE_BUSY"), (7, "MemoryParityError"), (10, "GatewayPathUnavailable"), (11, "GatewayNoResponse"), From d7fbf1720b6b7a91d746ac7b89f33454b8afe7cc Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 4 Nov 2024 20:48:43 +0100 Subject: [PATCH 9/9] Final. --- doc/source/library/simulator/calls_response.rst | 6 +++--- pymodbus/pdu/pdu.py | 8 ++++---- pymodbus/server/async_io.py | 2 +- pymodbus/server/simulator/http_server.py | 12 ++++++------ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/doc/source/library/simulator/calls_response.rst b/doc/source/library/simulator/calls_response.rst index 7d11c10de..51b89ae17 100644 --- a/doc/source/library/simulator/calls_response.rst +++ b/doc/source/library/simulator/calls_response.rst @@ -146,17 +146,17 @@ }, { "value": 7, - "text": "MemoryParityError", + "text": "MEMORY_PARITY_ERROR", "selected": false }, { "value": 10, - "text": "GatewayPathUnavailable", + "text": "GATEWAY_PATH_UNAVIABLE", "selected": false }, { "value": 11, - "text": "GatewayNoResponse", + "text": "GATEWAY_NO_RESPONSE", "selected": false } ], diff --git a/pymodbus/pdu/pdu.py b/pymodbus/pdu/pdu.py index 2022961ea..e2fcfb5a6 100644 --- a/pymodbus/pdu/pdu.py +++ b/pymodbus/pdu/pdu.py @@ -90,10 +90,10 @@ class ModbusExceptions(int, Enum): SLAVE_FAILURE = 0x04 ACKNOWLEDGE = 0x05 SLAVE_BUSY = 0x06 - NegativeACKNOWLEDGE = 0x07 # pylint: disable=invalid-name - MemoryParityError = 0x08 # pylint: disable=invalid-name - GatewayPathUnavailable = 0x0A # pylint: disable=invalid-name - GatewayNoResponse = 0x0B # pylint: disable=invalid-name + NEGATIVE_ACKNOWLEDGE = 0x07 + MEMORY_PARITY_ERROR = 0x08 + GATEWAY_PATH_UNAVIABLE = 0x0A + GATEWAY_NO_RESPONSE = 0x0B class ExceptionResponse(ModbusPDU): diff --git a/pymodbus/server/async_io.py b/pymodbus/server/async_io.py index f68db6136..48510d54d 100644 --- a/pymodbus/server/async_io.py +++ b/pymodbus/server/async_io.py @@ -198,7 +198,7 @@ async def _async_execute(self, request, *addr): Log.error("requested slave does not exist: {}", request.slave_id) if self.server.ignore_missing_slaves: return # the client will simply timeout waiting for a response - response = request.doException(merror.GatewayNoResponse) + response = request.doException(merror.GATEWAY_NO_RESPONSE) except Exception as exc: # pylint: disable=broad-except Log.error( "Datastore unable to fulfill request: {}; {}", diff --git a/pymodbus/server/simulator/http_server.py b/pymodbus/server/simulator/http_server.py index 6cc1a119a..761687fc3 100644 --- a/pymodbus/server/simulator/http_server.py +++ b/pymodbus/server/simulator/http_server.py @@ -370,9 +370,9 @@ def build_html_calls(self, params: dict, html: str) -> str: (4, "SLAVE_FAILURE"), (5, "ACKNOWLEDGE"), (6, "SLAVE_BUSY"), - (7, "MemoryParityError"), - (10, "GatewayPathUnavailable"), - (11, "GatewayNoResponse"), + (7, "MEMORY_PARITY_ERROR"), + (10, "GATEWAY_PATH_UNAVIABLE"), + (11, "GATEWAY_NO_RESPONSE"), ): selected = "selected" if i == self.call_response.error_response else "" function_error += f"" @@ -532,9 +532,9 @@ def build_json_calls(self, params: dict) -> dict: (4, "SLAVE_FAILURE"), (5, "ACKNOWLEDGE"), (6, "SLAVE_BUSY"), - (7, "MemoryParityError"), - (10, "GatewayPathUnavailable"), - (11, "GatewayNoResponse"), + (7, "MEMORY_PARITY_ERROR"), + (10, "GATEWAY_PATH_UNAVIABLE"), + (11, "GATEWAY_NO_RESPONSE"), ): function_error.append({ "value": i,