python-snap7 3.3.0__tar.gz → 3.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. {python_snap7-3.3.0 → python_snap7-3.3.1}/CHANGES.md +9 -0
  2. {python_snap7-3.3.0/python_snap7.egg-info → python_snap7-3.3.1}/PKG-INFO +1 -1
  3. {python_snap7-3.3.0 → python_snap7-3.3.1}/pyproject.toml +2 -1
  4. {python_snap7-3.3.0 → python_snap7-3.3.1/python_snap7.egg-info}/PKG-INFO +1 -1
  5. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/server/__init__.py +114 -2
  6. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_server.py +62 -1
  7. {python_snap7-3.3.0 → python_snap7-3.3.1}/LICENSE +0 -0
  8. {python_snap7-3.3.0 → python_snap7-3.3.1}/MANIFEST.in +0 -0
  9. {python_snap7-3.3.0 → python_snap7-3.3.1}/README.rst +0 -0
  10. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/client-base.rst +0 -0
  11. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/client.rst +0 -0
  12. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/connection.rst +0 -0
  13. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/datatypes.rst +0 -0
  14. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/demo.rst +0 -0
  15. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/discovery.rst +0 -0
  16. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/error.rst +0 -0
  17. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/log.rst +0 -0
  18. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/logo.rst +0 -0
  19. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/optimizer.rst +0 -0
  20. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/partner.rst +0 -0
  21. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/ppi.rst +0 -0
  22. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/rate-limiter.rst +0 -0
  23. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/s7protocol.rst +0 -0
  24. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/server.rst +0 -0
  25. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/szl.rst +0 -0
  26. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/tags.rst +0 -0
  27. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/type.rst +0 -0
  28. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/API/util.rst +0 -0
  29. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/Makefile +0 -0
  30. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/_static/.phony +0 -0
  31. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/advanced.rst +0 -0
  32. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/cli.rst +0 -0
  33. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/conf.py +0 -0
  34. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/connecting.rst +0 -0
  35. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/connection-issues.rst +0 -0
  36. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/development.rst +0 -0
  37. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/error-reference.rst +0 -0
  38. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/index.rst +0 -0
  39. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/installation.rst +0 -0
  40. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/introduction.rst +0 -0
  41. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/limitations.rst +0 -0
  42. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/multi-variable.rst +0 -0
  43. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/plc-support.rst +0 -0
  44. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/ppi.rst +0 -0
  45. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/reading-writing.rst +0 -0
  46. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/real-plc-testing.rst +0 -0
  47. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/server.rst +0 -0
  48. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/thread-safety.rst +0 -0
  49. {python_snap7-3.3.0 → python_snap7-3.3.1}/doc/tia-portal-config.rst +0 -0
  50. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/boolean.py +0 -0
  51. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/db1_layout.txt +0 -0
  52. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/db_layouts.py +0 -0
  53. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/example.py +0 -0
  54. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/logo_7_8.py +0 -0
  55. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/ppi.py +0 -0
  56. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/prometheus_metrics.py +0 -0
  57. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/read_multi.py +0 -0
  58. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/s7_symbols.py +0 -0
  59. {python_snap7-3.3.0 → python_snap7-3.3.1}/example/write_multi.py +0 -0
  60. {python_snap7-3.3.0 → python_snap7-3.3.1}/python_snap7.egg-info/SOURCES.txt +0 -0
  61. {python_snap7-3.3.0 → python_snap7-3.3.1}/python_snap7.egg-info/dependency_links.txt +0 -0
  62. {python_snap7-3.3.0 → python_snap7-3.3.1}/python_snap7.egg-info/entry_points.txt +0 -0
  63. {python_snap7-3.3.0 → python_snap7-3.3.1}/python_snap7.egg-info/requires.txt +0 -0
  64. {python_snap7-3.3.0 → python_snap7-3.3.1}/python_snap7.egg-info/top_level.txt +0 -0
  65. {python_snap7-3.3.0 → python_snap7-3.3.1}/s7/__init__.py +0 -0
  66. {python_snap7-3.3.0 → python_snap7-3.3.1}/setup.cfg +0 -0
  67. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/__init__.py +0 -0
  68. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/async_client.py +0 -0
  69. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/cli.py +0 -0
  70. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/client.py +0 -0
  71. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/client_base.py +0 -0
  72. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/connection.py +0 -0
  73. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/datatypes.py +0 -0
  74. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/demo.py +0 -0
  75. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/discovery.py +0 -0
  76. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/error.py +0 -0
  77. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/log.py +0 -0
  78. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/logo.py +0 -0
  79. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/optimizer.py +0 -0
  80. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/partner.py +0 -0
  81. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/ppi.py +0 -0
  82. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/py.typed +0 -0
  83. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/rate_limiter.py +0 -0
  84. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/s7protocol.py +0 -0
  85. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/server/__main__.py +0 -0
  86. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/szl.py +0 -0
  87. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/tags.py +0 -0
  88. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/type.py +0 -0
  89. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/util/__init__.py +0 -0
  90. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/util/db.py +0 -0
  91. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/util/getters.py +0 -0
  92. {python_snap7-3.3.0 → python_snap7-3.3.1}/snap7/util/setters.py +0 -0
  93. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/__init__.py +0 -0
  94. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/conftest.py +0 -0
  95. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/features/real_plc/connection.feature +0 -0
  96. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/features/real_plc/lifecycle.feature +0 -0
  97. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/features/real_plc/read.feature +0 -0
  98. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/features/real_plc/report.schema.json +0 -0
  99. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/features/real_plc/write.feature +0 -0
  100. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/plc_setup/e2e_test_dbs.scl +0 -0
  101. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/real_plc/__init__.py +0 -0
  102. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/real_plc/reporting.py +0 -0
  103. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/real_plc/support.py +0 -0
  104. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/real_plc/test_acceptance.py +0 -0
  105. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_api_surface.py +0 -0
  106. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_async_client.py +0 -0
  107. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_async_typed_access.py +0 -0
  108. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_behavioral_compatibility.py +0 -0
  109. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_cli.py +0 -0
  110. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_client.py +0 -0
  111. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_client_e2e.py +0 -0
  112. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_conformance.py +0 -0
  113. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_connection.py +0 -0
  114. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_coverage_gaps.py +0 -0
  115. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_datatypes.py +0 -0
  116. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_demo.py +0 -0
  117. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_discovery.py +0 -0
  118. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_error.py +0 -0
  119. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_finalize.py +0 -0
  120. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_hypothesis.py +0 -0
  121. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_lean_regressions.py +0 -0
  122. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_logging.py +0 -0
  123. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_logo_client.py +0 -0
  124. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_logo_reconnect_options.py +0 -0
  125. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_mainloop.py +0 -0
  126. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_multipacket.py +0 -0
  127. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_operation_hook.py +0 -0
  128. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_optimizer.py +0 -0
  129. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_partner.py +0 -0
  130. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_ppi.py +0 -0
  131. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_property.py +0 -0
  132. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_rate_limiter.py +0 -0
  133. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_real_plc_support.py +0 -0
  134. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_reconnect.py +0 -0
  135. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_routing.py +0 -0
  136. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_s7protocol.py +0 -0
  137. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_server_cli.py +0 -0
  138. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_stress.py +0 -0
  139. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_tags.py +0 -0
  140. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_typed_access.py +0 -0
  141. {python_snap7-3.3.0 → python_snap7-3.3.1}/tests/test_util.py +0 -0
  142. {python_snap7-3.3.0 → python_snap7-3.3.1}/tox.ini +0 -0
