"""" ModBus control routines Copyright (C) 2023 M.T. Konstapel This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ import minimalmodbus import serial class ModBusController(minimalmodbus.Instrument): """Instrument class for Epever Charge Controllers. Args: * portname (str): port name * slaveaddress (int): slave address in the range 1 to 247 """ def __init__(self, portname, slaveaddress): minimalmodbus.Instrument.__init__(self, portname, slaveaddress) self.serial.baudrate = 9600 self.serial.bytesize = 8 self.serial.parity = serial.PARITY_NONE self.serial.stopbits = 1 self.serial.timeout = 0.3 self.mode = minimalmodbus.MODE_RTU self.clear_buffers_before_each_transaction = True self.close_port_after_each_call = False #Address range 0x3000 def get_id(self): device_id = [None]*4 device_id[0] = self.read_register(0, 0, 4, False) device_id[1] = self.read_register(1, 0, 4, False) device_id[2] = self.read_register(2, 0, 4, False) device_id[3] = self.read_register(3, 0, 4, False) return device_id def enable_heater(self): self.write_bit(0, 1, 5) def disable_heater(self): self.write_bit(0, 0, 5)