Skip to content

Commit b907133

Browse files
authored
Update to use DecodePDU. (#2391)
1 parent c753455 commit b907133

File tree

17 files changed

+41
-60
lines changed

17 files changed

+41
-60
lines changed

examples/client_custom_msg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# Since the function code is already registered with the decoder factory,
2727
# this will be decoded as a read coil response. If you implement a new
2828
# method that is not currently implemented, you must register the request
29-
# and response with a DecoderResponses factory.
29+
# and response with the active DecodePDU object.
3030
# --------------------------------------------------------------------------- #
3131

3232

examples/message_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
FramerRTU,
1818
FramerSocket,
1919
)
20-
from pymodbus.pdu import DecoderRequests, DecoderResponses
20+
from pymodbus.pdu import DecodePDU
2121

2222

2323
_logger = logging.getLogger(__file__)
@@ -73,8 +73,8 @@ def decode(self, message):
7373
print(f"Decoding Message {value}")
7474
print("=" * 80)
7575
decoders = [
76-
self.framer(DecoderRequests()),
77-
self.framer(DecoderResponses()),
76+
self.framer(DecodePDU(True)),
77+
self.framer(DecodePDU(False)),
7878
]
7979
for decoder in decoders:
8080
print(f"{decoder.decoder.__class__.__name__}")

