scietex.hal.dmm 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,15 @@
1
+ """Scietex digital multimeter HAL package.
2
+
3
+ A digital multimeter measures voltage, current, resistance, frequency,
4
+ capacitance, continuity and diode drop, one function at a time. This package
5
+ defines the capability model that tells a caller which of those functions, ranges
6
+ and switches a given meter supports, the frozen snapshots it reports, the
7
+ transport and driver contracts a concrete instrument implements, and the registry
8
+ that resolves a driver by name.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ from .version import __version__
14
+
15
+ __all__ = ["__version__"]
@@ -0,0 +1,103 @@
1
+ """
2
+ Base digital multimeter subpackage.
3
+
4
+ Holds the transport-agnostic pieces every driver shares: the capability model
5
+ that advertises what a meter can do, the frozen snapshots it reports, the error
6
+ hierarchy, the transport contract, and the driver contract concrete protocols
7
+ implement.
8
+
9
+ Modules:
10
+ capabilities: Defines the capability model and the SCPI spelling enums.
11
+ data: Defines the identity, state and reading structs.
12
+ errors: Defines the errors shared by drivers.
13
+ protocol: Defines the `DigitalMultimeterProtocol` driver contract.
14
+ transport: Defines the `TransportProtocol` byte-pipe contract and the
15
+ message-size limits.
16
+
17
+ Exports:
18
+ Access: Whether a setting can be read and whether it can be written.
19
+ CommandSet: The SCPI dialect a meter speaks.
20
+ DigitalMultimeterProtocol: The contract every digital multimeter driver
21
+ implements.
22
+ DmmCapabilities: The complete capability set for one meter.
23
+ DmmError: Base class for every error raised by this package.
24
+ DmmSpecifications: Static limit data for one model.
25
+ DmmState: The function, range and switches a meter currently reports.
26
+ FramingError: A response is not one terminated SCPI message.
27
+ FunctionCapabilities: The supported functions and their range tables.
28
+ Identity: The vendor, model, serial and firmware strings of one meter.
29
+ InputImpedance: The selectable DC voltage input impedance.
30
+ InputImpedanceCapabilities: The input impedance switch and its options.
31
+ MAX_ERROR_DRAIN: Largest number of error-queue entries a drain may read.
32
+ MAX_MESSAGE_BYTES: Largest single response a transport may return.
33
+ MeasurementFunction: The measurement functions a meter can select.
34
+ RATE_READINGS_PER_SECOND: Readings per second for each integration rate.
35
+ Range: One fixed measurement range.
36
+ RangeSelector: The range selection sentinels accepted by a command.
37
+ Rate: The integration rate the meter applies to one reading.
38
+ Reading: One measurement value with its unit and function.
39
+ ScpiError: The device reported a SCPI error for a command.
40
+ TransportClosed: The transport was used after it was closed.
41
+ TransportError: The byte pipe failed.
42
+ TransportProtocol: A byte pipe to one instrument.
43
+ TransportTimeout: A transport operation exceeded its deadline.
44
+ UnexpectedResponse: A response cannot belong to the command it answers.
45
+ """
46
+
47
+ from __future__ import annotations
48
+
49
+ from .capabilities import (
50
+ RATE_READINGS_PER_SECOND,
51
+ Access,
52
+ CommandSet,
53
+ DmmCapabilities,
54
+ DmmSpecifications,
55
+ FunctionCapabilities,
56
+ InputImpedance,
57
+ InputImpedanceCapabilities,
58
+ MeasurementFunction,
59
+ Range,
60
+ RangeSelector,
61
+ Rate,
62
+ )
63
+ from .data import DmmState, Identity, Reading
64
+ from .errors import (
65
+ DmmError,
66
+ FramingError,
67
+ ScpiError,
68
+ TransportClosed,
69
+ TransportError,
70
+ TransportTimeout,
71
+ UnexpectedResponse,
72
+ )
73
+ from .protocol import DigitalMultimeterProtocol
74
+ from .transport import MAX_ERROR_DRAIN, MAX_MESSAGE_BYTES, TransportProtocol
75
+
76
+ __all__ = [
77
+ "Access",
78
+ "CommandSet",
79
+ "DigitalMultimeterProtocol",
80
+ "DmmCapabilities",
81
+ "DmmError",
82
+ "DmmSpecifications",
83
+ "DmmState",
84
+ "FramingError",
85
+ "FunctionCapabilities",
86
+ "Identity",
87
+ "InputImpedance",
88
+ "InputImpedanceCapabilities",
89
+ "MAX_ERROR_DRAIN",
90
+ "MAX_MESSAGE_BYTES",
91
+ "MeasurementFunction",
92
+ "RATE_READINGS_PER_SECOND",
93
+ "Range",
94
+ "RangeSelector",
95
+ "Rate",
96
+ "Reading",
97
+ "ScpiError",
98
+ "TransportClosed",
99
+ "TransportError",
100
+ "TransportProtocol",
101
+ "TransportTimeout",
102
+ "UnexpectedResponse",
103
+ ]
@@ -0,0 +1,307 @@
1
+ """
2
+ Digital multimeter capability model.
3
+
4
+ A meter advertises what it can do through a single :class:`DmmCapabilities`
5
+ value, grouped by function rather than by method name. A driver maps its own SCPI
6
+ vocabulary onto these groups, so a caller can ask whether a measurement function
7
+ is supported and which ranges it accepts without knowing the instrument's command
8
+ set.
9
+
10
+ Every group defaults to "unsupported": :class:`Access` is falsy, option tuples
11
+ are empty and range tables are absent. A driver declares only what it implements,
12
+ and the driver contract derives its ``supports_*`` and ``can_*`` properties from
13
+ these values. A capability answers whether a feature exists; it does not answer
14
+ whether the feature is currently on.
15
+
16
+ The groups are frozen dataclasses, so a driver declares them once as a class
17
+ attribute and callers cannot mutate them. Range tables are not validated here:
18
+ the protocol layer and the driver decide which range indices are legal for which
19
+ model.
20
+
21
+ Classes:
22
+ MeasurementFunction: The measurement functions a meter can select.
23
+ Rate: The integration rate the meter applies to one reading.
24
+ RangeSelector: The range selection sentinels accepted by a command.
25
+ CommandSet: The SCPI dialect a meter speaks.
26
+ InputImpedance: The selectable DC voltage input impedance.
27
+ Range: One fixed measurement range.
28
+ Access: Whether a setting can be read and whether it can be written.
29
+ FunctionCapabilities: The supported functions and their range tables.
30
+ InputImpedanceCapabilities: The input impedance switch and its options.
31
+ DmmSpecifications: Static limit data for one model.
32
+ DmmCapabilities: The complete capability set for one meter.
33
+
34
+ Constants:
35
+ RATE_READINGS_PER_SECOND: Readings per second for each integration rate.
36
+ """
37
+
38
+ from __future__ import annotations
39
+
40
+ from collections.abc import Mapping
41
+ from dataclasses import dataclass, field
42
+ from enum import Enum
43
+ from typing import Final
44
+
45
+
46
+ class MeasurementFunction(str, Enum):
47
+ """The measurement functions a meter can select.
48
+
49
+ The member value is the short SCPI spelling a driver sends on the wire. It
50
+ doubles as the serialised form because the class inherits from ``str``.
51
+ """
52
+
53
+ DCV = "VOLT:DC"
54
+ ACV = "VOLT:AC"
55
+ DCI = "CURR:DC"
56
+ ACI = "CURR:AC"
57
+ RES2W = "RES"
58
+ RES4W = "FRES"
59
+ FREQ = "FREQ"
60
+ PERIOD = "PER"
61
+ CAP = "CAP"
62
+ CONT = "CONT"
63
+ DIODE = "DIODe"
64
+
65
+
66
+ class Rate(str, Enum):
67
+ """The integration rate the meter applies to one reading.
68
+
69
+ A faster rate returns more readings per second at the cost of noise
70
+ rejection. The member value is the SCPI spelling of the rate.
71
+ """
72
+
73
+ FAST = "F"
74
+ MEDIUM = "M"
75
+ SLOW = "S"
76
+
77
+
78
+ RATE_READINGS_PER_SECOND: Final[Mapping[Rate, float]] = {
79
+ Rate.FAST: 123.0,
80
+ Rate.MEDIUM: 20.0,
81
+ Rate.SLOW: 2.5,
82
+ }
83
+ """Readings per second the meter sustains at each integration rate."""
84
+
85
+
86
+ class RangeSelector(str, Enum):
87
+ """The range selection sentinels accepted by a range command.
88
+
89
+ A numeric range selector picks a fixed range by index; these sentinels
90
+ delegate the choice to the meter. The member value is the SCPI spelling a
91
+ driver sends.
92
+ """
93
+
94
+ MIN = "MIN"
95
+ MAX = "MAX"
96
+ DEF = "DEF"
97
+
98
+
99
+ class CommandSet(str, Enum):
100
+ """The SCPI dialect a meter speaks.
101
+
102
+ The dialects share the measurement mnemonics but differ in setup, trigger and
103
+ error-queue syntax. The member value is the documented dialect name.
104
+ """
105
+
106
+ RIGOL = "RIGOL"
107
+ AGILENT = "AGILENT"
108
+ FLUKE = "FLUKE"
109
+
110
+
111
+ class InputImpedance(str, Enum):
112
+ """The selectable DC voltage input impedance.
113
+
114
+ The member value is the SCPI spelling: ten megohm is the usual default, ten
115
+ gigohm reduces the load on high-impedance sources. The setting applies to DC
116
+ voltage only.
117
+ """
118
+
119
+ MEG10 = "10M"
120
+ GIG10 = "10G"
121
+
122
+
123
+ @dataclass(frozen=True, slots=True)
124
+ class Range:
125
+ """
126
+ One fixed measurement range.
127
+
128
+ Attributes:
129
+ index: The device range index the meter accepts in a range command. It
130
+ is the driver's index, not a position in the capability table.
131
+ full_scale: The largest magnitude the range measures, in ``unit``.
132
+ unit: The unit of ``full_scale``: ``"V"``, ``"A"``, ``"OHM"``, ``"F"``,
133
+ ``"HZ"`` or ``"S"``.
134
+ label: A human-readable label, such as ``"200 mV"``.
135
+ """
136
+
137
+ index: int
138
+ full_scale: float
139
+ unit: str
140
+ label: str
141
+
142
+
143
+ @dataclass(frozen=True, slots=True)
144
+ class Access:
145
+ """
146
+ Whether a setting can be read and whether it can be written.
147
+
148
+ Attributes:
149
+ read: Whether the current value can be read back.
150
+ write: Whether the value can be set.
151
+ """
152
+
153
+ read: bool = False
154
+ write: bool = False
155
+
156
+ def __bool__(self) -> bool:
157
+ """Return whether the setting is supported in either direction."""
158
+ return self.read or self.write
159
+
160
+
161
+ @dataclass(frozen=True, slots=True)
162
+ class FunctionCapabilities:
163
+ """
164
+ The supported measurement functions and their range tables.
165
+
166
+ Attributes:
167
+ functions: The measurement functions the meter can select. An empty tuple
168
+ means no measurement function is supported.
169
+ ranges: Fixed ranges per function, in the meter's own order. A function
170
+ absent from the mapping has no range table, which means the driver
171
+ does not enumerate its ranges; it does **not** mean the function is
172
+ unsupported.
173
+ rate: Whether the integration rate can be read and set.
174
+ auto_range: Whether the auto-range switch can be read and set.
175
+ """
176
+
177
+ functions: tuple[MeasurementFunction, ...] = ()
178
+ ranges: Mapping[MeasurementFunction, tuple[Range, ...]] = field(
179
+ default_factory=dict
180
+ )
181
+ rate: Access = field(default_factory=Access)
182
+ auto_range: Access = field(default_factory=Access)
183
+
184
+ def supports(self, function: MeasurementFunction) -> bool:
185
+ """
186
+ Return whether ``function`` is selectable.
187
+
188
+ Args:
189
+ function: The measurement function to test.
190
+
191
+ Returns:
192
+ bool: ``True`` when ``function`` appears in :attr:`functions`.
193
+ """
194
+ return function in self.functions
195
+
196
+ def ranges_for(self, function: MeasurementFunction) -> tuple[Range, ...]:
197
+ """
198
+ Return the fixed ranges of ``function``.
199
+
200
+ Args:
201
+ function: The measurement function to look up.
202
+
203
+ Returns:
204
+ tuple[Range, ...]: The range table, or ``()`` when the driver does
205
+ not enumerate ranges for that function. A missing table is not an
206
+ error.
207
+ """
208
+ return self.ranges.get(function, ())
209
+
210
+ def range_for_index(
211
+ self, function: MeasurementFunction, index: int
212
+ ) -> Range | None:
213
+ """
214
+ Look up one range of ``function`` by its device index.
215
+
216
+ Args:
217
+ function: The measurement function to look up.
218
+ index: The device range index to find.
219
+
220
+ Returns:
221
+ Range | None: The matching range, or ``None`` when the function has
222
+ no table or the table holds no range with that index.
223
+ """
224
+ return next(
225
+ (item for item in self.ranges_for(function) if item.index == index),
226
+ None,
227
+ )
228
+
229
+
230
+ @dataclass(frozen=True, slots=True)
231
+ class InputImpedanceCapabilities:
232
+ """
233
+ The DC voltage input impedance switch and its options.
234
+
235
+ Attributes:
236
+ access: Whether the impedance can be read and set.
237
+ options: The impedances the meter accepts. An empty tuple means the
238
+ driver does not enumerate the domain.
239
+ """
240
+
241
+ access: Access = field(default_factory=Access)
242
+ options: tuple[InputImpedance, ...] = ()
243
+
244
+ def __bool__(self) -> bool:
245
+ """Return whether the impedance is supported in either direction."""
246
+ return bool(self.access)
247
+
248
+
249
+ @dataclass(frozen=True, slots=True)
250
+ class DmmSpecifications:
251
+ """
252
+ Static limit data for one meter model.
253
+
254
+ The values describe the instrument rather than its current state, so they
255
+ live in the capability set and do not change while the meter runs.
256
+
257
+ Attributes:
258
+ digits: Display digits, where a half digit is counted as ``0.5``.
259
+ max_dcv_v: Largest DC voltage magnitude the meter accepts, in V.
260
+ max_acv_v: Largest AC voltage magnitude the meter accepts, in V.
261
+ max_current_a: Largest current magnitude the meter accepts, in A.
262
+ max_resistance_ohm: Largest resistance the meter measures, in ohm.
263
+ best_dcv_resolution_v: Finest DC voltage resolution in V, or ``None``
264
+ when the driver does not know it.
265
+ continuity_max_ohm: Largest resistance the continuity function reports
266
+ as continuous, in ohm, or ``None`` when the driver does not know it.
267
+ diode_max_v: Largest forward drop the diode function measures, in V, or
268
+ ``None`` when the driver does not know it.
269
+ """
270
+
271
+ digits: float = 5.5
272
+ max_dcv_v: float = 1000.0
273
+ max_acv_v: float = 750.0
274
+ max_current_a: float = 10.0
275
+ max_resistance_ohm: float = 100.0e6
276
+ best_dcv_resolution_v: float | None = None
277
+ continuity_max_ohm: float | None = None
278
+ diode_max_v: float | None = None
279
+
280
+
281
+ @dataclass(frozen=True, slots=True)
282
+ class DmmCapabilities:
283
+ """
284
+ The complete capability set for one meter.
285
+
286
+ Every group defaults to "unsupported", so a driver declares only what it
287
+ implements. A feature that exists but is switched off stays truthy here; the
288
+ on/off state belongs to the snapshot.
289
+
290
+ Attributes:
291
+ measurement: Supported functions, their ranges, the rate switch and the
292
+ auto-range switch.
293
+ input_impedance: The DC voltage input impedance switch and its options.
294
+ identity: Whether the meter can be identified.
295
+ error_queue: Whether the device error queue can be read.
296
+ command_set: Whether the SCPI dialect can be read.
297
+ specifications: Static limit data for the model.
298
+ """
299
+
300
+ measurement: FunctionCapabilities = field(default_factory=FunctionCapabilities)
301
+ input_impedance: InputImpedanceCapabilities = field(
302
+ default_factory=InputImpedanceCapabilities
303
+ )
304
+ identity: Access = field(default_factory=Access)
305
+ error_queue: Access = field(default_factory=Access)
306
+ command_set: Access = field(default_factory=Access)
307
+ specifications: DmmSpecifications = field(default_factory=DmmSpecifications)
@@ -0,0 +1,108 @@
1
+ """Digital multimeter data structures.
2
+
3
+ Three frozen ``msgspec`` structs carry what a meter reports:
4
+ :class:`Identity` is the model and firmware string set, :class:`DmmState` is the
5
+ selected function, range and switches, and :class:`Reading` is one measurement.
6
+
7
+ ``None`` means "the driver cannot report this", never 0, 0.0 or False. A field
8
+ that is ``None`` has no known value at all, so a caller must check for ``None``
9
+ rather than treating a missing reading as a zero measurement or a missing switch
10
+ as switched off. A driver that does know the value reports it exactly, including
11
+ ``False`` for a flag that is off and ``0.0`` for a genuine zero reading.
12
+
13
+ Unknown keyword arguments are rejected on construction, so a typo in a driver
14
+ fails at the call site instead of silently leaving a field at its default.
15
+
16
+ Classes:
17
+ Identity: The vendor, model, serial and firmware strings of one meter.
18
+ DmmState: The function, range and switches a meter currently reports.
19
+ Reading: One measurement value with its unit and function.
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ import msgspec
25
+
26
+ from .capabilities import CommandSet, InputImpedance, MeasurementFunction, Rate
27
+
28
+
29
+ class Identity(msgspec.Struct, frozen=True, kw_only=True, forbid_unknown_fields=True):
30
+ """
31
+ The identity of one meter.
32
+
33
+ Attributes:
34
+ vendor: The vendor string the meter reports.
35
+ model: The model string the meter reports.
36
+ serial: The serial number string the meter reports.
37
+ firmware: The firmware revision string the meter reports.
38
+ raw: The identity response exactly as received, or ``""`` when the
39
+ driver did not keep it.
40
+ """
41
+
42
+ vendor: str
43
+ model: str
44
+ serial: str
45
+ firmware: str
46
+ raw: str = ""
47
+
48
+
49
+ class DmmState(msgspec.Struct, frozen=True, kw_only=True, forbid_unknown_fields=True):
50
+ """
51
+ A snapshot of the function, range and switches a meter reports.
52
+
53
+ Each field is ``None`` when the driver cannot report it. ``None`` is not
54
+ ``0``, not ``0.0`` and not ``False``: an unreported range index is unknown,
55
+ not range zero, and an unreadable auto-range flag is unknown, not off.
56
+
57
+ Attributes:
58
+ function: The selected measurement function, or ``None`` when the driver
59
+ cannot report it.
60
+ range_index: The selected fixed range index, or ``None`` when the driver
61
+ cannot report it. ``None`` is not range zero.
62
+ auto_range: Whether auto-range is on, or ``None`` when the driver cannot
63
+ report it. ``None`` is not ``False``.
64
+ rate: The integration rate, or ``None`` when the driver cannot report it.
65
+ input_impedance: The selected DC voltage input impedance, or ``None`` when
66
+ the driver cannot report it.
67
+ command_set: The SCPI dialect the meter speaks, or ``None`` when the
68
+ driver cannot report it.
69
+ trigger_source: The trigger source string as the meter spells it, or
70
+ ``None`` when the driver cannot report it.
71
+ """
72
+
73
+ function: MeasurementFunction | None = None
74
+ range_index: int | None = None
75
+ auto_range: bool | None = None
76
+ rate: Rate | None = None
77
+ input_impedance: InputImpedance | None = None
78
+ command_set: CommandSet | None = None
79
+ trigger_source: str | None = None
80
+
81
+
82
+ class Reading(msgspec.Struct, frozen=True, kw_only=True, forbid_unknown_fields=True):
83
+ """
84
+ One measurement reported by the meter.
85
+
86
+ Every field is ``None`` when the driver cannot report it. ``None`` is not
87
+ ``0.0``: an unreadable value is unknown, not a zero measurement, and an
88
+ unreadable overload flag is unknown, not clear.
89
+
90
+ Attributes:
91
+ value: The measured magnitude in :attr:`unit`, or ``None`` when the meter
92
+ reported no numeric value. An overload reported as a numeric value
93
+ keeps that value and sets :attr:`overload`.
94
+ unit: The unit of :attr:`value`, or ``None`` when the driver cannot
95
+ report it.
96
+ function: The measurement function that produced the reading, or ``None``
97
+ when the driver cannot report it.
98
+ overload: Whether the meter reported an overload, or ``None`` when the
99
+ driver cannot report it. ``None`` is not ``False``.
100
+ raw: The response text exactly as received, or ``""`` when the driver did
101
+ not keep it.
102
+ """
103
+
104
+ value: float | None = None
105
+ unit: str | None = None
106
+ function: MeasurementFunction | None = None
107
+ overload: bool | None = None
108
+ raw: str = ""
@@ -0,0 +1,84 @@
1
+ """Errors shared by digital multimeter drivers.
2
+
3
+ These failures are transport-independent: they describe a driver that cannot
4
+ answer, a response that is not one terminated message, a response that cannot
5
+ belong to its command, or a device that reported a SCPI error. The transport
6
+ raises its own subclasses of :class:`TransportError`, so a caller can catch one
7
+ package base class around the whole stack.
8
+
9
+ Classes:
10
+ DmmError: Base class for every error raised by this package.
11
+ TransportError: The byte pipe failed.
12
+ TransportClosed: The transport was used after it was closed.
13
+ TransportTimeout: A transport operation exceeded its deadline.
14
+ FramingError: A response is not one terminated SCPI message.
15
+ UnexpectedResponse: A response cannot belong to the command it answers.
16
+ ScpiError: The device reported a SCPI error for a command.
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+
22
+ class DmmError(Exception):
23
+ """Base class for every error raised by this package."""
24
+
25
+
26
+ class TransportError(DmmError):
27
+ """The byte pipe failed: open, write, read, reset or close."""
28
+
29
+
30
+ class TransportClosed(TransportError):
31
+ """The transport was used after it was closed."""
32
+
33
+
34
+ class TransportTimeout(TransportError):
35
+ """A transport operation exceeded its deadline.
36
+
37
+ Attributes:
38
+ requires_recovery (bool): Whether the transport must be reset or
39
+ reopened before the next request. A timeout leaves the command and
40
+ the response out of step, so the default is ``True``; a transport
41
+ that realigns itself overrides it.
42
+ """
43
+
44
+ requires_recovery: bool = True
45
+
46
+
47
+ class FramingError(DmmError):
48
+ """A response is not one terminated SCPI message."""
49
+
50
+
51
+ class UnexpectedResponse(DmmError):
52
+ """A response cannot belong to the command it answers."""
53
+
54
+
55
+ class ScpiError(DmmError):
56
+ """
57
+ The device reported a SCPI error for a command.
58
+
59
+ Args:
60
+ code: The numeric error code from ``:SYST:ERR?``.
61
+ message: The device-supplied error message.
62
+ command: The command that produced the error, when known.
63
+
64
+ Attributes:
65
+ code (int): The numeric error code.
66
+ message (str): The device-supplied message.
67
+ command (str | None): The offending command, or ``None`` when unknown.
68
+ """
69
+
70
+ def __init__(self, code: int, message: str, *, command: str | None = None) -> None:
71
+ self.code = code
72
+ self.message = message
73
+ self.command = command
74
+ super().__init__(self._render())
75
+
76
+ def __str__(self) -> str:
77
+ """Render the code, the quoted message and the command when known."""
78
+ return self._render()
79
+
80
+ def _render(self) -> str:
81
+ rendered = f'{self.code},"{self.message}"'
82
+ if self.command is None:
83
+ return rendered
84
+ return f"{rendered} ({self.command})"