python-snap7 3.3.1__tar.gz → 3.4.0__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.1 → python_snap7-3.4.0}/CHANGES.md +28 -0
  2. {python_snap7-3.3.1/python_snap7.egg-info → python_snap7-3.4.0}/PKG-INFO +1 -1
  3. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/tags.rst +17 -1
  4. {python_snap7-3.3.1 → python_snap7-3.4.0}/pyproject.toml +1 -1
  5. {python_snap7-3.3.1 → python_snap7-3.4.0/python_snap7.egg-info}/PKG-INFO +1 -1
  6. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/async_client.py +11 -22
  7. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/client.py +9 -195
  8. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/connection.py +5 -2
  9. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/partner.py +6 -2
  10. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/s7protocol.py +3 -2
  11. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/server/__init__.py +3 -2
  12. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/tags.py +230 -9
  13. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/util/setters.py +48 -2
  14. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_async_client.py +19 -1
  15. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_conformance.py +3 -3
  16. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_connection.py +23 -2
  17. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_multipacket.py +98 -1
  18. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_partner.py +19 -0
  19. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_s7protocol.py +6 -0
  20. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_server.py +44 -0
  21. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_tags.py +66 -1
  22. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_util.py +83 -1
  23. {python_snap7-3.3.1 → python_snap7-3.4.0}/LICENSE +0 -0
  24. {python_snap7-3.3.1 → python_snap7-3.4.0}/MANIFEST.in +0 -0
  25. {python_snap7-3.3.1 → python_snap7-3.4.0}/README.rst +0 -0
  26. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/client-base.rst +0 -0
  27. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/client.rst +0 -0
  28. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/connection.rst +0 -0
  29. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/datatypes.rst +0 -0
  30. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/demo.rst +0 -0
  31. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/discovery.rst +0 -0
  32. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/error.rst +0 -0
  33. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/log.rst +0 -0
  34. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/logo.rst +0 -0
  35. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/optimizer.rst +0 -0
  36. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/partner.rst +0 -0
  37. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/ppi.rst +0 -0
  38. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/rate-limiter.rst +0 -0
  39. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/s7protocol.rst +0 -0
  40. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/server.rst +0 -0
  41. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/szl.rst +0 -0
  42. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/type.rst +0 -0
  43. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/API/util.rst +0 -0
  44. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/Makefile +0 -0
  45. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/_static/.phony +0 -0
  46. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/advanced.rst +0 -0
  47. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/cli.rst +0 -0
  48. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/conf.py +0 -0
  49. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/connecting.rst +0 -0
  50. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/connection-issues.rst +0 -0
  51. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/development.rst +0 -0
  52. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/error-reference.rst +0 -0
  53. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/index.rst +0 -0
  54. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/installation.rst +0 -0
  55. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/introduction.rst +0 -0
  56. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/limitations.rst +0 -0
  57. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/multi-variable.rst +0 -0
  58. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/plc-support.rst +0 -0
  59. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/ppi.rst +0 -0
  60. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/reading-writing.rst +0 -0
  61. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/real-plc-testing.rst +0 -0
  62. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/server.rst +0 -0
  63. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/thread-safety.rst +0 -0
  64. {python_snap7-3.3.1 → python_snap7-3.4.0}/doc/tia-portal-config.rst +0 -0
  65. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/boolean.py +0 -0
  66. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/db1_layout.txt +0 -0
  67. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/db_layouts.py +0 -0
  68. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/example.py +0 -0
  69. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/logo_7_8.py +0 -0
  70. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/ppi.py +0 -0
  71. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/prometheus_metrics.py +0 -0
  72. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/read_multi.py +0 -0
  73. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/s7_symbols.py +0 -0
  74. {python_snap7-3.3.1 → python_snap7-3.4.0}/example/write_multi.py +0 -0
  75. {python_snap7-3.3.1 → python_snap7-3.4.0}/python_snap7.egg-info/SOURCES.txt +0 -0
  76. {python_snap7-3.3.1 → python_snap7-3.4.0}/python_snap7.egg-info/dependency_links.txt +0 -0
  77. {python_snap7-3.3.1 → python_snap7-3.4.0}/python_snap7.egg-info/entry_points.txt +0 -0
  78. {python_snap7-3.3.1 → python_snap7-3.4.0}/python_snap7.egg-info/requires.txt +0 -0
  79. {python_snap7-3.3.1 → python_snap7-3.4.0}/python_snap7.egg-info/top_level.txt +0 -0
  80. {python_snap7-3.3.1 → python_snap7-3.4.0}/s7/__init__.py +0 -0
  81. {python_snap7-3.3.1 → python_snap7-3.4.0}/setup.cfg +0 -0
  82. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/__init__.py +0 -0
  83. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/cli.py +0 -0
  84. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/client_base.py +0 -0
  85. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/datatypes.py +0 -0
  86. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/demo.py +0 -0
  87. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/discovery.py +0 -0
  88. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/error.py +0 -0
  89. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/log.py +0 -0
  90. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/logo.py +0 -0
  91. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/optimizer.py +0 -0
  92. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/ppi.py +0 -0
  93. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/py.typed +0 -0
  94. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/rate_limiter.py +0 -0
  95. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/server/__main__.py +0 -0
  96. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/szl.py +0 -0
  97. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/type.py +0 -0
  98. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/util/__init__.py +0 -0
  99. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/util/db.py +0 -0
  100. {python_snap7-3.3.1 → python_snap7-3.4.0}/snap7/util/getters.py +0 -0
  101. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/__init__.py +0 -0
  102. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/conftest.py +0 -0
  103. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/features/real_plc/connection.feature +0 -0
  104. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/features/real_plc/lifecycle.feature +0 -0
  105. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/features/real_plc/read.feature +0 -0
  106. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/features/real_plc/report.schema.json +0 -0
  107. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/features/real_plc/write.feature +0 -0
  108. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/plc_setup/e2e_test_dbs.scl +0 -0
  109. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/real_plc/__init__.py +0 -0
  110. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/real_plc/reporting.py +0 -0
  111. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/real_plc/support.py +0 -0
  112. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/real_plc/test_acceptance.py +0 -0
  113. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_api_surface.py +0 -0
  114. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_async_typed_access.py +0 -0
  115. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_behavioral_compatibility.py +0 -0
  116. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_cli.py +0 -0
  117. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_client.py +0 -0
  118. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_client_e2e.py +0 -0
  119. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_coverage_gaps.py +0 -0
  120. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_datatypes.py +0 -0
  121. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_demo.py +0 -0
  122. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_discovery.py +0 -0
  123. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_error.py +0 -0
  124. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_finalize.py +0 -0
  125. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_hypothesis.py +0 -0
  126. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_lean_regressions.py +0 -0
  127. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_logging.py +0 -0
  128. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_logo_client.py +0 -0
  129. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_logo_reconnect_options.py +0 -0
  130. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_mainloop.py +0 -0
  131. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_operation_hook.py +0 -0
  132. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_optimizer.py +0 -0
  133. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_ppi.py +0 -0
  134. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_property.py +0 -0
  135. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_rate_limiter.py +0 -0
  136. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_real_plc_support.py +0 -0
  137. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_reconnect.py +0 -0
  138. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_routing.py +0 -0
  139. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_server_cli.py +0 -0
  140. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_stress.py +0 -0
  141. {python_snap7-3.3.1 → python_snap7-3.4.0}/tests/test_typed_access.py +0 -0
  142. {python_snap7-3.3.1 → python_snap7-3.4.0}/tox.ini +0 -0
