Skip to content

Commit

Permalink
simplify pi18sv move common function to pi18
Browse files Browse the repository at this point in the history
  • Loading branch information
jblance committed Dec 17, 2022
1 parent 84aeb76 commit 6ba5268
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 95 deletions.
41 changes: 25 additions & 16 deletions mppsolar/protocols/pi18.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def __init__(self, *args, **kwargs) -> None:

def get_full_command(self, command) -> bytes:
"""
Override the default get_full_command as its different for PI18
Override the default get_full_command as its different
"""
log.info(
f"Using protocol {self._protocol_id} with {len(self.COMMANDS)} commands"
Expand All @@ -522,31 +522,40 @@ def get_full_command(self, command) -> bytes:
if self._command_defn is None:
return None

# Full command components
_cmd = bytes(self._command, "utf-8")
log.debug(f"_cmd is: {_cmd}")

_type = self._command_defn["type"]
log.debug(f"_type is: {_type}")

# data_length = len(_cmd) + 2 + 1
# Hand coded prefix
_prefix = self._command_defn["prefix"]
log.debug(f"_prefix: {_prefix}")
# Auto determined prefix - TODO
data_length = len(_cmd) + 3
if _type == "QUERY":
_prefix = self._command_defn["prefix"]
auto_prefix = f"^P{data_length:03}"
elif _type == "SETTER":
_prefix = self._command_defn["prefix"]
#
# _prefix = f"^S{data_length:03}"

auto_prefix = f"^S{data_length:03}"
else:
data_length = len(_cmd) + 2 + 1
_prefix = f"^P{data_length:03}"
log.info(f"No type defined for command {_cmd}")
auto_prefix = f"^P{data_length:03}"
log.debug(f"auto_prefix: {auto_prefix}")

_pre_cmd = bytes(_prefix, "utf-8") + _cmd

# _pre_cmd = bytes(auto_prefix, "utf-8") + _cmd
log.debug(f"_pre_cmd: {_pre_cmd}")
# calculate the CRC
crc_high, crc_low = crc(_pre_cmd)
# combine byte_cmd, CRC , return
# PI18 full command "^P005GS\x..\x..\r"
_crc = bytes([crc_high, crc_low, 13])
full_command = _pre_cmd + _crc

# For commands that dont need CRC
if "nocrc" in self._command_defn and self._command_defn["nocrc"] is True:
full_command = _pre_cmd + bytes([13])
# crc commands
else:
# calculate the CRC
crc_high, crc_low = crc(_pre_cmd)
# combine byte_cmd, CRC , return
full_command = _pre_cmd + bytes([crc_high, crc_low, 13])
log.debug(f"full command: {full_command}")
return full_command

Expand Down
103 changes: 26 additions & 77 deletions mppsolar/protocols/pi18sv.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging

from .pi18 import pi18
from .protocol_helpers import crcPI as crc

log = logging.getLogger("pi18sv")

Expand Down Expand Up @@ -540,7 +539,7 @@
# Now the setters
"LON": {
"name": "LON",
"prefix": "S007",
"prefix": "^S007",
"description": "Set enable/disable machine supply power to the loads",
"help": " -- examples: LON1 (0: disable, 1: enable)",
"type": "SETTER",
Expand All @@ -555,7 +554,7 @@
},
"PEA": {
"name": "PEA",
"prefix": "S006",
"prefix": "^S006",
"description": "Enable Silence buzzer or open buzzer",
"help": " -- Enable Silence buzzer or open buzzer",
"type": "SETTER",
Expand All @@ -569,7 +568,7 @@
},
"PDA": {
"name": "PDA",
"prefix": "S006",
"prefix": "^S006",
"description": "Disable Silence buzzer or open buzzer",
"help": " -- Disable Silence buzzer or open buzzer",
"type": "SETTER",
Expand All @@ -583,7 +582,7 @@
},
"PEB": {
"name": "PEB",
"prefix": "S006",
"prefix": "^S006",
"description": "Enable Overload bypass function",
"help": " -- Enable Overload bypass function",
"type": "SETTER",
Expand All @@ -597,7 +596,7 @@
},
"PDB": {
"name": "PDB",
"prefix": "S006",
"prefix": "^S006",
"description": "Disable Overload bypass function",
"help": " -- Disable Overload bypass function",
"type": "SETTER",
Expand All @@ -611,7 +610,7 @@
},
"PEC": {
"name": "PEC",
"prefix": "S006",
"prefix": "^S006",
"description": "Enable LCD display escape to default page after 1min timeout",
"help": " -- Enable LCD display escape to default page after 1min timeout",
"type": "SETTER",
Expand All @@ -625,7 +624,7 @@
},
"PDC": {
"name": "PDC",
"prefix": "S006",
"prefix": "^S006",
"description": "Disable LCD display escape to default page after 1min timeout",
"help": " -- Disable LCD display escape to default page after 1min timeout",
"type": "SETTER",
Expand All @@ -639,7 +638,7 @@
},
"PED": {
"name": "PED",
"prefix": "S006",
"prefix": "^S006",
"description": "Enable Overload restart",
"help": " -- Enable Overload restart",
"type": "SETTER",
Expand All @@ -653,7 +652,7 @@
},
"PDD": {
"name": "PDD",
"prefix": "S006",
"prefix": "^S006",
"description": "Disable Overload restart",
"help": " -- Disable Overload restart",
"type": "SETTER",
Expand All @@ -667,7 +666,7 @@
},
"PEE": {
"name": "PEE",
"prefix": "S006",
"prefix": "^S006",
"description": "Enable Over temperature restart",
"help": " -- Enable Over temperature restart",
"type": "SETTER",
Expand All @@ -681,7 +680,7 @@
},
"PDE": {
"name": "PDE",
"prefix": "S006",
"prefix": "^S006",
"description": "Disable Over temperature restart",
"help": " -- Disable Over temperature restart",
"type": "SETTER",
Expand All @@ -695,7 +694,7 @@
},
"PEF": {
"name": "PEF",
"prefix": "S006",
"prefix": "^S006",
"description": "Enable Backlight on",
"help": " -- Enable Backlight on",
"type": "SETTER",
Expand All @@ -709,7 +708,7 @@
},
"PDF": {
"name": "PDF",
"prefix": "S006",
"prefix": "^S006",
"description": "Disable Backlight on",
"help": " -- Disable Backlight on",
"type": "SETTER",
Expand All @@ -723,7 +722,7 @@
},
"PEG": {
"name": "PEG",
"prefix": "S006",
"prefix": "^S006",
"description": "Enable Alarm on when primary source interrupt",
"help": " -- Enable Alarm on when primary source interrupt",
"type": "SETTER",
Expand All @@ -737,7 +736,7 @@
},
"PDG": {
"name": "PDG",
"prefix": "S006",
"prefix": "^S006",
"description": "Disable Alarm on when primary source interrupt",
"help": " -- Disable Alarm on when primary source interrupt",
"type": "SETTER",
Expand All @@ -751,7 +750,7 @@
},
"PEH": {
"name": "PEH",
"prefix": "S006",
"prefix": "^S006",
"description": "Enable Fault code record",
"help": " -- Enable Fault code record",
"type": "SETTER",
Expand All @@ -765,7 +764,7 @@
},
"PDH": {
"name": "PDH",
"prefix": "S006",
"prefix": "^S006",
"description": "Disable Fault code record",
"help": " -- Disable Fault code record",
"type": "SETTER",
Expand All @@ -779,7 +778,7 @@
},
"PEI": {
"name": "PEI",
"prefix": "S006",
"prefix": "^S006",
"description": "Set Machine type Grid-Tie",
"help": " --Set Machine type Grid-Tie",
"type": "SETTER",
Expand All @@ -793,7 +792,7 @@
},
"PDI": {
"name": "PDI",
"prefix": "S006",
"prefix": "^S006",
"description": "Set Machine type Off-Grid",
"help": " -- Set Machine type Off-Grid",
"type": "SETTER",
Expand All @@ -807,7 +806,7 @@
},
"PF": {
"name": "PF",
"prefix": "S005",
"prefix": "^S005",
"description": "Set changeable parameter restore to default value",
"help": " -- Set changeable parameter restore to default value",
"type": "SETTER",
Expand All @@ -821,7 +820,7 @@
},
"MCHGC": {
"name": "MCHGC",
"prefix": "S013",
"prefix": "^S013",
"nocrc": True,
"description": "Set battery maximum charge current",
"help": " -- MCHGCRm,nnn with m : 0~Parallel number, if single model, it should be 0 ; n : current. Must choose a seable value returned by MCHGCR ; Ex: MCHGC0,050",
Expand All @@ -837,7 +836,7 @@
},
"MUCHGC": { # Not working
"name": "MUCHGC",
"prefix": "S014",
"prefix": "^S014",
"nocrc": True,
"description": "Set battery maximum AC charge current",
"help": " -- MUCHGCm,nnn with m : 0~Parallel number, if single model, it should be 0 ; n : current. Must choose a seable value returned by MUCHGCR ; Ex: MUCHGC0,050",
Expand All @@ -853,7 +852,7 @@
},
"MCHGV": {
"name": "MCHGV",
"prefix": "S015",
"prefix": "^S015",
"description": "Set battery maximum charge voltage",
"help": " -- MCHGVmmm,nnn with mmm battery constant charge voltage and nnn battery float voltage. Unit 0.1V",
"type": "SETTER",
Expand All @@ -868,7 +867,7 @@
},
"DAT": {
"name": "DAT",
"prefix": "S018",
"prefix": "^S018",
"description": "Set date time",
"help": " -- examples: DAT190518224530(YYMMDDHHMMSS-12digits)",
"type": "SETTER",
Expand All @@ -883,7 +882,7 @@
},
"POP": {
"name": "POP",
"prefix": "S007",
"prefix": "^S007",
"description": "Set output source priority",
"help": " -- POP0 : Solar-Utility-Battery ; POP1 : Solar-Battery-Utility",
"type": "SETTER",
Expand All @@ -898,7 +897,7 @@
},
"BUCD": {
"name": "BUCD",
"prefix": "S014",
"prefix": "^S014",
"description": "Battery re-charged and re-discharged voltage when utility is available",
"help": " -- BUCDmmm,nnn with mmm battery recharge voltage when utility available (44 to 51V) and nnn Battery re-discharged voltage when utility is available (0,48-58)",
"type": "SETTER",
Expand Down Expand Up @@ -972,56 +971,6 @@ def __init__(self, *args, **kwargs) -> None:
]
self.DEFAULT_COMMAND = "GS"

def get_full_command(self, command) -> bytes:
"""
Override the default get_full_command as its different
"""
log.info(
f"Using protocol {self._protocol_id} with {len(self.COMMANDS)} commands"
)
# These need to be set to allow other functions to work`
self._command = command
self._command_defn = self.get_command_defn(command)
# End of required variables setting
if self._command_defn is None:
return None

# Full command components
_cmd = bytes(self._command, "utf-8")
log.debug(f"_cmd is: {_cmd}")

_type = self._command_defn["type"]
log.debug(f"_type is: {_type}")

# Hand coded prefix
_prefix = self._command_defn["prefix"]
log.debug(f"_prefix: {_prefix}")
# Auto determined prefix - TODO
data_length = len(_cmd) + 3
if _type == "QUERY":
auto_prefix = f"^P{data_length:03}"
elif _type == "SETTER":
auto_prefix = f"^S{data_length:03}"
else:
log.info(f"No type defined for command {_cmd}")
auto_prefix = f"^P{data_length:03}"
log.debug(f"auto_prefix: {auto_prefix}")

_pre_cmd = bytes(_prefix, "utf-8") + _cmd
log.debug(f"_pre_cmd: {_pre_cmd}")

# For commands that dont need CRC
if "nocrc" in self._command_defn and self._command_defn["nocrc"] is True:
full_command = _pre_cmd + bytes([13])
# crc commands
else:
# calculate the CRC
crc_high, crc_low = crc(_pre_cmd)
# combine byte_cmd, CRC , return
full_command = _pre_cmd + bytes([crc_high, crc_low, 13])
log.debug(f"full command: {full_command}")
return full_command


# Bugs / Not working
# EMYYYYDD returns an error
2 changes: 1 addition & 1 deletion mppsolar/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.14.10"
__version__ = "0.14.11"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mppsolar"
version = "0.14.10"
version = "0.14.11"
description = "Package to communicate with Solar inverters and BMSs"
authors = ["John Blance"]

Expand Down
Loading

0 comments on commit 6ba5268

Please sign in to comment.