# pyMate controller # Author: Jared Sanson # # Allows communication with an Outback Systems MATE controller panel, # which provides diagnostic information of current charge state and power use # # Currently only supports the MX status page (from an Outback MX charge controller) # # NOTE: This is intended for communication with the MATE's RS232 port, not Outback's proprietary protocol. import serial import csvmake import datetime from value import Value from time import gmtime, strftime class MXStatusPacket(object): """ Represents an MX status packet, containing useful information such as charge current and PV voltage. """ def __init__(self, packet): fields = packet.split(',') self.address = fields[0] self.timestamp = strftime('%Y-%m-%d %H:%M:%S', gmtime()) # fields[1] unused self.charge_current = Value(float(fields[2]) + (float(fields[6]) / 10.0), resolution=1) self.pv_current = Value(fields[3], resolution=0) self.pv_voltage = Value(fields[4], resolution=0) self.daily_kwh = Value(float(fields[5]) / 10.0, resolution=1) self.aux_mode = fields[7] self.error_mode = fields[8] self.charger_mode = fields[9] self.bat_voltage = Value(float(fields[10]) / 10, resolution=1) self.daily_ah = Value(float(fields[11]), resolution=1) # fields[12] unused chk_expected = int(fields[13]) chk_actual = sum(ord(x)-48 for x in packet[:-4] if ord(x)>=48) if chk_expected != chk_actual: raise Exception("Checksum error in received packet") class MateCom(object): """ Interfaces with the MATE controller on a specific COM port. Must be a proper RS232 port with RTS/DTR pins. """ def __init__(self, port, baudrate=19200): self.ser = serial.Serial(port, baudrate, timeout=2) # Provide power to the Mate controller self.ser.setDTR(True) self.ser.setRTS(False) self.ser.readline() def read_status(self): ln = self.ser.readline().strip() return MXStatusPacket(ln) if ln else None if __name__ == "__main__": # Test mate = MateCom('COM3') status = mate.read_status() #print status.__dict__ print 'Writing to CSV' csvmake.write_to_csv(status.__dict__) print 'CSV Output written' #{'charger_mode': '00', 'error_mode': '000', 'pv_voltage': 18V, 'charge_current': 0.0A, 'daily_kwh': 7.8kWh, 'daily_ah': 144.0Ah, 'pv_current': 0A, 'address': 'A', 'aux_mode': '03', 'bat_voltage': 51.1V}