Skip to content

Commit a5af22b

Browse files
gijzelaerrclaude
andcommitted
Add human-readable S7 return code descriptions
Error messages now include descriptive text for S7 return codes: - 0x0a: "Object does not exist" - 0x05: "Invalid address" - 0x03: "Accessing the object not allowed" - etc. Example: "Read SZL failed: Object does not exist (0x0a)" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e96829a commit a5af22b

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

snap7/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717

1818
from .connection import ISOTCPConnection
19-
from .s7protocol import S7Protocol
19+
from .s7protocol import S7Protocol, get_return_code_description
2020
from .datatypes import S7Area, S7WordLen
2121
from .error import S7Error, S7ConnectionError, S7ProtocolError
2222

@@ -1305,7 +1305,8 @@ def read_szl(self, ssl_id: int, index: int = 0) -> S7SZL:
13051305
data_info = response.get("data", {})
13061306
return_code = data_info.get("return_code", 0xFF) if isinstance(data_info, dict) else 0xFF
13071307
if return_code != 0xFF:
1308-
raise RuntimeError(f"Read SZL failed with return code: {return_code:#02x}")
1308+
desc = get_return_code_description(return_code)
1309+
raise RuntimeError(f"Read SZL failed: {desc} (0x{return_code:02x})")
13091310

13101311
# Parse SZL response
13111312
szl_result = self.protocol.parse_read_szl_response(response)

snap7/s7protocol.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@ class S7UserDataSubfunction(IntEnum):
6969
SET_CLOCK = 0x02
7070

7171

72+
# S7 data section return codes with human-readable descriptions
73+
S7_RETURN_CODES: Dict[int, str] = {
74+
0x00: "Reserved",
75+
0x01: "Hardware error",
76+
0x03: "Accessing the object not allowed",
77+
0x05: "Invalid address",
78+
0x06: "Data type not supported",
79+
0x07: "Data type inconsistent",
80+
0x0A: "Object does not exist",
81+
0x10: "Invalid block type number",
82+
0x11: "Block not found in storage medium",
83+
0x12: "Block already exists",
84+
0x13: "Block is protected",
85+
0x14: "Block download without proper block first",
86+
0x19: "Block download sequence error",
87+
0x1A: "Insufficient working memory",
88+
0x1B: "Insufficient load memory",
89+
0x1C: "Not enough work retentive data (instance DBs)",
90+
0x1D: "Interface error",
91+
0x1E: "Delete block refused",
92+
0x20: "Invalid parameter",
93+
0x21: "PG resource error (max connections reached)",
94+
0xFF: "Success",
95+
}
96+
97+
98+
def get_return_code_description(return_code: int) -> str:
99+
"""Get human-readable description for S7 return code."""
100+
if return_code in S7_RETURN_CODES:
101+
return S7_RETURN_CODES[return_code]
102+
return "Unknown error"
103+
104+
72105
class S7Protocol:
73106
"""
74107
S7 protocol implementation.
@@ -1315,8 +1348,8 @@ def extract_read_data(self, response: Dict[str, Any], word_len: S7WordLen, count
13151348
return_code = data_info.get("return_code", 0)
13161349

13171350
if return_code != 0xFF: # 0xFF = Success
1318-
error_msg = f"Read operation failed with return code: {return_code:#02x}"
1319-
raise S7ProtocolError(error_msg)
1351+
desc = get_return_code_description(return_code)
1352+
raise S7ProtocolError(f"Read operation failed: {desc} (0x{return_code:02x})")
13201353

13211354
raw_data = data_info.get("data", b"")
13221355

@@ -1346,6 +1379,6 @@ def check_write_response(self, response: Dict[str, Any]) -> None:
13461379
return_code = data_info.get("return_code", 0xFF) # Default to success
13471380

13481381
if return_code != 0xFF: # 0xFF = Success
1349-
error_msg = f"Write operation failed with return code: {return_code:#02x}"
1350-
raise S7ProtocolError(error_msg)
1382+
desc = get_return_code_description(return_code)
1383+
raise S7ProtocolError(f"Write operation failed: {desc} (0x{return_code:02x})")
13511384
# If no data and no header error, the write was successful (ACK without data)

0 commit comments

Comments
 (0)