Skip to content

Commit b0c4b4b

Browse files
committed
feat: add register check
dcm230 contains a both holding and input registers, this is why i add want to add the check Signed-off-by: Bobo Bäck Engström <bobo@id8-engineering.io>
1 parent f572baa commit b0c4b4b

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

src/dcm230/dcm230.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class RegisterSpec:
2929
Attributes:
3030
address (int): Modbus register address.
3131
count (int): Number of consecutive registers to read.
32-
scale (int): Scaling factor to apply to the raw integer value.
3332
decimals (int): Number of decimal places to round the scaled value to.
3433
writable (bool): Whether this register can be written to.
3534
range (bool): Whether range validation should be performed.
@@ -39,8 +38,8 @@ class RegisterSpec:
3938

4039
address: int
4140
count: int
41+
reg_type: int
4242
decimals: int = 0
43-
scale: int = 1
4443
range: bool = False
4544
min: int = 0
4645
max: int = 0x7FFFFFFF
@@ -127,7 +126,7 @@ class Dcm230:
127126
MAX_REGS = 2
128127

129128
_register_specs: Final[dict[str, RegisterSpec]] = {
130-
"V": RegisterSpec(address=0x0000, count=2, decimals=1),
129+
"V": RegisterSpec(address=0x0000, count=2, decimals=1, reg_type=0x03),
131130
}
132131

133132
def __init__(self, device_address: int, client: ModbusSerialClient) -> None:
@@ -140,8 +139,8 @@ def __init__(self, device_address: int, client: ModbusSerialClient) -> None:
140139
self.device_address = device_address
141140
self.client = client
142141

143-
def _read_input_registers(self, address: int, count: int) -> list[int]:
144-
"""Safely read input registers from the Modbus device.
142+
def _read_registers(self, address: int, count: int, reg_type: int) -> list[int]:
143+
"""Safely read input or holding registers from the Modbus device.
145144
146145
Args:
147146
address: Starting register address to read.
@@ -152,10 +151,20 @@ def _read_input_registers(self, address: int, count: int) -> list[int]:
152151
153152
Raises:
154153
ModbusException: If the read operation fails or returns an error.
154+
ValueError: If register type is incorrect.
155155
"""
156-
result = self.client.read_input_registers(
157-
address=address, count=count, device_id=self.device_address
158-
)
156+
if reg_type == 0x03:
157+
result = self.client.read_input_registers(
158+
address=address, count=count, device_id=self.device_address
159+
)
160+
elif reg_type == 0x04:
161+
result = self.client.read_holding_registers(
162+
address=address, count=count, device_id=self.device_address
163+
)
164+
else:
165+
msg = f"Unsupported reg_type: {reg_type}"
166+
raise ValueError(msg)
167+
159168
if result.isError():
160169
msg = (
161170
"Failed to read input register. "
@@ -178,7 +187,7 @@ def _read_register(self, register_name: str) -> Decimal | int:
178187
ModbusException: If Modbus read operation fails.
179188
"""
180189
spec = self._register_specs[register_name]
181-
regs = self._read_input_registers(spec.address, spec.count)
190+
regs = self._read_registers(spec.address, spec.count, spec.reg_type)
182191

183192
if len(regs) < self.MAX_REGS:
184193
regs.append(0)

src/dcm230/test_dcm230.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,21 @@ def test_unpack() -> None:
2020
_ = meter._unpack(registers, 0x0001) # noqa: SLF001
2121

2222

23+
def test_read_register() -> None:
24+
"""Test read_register."""
25+
client = MagicMock()
26+
meter = Dcm230(1, client)
27+
28+
"""Test 1: Should raise exception due to incorrect register type"""
29+
address = 1
30+
count = 2
31+
reg_type = 0x05
32+
with pytest.raises(ValueError, match="Unsupported reg_type:"):
33+
_ = meter._read_registers(address, count, reg_type)
34+
35+
2336
def test_V() -> None:
24-
"""Test Get v."""
37+
"""test get v."""
2538
client = MagicMock()
2639
mock_result = MagicMock()
2740
mock_result.isError.return_value = False

0 commit comments

Comments
 (0)