@@ -1,6 +1,34 @@
1
1
  CHANGES
2
2
  ========
3
3
 
4
+ 3.4.0
5
+ -----
6
+
7
+ ### New features
8
+
9
+ * Add `snap7.tags.decode_tag` and `snap7.tags.encode_tag`, which convert between a tag's raw bytes and a Python
10
+ value, for applications that batch their own raw reads and writes. The private names in `snap7.client` still
11
+ work (#973).
12
+
13
+ ### Bug fixes
14
+
15
+ * `set_wchar` now raises `ValueError` for a character above U+FFFF, as `set_wstring` does. Previously it grew the
16
+ buffer by two bytes, shifting every later field (#968).
17
+ * Clock data with a milliseconds digit above 9 is now rejected, by the client when reading the clock and by the server
18
+ when setting it. Previously `12 a3` decoded to `.130` (#966).
19
+ * `parse_tag` now raises `ValueError` under `strict=True`, the default, for a type with no known size, such as
20
+ `DB1.DBB0:STRING` without a length. Previously this failed only when `.size` was read. `S5TIME` now has a size
21
+ and can be read, but not yet written (#974).
22
+ * `read_szl` and `list_blocks_of_type` now check the PDU reference of follow-up replies, in both clients. Previously a
23
+ stale fragment was added to the result (#967).
24
+ * `disconnect()` now addresses its COTP disconnect request to the PLC's connection reference, in both clients.
25
+ Previously it used the client's own reference. That is the likely reason a CP 343-1 kept each connection
26
+ allocated until it was restarted (#972, not yet confirmed on the CP). The passive `Partner` now echoes the active
27
+ partner's reference in its connection confirm (#979).
28
+ * All `snap7.util` setters now raise `ValueError` when the value doesn't fit in the buffer. Most of them silently grew
29
+ a short `bytearray`, which changed the length written to the PLC or shifted later fields, and `set_lreal`,
30
+ `set_string`, `set_bool` and the other single-byte setters raised `struct.error` or `IndexError` (#975).
31
+
4
32
  3.3.1
5
33
  -----
6
34
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-snap7
3
- Version: 3.3.1
3
+ Version: 3.4.0
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
@@ -79,6 +79,22 @@ forms like ``M7.1`` or ``IW22`` (dispatched to the nodeS7 parser)::
79
79
  parse_tag("M7.1") # raises: ambiguous under strict=True
80
80
  parse_tag("M7.1", strict=False) # → NodeS7Tag (BOOL)
81
81
 
82
+ Encoding and decoding
83
+ ---------------------
84
+
85
+ :func:`~snap7.tags.decode_tag` and :func:`~snap7.tags.encode_tag` convert
86
+ between a tag's raw bytes and a Python value, for applications that batch their
87
+ own raw reads and writes instead of using ``read_tag`` and ``write_tag``::
88
+
89
+ from s7.tags import decode_tag, encode_tag, parse_tag
90
+
91
+ tag = parse_tag("DB1.DBD0:REAL")
92
+ value = decode_tag(tag, client.db_read(tag.db_number, tag.byte_offset, tag.size))
93
+
94
+ buf = bytearray(tag.size)
95
+ encode_tag(tag, buf, 21.5)
96
+ client.db_write(tag.db_number, tag.byte_offset, buf)
97
+
82
98
  Supported types
83
99
  ---------------
84
100
 
@@ -86,7 +102,7 @@ Supported types
86
102
  ``UINT``, ``WORD``, ``DINT``, ``UDINT``, ``DWORD``, ``LINT``, ``ULINT``,
87
103
  ``LWORD``, ``REAL``, ``LREAL``, ``TIME``, ``LTIME``, ``TOD``, ``LTOD``,
88
104
  ``DATE``, ``DT``, ``LDT``, ``DTL``, ``STRING[n]``, ``WSTRING[n]``,
89
- ``FSTRING[n]``.
105
+ ``FSTRING[n]``. ``S5TIME`` can be read but not yet written.
90
106
 
91
107
  Arrays are supported for any fixed-size type via ``[count]`` suffix.
92
108
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "python-snap7"
7
- version = "3.3.1"
7
+ version = "3.4.0"
8
8
  description = "Pure Python S7 communication library for Siemens PLCs"
9
9
  readme = "README.rst"
10
10
  authors = [
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-snap7
3
- Version: 3.3.1
3
+ Version: 3.4.0
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
@@ -21,8 +21,8 @@ from .s7protocol import S7Protocol, S7UserDataGroup, S7UserDataSubfunction, get_
21
21
  from .datatypes import S7DataTypes, S7WordLen
22
22
  from .error import S7Error, S7ConnectionError, S7ProtocolError, S7TimeoutError
23
23
  from .client_base import ClientMixin, _instrumented
24
- from .tags import Tag
25
- from .client import Client, _decode_tag, _encode_tag
24
+ from .tags import Tag, decode_tag, encode_tag
25
+ from .client import Client
26
26
  from .szl import parse_cp_info_szl, parse_cpu_info_szl, parse_order_code_szl, parse_protection_szl
27
27
  from .client import _parse_force_szl
28
28
  from .rate_limiter import RateLimitAlgorithm, RateLimitBehavior, RequestRateLimiter
@@ -255,7 +255,8 @@ class AsyncISOTCPConnection:
255
255
  if pdu_type != self.COTP_CC:
256
256
  raise S7ConnectionError(f"Expected COTP CC, got {pdu_type:#02x}")
257
257
 
258
- self.dst_ref = dst_ref
258
+ # The CC's source reference is the peer's; see ISOTCPConnection._parse_cotp_cc (#972).
259
+ self.dst_ref = src_ref
259
260
 
260
261
  # Parse parameters
261
262
  offset = 7
@@ -794,7 +795,7 @@ class AsyncClient(ClientMixin):
794
795
  if resolved.is_symbolic:
795
796
  raise NotImplementedError("Symbolic (LID-based) tag access is not supported by the classic S7 client.")
796
797
  data = await self.read_area(Area(resolved.area), resolved.db_number, resolved.byte_offset, resolved.size)
797
- return _decode_tag(resolved, bytearray(data), encoding=encoding)
798
+ return decode_tag(resolved, bytearray(data), encoding=encoding)
798
799
 
799
800
  async def write_tag(self, tag: "Union[Tag, str]", value: Any, encoding: str = "latin-1") -> int:
800
801
  """Write a typed value by :class:`~snap7.tags.Tag` or address string. Returns 0 on success."""
@@ -806,7 +807,7 @@ class AsyncClient(ClientMixin):
806
807
  # Preserve the other bits of the byte
807
808
  current = await self.read_area(Area(resolved.area), resolved.db_number, resolved.byte_offset, 1)
808
809
  buf[0] = current[0]
809
- _encode_tag(resolved, buf, value, encoding=encoding)
810
+ encode_tag(resolved, buf, value, encoding=encoding)
810
811
  return await self.write_area(Area(resolved.area), resolved.db_number, resolved.byte_offset, buf)
811
812
 
812
813
  async def read_tags(self, tags: "Sequence[Union[Tag, str]]", encoding: str = "latin-1") -> list[Any]:
@@ -814,7 +815,7 @@ class AsyncClient(ClientMixin):
814
815
  resolved = [Tag.from_string(t) if isinstance(t, str) else t for t in tags]
815
816
  items = [{"area": Area(t.area), "db_number": t.db_number, "start": t.byte_offset, "size": t.size} for t in resolved]
816
817
  _code, data_list = await self.read_multi_vars(items)
817
- return [_decode_tag(t, bytearray(d), encoding=encoding) for t, d in zip(resolved, data_list)]
818
+ return [decode_tag(t, bytearray(d), encoding=encoding) for t, d in zip(resolved, data_list)]
818
819
 
819
820
  async def db_get(self, db_number: int, size: int = 0) -> bytearray:
820
821
  """Get entire DB.
@@ -1022,8 +1023,6 @@ class AsyncClient(ClientMixin):
1022
1023
  if not self.get_connected():
1023
1024
  raise S7ConnectionError("Not connected to PLC")
1024
1025
 
1025
- conn = self._get_connection()
1026
-
1027
1026
  block_type_codes = {
1028
1027
  Block.OB: 0x38,
1029
1028
  Block.DB: 0x41,
@@ -1057,12 +1056,8 @@ class AsyncClient(ClientMixin):
1057
1056
  if last_data_unit == 0x00:
1058
1057
  break
1059
1058
 
1060
- async with self._lock:
1061
- followup = self.protocol.build_userdata_followup_request(group, subfunction, sequence_number)
1062
- await self._send_data(conn, followup)
1063
- response_data = await conn.receive_data()
1064
-
1065
- response = self.protocol.parse_response(response_data)
1059
+ followup = self.protocol.build_userdata_followup_request(group, subfunction, sequence_number)
1060
+ response = await self._send_receive(followup)
1066
1061
  self.protocol.check_userdata_response(response, S7UserDataGroup.BLOCK_INFO, S7UserDataSubfunction.LIST_BLOCKS_OF_TYPE)
1067
1062
 
1068
1063
  data_info = response.get("data", {})
@@ -1346,8 +1341,6 @@ class AsyncClient(ClientMixin):
1346
1341
  if not self.get_connected():
1347
1342
  raise S7ConnectionError("Not connected to PLC")
1348
1343
 
1349
- conn = self._get_connection()
1350
-
1351
1344
  request = self.protocol.build_read_szl_request(ssl_id, index)
1352
1345
  response = await self._send_receive(request)
1353
1346
  self.protocol.check_userdata_response(response, S7UserDataGroup.SZL, S7UserDataSubfunction.READ_SZL)
@@ -1371,12 +1364,8 @@ class AsyncClient(ClientMixin):
1371
1364
  if last_data_unit == 0x00:
1372
1365
  break
1373
1366
 
1374
- async with self._lock:
1375
- followup = self.protocol.build_userdata_followup_request(group, subfunction, sequence_number)
1376
- await self._send_data(conn, followup)
1377
- response_data = await conn.receive_data()
1378
-
1379
- response = self.protocol.parse_response(response_data)
1367
+ followup = self.protocol.build_userdata_followup_request(group, subfunction, sequence_number)
1368
+ response = await self._send_receive(followup)
1380
1369
  self.protocol.check_userdata_response(response, S7UserDataGroup.SZL, S7UserDataSubfunction.READ_SZL)
1381
1370
 
1382
1371
  data_info = response.get("data", {})
@@ -29,8 +29,7 @@ from .client_base import ClientMixin, _instrumented
29
29
  from .log import PLCLoggerAdapter, OperationLogger
30
30
  from .optimizer import ReadItem, ReadPacket, sort_items, merge_items, packetize, extract_results
31
31
  from .rate_limiter import RateLimitAlgorithm, RateLimitBehavior, RequestRateLimiter
32
- from .tags import Tag, _STRING_RE
33
- from . import util
32
+ from .tags import Tag, decode_tag, encode_tag
34
33
 
35
34
  from .szl import parse_cp_info_szl, parse_cpu_info_szl, parse_order_code_szl, parse_protection_szl
36
35
  from .type import (
@@ -55,184 +54,9 @@ _VALID_AREA_VALUES: frozenset[int] = frozenset(a.value for a in Area)
55
54
 
56
55
  logger = logging.getLogger(__name__)
57
56
 
58
-
59
- def _decode_tag(tag: Tag, data: bytearray, encoding: str = "latin-1") -> Any:
60
- """Decode a Tag's raw bytes into a typed Python value."""
61
- upper = tag.datatype.upper()
62
- # Variable-length string types
63
- match = _STRING_RE.match(upper)
64
- if match:
65
- kind, length = match.group(1), int(match.group(2))
66
- if kind == "FSTRING":
67
- return util.get_fstring(data, 0, length, encoding=encoding)
68
- if kind == "STRING":
69
- return util.get_string(data, 0, encoding=encoding)
70
- if kind == "WSTRING":
71
- return util.get_wstring(data, 0)
72
-
73
- # Arrays
74
- if tag.count > 1:
75
- per = tag.size // tag.count
76
- return [_decode_scalar(upper, data[i * per : (i + 1) * per], tag.bit) for i in range(tag.count)]
77
-
78
- return _decode_scalar(upper, data, tag.bit)
79
-
80
-
81
- def _decode_scalar(datatype: str, data: bytearray, bit: int) -> Any:
82
- """Decode a single scalar value of the given type."""
83
- if datatype == "BOOL":
84
- return util.get_bool(data, 0, bit)
85
- if datatype == "BYTE":
86
- return util.get_byte(data, 0)
87
- if datatype == "SINT":
88
- return util.get_sint(data, 0)
89
- if datatype == "USINT":
90
- return util.get_usint(data, 0)
91
- if datatype == "CHAR":
92
- return util.get_char(data, 0)
93
- if datatype == "INT":
94
- return util.get_int(data, 0)
95
- if datatype == "UINT":
96
- return util.get_uint(data, 0)
97
- if datatype == "WORD":
98
- return util.get_word(data, 0)
99
- if datatype == "WCHAR":
100
- return util.get_wchar(data, 0)
101
- if datatype == "DATE":
102
- return util.get_date(data, 0)
103
- if datatype == "DINT":
104
- return util.get_dint(data, 0)
105
- if datatype == "UDINT":
106
- return util.get_udint(data, 0)
107
- if datatype == "DWORD":
108
- return util.get_dword(data, 0)
109
- if datatype == "REAL":
110
- return util.get_real(data, 0)
111
- if datatype == "TIME":
112
- return util.get_time(data, 0)
113
- if datatype == "TOD":
114
- return util.get_tod(data, 0)
115
- if datatype == "LINT":
116
- return util.get_lint(data, 0)
117
- if datatype == "ULINT":
118
- return util.get_ulint(data, 0)
119
- if datatype == "LWORD":
120
- return util.get_lword(data, 0)
121
- if datatype == "LREAL":
122
- return util.get_lreal(data, 0)
123
- if datatype == "LTIME":
124
- return util.get_ltime(data, 0)
125
- if datatype == "LTOD":
126
- return util.get_ltod(data, 0)
127
- if datatype == "LDT":
128
- return util.get_ldt(data, 0)
129
- if datatype == "DT":
130
- return util.get_dt(data, 0)
131
- if datatype == "DTL":
132
- return util.get_dtl(data, 0)
133
- raise ValueError(f"Unsupported tag datatype: {datatype}")
134
-
135
-
136
- def _encode_tag(tag: Tag, buf: bytearray, value: Any, encoding: str = "latin-1") -> None:
137
- """Encode a typed Python value into a Tag's byte buffer."""
138
- upper = tag.datatype.upper()
139
- match = _STRING_RE.match(upper)
140
- if match:
141
- kind, length = match.group(1), int(match.group(2))
142
- if kind == "FSTRING":
143
- util.set_fstring(buf, 0, value, length, encoding=encoding)
144
- return
145
- if kind == "STRING":
146
- util.set_string(buf, 0, value, length, encoding=encoding)
147
- return
148
- if kind == "WSTRING":
149
- util.set_wstring(buf, 0, value, length)
150
- return
151
-
152
- if tag.count > 1:
153
- per = tag.size // tag.count
154
- for i, v in enumerate(value):
155
- _encode_scalar(upper, buf, i * per, v, tag.bit)
156
- return
157
-
158
- _encode_scalar(upper, buf, 0, value, tag.bit)
159
-
160
-
161
- def _encode_scalar(datatype: str, buf: bytearray, offset: int, value: Any, bit: int) -> None:
162
- """Encode a single scalar value at the given offset."""
163
- if datatype == "BOOL":
164
- util.set_bool(buf, offset, bit, value)
165
- return
166
- if datatype in ("BYTE", "USINT"):
167
- util.set_byte(buf, offset, value) if datatype == "BYTE" else util.set_usint(buf, offset, value)
168
- return
169
- if datatype == "SINT":
170
- util.set_sint(buf, offset, value)
171
- return
172
- if datatype == "CHAR":
173
- util.set_char(buf, offset, value)
174
- return
175
- if datatype == "INT":
176
- util.set_int(buf, offset, value)
177
- return
178
- if datatype == "UINT":
179
- util.set_uint(buf, offset, value)
180
- return
181
- if datatype == "WORD":
182
- util.set_word(buf, offset, value)
183
- return
184
- if datatype == "WCHAR":
185
- util.set_wchar(buf, offset, value)
186
- return
187
- if datatype == "DATE":
188
- util.set_date(buf, offset, value)
189
- return
190
- if datatype == "DINT":
191
- util.set_dint(buf, offset, value)
192
- return
193
- if datatype == "UDINT":
194
- util.set_udint(buf, offset, value)
195
- return
196
- if datatype == "DWORD":
197
- util.set_dword(buf, offset, value)
198
- return
199
- if datatype == "REAL":
200
- util.set_real(buf, offset, value)
201
- return
202
- if datatype == "TIME":
203
- util.set_time(buf, offset, value)
204
- return
205
- if datatype == "TOD":
206
- util.set_tod(buf, offset, value)
207
- return
208
- if datatype == "LINT":
209
- util.set_lint(buf, offset, value)
210
- return
211
- if datatype == "ULINT":
212
- util.set_ulint(buf, offset, value)
213
- return
214
- if datatype == "LWORD":
215
- util.set_lword(buf, offset, value)
216
- return
217
- if datatype == "LREAL":
218
- util.set_lreal(buf, offset, value)
219
- return
220
- if datatype == "LTIME":
221
- util.set_ltime(buf, offset, value)
222
- return
223
- if datatype == "LTOD":
224
- util.set_ltod(buf, offset, value)
225
- return
226
- if datatype == "LDT":
227
- util.set_ldt(buf, offset, value)
228
- return
229
- if datatype == "DT":
230
- util.set_dt(buf, offset, value)
231
- return
232
- if datatype == "DTL":
233
- util.set_dtl(buf, offset, value)
234
- return
235
- raise ValueError(f"Unsupported tag datatype: {datatype}")
57
+ # Old private names; ha-s7 releases up to 0.1.4 import these from here.
58
+ _decode_tag = decode_tag
59
+ _encode_tag = encode_tag
236
60
 
237
61
 
238
62
  def _parse_force_szl(raw: bytes) -> list[ForceEntry]:
@@ -864,7 +688,7 @@ class Client(ClientMixin):
864
688
  if resolved.is_symbolic:
865
689
  raise NotImplementedError("Symbolic (LID-based) tag access is not supported by the classic S7 client.")
866
690
  data = self.read_area(Area(resolved.area), resolved.db_number, resolved.byte_offset, resolved.size)
867
- return _decode_tag(resolved, bytearray(data), encoding=encoding)
691
+ return decode_tag(resolved, bytearray(data), encoding=encoding)
868
692
 
869
693
  def write_tag(self, tag: "Union[Tag, str]", value: Any, encoding: str = "latin-1") -> int:
870
694
  """Write a typed value by :class:`~snap7.tags.Tag` or address string.
@@ -886,7 +710,7 @@ class Client(ClientMixin):
886
710
  if resolved.datatype.upper() == "BOOL":
887
711
  current = self.read_area(Area(resolved.area), resolved.db_number, resolved.byte_offset, 1)
888
712
  buf[0] = current[0]
889
- _encode_tag(resolved, buf, value, encoding=encoding)
713
+ encode_tag(resolved, buf, value, encoding=encoding)
890
714
  return self.write_area(Area(resolved.area), resolved.db_number, resolved.byte_offset, buf)
891
715
 
892
716
  def read_tags(self, tags: "Sequence[Union[Tag, str]]", encoding: str = "latin-1") -> list[Any]:
@@ -905,7 +729,7 @@ class Client(ClientMixin):
905
729
  resolved = [Tag.from_string(t) if isinstance(t, str) else t for t in tags]
906
730
  items = [{"area": Area(t.area), "db_number": t.db_number, "start": t.byte_offset, "size": t.size} for t in resolved]
907
731
  _code, data_list = self.read_multi_vars(items)
908
- return [_decode_tag(t, bytearray(d), encoding=encoding) for t, d in zip(resolved, data_list)]
732
+ return [decode_tag(t, bytearray(d), encoding=encoding) for t, d in zip(resolved, data_list)]
909
733
 
910
734
  def db_read(self, db_number: int, start: int, size: int) -> bytearray:
911
735
  """
@@ -1491,8 +1315,6 @@ class Client(ClientMixin):
1491
1315
  if not self.get_connected():
1492
1316
  raise S7ConnectionError("Not connected to PLC")
1493
1317
 
1494
- conn = self._get_connection()
1495
-
1496
1318
  # Map Block enum to S7 block type codes
1497
1319
  block_type_codes = {
1498
1320
  Block.OB: 0x38, # Organization Block
@@ -1534,10 +1356,7 @@ class Client(ClientMixin):
1534
1356
  break
1535
1357
 
1536
1358
  followup = self.protocol.build_userdata_followup_request(group, subfunction, sequence_number)
1537
- self._send_data(conn, followup)
1538
-
1539
- response_data = conn.receive_data()
1540
- response = self.protocol.parse_response(response_data)
1359
+ response = self._send_receive(followup)
1541
1360
  self.protocol.check_userdata_response(response, S7UserDataGroup.BLOCK_INFO, S7UserDataSubfunction.LIST_BLOCKS_OF_TYPE)
1542
1361
 
1543
1362
  # Check for errors
@@ -2143,8 +1962,6 @@ class Client(ClientMixin):
2143
1962
  if not self.get_connected():
2144
1963
  raise S7ConnectionError("Not connected to PLC")
2145
1964
 
2146
- conn = self._get_connection()
2147
-
2148
1965
  # Build and send read SZL request
2149
1966
  request = self.protocol.build_read_szl_request(ssl_id, index)
2150
1967
  response = self._send_receive(request)
@@ -2174,10 +1991,7 @@ class Client(ClientMixin):
2174
1991
  break
2175
1992
 
2176
1993
  followup = self.protocol.build_userdata_followup_request(group, subfunction, sequence_number)
2177
- self._send_data(conn, followup)
2178
-
2179
- response_data = conn.receive_data()
2180
- response = self.protocol.parse_response(response_data)
1994
+ response = self._send_receive(followup)
2181
1995
  self.protocol.check_userdata_response(response, S7UserDataGroup.SZL, S7UserDataSubfunction.READ_SZL)
2182
1996
 
2183
1997
  # Check for errors
@@ -342,7 +342,10 @@ class ISOTCPConnection:
342
342
  """
343
343
  Parse COTP Connection Confirm PDU.
344
344
 
345
- Extracts destination reference and negotiated PDU size.
345
+ Extracts the peer's reference and the negotiated PDU size. The CC's
346
+ source reference is the peer's own reference, which every later TPDU
347
+ addressed to it (the DR) carries as its destination reference. Its
348
+ destination reference only echoes ours (#972).
346
349
  """
347
350
  if len(data) < 7:
348
351
  raise S7ConnectionError("Invalid COTP CC: too short")
@@ -352,7 +355,7 @@ class ISOTCPConnection:
352
355
  if pdu_type != self.COTP_CC:
353
356
  raise S7ConnectionError(f"Expected COTP CC, got {pdu_type:#02x}")
354
357
 
355
- self.dst_ref = dst_ref
358
+ self.dst_ref = src_ref
356
359
 
357
360
  # Parse parameters if present
358
361
  if len(data) > 7:
@@ -838,12 +838,16 @@ class Partner:
838
838
  if self._connection is None:
839
839
  raise S7ConnectionError("No connection object")
840
840
 
841
+ # The CR's source reference is the active partner's own; the CC echoes it
842
+ # as its destination reference, and our DR addresses it later (#972).
843
+ peer_ref = struct.unpack(">H", payload[4:6])[0]
844
+ self._connection.dst_ref = peer_ref
841
845
  cc_pdu = struct.pack(
842
846
  ">BBHHB",
843
847
  6, # PDU length
844
848
  0xD0, # COTP_CC
845
- self._connection.src_ref, # Destination reference (our src_ref)
846
- 0x0001, # Source reference
849
+ peer_ref, # Destination reference (the active partner's)
850
+ self._connection.src_ref, # Source reference (ours)
847
851
  0x00, # Class 0
848
852
  )
849
853
  # Add PDU size parameter
@@ -1312,9 +1312,10 @@ class S7Protocol:
1312
1312
  microsecond = 0
1313
1313
  if len(raw_data) == 10:
1314
1314
  # Byte 8: milliseconds hundreds/tens (BCD); high nibble of byte 9: milliseconds ones
1315
- microsecond = (from_bcd(time_bytes[6]) * 10 + (time_bytes[7] >> 4)) * 1000
1316
- if microsecond > 999000:
1315
+ millisecond_ones = time_bytes[7] >> 4
1316
+ if millisecond_ones > 9:
1317
1317
  raise S7ProtocolError("Clock response contains an invalid BCD timestamp")
1318
+ microsecond = (from_bcd(time_bytes[6]) * 10 + millisecond_ones) * 1000
1318
1319
 
1319
1320
  # The two-digit S7 year maps 90-99 to 1990-1999 and 00-89 to 2000-2089
1320
1321
  full_year = 2000 + year if year < 90 else 1900 + year
@@ -2036,9 +2036,10 @@ class Server:
2036
2036
  try:
2037
2037
  from datetime import datetime
2038
2038
 
2039
- millisecond = from_bcd(raw_data[8]) * 10 + (raw_data[9] >> 4)
2040
- if millisecond > 999 or not 1 <= (raw_data[9] & 0x0F) <= 7:
2039
+ millisecond_ones = raw_data[9] >> 4
2040
+ if millisecond_ones > 9 or not 1 <= (raw_data[9] & 0x0F) <= 7:
2041
2041
  raise ValueError("Invalid clock millisecond or weekday")
2042
+ millisecond = from_bcd(raw_data[8]) * 10 + millisecond_ones
2042
2043
  year = from_bcd(raw_data[2])
2043
2044
  timestamp = datetime(
2044
2045
  2000 + year if year < 90 else 1900 + year,