Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve implementation of get_management_info function #20

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions pylontech/pylontech.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,32 @@ class Pylontech:
)

management_info_fmt = construct.Struct(
"CommandValue" / construct.Byte,
"ChargeVoltageLimit" / construct.Array(2, construct.Byte),
"DischargeVoltageLimit" / construct.Array(2, construct.Byte),
"ChargeCurrentLimit" / construct.Array(2, construct.Byte),
"DishargeCurrentLimit" / construct.Array(2, construct.Byte),
"Status" / construct.Byte,
"ChargeVoltageLimit" / DivideBy1000(construct.Int16sb),
"DischargeVoltageLimit" / DivideBy1000(construct.Int16sb),
"ChargeCurrentLimit" / ToAmp(construct.Int16sb),
"DischargeCurrentLimit" / ToAmp(construct.Int16sb),
"status"
/ construct.BitStruct(
"ChargeEnable" / construct.Flag,
"DischargeEnable" / construct.Flag,
"ChargeImmediately2" / construct.Flag,
"ChargeImmediately1" / construct.Flag,
"FullChargeRequest" / construct.Flag,
"ShouldCharge"
/ construct.Computed(
lambda this: this.ChargeImmediately2
| this.ChargeImmediately1
| this.FullChargeRequest
),
"_padding" / construct.BitsInteger(3),
),
)

module_serial_number_fmt = construct.Struct(
"CommandValue" / construct.Byte,
"ModuleSerialNumber" / JoinBytes(construct.Array(16, construct.Byte)),
)


get_values_fmt = construct.Struct(
"NumberOfModules" / construct.Byte,
"Module" / construct.Array(construct.this.NumberOfModules, construct.Struct(
Expand Down Expand Up @@ -243,14 +255,14 @@ def get_system_parameters(self, dev_id=None):
f = self.read_frame()
return self.system_parameters_fmt.parse(f.info[1:])

def get_management_info(self):
raise Exception('Dont touch this for now')
self.send_cmd(2, 0x92)
def get_management_info(self, dev_id):
bdevid = "{:02X}".format(dev_id).encode()
self.send_cmd(dev_id, 0x92, bdevid)
f = self.read_frame()

print(f.info)
print(len(f.info))
ff = self.management_info_fmt.parse(f.info)
ff = self.management_info_fmt.parse(f.info[1:])
print(ff)
return ff

Expand Down
38 changes: 13 additions & 25 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,28 +382,16 @@ def test_up2500_1module_status_info_parsing_1():

def test_up2500_management_info():
p = Pylontech([b"~20024600B014026EF05AA0022BFDD5C0F915\r"])
p.send_cmd(2, 0x92, b"02")

mgmt = construct.Struct(
"ChargeVoltageLimit" / DivideBy1000(construct.Int16sb),
"DischargeVoltageLimit" / DivideBy1000(construct.Int16sb),
"ChargeCurrentLimit" / ToAmp(construct.Int16sb),
"DischargeCurrentLimit" / ToAmp(construct.Int16sb),
"status"
/ construct.BitStruct(
"ChargeEnable" / construct.Flag,
"DischargeEnable" / construct.Flag,
"ChargeImmediately2" / construct.Flag,
"ChargeImmediately1" / construct.Flag,
"FullChargeRequest" / construct.Flag,
"ShouldCharge"
/ construct.Computed(
lambda this: this.ChargeImmediately2
| this.ChargeImmediately1
| this.FullChargeRequest
),
"_padding" / construct.BitsInteger(3),
),
)
f = p.read_frame()
print(mgmt.parse(f.info[1:]))

d = p.get_management_info(2)

assert d.ChargeVoltageLimit == 28.4
assert d.DischargeVoltageLimit == 23.2
assert d.ChargeCurrentLimit == 55.5
assert d.DischargeCurrentLimit == -55.5
assert d.status.ChargeEnable
assert d.status.DischargeEnable
assert not d.status.ChargeImmediately2
assert not d.status.ChargeImmediately1
assert not d.status.FullChargeRequest
assert not d.status.ShouldCharge