Skip to content

Instantly share code, notes, and snippets.

@Bouni
Forked from vschlegel/solar.py
Last active December 23, 2021 16:04
Show Gist options
  • Save Bouni/3c5fe37e0c437f9c154850d81aefff58 to your computer and use it in GitHub Desktop.
Save Bouni/3c5fe37e0c437f9c154850d81aefff58 to your computer and use it in GitHub Desktop.
import socket
# https://www.solarmax.com/Downloads/MaxComm_Protokollbeschreibung_DE.pdf
# Communication
ip = "192.168.50.2"
port = 12345
# inverters = ("01", "02", "03")
inverters = ("01",)
# Parameter Set (Code, Measurement, Unit, Factor)
parameter = [
("KDY", "Energy Day", "Wh", 100),
("KMT", "Energy Month", "kWh", 1),
("KYR", "Energy Year", "kWh", 1),
("KT0", "Energy Total", "kWh", 1),
("UDC", "DC Voltage", "mV", 100),
("IDC", "DC Current", "mA", 10),
("UAC", "AC Voltage", "mV", 100),
("IAC", "AC Current", "mA", 10),
("PAC", "AC Power", "mW", 500),
]
def debug(f, s):
print(f"DEBUG: {f:<20}{s}")
def solar(host, port, inverter, parameter):
parameters = ";".join(
[x[0] for x in parameter]
) # Concatinate all Parameters with ; -> KDY;KMT;KYR;KT0;UDC;IDC;UAC;IAC;PAC
debug("Parameters", parameters)
data = f"64:{parameters}" # build data section -> 64:KDY;KMT;KYR;KT0;UDC;IDC;UAC;IAC;PAC
debug("Data", data)
length = f"{len(data):X}" # calculate length of data section in hex -> 38 = 0x26
debug("Length", length)
request = f"FB;{inverter};{length}|{data}|" # build request string -> FB;01;26|64:KDY;KMT;KYR;KT0;UDC;IDC;UAC;IAC;PAC|
debug("Request string", request)
checksum = sum([ord(x) for x in request]) # sum of all unicode codes in request
checksum = f"{checksum:0>4X}" # convert to 4 digit, 0-padded HEX
request += checksum # append checksum
packet = f"{{{request}}}" # complete packet to send to inverter -> {FB;01;26|64:KDY;KMT;KYR;KT0;UDC;IDC;UAC;IAC;PAC|0CFA}
debug("Packet", packet)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, port))
s.send(packet.encode())
raw = clientSocket.recv()
response = raw.decode()
data = response.split("|")[1]
parameters = data.split(":")[1].split(";")
for p in parameters:
for p in parameters:
k, v = p.split("=")
print(f"Parameter {k} = {v}")
# clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# clientSocket.connect((host, port))
# clientSocket.send(request.encode())
# response = clientSocket.recv(port).decode()
# response = response.split(':')[1].split('|')[0].split(';')
# response = [{pair.split('=')[0] : pair.split('=')[1]} for pair in response]
# return response
for inverter in inverters:
solar(ip, port, inverter, parameter)
@Bouni
Copy link
Author

Bouni commented Dec 23, 2021

DEBUG: Parameters          KDY;KMT;KYR;KT0;UDC;IDC;UAC;IAC;PAC
DEBUG: Data                64:KDY;KMT;KYR;KT0;UDC;IDC;UAC;IAC;PAC
DEBUG: Length              26
DEBUG: Request string      FB;01;26|64:KDY;KMT;KYR;KT0;UDC;IDC;UAC;IAC;PAC|
DEBUG: Packet              {FB;01;26|64:KDY;KMT;KYR;KT0;UDC;IDC;UAC;IAC;PAC|0CFA}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment