forked from Interster/PylonTechBattery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpylonpacket.py
executable file
·416 lines (319 loc) · 10.6 KB
/
pylonpacket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import logging
class PylonPacket:
def __init__(self):
self.header = bytearray(6)
self.info = bytearray()
self.checksum = bytearray(2)
self.VER=0x20
self.ADR=0x02
@property
def VER(self):
return self.header[0]
@VER.setter
def VER(self, value):
self.header[0] = value
@property
def ADR(self):
return self.header[1]
@ADR.setter
def ADR(self, value):
self.header[1] = value
@property
def CID1(self):
return self.header[2]
@CID1.setter
def CID1(self, value):
self.header[2] = value
@property
def CID2(self):
return self.header[3]
@CID2.setter
def CID2(self, value):
self.header[3] = value
@property
def LENGTH(self):
return (((self.header[4] & 0x0F) << 8) | self.header[5])
@LENGTH.setter
def LENGTH(self, value):
if value>0xfff or value<0: raise OverflowError("Invalid length")
sum = (value & 0x000F) + ((value >> 4) & 0x000F) + ((value >> 8 ) & 0x000F);
sum = sum % 16;
sum = ~sum;
sum = sum + 1;
val = (sum << 12) + value;
self.header[5] = (val & 0xff)
self.header[4] = (val>>8) & 0xff
@property
def INFO(self):
return self.info
@INFO.setter
def INFO(self, value):
self.info = value
@property
def CHKSUM(self):
return self.checksum[0:2]
@CHKSUM.setter
def CHKSUM(self, value):
self.checksum[0:2] = value
def UpdateChecksum(self):
sum=0
for char in bytes(self.header).hex().upper():
sum+=ord(char)
for v in bytes(self.info).hex().upper():
sum+=ord(v)
sum=sum%65536
sum=~sum
sum=sum+1
self.checksum[0]=(sum >>8) & 0xff
self.checksum[1]=sum & 0xff
def GetAsciiBytes(self):
self.UpdateChecksum()
ret=bytearray()
ret.extend(self.header)
ret.extend(self.info)
ret.extend(self.checksum)
rh='~'+ret.hex().upper()+"\r"
logging.debug("Encoded sentence is %s",rh)
return rh.encode()
def Parse(ascii, packet_type):
if (len(ascii)==0): return None
#logging.debug("Value to decode is %s",ascii)
if ascii[0] != 0x7E or ascii[-1] != 0x0D:
raise ValueError("Invalid packet format")
content=ascii[1:-1].decode()
logging.debug("Content of packet: %s",content)
bdata = bytes.fromhex(content)
pret = packet_type()
for i in range(0,len(pret.header)):
pret.header[i] = bdata[i]
if pret.LENGTH>0:
pret.info=bdata[6:-2]
logging.debug("Info content is %s", pret.info.hex())
else:
pret.info=bytearray()
pret.UpdateChecksum()
if pret.checksum[0]!=bdata[-2] or pret.checksum[1]!=bdata[-1]:
logging.error("Invalid checksum!")
raise ValueError("Invalid checksum")
pret.PostParse()
return pret
def PostParse(self):
pass
def GetInt2(self, idx):
val=self.info[idx]<<8 | self.info[idx+1]
return val
def GetInt2Complement(self, idx):
val=self.info[idx]<<8 | self.info[idx+1]
if (val & 0x8000)==0x8000:
val = val - 0x10000
return val
def __str__(self, **kwargs):
return "VER: 0x%02x, ADR: 0x%02x, CID1: 0x%02x, CID2: 0x%02x, LENGTH: %s, len(INFO): %s, CHKSUM: 0x%02X%02X"%(self.VER,self.ADR,self.CID1,self.CID2,self.LENGTH,len(self.INFO),self.CHKSUM[0],self.CHKSUM[1])
class PPGetVersionInfo(PylonPacket):
def __init__(self):
super().__init__()
self.CID1=0x46
self.CID2=0x4F
class PPVersionInfo(PylonPacket):
pass
class PPGetManufacturerInfo(PylonPacket):
def __init__(self):
super().__init__()
self.CID1=0x46
self.CID2=0x51
class PPManufacturerInfo(PylonPacket):
@property
def DeviceName(self):
return (self.info[0:10]).decode().strip()
@property
def SoftwareVersion(self):
return self.info[10:13]
@property
def ManufacturerName(self):
return (self.info[12:]).decode().strip()
def __str__(self, **kwargs):
#print(int(self.SoftwareVersion[1]))
return super().__str__(**kwargs)+("\r\n> DeviceName: %s, SoftwareVersion %s.%s, ManufacturerName: %s"%(self.DeviceName,'?','?',self.ManufacturerName))
class PPGetAnalogValue(PylonPacket):
def __init__(self):
super().__init__()
self.info=bytearray(1)
self.LENGTH=0x02
self.CID1=0x46
self.CID2=0x42
@property
def Command(self):
return self.info[0]
@Command.setter
def Command(self, value):
self.info[0] = value
def __str__(self, **kwargs):
return super().__str__(**kwargs)+(", Command: %s"%(self.Command))
class PPAnalogValue(PylonPacket):
def __init__(self):
super().__init__()
self.voltages=[]
self.temperatures=[]
@property
def CellsCount(self):
return self.info[2]
@property
def CellVoltages(self):
return self.voltages
@property
def TemperaturesCount(self):
idx=(self.CellsCount*2)+3
return self.info[idx]
@property
def Temperatures(self):
return self.temperatures
@property
def TotalCurrent(self):
return self.GetInt2Complement(-11)/10.0
@property
def TotalVoltage(self):
return self.GetInt2(-9)/1000.0
@property
def RemainingCapacity(self):
return self.GetInt2(-7)/1000.0
@property
def Quantity(self):
return self.info[-5]
@property
def TotalCapacity(self):
return self.GetInt2(-4)/1000.0
@property
def Cycles(self):
return self.GetInt2(-2)
def PostParse(self):
logging.debug("Post processing parsed data %s",self.info.hex())
self.voltages=[]
self.temperatures=[]
for v in range(0,self.CellsCount):
cv=self.GetInt2(3+(2*v))/1000.0
logging.debug("Voltage ",v,cv)
self.voltages.append(cv)
idx=(self.CellsCount*2)+3
for t in range(0,self.TemperaturesCount):
tv=self.GetInt2Complement(idx+1+(2*t))
tv=(tv-2731)/10.0
logging.debug("Temperature %s: %s",v,tv)
self.temperatures.append(tv)
def __str__(self, **kwargs):
ret=super().__str__(**kwargs)
ret+=("\r\n> CellsCount: %s, TemperaturesCount: %s"%(self.CellsCount, self.TemperaturesCount))
ret+=("\r\n> TotalCurrent: %.3f, TotalVoltage: %.3f, RemainingCapacity: %.3f, P: %.2f"%(self.TotalCurrent, self.TotalVoltage, self.RemainingCapacity,(self.TotalCurrent*self.TotalVoltage)))
ret+=("\r\n> Quantity: %s, TotalCapacity: %s, Cycles: %s"%(self.Quantity, self.TotalCapacity, self.Cycles))
ret+=("\r\n> CellVoltages: %s"%(self.CellVoltages))
ret+=("\r\n> Temperatures: %s"%(self.Temperatures))
return ret
class PPGetSystemParameter(PylonPacket):
def __init__(self):
super().__init__()
self.CID1=0x46
self.CID2=0x47
class PPSystemParameter(PylonPacket):
@property
def INFOFLAG(self):
return self.info[0]
@property
def UnitCellVoltage(self):
return self.GetInt2(1)/1000.0
@property
def UnitCellLowVoltage(self):
return self.GetInt2(3)/1000.0
@property
def UnitCellUnderVoltage(self):
return self.GetInt2(5)/1000.0
#TODO: Doplnit dalsi neuzitecne property
def __str__(self, **kwargs):
return super().__str__(**kwargs)+("\r\n> FLAG: %s, UnitCellVoltage: %s, UnitCellLowVoltage %s, UnitCellUnderVoltage: %s"%(bin(self.INFOFLAG), self.UnitCellVoltage,self.UnitCellLowVoltage,self.UnitCellUnderVoltage))
class PPGetAlarmInformation(PylonPacket):
def __init__(self):
super().__init__()
self.CID1=0x46
self.CID2=0x44
self.info=bytearray(1)
self.LENGTH=0x02
@property
def Command(self):
return self.info[0]
@Command.setter
def Command(self, value):
self.info[0] = value
def __str__(self, **kwargs):
return super().__str__(**kwargs)+(", Command: %s"%(self.Command))
class PPAlarmInformation(PylonPacket):
pass
class PPGetChargeManagementInformation(PylonPacket):
def __init__(self):
super().__init__()
self.CID1=0x46
self.CID2=0x92
self.info=bytearray(1)
self.LENGTH=0x02
@property
def Command(self):
return self.info[0]
@Command.setter
def Command(self, value):
self.info[0] = value
def __str__(self, **kwargs):
return super().__str__(**kwargs)+(", Command: %s"%(self.Command))
class PPChargeManagementInformation(PylonPacket):
@property
def VoltageUpLimit(self):
return self.GetInt2(1)/1000.0
@property
def VoltageDownLimit(self):
return self.GetInt2(3)/1000.0
@property
def MaxChargeCurrent(self):
return self.GetInt2Complement(5)/1.0
@property
def MaxDischargeCurrent(self):
return self.GetInt2Complement(7)/1.0
@property
def Status(self):
return self.info[9]
def __str__(self, **kwargs):
print(self.info.hex())
return super().__str__(**kwargs)+("\r\n> VoltageUpLimit: %s, VoltageDownLimit: %s, MaxChargeCurrent: %s, MaxDischargeCurrent: %s, Status: %s"%(self.VoltageUpLimit,self.VoltageDownLimit,self.MaxChargeCurrent,self.MaxDischargeCurrent,self.Status))
class PPGetSeriesNumber(PylonPacket):
def __init__(self):
super().__init__()
self.CID1=0x46
self.CID2=0x93
self.info=bytearray(1)
self.LENGTH=0x02
@property
def Command(self):
return self.info[0]
@Command.setter
def Command(self, value):
self.info[0] = value
def __str__(self, **kwargs):
return super().__str__(**kwargs)+(", Command: %s"%(self.Command))
class PPSeriesNumber(PylonPacket):
@property
def SeriesNumber(self):
return self.info[1:].decode()
def __str__(self, **kwargs):
return super().__str__(**kwargs)+("\r\n> Series Number: %s"%(self.SeriesNumber))
class PPTurnOff(PylonPacket):
def __init__(self):
super().__init__()
self.CID1=0x46
self.CID2=0x95
self.info=bytearray(1)
self.LENGTH=0x02
@property
def Command(self):
return self.info[0]
@Command.setter
def Command(self, value):
self.info[0] = value
def __str__(self, **kwargs):
return super().__str__(**kwargs)+(", Command: %s"%(self.Command))
class PPTurnOffReply(PylonPacket):
pass