Skip to content

Commit 6255788

Browse files
authored
Merge pull request #20 from abelsson/improve_management_info
Improve implementation of get_management_info function
2 parents dee5098 + 857012e commit 6255788

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

pylontech/pylontech.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,32 @@ class Pylontech:
6666
)
6767

6868
management_info_fmt = construct.Struct(
69-
"CommandValue" / construct.Byte,
70-
"ChargeVoltageLimit" / construct.Array(2, construct.Byte),
71-
"DischargeVoltageLimit" / construct.Array(2, construct.Byte),
72-
"ChargeCurrentLimit" / construct.Array(2, construct.Byte),
73-
"DishargeCurrentLimit" / construct.Array(2, construct.Byte),
74-
"Status" / construct.Byte,
69+
"ChargeVoltageLimit" / DivideBy1000(construct.Int16sb),
70+
"DischargeVoltageLimit" / DivideBy1000(construct.Int16sb),
71+
"ChargeCurrentLimit" / ToAmp(construct.Int16sb),
72+
"DischargeCurrentLimit" / ToAmp(construct.Int16sb),
73+
"status"
74+
/ construct.BitStruct(
75+
"ChargeEnable" / construct.Flag,
76+
"DischargeEnable" / construct.Flag,
77+
"ChargeImmediately2" / construct.Flag,
78+
"ChargeImmediately1" / construct.Flag,
79+
"FullChargeRequest" / construct.Flag,
80+
"ShouldCharge"
81+
/ construct.Computed(
82+
lambda this: this.ChargeImmediately2
83+
| this.ChargeImmediately1
84+
| this.FullChargeRequest
85+
),
86+
"_padding" / construct.BitsInteger(3),
87+
),
7588
)
7689

7790
module_serial_number_fmt = construct.Struct(
7891
"CommandValue" / construct.Byte,
7992
"ModuleSerialNumber" / JoinBytes(construct.Array(16, construct.Byte)),
8093
)
8194

82-
8395
get_values_fmt = construct.Struct(
8496
"NumberOfModules" / construct.Byte,
8597
"Module" / construct.Array(construct.this.NumberOfModules, construct.Struct(
@@ -246,14 +258,14 @@ def get_system_parameters(self, dev_id=None):
246258
f = self.read_frame()
247259
return self.system_parameters_fmt.parse(f.info[1:])
248260

249-
def get_management_info(self):
250-
raise Exception('Dont touch this for now')
251-
self.send_cmd(2, 0x92)
261+
def get_management_info(self, dev_id):
262+
bdevid = "{:02X}".format(dev_id).encode()
263+
self.send_cmd(dev_id, 0x92, bdevid)
252264
f = self.read_frame()
253265

254266
print(f.info)
255267
print(len(f.info))
256-
ff = self.management_info_fmt.parse(f.info)
268+
ff = self.management_info_fmt.parse(f.info[1:])
257269
print(ff)
258270
return ff
259271

tests/test_basic.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -382,28 +382,16 @@ def test_up2500_1module_status_info_parsing_1():
382382

383383
def test_up2500_management_info():
384384
p = Pylontech([b"~20024600B014026EF05AA0022BFDD5C0F915\r"])
385-
p.send_cmd(2, 0x92, b"02")
386-
387-
mgmt = construct.Struct(
388-
"ChargeVoltageLimit" / DivideBy1000(construct.Int16sb),
389-
"DischargeVoltageLimit" / DivideBy1000(construct.Int16sb),
390-
"ChargeCurrentLimit" / ToAmp(construct.Int16sb),
391-
"DischargeCurrentLimit" / ToAmp(construct.Int16sb),
392-
"status"
393-
/ construct.BitStruct(
394-
"ChargeEnable" / construct.Flag,
395-
"DischargeEnable" / construct.Flag,
396-
"ChargeImmediately2" / construct.Flag,
397-
"ChargeImmediately1" / construct.Flag,
398-
"FullChargeRequest" / construct.Flag,
399-
"ShouldCharge"
400-
/ construct.Computed(
401-
lambda this: this.ChargeImmediately2
402-
| this.ChargeImmediately1
403-
| this.FullChargeRequest
404-
),
405-
"_padding" / construct.BitsInteger(3),
406-
),
407-
)
408-
f = p.read_frame()
409-
print(mgmt.parse(f.info[1:]))
385+
386+
d = p.get_management_info(2)
387+
388+
assert d.ChargeVoltageLimit == 28.4
389+
assert d.DischargeVoltageLimit == 23.2
390+
assert d.ChargeCurrentLimit == 55.5
391+
assert d.DischargeCurrentLimit == -55.5
392+
assert d.status.ChargeEnable
393+
assert d.status.DischargeEnable
394+
assert not d.status.ChargeImmediately2
395+
assert not d.status.ChargeImmediately1
396+
assert not d.status.FullChargeRequest
397+
assert not d.status.ShouldCharge

0 commit comments

Comments
 (0)