Skip to content

Commit 08ce17c

Browse files
committed
Allow sub_function_code is custom PDU.
1 parent b4fd9e7 commit 08ce17c

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

pymodbus/pdu/diag_message.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def encode(self) -> bytes:
4444
raise TypeError(f"UNKNOWN DIAG message type: {type(self.message)}")
4545
return packet
4646

47+
@classmethod
48+
def decode_sub_function_code(cls, data: bytes) -> int:
49+
"""Decode sub function code (2 bytes)."""
50+
return int.from_bytes(data[2:4], "big")
51+
4752
def decode(self, data: bytes) -> None:
4853
"""Decode a diagnostic request."""
4954
(self.sub_function_code, ) = struct.unpack(">H", data[:2])

pymodbus/pdu/mei_message.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def encode(self) -> bytes:
5050
)
5151
return packet
5252

53+
@classmethod
54+
def decode_sub_function_code(cls, data: bytes) -> int:
55+
"""Decode sub function code (1 byte)."""
56+
return int(data[2])
57+
5358
def decode(self, data: bytes) -> None:
5459
"""Decode data part of the message."""
5560
self.sub_function_code, self.read_code, self.object_id = struct.unpack(">BBB", data[:3])
@@ -134,6 +139,11 @@ def encode(self) -> bytes:
134139
packet += objects
135140
return packet
136141

142+
@classmethod
143+
def decode_sub_function_code(cls, data: bytes) -> int:
144+
"""Decode sub function code (1 byte)."""
145+
return int(data[2])
146+
137147
def decode(self, data: bytes) -> None:
138148
"""Decode a the response."""
139149
params = struct.unpack(">BBBBBB", data[0:6])

pymodbus/pdu/pdu.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def verifyAddress(self, address: int = -1) -> None:
5858
if not 0 <= address <= 65535:
5959
raise ValueError(f"0 < address {address} < 65535 !")
6060

61+
@classmethod
62+
def decode_sub_function_code(cls, data: bytes) -> int:
63+
"""Decode sub function code."""
64+
_ = data
65+
return -1
66+
6167
def __str__(self) -> str:
6268
"""Build a representation of a Modbus response."""
6369
return (

0 commit comments

Comments
 (0)