Repository navigation
sdm compatibility mode - #3212
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces an SDM “compatibility mode” to improve Modbus read reliability for SDM meters that can’t handle larger bulk register reads.
Changes:
- Add logger and a
fast_modeflag to control bulk-read vs. compatibility/slow-read behavior. - Update
Sdm630_72.get_counter_state()to attempt bulk reads first and fall back to smaller reads on failure. - Reduce bulk-read sizes (e.g.,
bulk_2from 8 to 6 registers) to better match actual mapped registers.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
17
to
23
| class Sdm(AbstractCounter): | ||
| def __init__(self, modbus_id: int, client: modbus.ModbusTcpClient_) -> None: | ||
| self.client = client | ||
| self.id = modbus_id | ||
| self.fast_mode = True | ||
| with client: | ||
| self.serial_number = str(self.client.read_holding_registers(0xFC00, ModbusDataType.UINT_32, unit=self.id)) |
Comment on lines
70
to
+76
| # entgegen der Doku können nicht bei allen SDM72 80 Register auf einmal gelesen werden, | ||
| # manche können auch nur 20 | ||
| time.sleep(0.1) | ||
| bulk_1 = self.client.read_input_registers_bulk( | ||
| SdmRegister.VOLTAGE_L1, 20, mapping=self.REG_MAPPING_BULK_1, unit=self.id) | ||
| time.sleep(0.1) | ||
| power_factors = self.client.read_input_registers(0x1E, [ModbusDataType.FLOAT_32]*3, unit=self.id) | ||
| time.sleep(0.1) | ||
| bulk_2 = self.client.read_input_registers_bulk( | ||
| SdmRegister.FREQUENCY, 8, mapping=self.REG_MAPPING_BULK_2, unit=self.id) | ||
| resp = {**bulk_1, **bulk_2} | ||
| # manche können auch nur 10 | ||
| if self.fast_mode: | ||
| try: | ||
| time.sleep(0.1) | ||
| bulk_1 = self.client.read_input_registers_bulk( | ||
| SdmRegister.VOLTAGE_L1, 18, mapping=self.REG_MAPPING_BULK_1, unit=self.id) |
Comment on lines
69
to
+112
| def get_counter_state(self) -> CounterState: | ||
| # entgegen der Doku können nicht bei allen SDM72 80 Register auf einmal gelesen werden, | ||
| # manche können auch nur 20 | ||
| time.sleep(0.1) | ||
| bulk_1 = self.client.read_input_registers_bulk( | ||
| SdmRegister.VOLTAGE_L1, 20, mapping=self.REG_MAPPING_BULK_1, unit=self.id) | ||
| time.sleep(0.1) | ||
| power_factors = self.client.read_input_registers(0x1E, [ModbusDataType.FLOAT_32]*3, unit=self.id) | ||
| time.sleep(0.1) | ||
| bulk_2 = self.client.read_input_registers_bulk( | ||
| SdmRegister.FREQUENCY, 8, mapping=self.REG_MAPPING_BULK_2, unit=self.id) | ||
| resp = {**bulk_1, **bulk_2} | ||
| # manche können auch nur 10 | ||
| if self.fast_mode: | ||
| try: | ||
| time.sleep(0.1) | ||
| bulk_1 = self.client.read_input_registers_bulk( | ||
| SdmRegister.VOLTAGE_L1, 18, mapping=self.REG_MAPPING_BULK_1, unit=self.id) | ||
| time.sleep(0.1) | ||
| power_factors = self.client.read_input_registers( | ||
| SdmRegister.POWER_FACTOR_L1, [ModbusDataType.FLOAT_32]*3, unit=self.id) | ||
| time.sleep(0.1) | ||
| bulk_2 = self.client.read_input_registers_bulk( | ||
| SdmRegister.FREQUENCY, 6, mapping=self.REG_MAPPING_BULK_2, unit=self.id) | ||
| resp = {**bulk_1, **bulk_2} | ||
| except Exception: | ||
| log.exception("Fehler beim Auslesen des Zählers") | ||
| self.client.read_input_registers( | ||
| SdmRegister.VOLTAGE_L1, [ModbusDataType.FLOAT_32]*3, unit=self.id) | ||
| self.fast_mode = False | ||
| if self.fast_mode is False: | ||
| # im gleichen Durchlauf noch im slow mode versuchen, sonst schlägt der Hardware-Check fehl | ||
| log.debug("Auslesung des Zählers im Kompatibilitäts-Modus") | ||
| resp = {} | ||
| time.sleep(0.1) | ||
| resp[SdmRegister.VOLTAGE_L1] = self.client.read_input_registers( | ||
| SdmRegister.VOLTAGE_L1, [ModbusDataType.FLOAT_32]*3, unit=self.id) | ||
| time.sleep(0.1) | ||
| resp[SdmRegister.CURRENT_L1] = self.client.read_input_registers( | ||
| SdmRegister.CURRENT_L1, [ModbusDataType.FLOAT_32]*3, unit=self.id) | ||
| time.sleep(0.1) | ||
| resp[SdmRegister.POWER_L1] = self.client.read_input_registers( | ||
| SdmRegister.POWER_L1, [ModbusDataType.FLOAT_32]*3, unit=self.id) | ||
| time.sleep(0.1) | ||
| power_factors = self.client.read_input_registers( | ||
| SdmRegister.POWER_FACTOR_L1, [ModbusDataType.FLOAT_32]*3, unit=self.id) | ||
| time.sleep(0.1) | ||
| # frequency noch mit auslesen klappt nicht | ||
| resp[SdmRegister.IMPORTED], resp[SdmRegister.EXPORTED] = self.client.read_input_registers( | ||
| SdmRegister.IMPORTED, [ModbusDataType.FLOAT_32]*2, unit=self.id) | ||
| time.sleep(0.1) | ||
| resp[SdmRegister.FREQUENCY] = self.client.read_input_registers( | ||
| SdmRegister.FREQUENCY, ModbusDataType.FLOAT_32, unit=self.id) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ticket #66001671