@@ -1,6 +1,15 @@
1
1
  CHANGES
2
2
  ========
3
3
 
4
+ 3.3.1
5
+ -----
6
+
7
+ ### Bug fixes
8
+
9
+ * The server now handles multi-item WRITE_VAR requests, such as a `PUT` with several areas. Previously only the
10
+ first item was written while the response still reported success; now every item is written and gets its own
11
+ return code (#959).
12
+
4
13
  3.3.0
5
14
  -----
6
15
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-snap7
3
- Version: 3.3.0
3
+ Version: 3.3.1
4
4
  Summary: Pure Python S7 communication library for Siemens PLCs
5
5
  Author-email: Gijs Molenaar <gijsmolenaar@gmail.com>
6
6
  License-Expression: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "python-snap7"
7
- version = "3.3.0"
7
+ version = "3.3.1"
8
8
  description = "Pure Python S7 communication library for Siemens PLCs"
9
9
  readme = "README.rst"
10
10
  authors = [
@@ -91,5 +91,6 @@ line-length = 130
91
91
  target-version = "py310"
92
92
 
93
93
  [tool.ruff.lint]
94
+ select = ["E4", "E7", "E9", "F"]
94
95
  ignore = []
95
96
  mccabe.max-complexity = 10
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-snap7
3
- Version: 3.3.0
3
+ Version: 3.3.1
4
4
  Summary: Pure Python S7 communication library for Siemens PLCs
5
5
  Author-email: Gijs Molenaar <gijsmolenaar@gmail.com>
6
6
  License-Expression: MIT
@@ -1070,8 +1070,12 @@ class Server:
1070
1070
  return (0x01, bytearray())
1071
1071
 
1072
1072
  def _handle_write_area(self, request: Dict[str, Any], client_address: Tuple[str, int]) -> bytes:
1073
- """Handle write area request."""
1073
+ """Handle write area request (single or multi-item)."""
1074
1074
  try:
1075
+ params = request.get("parameters", {})
1076
+ if params.get("item_count", 1) > 1 and "address_specs" in params:
1077
+ return self._handle_multi_write_area(request, client_address)
1078
+
1075
1079
  # Parse address specification from request parameters
1076
1080
  addr_info = self._parse_write_address(request)
1077
1081
  if not addr_info:
@@ -1122,6 +1126,70 @@ class Server:
1122
1126
  logger.error(f"Error handling write request: {e}")
1123
1127
  return self._build_error_response(request, 0x8000)
1124
1128
 
1129
+ def _handle_multi_write_area(self, request: Dict[str, Any], client_address: Tuple[str, int]) -> bytes:
1130
+ """Handle multi-item write area request.
1131
+
1132
+ Splits the data section into one block per address specification, writes
1133
+ each item to its own area, and returns one return code per item.
1134
+ """
1135
+ params = request["parameters"]
1136
+ address_specs: List[Dict[str, Any]] = params["address_specs"]
1137
+ item_count = len(address_specs)
1138
+ data_section: bytes = request.get("raw_data", b"")
1139
+
1140
+ return_codes = bytearray()
1141
+ offset = 0
1142
+ for i, addr in enumerate(address_specs):
1143
+ if offset + 4 > len(data_section):
1144
+ # Data section is missing this item
1145
+ return_codes.append(0x07)
1146
+ continue
1147
+
1148
+ transport_size = data_section[offset + 1]
1149
+ length = struct.unpack(">H", data_section[offset + 2 : offset + 4])[0]
1150
+ # BIT, REAL and OCTET STRING lengths are in bytes, the others in bits
1151
+ byte_length = length if transport_size in (0x03, 0x07, 0x09) else length // 8
1152
+ write_data = data_section[offset + 4 : offset + 4 + byte_length]
1153
+ offset += 4 + byte_length
1154
+ # Fill byte for even alignment (not after last item)
1155
+ if i < item_count - 1 and byte_length % 2 != 0:
1156
+ offset += 1
1157
+
1158
+ if len(write_data) != byte_length:
1159
+ return_codes.append(0x07)
1160
+ continue
1161
+
1162
+ area = addr.get("area", S7Area.DB)
1163
+ db_number = addr.get("db_number", 0)
1164
+ start = addr.get("start", 0)
1165
+ return_code = self._write_item_to_memory_area(area, db_number, start, bytearray(write_data))
1166
+ return_codes.append(return_code)
1167
+ if return_code == 0xFF:
1168
+ self._emit_event(
1169
+ EVC_DATA_WRITE,
1170
+ param1=self._event_area(area),
1171
+ param2=db_number,
1172
+ param3=start,
1173
+ param4=len(write_data),
1174
+ sender=self._event_sender(client_address),
1175
+ )
1176
+
1177
+ header = struct.pack(
1178
+ ">BBHHHHBB",
1179
+ 0x32,
1180
+ S7PDUType.ACK_DATA,
1181
+ 0x0000,
1182
+ request["sequence"],
1183
+ 0x0002, # param length
1184
+ len(return_codes),
1185
+ 0x00,
1186
+ 0x00,
1187
+ )
1188
+
1189
+ parameters = struct.pack(">BB", S7Function.WRITE_AREA, item_count)
1190
+
1191
+ return header + parameters + bytes(return_codes)
1192
+
1125
1193
  def _handle_plc_control(self, request: Dict[str, Any], client_address: Tuple[str, int]) -> bytes:
1126
1194
  """Handle PLC control request (start, compress, copy_ram_to_rom)."""
1127
1195
  try:
@@ -1236,6 +1304,38 @@ class Server:
1236
1304
  logger.error(f"Error parsing write address: {e}")
1237
1305
  return None
1238
1306
 
1307
+ def _write_item_to_memory_area(self, area: S7Area, db_number: int, start: int, write_data: bytearray) -> int:
1308
+ """
1309
+ Write one item to a registered memory area.
1310
+
1311
+ Returns:
1312
+ Item return code: ``0xFF`` on success, ``0x0A`` when the area is not
1313
+ registered, and ``0x05`` when the range is outside the registered area.
1314
+ Nothing is written unless the whole item fits.
1315
+ """
1316
+ try:
1317
+ area_key = (area, db_number)
1318
+
1319
+ if area_key not in self.memory_areas:
1320
+ logger.warning(f"Memory area {area}#{db_number} not registered for write")
1321
+ return 0x0A
1322
+
1323
+ with self.area_locks[area_key]:
1324
+ area_data = self.memory_areas[area_key]
1325
+
1326
+ if start < 0 or start + len(write_data) > len(area_data):
1327
+ logger.warning(f"Write range [{start}, {start + len(write_data)}) exceeds area size {len(area_data)}")
1328
+ return 0x05
1329
+
1330
+ area_data[start : start + len(write_data)] = write_data
1331
+
1332
+ logger.debug(f"Wrote {len(write_data)} bytes to {area}#{db_number} at offset {start}")
1333
+ return 0xFF
1334
+
1335
+ except Exception as e:
1336
+ logger.error(f"Error writing to memory area: {e}")
1337
+ return 0x01
1338
+
1239
1339
  def _write_to_memory_area(self, area: S7Area, db_number: int, start: int, write_data: bytearray) -> bool:
1240
1340
  """
1241
1341
  Write data to registered memory area.
@@ -1339,6 +1439,7 @@ class Server:
1339
1439
  raise S7ProtocolError("Data section extends beyond PDU")
1340
1440
 
1341
1441
  data_section = pdu[offset : offset + data_len]
1442
+ request["raw_data"] = data_section
1342
1443
  request["data"] = self._parse_data_section(data_section)
1343
1444
  offset += data_len
1344
1445
 
@@ -1393,9 +1494,20 @@ class Server:
1393
1494
  elif function_code == S7Function.WRITE_AREA:
1394
1495
  # Parse write area parameters (same format as read)
1395
1496
  if len(param_data) >= 14: # Minimum for write area request
1396
- # Function code (1) + item count (1) + address spec (12)
1497
+ # Function code (1) + item count (1) + N * address spec (12 each)
1397
1498
  item_count = param_data[1]
1398
1499
 
1500
+ if item_count > 1:
1501
+ # Multi-item write: parse all address specs
1502
+ write_specs: List[Dict[str, Any]] = []
1503
+ offset = 2
1504
+ for _ in range(item_count):
1505
+ if offset + 12 > len(param_data):
1506
+ break
1507
+ write_specs.append(self._parse_address_specification(param_data[offset : offset + 12]))
1508
+ offset += 12
1509
+ return {"function_code": function_code, "item_count": item_count, "address_specs": write_specs}
1510
+
1399
1511
  # Parse address specification starting at byte 2
1400
1512
  if len(param_data) >= 14:
1401
1513
  addr_spec = param_data[2:14] # 12 bytes of address specification
@@ -13,8 +13,9 @@ import pytest
13
13
  from hypothesis import given, settings, strategies as st
14
14
 
15
15
  from snap7.client import Client
16
- from snap7.datatypes import S7Area, S7WordLen
16
+ from snap7.datatypes import S7Area, S7DataTypes, S7WordLen
17
17
  from snap7.error import S7ConnectionError, S7ProtocolError, error_text, server_errors
18
+ from snap7.s7protocol import S7Function, S7PDUType
18
19
  from snap7.server import EVC_DATA_READ, EVC_DATA_WRITE, EVC_SERVER_STARTED, EVC_SERVER_STOPPED, Server, ServerISOConnection
19
20
  from snap7.type import Block, Parameter, SrvArea, SrvEvent, mkEvent, mkLog
20
21
 
@@ -1299,5 +1300,65 @@ def test_download_fragment_coverage_and_commit_boundary(size: int) -> None:
1299
1300
  assert server.memory_areas[(S7Area.DB, 7)] == expected
1300
1301
 
1301
1302
 
1303
+ def _build_multi_write_request(items: list[tuple[int, int, bytes]]) -> bytes:
1304
+ """Build a WRITE_VAR request with one BYTE item per (db_number, start, data)."""
1305
+ param_data = struct.pack(">BB", S7Function.WRITE_AREA, len(items))
1306
+ param_data += b"".join(
1307
+ S7DataTypes.encode_address(S7Area.DB, db, start, S7WordLen.BYTE, len(data)) for db, start, data in items
1308
+ )
1309
+ data_section = bytearray()
1310
+ for i, (_, _, data) in enumerate(items):
1311
+ data_section += struct.pack(">BBH", 0x00, 0x04, len(data) * 8) + data
1312
+ if i < len(items) - 1 and len(data) % 2:
1313
+ data_section.append(0x00) # fill byte
1314
+ header = struct.pack(">BBHHHH", 0x32, S7PDUType.REQUEST, 0, 1, len(param_data), len(data_section))
1315
+ return header + param_data + bytes(data_section)
1316
+
1317
+
1318
+ @pytest.mark.server
1319
+ def test_multi_item_write_writes_every_item() -> None:
1320
+ """A multi-item WRITE_VAR (e.g. a PUT with several areas) writes all items."""
1321
+ server = Server()
1322
+ buffers = {n: bytearray(100) for n in (6, 7, 8, 9)}
1323
+ for n, buf in buffers.items():
1324
+ server.register_area(SrvArea.DB, n, buf)
1325
+
1326
+ items = [
1327
+ (6, 0, bytes([11, 12, 13, 14])),
1328
+ (7, 10, bytes([21, 22])),
1329
+ (8, 20, bytes([31])),
1330
+ (9, 30, bytes([41, 42, 43])),
1331
+ ]
1332
+ response = server._process_request(_build_multi_write_request(items), ("127.0.0.1", 12345))
1333
+
1334
+ assert response is not None
1335
+ assert response[12:14] == bytes([S7Function.WRITE_AREA, 4])
1336
+ assert response[14:] == b"\xff\xff\xff\xff"
1337
+ assert buffers[6][0:4] == bytes([11, 12, 13, 14])
1338
+ assert buffers[7][10:12] == bytes([21, 22])
1339
+ assert buffers[8][20:21] == bytes([31])
1340
+ assert buffers[9][30:33] == bytes([41, 42, 43])
1341
+
1342
+
1343
+ @pytest.mark.server
1344
+ def test_multi_item_write_reports_per_item_errors() -> None:
1345
+ """Failing items get their own return code and do not block the others."""
1346
+ server = Server()
1347
+ buffer = bytearray(10)
1348
+ server.register_area(SrvArea.DB, 1, buffer)
1349
+
1350
+ items = [
1351
+ (99, 0, bytes([1])), # unregistered DB
1352
+ (1, 8, bytes([2, 3, 4])), # crosses the end of DB1
1353
+ (1, 0, bytes([5, 6])),
1354
+ ]
1355
+ response = server._process_request(_build_multi_write_request(items), ("127.0.0.1", 12345))
1356
+
1357
+ assert response is not None
1358
+ assert response[12:14] == bytes([S7Function.WRITE_AREA, 3])
1359
+ assert response[14:] == b"\x0a\x05\xff"
1360
+ assert buffer == bytearray([5, 6, 0, 0, 0, 0, 0, 0, 0, 0])
1361
+
1362
+
1302
1363
  if __name__ == "__main__":
1303
1364
  unittest.main()
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes