scietex.hal.current-amplifier 0.3.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.
- scietex/hal/current_amplifier/__init__.py +10 -0
- scietex/hal/current_amplifier/base/__init__.py +71 -0
- scietex/hal/current_amplifier/base/capabilities.py +541 -0
- scietex/hal/current_amplifier/base/data.py +88 -0
- scietex/hal/current_amplifier/base/errors.py +47 -0
- scietex/hal/current_amplifier/base/protocol.py +411 -0
- scietex/hal/current_amplifier/py.typed +0 -0
- scietex/hal/current_amplifier/registry.py +66 -0
- scietex/hal/current_amplifier/scietex/__init__.py +5 -0
- scietex/hal/current_amplifier/scietex/ebic/__init__.py +5 -0
- scietex/hal/current_amplifier/scietex/ebic/modbus/__init__.py +5 -0
- scietex/hal/current_amplifier/scietex/ebic/modbus/v1/__init__.py +38 -0
- scietex/hal/current_amplifier/scietex/ebic/modbus/v1/client.py +497 -0
- scietex/hal/current_amplifier/scietex/ebic/modbus/v1/constants.py +108 -0
- scietex/hal/current_amplifier/scietex/ebic/modbus/v1/identity.py +80 -0
- scietex/hal/current_amplifier/scietex/ebic/modbus/v1/relaymap.py +347 -0
- scietex/hal/current_amplifier/scietex/ebic/modbus/v1/semantic.py +690 -0
- scietex/hal/current_amplifier/version.py +1 -0
- scietex_hal_current_amplifier-0.3.0.dist-info/METADATA +393 -0
- scietex_hal_current_amplifier-0.3.0.dist-info/RECORD +24 -0
- scietex_hal_current_amplifier-0.3.0.dist-info/WHEEL +5 -0
- scietex_hal_current_amplifier-0.3.0.dist-info/entry_points.txt +3 -0
- scietex_hal_current_amplifier-0.3.0.dist-info/licenses/LICENSE +21 -0
- scietex_hal_current_amplifier-0.3.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
"""Modbus RTU driver for the EBIC v1.0 preamp relay layer.
|
|
2
|
+
|
|
3
|
+
The firmware in ``ebic_control`` exposes seventeen relay outputs and the
|
|
4
|
+
communication settings that reach them. It has no ADC, DAC, measurement, status
|
|
5
|
+
or unlock register, so this driver implements the relay layer. It declares only
|
|
6
|
+
what the raw coil layer alone can declare: the pre-amp's 5 V clip point, the
|
|
7
|
+
booster's +/-10 V linear range, and the physical presence of the Overcurrent
|
|
8
|
+
comparator. The relay-to-function map now fixes the selectable controls, but this
|
|
9
|
+
driver still addresses raw coils, so the semantic layer built on it
|
|
10
|
+
(``semantic.EBICModbusV1Amplifier``) declares them.
|
|
11
|
+
|
|
12
|
+
The transport is the shared :class:`RS485Client`, which opens the port once per
|
|
13
|
+
operation; ``connect`` therefore has nothing to open and every accessor is a
|
|
14
|
+
coroutine. All requests carry the address the instance was built with. A slave
|
|
15
|
+
address or baud change is acknowledged at the old settings and followed by a
|
|
16
|
+
firmware restart after ``REBOOT_DELAY_S``; see the setters for the consequences.
|
|
17
|
+
|
|
18
|
+
Classes:
|
|
19
|
+
EBICModbusV1Preamp: The EBIC v1.0 relay driver.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from collections.abc import Sequence
|
|
23
|
+
from logging import Logger
|
|
24
|
+
from typing import ClassVar
|
|
25
|
+
|
|
26
|
+
from pymodbus.pdu import ModbusPDU
|
|
27
|
+
from pymodbus.pdu.bit_message import (
|
|
28
|
+
ReadCoilsRequest,
|
|
29
|
+
ReadCoilsResponse,
|
|
30
|
+
WriteMultipleCoilsRequest,
|
|
31
|
+
WriteMultipleCoilsResponse,
|
|
32
|
+
WriteSingleCoilRequest,
|
|
33
|
+
WriteSingleCoilResponse,
|
|
34
|
+
)
|
|
35
|
+
from pymodbus.pdu.register_message import (
|
|
36
|
+
ReadHoldingRegistersRequest,
|
|
37
|
+
ReadHoldingRegistersResponse,
|
|
38
|
+
ReadInputRegistersRequest,
|
|
39
|
+
ReadInputRegistersResponse,
|
|
40
|
+
WriteSingleRegisterRequest,
|
|
41
|
+
WriteSingleRegisterResponse,
|
|
42
|
+
)
|
|
43
|
+
from scietex.hal.serial import (
|
|
44
|
+
ModbusSerialConnectionConfig,
|
|
45
|
+
RS485Client,
|
|
46
|
+
SerialConnectionConfig,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
from .....base import (
|
|
50
|
+
AmplifierState,
|
|
51
|
+
CurrentAmplifierCapabilities,
|
|
52
|
+
CurrentAmplifierProtocol,
|
|
53
|
+
IndicationCapabilities,
|
|
54
|
+
OutputVoltageCapabilities,
|
|
55
|
+
PreampCapabilities,
|
|
56
|
+
UnexpectedResponse,
|
|
57
|
+
)
|
|
58
|
+
from .constants import (
|
|
59
|
+
BAUD_INDEX_TO_RATE,
|
|
60
|
+
COILS,
|
|
61
|
+
DEFAULT_SLAVE_ADDRESS,
|
|
62
|
+
INPUT_REGISTERS,
|
|
63
|
+
RATE_TO_BAUD_INDEX,
|
|
64
|
+
REGISTERS,
|
|
65
|
+
RELAY_COUNT,
|
|
66
|
+
SLAVE_ADDRESS_MAX,
|
|
67
|
+
SLAVE_ADDRESS_MIN,
|
|
68
|
+
)
|
|
69
|
+
from .identity import IDENTITY_REGISTER_COUNT, DeviceIdentity, decode_identity
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class EBICModbusV1Preamp(RS485Client, CurrentAmplifierProtocol):
|
|
73
|
+
"""Modbus RTU client for the EBIC v1.0 relay outputs.
|
|
74
|
+
|
|
75
|
+
Attributes:
|
|
76
|
+
capabilities: The static analog geometry the board fixes: the pre-amp
|
|
77
|
+
clip point, the booster linear range and the physical Overcurrent
|
|
78
|
+
comparator. No selectable control is declared here, because this
|
|
79
|
+
class drives coils rather than functions; the semantic layer owns the
|
|
80
|
+
relay map and declares the selectable groups.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
# Only geometry is declared on the raw driver: it speaks coil indices, not
|
|
84
|
+
# functions. The pre-amp clips at 5 V, the booster is linear over +/-10 V,
|
|
85
|
+
# and the Overcurrent comparator exists on the board but is unreachable over
|
|
86
|
+
# Modbus. The selectable groups are declared by the semantic layer, which
|
|
87
|
+
# owns the relay-to-function map and the Rf values.
|
|
88
|
+
capabilities: ClassVar[CurrentAmplifierCapabilities] = CurrentAmplifierCapabilities(
|
|
89
|
+
preamp=PreampCapabilities(max_output_v=5.0),
|
|
90
|
+
output_voltage=OutputVoltageCapabilities(min_v=-10.0, max_v=10.0),
|
|
91
|
+
input_overcurrent=IndicationCapabilities(present=True, read=False),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
def __init__(
|
|
95
|
+
self,
|
|
96
|
+
con_params: SerialConnectionConfig | ModbusSerialConnectionConfig,
|
|
97
|
+
address: int = DEFAULT_SLAVE_ADDRESS,
|
|
98
|
+
label: str = "EBIC Preamp",
|
|
99
|
+
logger: Logger | None = None,
|
|
100
|
+
) -> None:
|
|
101
|
+
"""
|
|
102
|
+
Initialize the client.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
con_params: Serial port and framing configuration.
|
|
106
|
+
address: Modbus slave address the device answers at.
|
|
107
|
+
label: Human-readable label used in log messages.
|
|
108
|
+
logger: Optional logger for transport diagnostics.
|
|
109
|
+
"""
|
|
110
|
+
super().__init__(
|
|
111
|
+
con_params=con_params,
|
|
112
|
+
address=address,
|
|
113
|
+
label=label,
|
|
114
|
+
logger=logger,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
async def connect(self) -> None:
|
|
118
|
+
"""Nothing to open: the serial client connects once per operation."""
|
|
119
|
+
self.logger.debug("%s: Connect (per-operation serial client).", self.label)
|
|
120
|
+
|
|
121
|
+
async def close(self) -> None:
|
|
122
|
+
"""Close the underlying Modbus client and release the serial port."""
|
|
123
|
+
await super().close()
|
|
124
|
+
|
|
125
|
+
# `RS485Client.read_data` returns a raw register dictionary; the current
|
|
126
|
+
# amplifier contract replaces it with a typed snapshot, so the override is
|
|
127
|
+
# deliberate and the Liskov warning is expected. Remove the ignore once the
|
|
128
|
+
# serial client's `read_data` matches the contract signature or the contract
|
|
129
|
+
# stops narrowing the return type.
|
|
130
|
+
async def read_data(self) -> AmplifierState: # ty: ignore[incompatible-method-override]
|
|
131
|
+
"""
|
|
132
|
+
Return an empty snapshot.
|
|
133
|
+
|
|
134
|
+
The EBIC v1.0 firmware reports no output voltage, current, gain, bias,
|
|
135
|
+
bandwidth, filter or status value. Every field therefore stays
|
|
136
|
+
``None`` and the filter tuple stays empty. ``None`` is not zero and not
|
|
137
|
+
``False``: the quantities are unknown, not idle. Relay levels are read
|
|
138
|
+
through :meth:`read_relays` instead.
|
|
139
|
+
|
|
140
|
+
Returns:
|
|
141
|
+
AmplifierState: A snapshot with every field unset.
|
|
142
|
+
"""
|
|
143
|
+
return AmplifierState()
|
|
144
|
+
|
|
145
|
+
# -- relays ------------------------------------------------------------
|
|
146
|
+
|
|
147
|
+
async def read_relays(self) -> tuple[bool, ...]:
|
|
148
|
+
"""
|
|
149
|
+
Read all seventeen relay output levels.
|
|
150
|
+
|
|
151
|
+
The levels are read with FC01 from coils 0-16, the table the firmware
|
|
152
|
+
writes on FC05/FC0F and the one its tester reads. The same levels are
|
|
153
|
+
mirrored in input registers 36-52, but that mirror is only reachable
|
|
154
|
+
through a 53-register input read, so coils are the narrower path for the
|
|
155
|
+
relay layer.
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
tuple[bool, ...]: Relay levels in coil order, relay 0 first. The
|
|
159
|
+
firmware zeroes the unused high bits of the last data byte, so they
|
|
160
|
+
are discarded.
|
|
161
|
+
|
|
162
|
+
Raises:
|
|
163
|
+
UnexpectedResponse: If the response carries fewer than seventeen
|
|
164
|
+
coils or is not a coil response.
|
|
165
|
+
"""
|
|
166
|
+
response = await self.execute(
|
|
167
|
+
ReadCoilsRequest(
|
|
168
|
+
address=COILS.RELAY_START,
|
|
169
|
+
count=RELAY_COUNT,
|
|
170
|
+
dev_id=self.address,
|
|
171
|
+
)
|
|
172
|
+
)
|
|
173
|
+
if not isinstance(response, ReadCoilsResponse) or response.bits is None:
|
|
174
|
+
raise UnexpectedResponse(
|
|
175
|
+
"relays", f"expected {RELAY_COUNT} coils, got {response!r}"
|
|
176
|
+
)
|
|
177
|
+
if len(response.bits) < RELAY_COUNT:
|
|
178
|
+
raise UnexpectedResponse(
|
|
179
|
+
"relays", f"expected {RELAY_COUNT} coils, got {len(response.bits)}"
|
|
180
|
+
)
|
|
181
|
+
return tuple(response.bits[:RELAY_COUNT])
|
|
182
|
+
|
|
183
|
+
async def read_relay(self, index: int) -> bool:
|
|
184
|
+
"""
|
|
185
|
+
Read one relay output level.
|
|
186
|
+
|
|
187
|
+
Args:
|
|
188
|
+
index: Relay index, 0-16.
|
|
189
|
+
|
|
190
|
+
Returns:
|
|
191
|
+
bool: The level the firmware drives on that relay.
|
|
192
|
+
|
|
193
|
+
Raises:
|
|
194
|
+
IndexError: If ``index`` is outside 0-16.
|
|
195
|
+
UnexpectedResponse: If the response carries no coil value.
|
|
196
|
+
"""
|
|
197
|
+
_check_relay_index(index)
|
|
198
|
+
response = await self.execute(
|
|
199
|
+
ReadCoilsRequest(address=index, count=1, dev_id=self.address)
|
|
200
|
+
)
|
|
201
|
+
if not isinstance(response, ReadCoilsResponse) or not response.bits:
|
|
202
|
+
raise UnexpectedResponse(f"relay {index}", f"no coil value in {response!r}")
|
|
203
|
+
return bool(response.bits[0])
|
|
204
|
+
|
|
205
|
+
async def set_relay(self, index: int, state: bool) -> None:
|
|
206
|
+
"""
|
|
207
|
+
Drive one relay output.
|
|
208
|
+
|
|
209
|
+
The firmware writes the level to EEPROM before changing the GPIO, so the
|
|
210
|
+
setting survives a restart. Frequent switching consumes flash write
|
|
211
|
+
cycles; this is a state interface, not a PWM output.
|
|
212
|
+
|
|
213
|
+
Args:
|
|
214
|
+
index: Relay index, 0-16.
|
|
215
|
+
state: ``True`` for logic high, ``False`` for logic low. The level
|
|
216
|
+
reaches the GPIO, not the relay contact: the driver board may
|
|
217
|
+
invert it.
|
|
218
|
+
|
|
219
|
+
Raises:
|
|
220
|
+
IndexError: If ``index`` is outside 0-16.
|
|
221
|
+
UnexpectedResponse: If the device does not echo the written coil.
|
|
222
|
+
"""
|
|
223
|
+
_check_relay_index(index)
|
|
224
|
+
response = await self.execute(
|
|
225
|
+
WriteSingleCoilRequest(
|
|
226
|
+
address=index,
|
|
227
|
+
bits=[bool(state)],
|
|
228
|
+
dev_id=self.address,
|
|
229
|
+
)
|
|
230
|
+
)
|
|
231
|
+
if (
|
|
232
|
+
not isinstance(response, WriteSingleCoilResponse)
|
|
233
|
+
or response.address != index
|
|
234
|
+
or response.bits != [bool(state)]
|
|
235
|
+
):
|
|
236
|
+
raise UnexpectedResponse(
|
|
237
|
+
f"relay {index}",
|
|
238
|
+
f"expected echo of coil {index} = {bool(state)}, got {response!r}",
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
async def set_relays(self, states: Sequence[bool]) -> None:
|
|
242
|
+
"""
|
|
243
|
+
Drive all seventeen relay outputs with FC0F.
|
|
244
|
+
|
|
245
|
+
The firmware applies the levels one by one and persists each to EEPROM,
|
|
246
|
+
so the group write is not atomic: a flash failure or power loss can
|
|
247
|
+
leave only part of the group applied. Re-read :meth:`read_relays` after
|
|
248
|
+
an error instead of assuming either the old or the new state.
|
|
249
|
+
|
|
250
|
+
Args:
|
|
251
|
+
states: Seventeen levels in coil order, relay 0 first.
|
|
252
|
+
|
|
253
|
+
Raises:
|
|
254
|
+
ValueError: If fewer or more than seventeen levels are given, before
|
|
255
|
+
any bus traffic.
|
|
256
|
+
UnexpectedResponse: If the device does not echo the written range.
|
|
257
|
+
"""
|
|
258
|
+
values = tuple(bool(state) for state in states)
|
|
259
|
+
if len(values) != RELAY_COUNT:
|
|
260
|
+
raise ValueError(f"expected {RELAY_COUNT} relay states, got {len(values)}")
|
|
261
|
+
response = await self.execute(
|
|
262
|
+
WriteMultipleCoilsRequest(
|
|
263
|
+
address=COILS.RELAY_START,
|
|
264
|
+
bits=list(values),
|
|
265
|
+
dev_id=self.address,
|
|
266
|
+
)
|
|
267
|
+
)
|
|
268
|
+
if (
|
|
269
|
+
not isinstance(response, WriteMultipleCoilsResponse)
|
|
270
|
+
or response.address != COILS.RELAY_START
|
|
271
|
+
or response.count != RELAY_COUNT
|
|
272
|
+
):
|
|
273
|
+
raise UnexpectedResponse(
|
|
274
|
+
"relays",
|
|
275
|
+
f"expected echo of {RELAY_COUNT} coils at {COILS.RELAY_START}, "
|
|
276
|
+
f"got {response!r}",
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
# -- identity ----------------------------------------------------------
|
|
280
|
+
|
|
281
|
+
async def read_identity(self) -> DeviceIdentity:
|
|
282
|
+
"""
|
|
283
|
+
Read the vendor, product, version and serial number.
|
|
284
|
+
|
|
285
|
+
One FC04 request reads input registers 0-34, the whole identity block.
|
|
286
|
+
|
|
287
|
+
Returns:
|
|
288
|
+
DeviceIdentity: The decoded identity.
|
|
289
|
+
|
|
290
|
+
Raises:
|
|
291
|
+
UnexpectedResponse: If the response carries a different number of
|
|
292
|
+
registers or is not an input-register response.
|
|
293
|
+
"""
|
|
294
|
+
registers = await self._read_input(
|
|
295
|
+
INPUT_REGISTERS.VENDOR_START,
|
|
296
|
+
IDENTITY_REGISTER_COUNT,
|
|
297
|
+
"identity",
|
|
298
|
+
)
|
|
299
|
+
return decode_identity(registers)
|
|
300
|
+
|
|
301
|
+
async def read_firmware_version(self) -> tuple[int, int, int]:
|
|
302
|
+
"""
|
|
303
|
+
Read the firmware version.
|
|
304
|
+
|
|
305
|
+
Returns:
|
|
306
|
+
tuple[int, int, int]: ``(major, minor, patch)``.
|
|
307
|
+
|
|
308
|
+
Raises:
|
|
309
|
+
UnexpectedResponse: If the response does not carry three registers.
|
|
310
|
+
"""
|
|
311
|
+
registers = await self._read_input(
|
|
312
|
+
INPUT_REGISTERS.VERSION_START,
|
|
313
|
+
INPUT_REGISTERS.SERIAL_NUMBER_START - INPUT_REGISTERS.VERSION_START,
|
|
314
|
+
"firmware version",
|
|
315
|
+
)
|
|
316
|
+
major, minor, patch = registers
|
|
317
|
+
return (major, minor, patch)
|
|
318
|
+
|
|
319
|
+
async def read_serial_number(self) -> int:
|
|
320
|
+
"""
|
|
321
|
+
Read the 32-bit serial number.
|
|
322
|
+
|
|
323
|
+
Returns:
|
|
324
|
+
int: The serial number built from the high and low words.
|
|
325
|
+
|
|
326
|
+
Raises:
|
|
327
|
+
UnexpectedResponse: If the response does not carry two registers.
|
|
328
|
+
"""
|
|
329
|
+
registers = await self._read_input(
|
|
330
|
+
INPUT_REGISTERS.SERIAL_NUMBER_START,
|
|
331
|
+
IDENTITY_REGISTER_COUNT - INPUT_REGISTERS.SERIAL_NUMBER_START,
|
|
332
|
+
"serial number",
|
|
333
|
+
)
|
|
334
|
+
serial_hi, serial_lo = registers
|
|
335
|
+
return (serial_hi << 16) | serial_lo
|
|
336
|
+
|
|
337
|
+
# -- communication settings -------------------------------------------
|
|
338
|
+
|
|
339
|
+
async def get_slave_address(self) -> int:
|
|
340
|
+
"""
|
|
341
|
+
Read the persisted Modbus slave address.
|
|
342
|
+
|
|
343
|
+
Returns:
|
|
344
|
+
int: The address in holding register 0, normally 1-247.
|
|
345
|
+
|
|
346
|
+
Raises:
|
|
347
|
+
UnexpectedResponse: If the response does not carry one in-range
|
|
348
|
+
register.
|
|
349
|
+
"""
|
|
350
|
+
value = (await self._read_holding(REGISTERS.SLAVE_ADDRESS, 1, "slave address"))[
|
|
351
|
+
0
|
|
352
|
+
]
|
|
353
|
+
if not SLAVE_ADDRESS_MIN <= value <= SLAVE_ADDRESS_MAX:
|
|
354
|
+
raise UnexpectedResponse(
|
|
355
|
+
"slave address",
|
|
356
|
+
f"expected {SLAVE_ADDRESS_MIN}-{SLAVE_ADDRESS_MAX}, got {value}",
|
|
357
|
+
)
|
|
358
|
+
return value
|
|
359
|
+
|
|
360
|
+
async def set_slave_address(self, address: int) -> None:
|
|
361
|
+
"""
|
|
362
|
+
Persist a new Modbus slave address.
|
|
363
|
+
|
|
364
|
+
The device writes the address to EEPROM and acknowledges the request at
|
|
365
|
+
the OLD address. It then waits ``REBOOT_DELAY_S`` and restarts, after
|
|
366
|
+
which it answers only at the new address. This client keeps requesting
|
|
367
|
+
the old address, so construct a new instance with the new address after
|
|
368
|
+
the restart. Writing the address already stored still answers at the old
|
|
369
|
+
address but causes no restart.
|
|
370
|
+
|
|
371
|
+
Args:
|
|
372
|
+
address: The new slave address, 1-247.
|
|
373
|
+
|
|
374
|
+
Raises:
|
|
375
|
+
ValueError: If the address is outside 1-247, before any bus traffic.
|
|
376
|
+
UnexpectedResponse: If the device does not echo the written value.
|
|
377
|
+
"""
|
|
378
|
+
if not SLAVE_ADDRESS_MIN <= address <= SLAVE_ADDRESS_MAX:
|
|
379
|
+
raise ValueError(
|
|
380
|
+
f"slave address must be {SLAVE_ADDRESS_MIN}-"
|
|
381
|
+
f"{SLAVE_ADDRESS_MAX}, got {address}"
|
|
382
|
+
)
|
|
383
|
+
await self._write_holding(REGISTERS.SLAVE_ADDRESS, address, "slave address")
|
|
384
|
+
|
|
385
|
+
async def get_baud_index(self) -> int:
|
|
386
|
+
"""
|
|
387
|
+
Read the persisted baud-rate index.
|
|
388
|
+
|
|
389
|
+
Returns:
|
|
390
|
+
int: The index in holding register 1, 0-5. Use
|
|
391
|
+
``BAUD_INDEX_TO_RATE`` to convert it to a baud rate.
|
|
392
|
+
|
|
393
|
+
Raises:
|
|
394
|
+
UnexpectedResponse: If the response does not carry one known index.
|
|
395
|
+
"""
|
|
396
|
+
value = (await self._read_holding(REGISTERS.BAUD_INDEX, 1, "baud index"))[0]
|
|
397
|
+
if value not in BAUD_INDEX_TO_RATE:
|
|
398
|
+
raise UnexpectedResponse(
|
|
399
|
+
"baud index", f"expected 0-{len(BAUD_INDEX_TO_RATE) - 1}, got {value}"
|
|
400
|
+
)
|
|
401
|
+
return value
|
|
402
|
+
|
|
403
|
+
async def set_baud_rate(self, baud_rate: int) -> None:
|
|
404
|
+
"""
|
|
405
|
+
Persist a new baud rate by writing its index.
|
|
406
|
+
|
|
407
|
+
The firmware stores the index, never the baud number itself. It writes
|
|
408
|
+
the index to EEPROM and acknowledges the request at the OLD baud rate.
|
|
409
|
+
It then waits ``REBOOT_DELAY_S`` and restarts, after which it answers
|
|
410
|
+
only at the new rate. Keep the port configured for the old rate until
|
|
411
|
+
the restart completes, then reconfigure it. Writing the rate already
|
|
412
|
+
stored still answers at the old rate but causes no restart.
|
|
413
|
+
|
|
414
|
+
Args:
|
|
415
|
+
baud_rate: One of 9600, 14400, 19200, 38400, 57600 or 115200.
|
|
416
|
+
|
|
417
|
+
Raises:
|
|
418
|
+
ValueError: If the rate is not one the firmware supports, before any
|
|
419
|
+
bus traffic.
|
|
420
|
+
UnexpectedResponse: If the device does not echo the written index.
|
|
421
|
+
"""
|
|
422
|
+
index = RATE_TO_BAUD_INDEX.get(baud_rate)
|
|
423
|
+
if index is None:
|
|
424
|
+
supported = ", ".join(str(rate) for rate in RATE_TO_BAUD_INDEX)
|
|
425
|
+
raise ValueError(
|
|
426
|
+
f"unsupported baud rate {baud_rate}; expected one of {supported}"
|
|
427
|
+
)
|
|
428
|
+
await self._write_holding(REGISTERS.BAUD_INDEX, index, "baud index")
|
|
429
|
+
|
|
430
|
+
# -- transport helpers -------------------------------------------------
|
|
431
|
+
|
|
432
|
+
async def _read_holding(self, address: int, count: int, label: str) -> list[int]:
|
|
433
|
+
"""Read holding registers and check the response length."""
|
|
434
|
+
response = await self.execute(
|
|
435
|
+
ReadHoldingRegistersRequest(
|
|
436
|
+
address=address,
|
|
437
|
+
count=count,
|
|
438
|
+
dev_id=self.address,
|
|
439
|
+
)
|
|
440
|
+
)
|
|
441
|
+
return _expect_registers(response, count, label)
|
|
442
|
+
|
|
443
|
+
async def _read_input(self, address: int, count: int, label: str) -> list[int]:
|
|
444
|
+
"""Read input registers and check the response length."""
|
|
445
|
+
response = await self.execute(
|
|
446
|
+
ReadInputRegistersRequest(
|
|
447
|
+
address=address,
|
|
448
|
+
count=count,
|
|
449
|
+
dev_id=self.address,
|
|
450
|
+
)
|
|
451
|
+
)
|
|
452
|
+
return _expect_registers(response, count, label)
|
|
453
|
+
|
|
454
|
+
async def _write_holding(self, address: int, value: int, label: str) -> None:
|
|
455
|
+
"""Write one holding register and check that the device echoed it."""
|
|
456
|
+
response = await self.execute(
|
|
457
|
+
WriteSingleRegisterRequest(
|
|
458
|
+
address=address,
|
|
459
|
+
registers=[value],
|
|
460
|
+
dev_id=self.address,
|
|
461
|
+
)
|
|
462
|
+
)
|
|
463
|
+
if (
|
|
464
|
+
not isinstance(response, WriteSingleRegisterResponse)
|
|
465
|
+
or response.address != address
|
|
466
|
+
or response.registers != [value]
|
|
467
|
+
):
|
|
468
|
+
raise UnexpectedResponse(
|
|
469
|
+
label,
|
|
470
|
+
f"expected echo of register {address} = {value}, got {response!r}",
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def _check_relay_index(index: int) -> None:
|
|
475
|
+
"""Reject a relay index outside 0-16 before any bus traffic."""
|
|
476
|
+
if not 0 <= index < RELAY_COUNT:
|
|
477
|
+
raise IndexError(f"relay index must be 0-{RELAY_COUNT - 1}, got {index}")
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
def _expect_registers(
|
|
481
|
+
response: ModbusPDU | None,
|
|
482
|
+
count: int,
|
|
483
|
+
label: str,
|
|
484
|
+
) -> list[int]:
|
|
485
|
+
"""Return the registers of a response, or raise on a mismatch."""
|
|
486
|
+
if response is None or not isinstance(
|
|
487
|
+
response, (ReadHoldingRegistersResponse, ReadInputRegistersResponse)
|
|
488
|
+
):
|
|
489
|
+
raise UnexpectedResponse(
|
|
490
|
+
label, f"expected {count} register(s), got {response!r}"
|
|
491
|
+
)
|
|
492
|
+
registers = response.registers
|
|
493
|
+
if registers is None or len(registers) != count:
|
|
494
|
+
raise UnexpectedResponse(
|
|
495
|
+
label, f"expected {count} register(s), got {registers!r}"
|
|
496
|
+
)
|
|
497
|
+
return registers
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""Wire constants for the EBIC v1.0 preamp Modbus RTU interface.
|
|
2
|
+
|
|
3
|
+
The values transcribe the firmware in ``ebic_control`` at commit ``66d3b08``.
|
|
4
|
+
Nothing is inferred beyond what that firmware implements: two writeable holding
|
|
5
|
+
registers, two read-only holding registers, read-only input registers 0-52 and
|
|
6
|
+
seventeen coils. The device exposes no ADC, DAC, measurement, status or unlock
|
|
7
|
+
register.
|
|
8
|
+
|
|
9
|
+
Classes:
|
|
10
|
+
BaudRate: Baud rates the firmware selects by index.
|
|
11
|
+
REGISTERS: Holding-register addresses.
|
|
12
|
+
INPUT_REGISTERS: Input-register addresses.
|
|
13
|
+
COILS: Coil addresses.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from enum import IntEnum
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BaudRate(IntEnum):
|
|
20
|
+
"""Baud rates the firmware selects by index, in index order."""
|
|
21
|
+
|
|
22
|
+
B9600 = 9600
|
|
23
|
+
B14400 = 14400
|
|
24
|
+
B19200 = 19200
|
|
25
|
+
B38400 = 38400
|
|
26
|
+
B57600 = 57600
|
|
27
|
+
B115200 = 115200
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
BAUD_INDEX_TO_RATE: dict[int, BaudRate] = dict(enumerate(BaudRate))
|
|
31
|
+
"""Map a holding-register 1 value to the baud rate it selects."""
|
|
32
|
+
|
|
33
|
+
RATE_TO_BAUD_INDEX: dict[int, int] = {
|
|
34
|
+
rate.value: index for index, rate in enumerate(BaudRate)
|
|
35
|
+
}
|
|
36
|
+
"""Map a baud rate to the index written into holding register 1."""
|
|
37
|
+
|
|
38
|
+
SLAVE_ADDRESS_MIN = 1
|
|
39
|
+
"""Lowest accepted Modbus slave address."""
|
|
40
|
+
|
|
41
|
+
SLAVE_ADDRESS_MAX = 247
|
|
42
|
+
"""Highest accepted Modbus slave address."""
|
|
43
|
+
|
|
44
|
+
DEFAULT_SLAVE_ADDRESS = 2
|
|
45
|
+
"""Slave address the firmware uses on an empty EEPROM."""
|
|
46
|
+
|
|
47
|
+
DEFAULT_BAUD_INDEX = 2
|
|
48
|
+
"""Baud index the firmware uses on an empty EEPROM, selecting 19200 baud."""
|
|
49
|
+
|
|
50
|
+
RELAY_COUNT = 17
|
|
51
|
+
"""Number of relay outputs driven by coils 0-16."""
|
|
52
|
+
|
|
53
|
+
REBOOT_DELAY_S = 0.5
|
|
54
|
+
"""Delay between acknowledging an address or baud change and restarting."""
|
|
55
|
+
|
|
56
|
+
SERIAL_NUMBER_DEFAULT = 0x12345678
|
|
57
|
+
"""Serial number the firmware uses on an empty EEPROM."""
|
|
58
|
+
|
|
59
|
+
VENDOR_NAME = "SCIETEX.RU"
|
|
60
|
+
"""Vendor string in input registers 0-9."""
|
|
61
|
+
|
|
62
|
+
PRODUCT_NAME = "EBIC_Preamp"
|
|
63
|
+
"""Product string in input registers 10-29, space-padded by the firmware."""
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class REGISTERS:
|
|
67
|
+
"""Holding-register addresses (FC03 read, FC06/FC10 write)."""
|
|
68
|
+
|
|
69
|
+
SLAVE_ADDRESS = 0
|
|
70
|
+
"""Slave address, 1-247, persisted to EEPROM."""
|
|
71
|
+
|
|
72
|
+
BAUD_INDEX = 1
|
|
73
|
+
"""Baud index, 0-5, persisted to EEPROM."""
|
|
74
|
+
|
|
75
|
+
SERIAL_NUMBER_HI = 10
|
|
76
|
+
"""High word of the serial number, read only."""
|
|
77
|
+
|
|
78
|
+
SERIAL_NUMBER_LO = 11
|
|
79
|
+
"""Low word of the serial number, read only."""
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class INPUT_REGISTERS:
|
|
83
|
+
"""Input-register addresses (FC04 read only)."""
|
|
84
|
+
|
|
85
|
+
VENDOR_START = 0
|
|
86
|
+
"""First vendor-string register; ten registers, one character each."""
|
|
87
|
+
|
|
88
|
+
PRODUCT_START = 10
|
|
89
|
+
"""First product-string register; twenty registers, one character each."""
|
|
90
|
+
|
|
91
|
+
VERSION_START = 30
|
|
92
|
+
"""First of three version registers: major, minor, patch."""
|
|
93
|
+
|
|
94
|
+
SERIAL_NUMBER_START = 33
|
|
95
|
+
"""First of two serial-number registers: high word, low word."""
|
|
96
|
+
|
|
97
|
+
SERVICE_CONSTANT = 35
|
|
98
|
+
"""Service constant, always 7."""
|
|
99
|
+
|
|
100
|
+
RELAY_LEVEL_START = 36
|
|
101
|
+
"""First of seventeen registers mirroring relay levels 0 and 1."""
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class COILS:
|
|
105
|
+
"""Coil addresses (FC01 read, FC05/FC0F write)."""
|
|
106
|
+
|
|
107
|
+
RELAY_START = 0
|
|
108
|
+
"""First relay coil; seventeen coils follow it."""
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""Device identity decoded from the EBIC v1.0 input registers.
|
|
2
|
+
|
|
3
|
+
The firmware reports the identity as one block of read-only input registers:
|
|
4
|
+
ten vendor characters, twenty product characters, three version words and two
|
|
5
|
+
serial-number words. This module decodes that block without touching the bus.
|
|
6
|
+
|
|
7
|
+
Classes:
|
|
8
|
+
DeviceIdentity: The vendor, product, version and serial number of a device.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from collections.abc import Sequence
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
|
|
14
|
+
from .constants import INPUT_REGISTERS
|
|
15
|
+
|
|
16
|
+
IDENTITY_REGISTER_COUNT = INPUT_REGISTERS.SERIAL_NUMBER_START + 2
|
|
17
|
+
"""Number of input registers covered by the identity block, addresses 0-34."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True, slots=True)
|
|
21
|
+
class DeviceIdentity:
|
|
22
|
+
"""The identity the firmware reports through input registers 0-34.
|
|
23
|
+
|
|
24
|
+
Attributes:
|
|
25
|
+
vendor: Vendor string, ``SCIETEX.RU`` on a factory device.
|
|
26
|
+
product: Product string, ``EBIC_Preamp`` on a factory device.
|
|
27
|
+
version: Firmware version as ``(major, minor, patch)``.
|
|
28
|
+
serial_number: 32-bit serial number built from the high and low words.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
vendor: str
|
|
32
|
+
product: str
|
|
33
|
+
version: tuple[int, int, int]
|
|
34
|
+
serial_number: int
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def decode_identity(registers: Sequence[int]) -> DeviceIdentity:
|
|
38
|
+
"""
|
|
39
|
+
Decode the input-register block into a :class:`DeviceIdentity`.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
registers: Input-register words starting at address 0. At least
|
|
43
|
+
``IDENTITY_REGISTER_COUNT`` words are required.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
DeviceIdentity: The decoded identity. Trailing spaces the firmware pads
|
|
47
|
+
the strings with are removed.
|
|
48
|
+
|
|
49
|
+
Raises:
|
|
50
|
+
ValueError: If fewer than ``IDENTITY_REGISTER_COUNT`` words are given.
|
|
51
|
+
"""
|
|
52
|
+
if len(registers) < IDENTITY_REGISTER_COUNT:
|
|
53
|
+
raise ValueError(
|
|
54
|
+
f"identity needs {IDENTITY_REGISTER_COUNT} input registers, "
|
|
55
|
+
f"got {len(registers)}"
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
vendor = _decode_string(
|
|
59
|
+
registers[INPUT_REGISTERS.VENDOR_START : INPUT_REGISTERS.PRODUCT_START]
|
|
60
|
+
)
|
|
61
|
+
product = _decode_string(
|
|
62
|
+
registers[INPUT_REGISTERS.PRODUCT_START : INPUT_REGISTERS.VERSION_START]
|
|
63
|
+
)
|
|
64
|
+
major, minor, patch = registers[
|
|
65
|
+
INPUT_REGISTERS.VERSION_START : INPUT_REGISTERS.SERIAL_NUMBER_START
|
|
66
|
+
]
|
|
67
|
+
serial_hi, serial_lo = registers[
|
|
68
|
+
INPUT_REGISTERS.SERIAL_NUMBER_START : IDENTITY_REGISTER_COUNT
|
|
69
|
+
]
|
|
70
|
+
return DeviceIdentity(
|
|
71
|
+
vendor=vendor,
|
|
72
|
+
product=product,
|
|
73
|
+
version=(major, minor, patch),
|
|
74
|
+
serial_number=(serial_hi << 16) | serial_lo,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _decode_string(registers: Sequence[int]) -> str:
|
|
79
|
+
"""Decode one character per register and strip the firmware's padding."""
|
|
80
|
+
return "".join(chr(word) for word in registers).rstrip(" \x00")
|