pymodbus/client/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pymodbus.exceptions import ConnectionException, ModbusIOException
1212
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerBase, FramerType
1313
from pymodbus.logging import Log
14-
from pymodbus.pdu import DecoderResponses, ModbusPDU
14+
from pymodbus.pdu import DecodePDU, ModbusPDU
1515
from pymodbus.transaction import SyncModbusTransactionManager
1616
from pymodbus.transport import CommParams
1717
from pymodbus.utilities import ModbusTransactionState
@@ -182,7 +182,7 @@ def __init__(
182182
self.slaves: list[int] = []
183183

184184
# Common variables.
185-
self.framer: FramerBase = (FRAMER_NAME_TO_CLASS[framer])(DecoderResponses())
185+
self.framer: FramerBase = (FRAMER_NAME_TO_CLASS[framer])(DecodePDU(False))
186186
self.transaction = SyncModbusTransactionManager(
187187
self,
188188
self.retries,

pymodbus/client/modbusclientprotocol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
FramerType,
1010
)
1111
from pymodbus.logging import Log
12-
from pymodbus.pdu import DecoderResponses
12+
from pymodbus.pdu import DecodePDU
1313
from pymodbus.transaction import ModbusTransactionManager
1414
from pymodbus.transport import CommParams, ModbusProtocol
1515

@@ -35,7 +35,7 @@ def __init__(
3535
self.on_connect_callback = on_connect_callback
3636

3737
# Common variables.
38-
self.framer: FramerBase = (FRAMER_NAME_TO_CLASS[framer])(DecoderResponses())
38+
self.framer: FramerBase = (FRAMER_NAME_TO_CLASS[framer])(DecodePDU(False))
3939
self.transaction = ModbusTransactionManager()
4040

4141
def _handle_response(self, reply):

pymodbus/framer/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from pymodbus.exceptions import ModbusIOException
1212
from pymodbus.logging import Log
13-
from pymodbus.pdu import DecoderRequests, DecoderResponses, ModbusPDU
13+
from pymodbus.pdu import DecodePDU, ModbusPDU
1414

1515

1616
class FramerType(str, Enum):
@@ -30,7 +30,7 @@ class FramerBase:
3030

3131
def __init__(
3232
self,
33-
decoder: DecoderResponses | DecoderRequests,
33+
decoder: DecodePDU,
3434
) -> None:
3535
"""Initialize a ADU (framer) instance."""
3636
self.decoder = decoder

pymodbus/pdu/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""Framer."""
22
__all__ = [
3-
"DecoderRequests",
4-
"DecoderResponses",
3+
"DecodePDU",
54
"ExceptionResponse",
65
"ModbusExceptions",
76
"ModbusPDU",
87
]
98

10-
from pymodbus.pdu.decoders import DecoderRequests, DecoderResponses
9+
from pymodbus.pdu.decoders import DecodePDU
1110
from pymodbus.pdu.pdu import (
1211
ExceptionResponse,
1312
ModbusExceptions,

pymodbus/pdu/decoders.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class DecodePDU:
6262

6363
def __init__(self, is_server: bool) -> None:
6464
"""Initialize function_tables."""
65-
inx = 1 if is_server else 0
65+
inx = 0 if is_server else 1
6666
self.lookup: dict[int, Callable] = {cl[inx].function_code: cl[inx] for cl in self._pdu_class_table} # type: ignore[attr-defined]
6767
self.sub_lookup: dict[int, dict[int, Callable]] = {f: {} for f in self.lookup}
6868
for f in self._pdu_sub_class_table:
@@ -116,19 +116,3 @@ def decode(self, frame):
116116
except ModbusException as exc:
117117
Log.warning("Unable to decode frame {}", exc)
118118
return None
119-
120-
121-
class DecoderRequests(DecodePDU):
122-
"""Decode request Message (Server)."""
123-
124-
def __init__(self) -> None:
125-
"""Initialize the client lookup tables."""
126-
super().__init__(False)
127-
128-
129-
class DecoderResponses(DecodePDU):
130-
"""Decode response Message (Client)."""
131-
132-
def __init__(self) -> None:
133-
"""Initialize the client lookup tables."""
134-
super().__init__(True)

pymodbus/server/async_io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pymodbus.exceptions import ModbusException, NoSuchSlaveException
1313
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerBase, FramerType
1414
from pymodbus.logging import Log
15-
from pymodbus.pdu import DecoderRequests
15+
from pymodbus.pdu import DecodePDU
1616
from pymodbus.pdu import ModbusExceptions as merror
1717
from pymodbus.pdu.pdu import ExceptionResponse
1818
from pymodbus.transport import CommParams, CommType, ModbusProtocol
@@ -267,7 +267,7 @@ def __init__(
267267
True,
268268
)
269269
self.loop = asyncio.get_running_loop()
270-
self.decoder = DecoderRequests()
270+
self.decoder = DecodePDU(True)
271271
self.context = context or ModbusServerContext()
272272
self.control = ModbusControlBlock()
273273
self.ignore_missing_slaves = ignore_missing_slaves

pymodbus/server/simulator/http_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from pymodbus.datastore.simulator import Label
2626
from pymodbus.device import ModbusDeviceIdentification
2727
from pymodbus.logging import Log
28-
from pymodbus.pdu import DecoderRequests, ExceptionResponse
28+
from pymodbus.pdu import DecodePDU, ExceptionResponse
2929
from pymodbus.server.async_io import (
3030
ModbusSerialServer,
3131
ModbusTcpServer,
@@ -214,7 +214,7 @@ def __init__(
214214
self.refresh_rate = 0
215215
self.register_filter: list[int] = []
216216
self.call_list: list[CallTracer] = []
217-
self.request_lookup = DecoderRequests().lookup
217+
self.request_lookup = DecodePDU(True).lookup
218218
self.call_monitor = CallTypeMonitor()
219219
self.call_response = CallTypeResponse()
220220
app_key = getattr(web, 'AppKey', str) # fall back to str for aiohttp < 3.9.0

test/framer/conftest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerType
7-
from pymodbus.pdu import DecoderRequests, DecoderResponses
7+
from pymodbus.pdu import DecodePDU
88

99

1010
@pytest.fixture(name="entry")
@@ -20,6 +20,4 @@ def prepare_is_server():
2020
@pytest.fixture(name="test_framer")
2121
async def prepare_test_framer(entry, is_server):
2222
"""Return framer object."""
23-
return FRAMER_NAME_TO_CLASS[entry](
24-
(DecoderRequests if is_server else DecoderResponses)(),
25-
)
23+
return FRAMER_NAME_TO_CLASS[entry](DecodePDU(is_server))

0 commit comments

Comments
 (0)