|
| 1 | +"""Driver class for EM511""" |
| 2 | + |
| 3 | +class Em511: |
| 4 | + """Em511 driver class""" |
| 5 | + |
| 6 | + # --------------------------------------------------------------------- |
| 7 | + # Initialization & Connection |
| 8 | + # --------------------------------------------------------------------- |
| 9 | + |
| 10 | + def __init__(self, device_address: int, client, auto_connect=True) -> None: |
| 11 | + """Constructor""" |
| 12 | + self.device_address = device_address |
| 13 | + self.client = client |
| 14 | + self.connected = False |
| 15 | + |
| 16 | + if auto_connect: |
| 17 | + self.connect() |
| 18 | + |
| 19 | + def connect(self) -> bool: |
| 20 | + """Try to connect Modbus client.""" |
| 21 | + if not self.client.connect(): |
| 22 | + print("Could not open COM port", flush=True) |
| 23 | + self.connected = False |
| 24 | + return False |
| 25 | + self.connected = True |
| 26 | + return True |
| 27 | + |
| 28 | + #--------------------------------------------------------------------- |
| 29 | + |
| 30 | + def _read_registers(self, address: int, count: int) -> list[int] | None: |
| 31 | + """Internal helper to read Modbus registers safely.""" |
| 32 | + if not self.connected and not self.connect(): |
| 33 | + print("Connection failed", flush=True) |
| 34 | + return None |
| 35 | + |
| 36 | + result = self.client.read_input_registers(address=address, count=count, device_id=self.device_address) |
| 37 | + if result.isError(): |
| 38 | + print(f"Read error at {address}: {result}", flush=True) |
| 39 | + return None |
| 40 | + return result.registers |
| 41 | + |
| 42 | + # --------------------------------------------------------------------- |
| 43 | + # Core Read Functions |
| 44 | + # --------------------------------------------------------------------- |
| 45 | + |
| 46 | + def read_V(self) -> float | None: |
| 47 | + """Return V value""" |
| 48 | + regs = self._read_registers(0, 2) |
| 49 | + if not regs: |
| 50 | + return None |
| 51 | + value = (regs[1] << 16) + regs[0] |
| 52 | + return value / 10 |
| 53 | + |
| 54 | + #--------------------------------------------------------------------- |
| 55 | + |
| 56 | + def read_A(self) -> float | None: |
| 57 | + """Return A value""" |
| 58 | + regs = self._read_registers(2, 2) |
| 59 | + if not regs: |
| 60 | + return None |
| 61 | + value = (regs[1] << 16) + regs[0] |
| 62 | + return value / 1000 |
| 63 | + |
| 64 | + #--------------------------------------------------------------------- |
| 65 | + |
| 66 | + def read_W(self) -> float | None: |
| 67 | + """Return W value""" |
| 68 | + regs = self._read_registers(4, 2) |
| 69 | + if not regs: |
| 70 | + return None |
| 71 | + value = (regs[1] << 16) + regs[0] |
| 72 | + return value / 10 |
| 73 | + |
| 74 | + #--------------------------------------------------------------------- |
| 75 | + |
| 76 | + def read_W_dmd(self) -> float | None: |
| 77 | + """Return W Dmd value""" |
| 78 | + regs = self._read_registers(10, 2) |
| 79 | + if not regs: |
| 80 | + return None |
| 81 | + value = (regs[1] << 16) + regs[0] |
| 82 | + return value / 10 |
| 83 | + |
| 84 | + #--------------------------------------------------------------------- |
| 85 | + |
| 86 | + def read_W_dmd_peak(self) -> float | None: |
| 87 | + """Return W dmd peak value""" |
| 88 | + regs = self._read_registers(12, 2) |
| 89 | + if not regs: |
| 90 | + return None |
| 91 | + value = (regs[1] << 16) + regs[0] |
| 92 | + return value / 10 |
| 93 | + |
| 94 | + #--------------------------------------------------------------------- |
| 95 | + |
| 96 | + def read_Hz(self) -> float | None: |
| 97 | + """Return Hz value""" |
| 98 | + regs = self._read_registers(15, 1) |
| 99 | + if not regs: |
| 100 | + return None |
| 101 | + value = regs[0] |
| 102 | + return value / 10 |
| 103 | + |
| 104 | + #--------------------------------------------------------------------- |
| 105 | + |
| 106 | + def read_kWh_tot(self) -> float | None: |
| 107 | + """Return kWh value""" |
| 108 | + regs = self._read_registers(16, 2) |
| 109 | + if not regs: |
| 110 | + return None |
| 111 | + value = (regs[1] << 16) + regs[0] |
| 112 | + return value / 10 |
| 113 | + |
| 114 | + #--------------------------------------------------------------------- |
0 commit comments