-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAN_Parser_Class.py
614 lines (557 loc) · 40.7 KB
/
CAN_Parser_Class.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
import serial
import time
from datetime import datetime
import threading
from serial.tools import list_ports
#from queue import Queue
import csv
import ntplib
import pynmea2
import random
import json
filestamp = datetime.now().strftime("%Y:%m:%d-%H:%M:%S")
filename = open("CAN_log_hr_%s.txt"%filestamp,'a+')
filename1 = open("CAN_log_json_%s.txt"%filestamp,'a+')
filename.close()
filename1.close
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
class CANReader:
def CAN_Parser(self,raw_msg):
#with open("CAN_log.txt") as log:
while 1:
time = datetime.now().strftime("%H:%M:%S")
port.reset_input_buffer()
receive = port.read(26)
print(receive)
split_msg = receive.split(',')
print(split_msg[0])
print(split_msg)
if(len(split_msg)==9):
if(split_msg[8].endswith('\x00') and len(split_msg)==9):
split_msg[8] = split_msg[8][:-4]
if (split_msg[0] == "A7") or (split_msg[0] == "a7" or (split_msg[0] == "167")) :
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
voltage_list_baby = []
voltage_list_baby.append(time)
voltage_list_baby.append(int(split_msg[1]+split_msg[2], 16)/10)
voltage_list_baby.append(int(split_msg[3]+split_msg[4], 16)/10)
voltage_list_baby.append(int(split_msg[5]+split_msg[6], 16)/10)
voltage_list_baby.append(int(split_msg[7]+split_msg[8], 16)/10)
# voltage_list.append(voltage_list_baby)
voltage_print = "Time: " + str(voltage_list_baby[0]) + ", Voltage_Data - DC Bus Voltage: " + str(voltage_list_baby[1]) + "V, Output Voltage: " + str(voltage_list_baby[2]) + "V, VAB_Vd_Voltage: " + str(voltage_list_baby[3]) + "V, VBC_Vq_Voltage: " + str(voltage_list_baby[4]) + "V\n"
voltage_json = {"Time":str(voltage_list_baby[0]), "DC Bus Voltage":str(voltage_list_baby[1]), "Output Voltage":str(voltage_list_baby[2]), "VAB_Vd_Voltage":str(voltage_list_baby[3]), "VBC_Vq_Voltage":str(voltage_list_baby[4])}
voltage_json = json.dumps(voltage_json)
print(voltage_print)
log.write(voltage_print)
log.close()
log1.write(voltage_json)
log1.close()
elif (split_msg[0] == "A6") or (split_msg[0] == "a6") or (split_msg[0] == "166"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
current_list_baby = []
current_list_baby.append(time)
current_list_baby.append(int(split_msg[1]+split_msg[2], 16)/10)
current_list_baby.append(int(split_msg[3]+split_msg[4], 16)/10)
current_list_baby.append(int(split_msg[5]+split_msg[6], 16)/10)
current_list_baby.append(int(split_msg[7]+split_msg[8], 16)/10)
#current_list.append(current_list_baby)
current_print = "Time: " + str(current_list_baby[0]) + ", Current_Data - Phase A: " + str(current_list_baby[1]) + "A, Phase B: " + str(current_list_baby[2]) + "A, Phase C: " + str(current_list_baby[3]) + "A, DC Bus: " + str(current_list_baby[4]) + "A\n"
current_json = {"Time":str(current_list_baby[0]),"Current_Data - Phase A:": str(current_list_baby[1]), "Phase B:":str(current_list_baby[2])","Phase C":str(current_list_baby[3]), "DC Bus:":str(current_list_baby[4])}
current_json = json.dumps(current_json)
print(current_print)
log.write(current_print)
log.close()
log1.write(current_json)
log1.close()
elif (split_msg[0] == "AC") or (split_msg[0] == "ac") or (split_msg[0] == "172"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
torque_list_baby = []
torque_list_baby.append(time)
torque_list_baby.append(int(split_msg[1]+split_msg[2], 16)/10)
torque_list_baby.append(int(split_msg[3]+split_msg[4], 16)/10)
torque_list_baby.append((int(split_msg[5],16) * 2^23 + int(split_msg[6],16) * 2^15 + int(split_msg[7], 16) * 2^7 + int(split_msg[8],16))/10)
#torque_list.append(torque_list_baby)
torque_print = "Time: " + str(torque_list_baby[0]) + ", Torque_Data - Command Torque: " + str(torque_list_baby[1]) + "Nm, Torque Feedback: " + str(torque_list_baby[2]) + ", Power on Timer: " + str(torque_list_baby[3]) + "sec\n"
torque_json = {"Time":str(torque_list_baby[0]), "Command Torque":str(torque_list_baby[1]), "Torque Feedback":str(torque_list_baby[2]), Power on Timer":str(torque_list_baby[3]}
torque_json = json.dumps(torque_json)
print(torque_print)
log.write(torque_print)
log.close()
log1.write(torque_json)
log1.close()
elif (split_msg[0] == "A8") or (split_msg[0] == "a8") or (split_msg[0] == "168"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
flux_list_baby = []
flux_list_baby.append(time)
flux_list_baby.append(int(split_msg[1]+split_msg[2], 16)/1000)
flux_list_baby.append(int(split_msg[3]+split_msg[4], 16)/1000)
flux_list_baby.append(int(split_msg[5]+split_msg[6], 16)/1000)
flux_list_baby.append(int(split_msg[7]+split_msg[8], 16)/1000)
#flux_list.append(flux_list_baby)
flux_print = "Time: " + str(flux_list_baby[0]) + ", Flux_Data - Flux Command: " + str(flux_list_baby[1]) + ", Flux Feedback:" + str(flux_list_baby[2]) + ", Id Feedback:" + str(flux_list_baby[3]) + "A, Iq Feedback:" + str(flux_list_baby[4]) + "A\n"
flux_json = {"Time":str(flux_list_baby[0]),"Flux Command":str(flux_list_baby[1]), "Flux Feedback":str(flux_list_baby[2]), "Id Feedback":str(flux_list_baby[3]), "Iq Feedback":str(flux_list_baby[4])}
flux_json = json.dumps(flux_json)
print(flux_print)
log.write(flux_print)
log.close()
log1.write(flux_json)
log1.close()
elif (split_msg[0] == "A0") or (split_msg[0] == "a0") or (split_msg[0] == "160"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
temperature1_list_baby = []
temperature1_list_baby.append(time)
temperature1_list_baby.append(int(split_msg[1]+split_msg[2], 16)/10)
temperature1_list_baby.append(int(split_msg[3]+split_msg[4], 16)/10)
temperature1_list_baby.append(int(split_msg[5]+split_msg[6], 16)/10)
temperature1_list_baby.append(int(split_msg[7]+split_msg[8], 16)/10)
#temperature1_list.append(temperature1_list_baby)
temp1_print = "Time: " + str(temperature1_list_baby[0]) + ", Temperature1_data - Module A Temperature: " + str(temperature1_list_baby[1]) + "C, Module B Temperature: " + str(temperature1_list_baby[2]) + "C, Module C Temperature: " + str(temperature1_list_baby[3]) + "C, Gate Driver Board Temperature: " + str(temperature1_list_baby[4]) + "C\n"
temp1_json = {"Time":str(temperature1_list_baby[0]), "Module A Temperature":str(temperature1_list_baby[1]), "Module B Temperature":str(temperature1_list_baby[2]), "Module C Temperature":str(temperature1_list_baby[3]), :Gate Driver Board Temperature":str(temperature1_list_baby[4])}
temp1_json = json.dumps(temp1_json)
print(temp1_print)
log.write(temp1_print)
log.close()
log1.write(temp1_json)
log1.close()
elif (split_msg[0] == "A1") or (split_msg[0] == "a1") or (split_msg[0] == "161"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
temperature2_list_baby = []
temperature2_list_baby.append(time)
temperature2_list_baby.append(int(split_msg[1]+split_msg[2], 16)/10)
temperature2_list_baby.append(int(split_msg[3]+split_msg[4], 16)/10)
temperature2_list_baby.append(int(split_msg[5]+split_msg[6], 16)/10)
temperature2_list_baby.append(int(split_msg[7]+split_msg[8], 16)/10)
#temperature2_list.append(temperature2_list_baby)
temp2_print = "Time: " + str(temperature2_list_baby[0]) + ", Temperature2_data - Central Board Temperature: " + str(temperature2_list_baby[1]) + "C, RTD_1 Temperature: " + str(temperature2_list_baby[2]) + "V, RTD_2 Temperature: " + str(temperature2_list_baby[3]) + "C, RTD_3 Temperature: " + str(temperature2_list_baby[4]) + "C\n"
temp2_json = {"Time":str(temperature2_list_baby[0]), "Central Board Temperature":str(temperature2_list_baby[1]),"RTD_1 Temperature":str(temperature2_list_baby[2]), "RTD_2 Temperature":str(temperature2_list_baby[3]), "RTD_3 Temperature":str(temperature2_list_baby[4])}
temp2_json = json.dumps(temp2_json)
print(temp2_print)
log.write(temp2_print)
log.close()
log1.write(temp2_json)
log1.close()
elif (split_msg[0] == "A2") or (split_msg[0] == "a2") or (split_msg[0] == "162"):
log = open('CAN_log_hr_%s.txt'%filestamp, 'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
temperature3_list_baby = []
temperature3_list_baby.append(time)
temperature3_list_baby.append(int(split_msg[1]+split_msg[2], 16)/10)
temperature3_list_baby.append(int(split_msg[3]+split_msg[4], 16)/10)
temperature3_list_baby.append(int(split_msg[5]+split_msg[6], 16)/10)
temperature3_list_baby.append(int(split_msg[7]+split_msg[8], 16)/10)
#temperature3_list.append(temperature3_list_baby)
temp3_print = "Time: " + str(temperature3_list_baby[0]) + ", Temperature_Data - RTD_4 Temperature: " + str(temperature3_list_baby[1]) + "C, RTD_5 Temperature: " + str(temperature3_list_baby[2]) + "C, Motor Temperature: " + str(temperature3_list_baby[3]) + "C, Torque Shudder: " + str(temperature3_list_baby[4]) + "Nm\n"
temp3_json = {"Time":str(temperature3_list_baby[0]), "RTD_4 Temperature":str(temperature3_list_baby[1]), "RTD_5 Temperature":str(temperature3_list_baby[2]), "Motor Temperature":str(temperature3_list_baby[3]),"Torque Shudder":str(temperature3_list_baby[4])}
temp3_json = json.dumps(temp3_json)
print(temp3_print)
log.write(temp3_print)
log.close()
log1.write(temp3_json)
log1.close()
elif (split_msg[0] == "A9") or split_msg[0] == 'a9':
log = open('CAN_log_hr_%s.txt'%filestamp, 'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
internal_voltage_list_baby = []
internal_voltage_list_baby.append(time)
internal_voltage_list_baby.append(int(split_msg[1]+split_msg[2], 16)/10)
internal_voltage_list_baby.append(int(split_msg[3]+split_msg[4], 16)/10)
internal_voltage_list_baby.append(int(split_msg[5]+split_msg[6], 16)/10)
internal_voltage_list_baby.append(int(split_msg[7]+split_msg[8], 16)/10)
#internal_voltage_list.append(internal_voltage_list_baby)
internal_voltage_print = "Time: " + str(internal_voltage_list_baby[0]) + ", Internal_Voltage_data - 1.5V Reference Voltage: " + str(internal_voltage_list_baby[1]) + "V, 2.5V Reference Voltage: " + str(internal_voltage_list_baby[2]) + "V, 5.0V Reference Voltage: " + str(internal_voltage_list_baby[3]) + "V, 12V Reference Voltage: " + str(internal_voltage_list_baby[4]) + "V\n"
internal_voltage_json = {"Time":str(internal_voltage_list_baby[0]), "1.5V Reference Voltage":str(internal_voltage_list_baby[1]), "2.5V Reference Voltage":str(internal_voltage_list_baby[2]), "5.0V Reference Voltage":str(internal_voltage_list_baby[3]), "12V Reference Voltage":str(internal_voltage_list_baby[4])}
internal_voltage_json = json.dumps(internal_voltage_json)
print(internal_voltage_print)
log.write(internal_voltage_print)
log.close()
log1.write(internal_voltage_json)
log1.close()
#NEEDS WORK
elif (split_msg[0] == "AAAAAAAAAA") or (split_msg[0] == "aaaaaa") or (split_msg[0] == "170,170,170"):
internal_state_list_baby = []
internal_state_list_output = []
internal_state_list_baby.append(time)
internal_state_list_baby.append(int((split_msg[1]) + (split_msg[2]),16))
internal_state_list_baby.append(int(split_msg[3], 16))
internal_state_list_baby.append(split_msg[4])
internal_state_list_baby.append(int(split_msg[5] , 16)& 1) #internal_state_list_baby[4]
internal_state_list_baby.append((int(split_msg[5] , 16)& 224)/32) #internal_state_list_baby[5]
internal_state_list_baby.append(int(split_msg[6], 16)) #internal_state_list_baby[6]
internal_state_list_baby.append(int(split_msg[7] , 16)& 1) #internal_state_list_baby[7]
internal_state_list_baby.append((int(split_msg[7] , 16)& 224)/32) #internal_state_list_baby[8]
internal_state_list_baby.append(int(split_msg[8] , 16)& 1) #internal_state_list_baby[9]
internal_state_list_baby.append(int(split_msg[8] , 16)& 2) #internal_state_list_baby[10]
internal_state_list_baby.append(int(split_msg[8] , 16)& 4) #internal_state_list_baby[11]
#internal_state_list.append(internal_state_list_baby)
if internal_state_list_baby[1] == 0:
internal_state_list_output.append('VSM Start State')
elif internal_state_list_baby[1] == 1:
internal_state_list_output.append('Pre-charge Init State')
elif internal_state_list_baby[1] == 2:
internal_state_list_output.append('Pre-charge Active State')
elif internal_state_list_baby[1] == 3:
internal_state_list_output.append('Pre-charge Complete State')
elif internal_state_list_baby[1] == 4:
internal_state_list_output.append('VSM Wait State')
elif internal_state_list_baby[1] == 5:
internal_state_list_output.append('VSM Ready State')
elif internal_state_list_baby[1] == 6:
internal_state_list_output.append('Motor Running State')
elif internal_state_list_baby[1] == 7:
internal_state_list_output.append('Blink Fault Code State')
elif internal_state_list_baby[1] == 14:
internal_state_list_output.append('Shutdown in Process')
elif internal_state_list_baby[1] == 15:
internal_state_list_output.append('Recycle Power State')
else:
internal_state_list_output.append('Error Unknown Value')
if internal_state_list_baby[2] == 0:
internal_state_list_output.append('Power on State')
elif internal_state_list_baby[2] == 1:
internal_state_list_output.append('Stop State')
elif internal_state_list_baby[2] == 2:
internal_state_list_output.append('Open Loop State')
elif internal_state_list_baby[2] == 3:
internal_state_list_output.append('Closed Loop State')
elif internal_state_list_baby[2] == 4:
internal_state_list_output.append('Wait State')
elif internal_state_list_baby[2] == 5 or internal_state_list_baby[2] == 6 or internal_state_list_baby[2] == 7:
internal_state_list_output.append('Internal States')
elif internal_state_list_baby[2] == 8:
internal_state_list_output.append('Idle Run State')
elif internal_state_list_baby[2] == 9:
internal_state_list_output.append('Idle Stop State')
elif internal_state_list_baby[2] == 10 or internal_state_list_baby[2] == 11 or internal_state_list_baby[2] == 12:
internal_state_list_output.append('Internal States')
if (int(internal_state_list_baby[3],16) & 1) == 0:
internal_state_list_output.append('Relay 1 Off')
else:
internal_state_list_output.append('Relay 1 On')
if (int(internal_state_list_baby[3],16) & 2) == 0:
internal_state_list_output.append('Relay 2 Off')
else:
internal_state_list_output.append('Relay 2 On')
if (int(internal_state_list_baby[3],16) & 4) == 0:
internal_state_list_output.append('Relay 3 Off')
else:
internal_state_list_output.append('Relay 3 On')
if (int(internal_state_list_baby[3],16) & 8) == 0:
internal_state_list_output.append('Relay 4 Off')
else:
internal_state_list_output.append('Relay 4 On')
if (int(internal_state_list_baby[3],16) & 16) == 0:
internal_state_list_output.append('Relay 5 Off')
else:
internal_state_list_output.append('Relay 5 On')
if (int(internal_state_list_baby[3],16) & 32) == 0:
internal_state_list_output.append('Relay 6 Off')
else:
internal_state_list_output.append('Relay 6 On')
if internal_state_list_baby[4] == 0:
internal_state_list_output.append('Torque Mode')
elif internal_state_list_baby[4] == 1:
internal_state_list_output.append('Speed Mode')
if internal_state_list_baby[5] == 0:
internal_state_list_output.append('Current Inverter Active Discharge State: Discharge Disabled')
elif internal_state_list_baby[5] == 1:
internal_state_list_output.append('Current Inverter Active Discharge State: Discharge Enabled, Waiting')
elif internal_state_list_baby[5] == 2:
internal_state_list_output.append('Current Inverter Active Discharge State: Performing Speed Check')
elif internal_state_list_baby[5] == 3:
internal_state_list_output.append('Current Inverter Active Discharge State: Discharge Actively Occurring')
elif internal_state_list_baby[5] == 4:
internal_state_list_output.append('Current Inverter Active Discharge State: Discharge Completed')
if internal_state_list_baby[6] == 0:
internal_state_list_output.append('CAN Mode')
elif internal_state_list_baby[6] == 1:
internal_state_list_output.append('VSM Mode')
if internal_state_list_baby[7] == 0:
internal_state_list_output.append('Inverter is Disabled')
elif internal_state_list_baby[7] == 1:
internal_state_list_output.append('Inverter is Enabled')
if internal_state_list_baby[8] == 0:
internal_state_list_output.append('Inverter can be Enabled')
else :
internal_state_list_output.append('Inverter canot be Enabled')
if internal_state_list_baby[9] == 0:
internal_state_list_output.append('Reverse, if inverter is enabled - Stopped, if inverter is disabled')
elif internal_state_list_baby[9] == 1:
internal_state_list_output.append('Forward')
if internal_state_list_baby[10] == 0:
internal_state_list_output.append('BMS Message is NOT being Received')
else:
internal_state_list_output.append('BMS Message is being Received')
if internal_state_list_baby[11] == 0:
internal_state_list_output.append('Torque is NOT being limited by the BMS')
else:
internal_state_list_output.append('Torque is being limited by the BMS')
print(internal_state_list_output)
#print("Internal_State_data - Time: " internal_state_list_baby[0] ", VSM State: " internal_state_list_baby[1] ", Inverter State: " internal_state_list_baby[2] ", Relay State: " internal_state_list_baby[3])
#print(", Inverter Run Mode: " internal_state_list_baby[4] ", Inverter Active Discharge State: " internal_state_list_baby[5] "Inverter Command Mode: " internal_state_list_baby[6])
#print(", Inverter Enable State: " internal_state_list_baby[7])
#INCLUDE Analog Singals NEEDS WORK
elif (split_msg[0] == "A3") or (split_msg[0] == "a3") or (split_msg[0] == "163"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_hr_json%s.txt'%filestamp,'a')
analog_input_voltages_list_baby = []
analog_input_voltages_list_baby.append(time)
analog_input_voltages_list_baby.append((int(split_msg[1] + split_msg[2] , 16)&int('1111111111000000',2))/64/10.)
analog_input_voltages_list_baby.append((int(split_msg[2] + split_msg[3] , 16)&int('0011111111110000',2))/16/10.)
analog_input_voltages_list_baby.append((int(split_msg[3] + split_msg[4] , 16)&int('0000111111111100',2))/4/10.)
analog_input_voltages_list_baby.append((int(split_msg[4] + split_msg[5] , 16)&int('0000001111111111',2))/10.)
analog_input_voltages_list_baby.append((int(split_msg[6] + split_msg[7] , 16)&int('1111111111000000',2))/64/10.)
analog_input_voltages_list_baby.append((int(split_msg[7] + split_msg[8] , 16)&int('0011111111110000',2))/16/10.)
#analog_input_voltages_list.append(analog_input_voltages_list_baby)
analog_input_volt_print = "Time: " + str(analog_input_voltages_list_baby[0]) + ", Analog_Input_Voltages - Analog Input 1: " + str(analog_input_voltages_list_baby[1]) + "V, Analog Input 2: " + str(analog_input_voltages_list_baby[2]) + "V, Analog Input 3: " + str(analog_input_voltages_list_baby[3]) + "V, Analog Input 4: " + str(analog_input_voltages_list_baby[4]) + "V, Analog Input 5: " + str(analog_input_voltages_list_baby[5]) + "V, Analog Input 6: " + str(analog_input_voltages_list_baby[6]) + '\n'
analog_input_volt_json = {"Time":str(analog_input_voltages_list_baby[0]), "Analog Input 1":str(analog_input_voltages_list_baby[1]), "Analog Input 2":str(analog_input_voltages_list_baby[2]), "Analog Input 3":str(analog_input_voltages_list_baby[3]), "Analog Input 4":str(analog_input_voltages_list_baby[4]), "Analog Input 5":str(analog_input_voltages_list_baby[5]), "Analog Input 6":str(analog_input_voltages_list_baby[6])}
analog_input_volt_json = json.dumps(analog_input_volt_json)
print(analog_input_volt_print)
log.write(analog_input_volt_print)
log.close()
log1.write(analog_input_volt_json)
log1.close()
elif (split_msg[0] == "AB") or (split_msg[0] == "ab") or (split_msg[0] == "171"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
fault_codes_list_baby = []
fault_codes_list_baby.append(time)
fault_codes_list_baby.append(int(split_msg[1]+split_msg[2], 16)/10)
fault_codes_list_baby.append(int(split_msg[3]+split_msg[4], 16)/10)
fault_codes_list_baby.append(int(split_msg[5]+split_msg[6], 16)/10)
fault_codes_list_baby.append(int(split_msg[7]+split_msg[8], 16)/10)
#fault_codes_list.append(fault_codes_list_baby)
fault_codes_print = "Time: " + str(fault_codes_list_baby[0]) + ", Fault_Codes_Data - POST Fault Lo: " + str(fault_codes_list_baby[1]) + " POST Fault HI: " + str(fault_codes_list_baby[2]) + " Run Fault Lo: " + str(fault_codes_list_baby[3]) + " Run Fault Hi: " + str(fault_codes_list_baby[4]) + '\n'
fault_codes_json = {"Time":str(fault_codes_list_baby[0]), "POST Fault Lo":str(fault_codes_list_baby[1]), "POST Fault HI":str(fault_codes_list_baby[2]), "Run Fault Lo":str(fault_codes_list_baby[3]),"Run Fault Hi":str(fault_codes_list_baby[4])}
fault_codes_json = json.dumps(fault_codes_json)
print(fault_codes_print)
log.write(fault_codes_print)
log.close()
log1.write(fault_codes_json)
log1.close()
elif (split_msg[0] == "007") or (split_msg[0] == "7"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
cell_data_list_baby = []
cell_data_output = []
cell_data_list_baby.append(time)
cell_data_list_baby.append(int(split_msg[1], 16))
cell_data_list_baby.append(int(split_msg[2], 16))
cell_data_list_baby.append(int(split_msg[3], 16)/1000.)
cell_data_list_baby.append(int(split_msg[4], 16)/1000.)
cell_data_list_baby.append(int(split_msg[5], 16)/500.-20)
cell_data_list_baby.append(int(split_msg[6], 16)/500.-20)
#cell_data_list.append(cell_data_list_baby)
status_byte = cell_data_list_baby[2]
if(status_byte & 1 ==0):
cell_data_output.append(", Cell is NOT CONNECTED")
else:
cell_data_output.append(", Cell is CONNECTED")
if(status_byte & 2 ==0):
cell_data_output.append(", Data is INVALID")
else:
cell_data_output.append(", Cell is VALID")
if(status_byte & 4 ==0):
cell_data_output.append(", Cell is NOT BEING BALANCED")
else:
cell_data_output.append(", Cell is BEING BALACNED")
if(status_byte & 1 ==0):
cell_data_output.append(", temp sensor is CONNECTED")
else:
cell_data_output.append(", temp sensor is DISCONNECTED")
cell_data_print = ("Time: " + str(fault_codes_list_baby[0]) + ", Cell_data - Cell Number:" + str(fault_codes_list_baby[1])+ "Cell Status: " + str(status_byte) + ", Voltage H: "+str(fault_codes_list_baby[3]) + ", Voltage L: "+str(fault_codes_list_baby[4]) + ", Temp H: "+str(fault_codes_list_baby[5]) + ", Temp L: "+str(fault_codes_list_baby[6]))
cell_data_json = {"Time":str(fault_codes_list_baby[0]), "Cell Number":str(fault_codes_list_baby[1]), "Cell Status":str(status_byte), "Voltage H":str(fault_codes_list_baby[3]), "Voltage L":str(fault_codes_list_baby[4]), "Temp H":str(fault_codes_list_baby[5]), "Temp L":str(fault_codes_list_baby[6])}
cell_data_json = json.dumps(cell_data_json)
print(cell_data_print)
log.write(cell_data_print)
log.close()
log1.write(fault_codes_json)
log1.close()
elif (split_msg[0] == "008") or (split_msg[0] == "8"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
bms_data_list_baby = []
bms_data_output = []
bms_data_list_baby.append(time)
bms_data_list_baby.append(int(split_msg[1], 16))
bms_data_list_baby.append(int(split_msg[2], 16))
bms_data_list_baby.append(int(split_msg[3], 16))
bms_data_list_baby.append(int(split_msg[4], 16))
bms_data_list_baby.append(int(split_msg[5], 16))
bms_data_list_baby.append(int(split_msg[6], 16))
#bms_data_list.append(bms_data_list_baby)
status_byte = bms_data_list_baby[2]
if(status_byte & 1 ==0):
bms_data_output.append(", There is no OV fault")
else:
bms_data_output.append(", OV FAULT")
if(status_byte & 2 ==0):
bms_data_output.append(", There is no UV fault")
else:
bms_data_output.append(", UV FAULT")
if(status_byte & 4 ==0):
bms_data_output.append(", There is no OT fault")
else:
bms_data_output.append(", OT FAULT")
if(status_byte & 8 ==0):
bms_data_output.append(", There is no disconnected cell fault")
else:
bms_data_output.append(", DISCONNECTED CELL FAULT")
if(status_byte & 16 ==0):
bms_data_output.append(", There is no disconnected temp sensor fault")
else:
bms_data_output.append(", DISCONNECTED TEMP SENSOR FAULT")
bms_data_print = ("Time: " + str(bms_data_list_baby[0]) + ", bms_data - Status: " + bms_data_output + ", OV Cell Num: "+str(bms_data_list_baby[2]) + ", UV Cell Num: "+str(bms_data_list_baby[3]) + ", OT Cell Num: "+str(bms_data_list_baby[4]) + ", Disconnected Voltage Cell Num: " +str(bms_data_list_baby[5])+ ", Disconnected Temp Cell Num: " +str(bms_data_list_baby[6]))
bms_data_json = {"Time":str(bms_data_list_baby[0]), "Status":bms_data_output, "OV Cell Num":str(bms_data_list_baby[2]), "UV Cell Num":str(bms_data_list_baby[3]), "OT Cell Num":str(bms_data_list_baby[4]), "Disconnected Voltage Cell Num":str(bms_data_list_baby[5]), "Disconnected Temp Cell Num":str(bms_data_list_baby[6])}
bms_data_json = json.dumps(bms_data_json)
print(bms_data_print)
log.write(bms_data_print)
log.close()
log1.write(fault_codes_json)
log1.close()
elif (split_msg[0] == "009") or (split_msg[0] == "9"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
bms_Vin_list_baby = []
bms_Vin_list_baby.append(time)
bms_Vin_list_baby.append(int(split_msg[1], 16)/1000.)
bms_Vin_list_baby.append(int(split_msg[2], 16)/1000.)
bms_Vin_list_baby.append(int(split_msg[3], 16)/1000.)
bms_Vin_list_baby.append(int(split_msg[4], 16)/1000.)
bms_Vin_list_baby.append(int(split_msg[5], 16)/1000.)
bms_Vin_list_baby.append(int(split_msg[6], 16)/1000.)
bms_Vin_list_baby.append(int(split_msg[7], 16)/1000.)
bms_Vin_list_baby.append(int(split_msg[8], 16)/1000.)
#bms_Vin_list.append(bms_Vin_list_baby)
bms_Vin_print = ("Time: " + str(bms_Vin_list_baby[0]) + ", bms_Vin_data - Voltage Max H: " + str(bms_Vin_list_baby[1]) + ", Voltage Max L: " + str(bms_Vin_list_baby[2]) + ", Cell Max Num: " + str(bms_Vin_list_baby[3]) + ", Voltage Min H: " + str(bms_Vin_list_baby[4]) + ", Voltage Min L: " + str(bms_Vin_list_baby[5]) + ", Cell Min Num " + str(bms_Vin_list_baby[6]) + ", Voltage Avg H: " + str(bms_Vin_list_baby[7]) + ", Voltage Avg L: " + str(bms_Vin_list_baby[8]))
bms_Vin_json = {"Time":str(bms_Vin_list_baby[0]), "Voltage Max H":str(bms_Vin_list_baby[1]), "Voltage Max L":str(bms_Vin_list_baby[2]), "Cell Max Num":str(bms_Vin_list_baby[3]), "Voltage Min H":str(bms_Vin_list_baby[4]), "Voltage Min L":str(bms_Vin_list_baby[5]), "Cell Min Num":str(bms_Vin_list_baby[6]), "Voltage Avg H":str(bms_Vin_list_baby[7]), "Voltage Avg L":str(bms_Vin_list_baby[8])}
bms_Vin_json = json.dumps(bms_Vin_json)
print(bms_Vin_print)
log.write(bms_Vin_print)
log.close()
log1.write(fault_codes_json)
log1.close()
elif (split_msg[0] == "0A") or (split_msg[0] == "10"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
bms_Tin_list_baby = []
bms_Tin_list_baby.append(time)
bms_Tin_list_baby.append(int(split_msg[1], 16)/500.-20)
bms_Tin_list_baby.append(int(split_msg[2], 16)/500.-20)
bms_Tin_list_baby.append(int(split_msg[3], 16)/500.-20)
bms_Tin_list_baby.append(int(split_msg[4], 16)/500.-20)
bms_Tin_list_baby.append(int(split_msg[5], 16)/500.-20)
bms_Tin_list_baby.append(int(split_msg[6], 16)/500.-20)
bms_Tin_list_baby.append(int(split_msg[7], 16)/500.-20)
bms_Tin_list_baby.append(int(split_msg[8], 16)/500.-20)
#bms_Tin_list.append(bms_Tin_list_baby)
bms_Tin_print = print("Time: " + str(bms_Tin_list_baby[0]) + ", bms_Tin_Data - Temp Max H: " + str(bms_Tin_list_baby[1]) + ", Temp Max L: " + str(bms_Tin_list_baby[2]) + ", Cell Max Num: " + str(bms_Tin_list_baby[3]) + ", Temp Min H: " + str(bms_Tin_list_baby[4]) + ", Temp Min L: " + str(bms_Tin_list_baby[5]) + ", Cell Min Num " + str(bms_Tin_list_baby[6]) + ", Temp Avg H: " + str(bms_Tin_list_baby[7]) + ", Temp Avg L: " + str(bms_Tin_list_baby[8]))
bms_Tin_json = {"Time":str(bms_Tin_list_baby[0]), "Temp Max H":str(bms_Tin_list_baby[1]), "Temp Max L":str(bms_Tin_list_baby[2]),"Cell Max Num":str(bms_Tin_list_baby[3]), "Temp Min H":str(bms_Tin_list_baby[4]), "Temp Min L":str(bms_Tin_list_baby[5]), "Cell Min Num":str(bms_Tin_list_baby[6]), "Temp Avg H":str(bms_Tin_list_baby[7]),"Temp Avg L":str(bms_Tin_list_baby[8])}
bms_Tin_json = json.dumps(bms_Tin_json)
print(bms_Tin_print)
log.write(bms_Tin_print)
log.close()
log1.write(fault_codes_json)
log1.close()
elif (split_msg[0] == "0B") or (split_msg[0] == "11"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
bms_packstat_baby = []
bms_packstat_baby.append(time)
bms_packstat_baby.append(int(split_msg[1], 16))
bms_packstat_baby.append(int(split_msg[2], 16))
bms_packstat_baby.append(int(split_msg[3], 16))
bms_packstat_baby.append(int(split_msg[4], 16))
#bms_packstatTin_list.append(bms_packstat_baby)
bms_packstat_print = print("Time: " + str(bms_packstat_baby[0]) + ", Packstat - Pack Voltage H: " + str(bms_packstat_baby[1]) + ", Pack Voltage L: " + str(bms_packstat_baby[2]) + ", Pack Current H: " + str(bms_packstat_baby[3]) + ", Pack Current L: " + str(bms_packstat_baby[4]))
bms_packstat_json = {"Time":str(bms_packstat_baby[0]), "Pack Voltage H":str(bms_packstat_baby[1]), "Pack Voltage L":str(bms_packstat_baby[2]), "Pack Current H":str(bms_packstat_baby[3]), "Pack Current L":str(bms_packstat_baby[4])}
bms_packstat_json = json.dumps(bms_packstat_json)
print(bms_packstat_print)
log.write(bms_packstat_print)
log.close()
log1.write(fault_codes_json)
log1.close()
elif split_msg[0] == bin(int("0D0",16))[2:]:
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
saftey_faults_list_baby = []
saftey_faults_list_baby.append(time)
saftey_faults_list_baby.append(int(split_msg[1],16))
saftey_faults_list_baby.append(int(split_msg[2],16))
saftey_faults_list_baby.append(int(split_msg[3],16))
saftey_faults_list_baby.append(int(split_msg[4],16))
#saftey_faults_list.append(saftey_faults_list_baby)
output = []
if(saftey_faults_list_baby[1] !=0):
output.append("BMS: Fault Present ")
else:
output.append("BMS: no fault ")
if(saftey_faults_list_baby[2] !=0):
output.append("IMD: Fault Present ")
else:
output.append("IMD: no fault")
if(saftey_faults_list_baby[3] !=0):
output.append("BSPD: Fault Present ")
else:
output.append("BSPD: no fault")
if(saftey_faults_list_baby[4] !=0):
output.append("APPS: Fault Present ")
else:
output.append("APPS: no fault ")
safety_faults_print = ("Time: "+str(saftey_faults_list_baby[0])+", Safety Faults - "+output)
safety_faults_json = {"Time":str(saftey_faults_list_baby[0]), "Safety Faults":output}
safety_faults_json = json.dumps(safety_faults_json)
print(safety_faults_print)
log.write(safety_faults_print)
log.close()
log1.write(fault_codes_json)
log1.close()
elif (split_msg[0] == "0D1") or (split_msg[0] == "209"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
saftey_precharge_list_baby = []
saftey_precharge_list_baby.append(time)
saftey_precharge_list_baby.append(int(split_msg[1],16))
#saftey_precharge_list.append(saftey_precharge_list_baby)
output = []
if(saftey_precharge_list_baby[1] !=0):
output.append("not charged")
else:
output.append("Charged")
safety_precharge_print = ("Time: "+str(saftey_precharge_list_baby[0])+", Precharge - " +output)
safety_precharge_json = {"Time":str(saftey_precharge_list_baby[0]), "Precharge":output}
safety_precharge_json = json.dumps(safety_precharge_json)
print(safety_precharge_print)
log.write(safety_precharge_print)
log.close()
log1.write(fault_codes_json)
log1.close()
elif (split_msg[0] == "0D2") or (split_msg[0] == "210"):
log = open('CAN_log_hr_%s.txt'%filestamp,'a')
log1 = open('CAN_log_json_%s.txt'%filestamp,'a')
saftey_enable_list_baby = []
saftey_enable_list_baby.append(time)
saftey_enable_list_baby.append(int(split_msg[1],16))
#saftey_enable_list.append(saftey_enable_list_baby)
output = []
if(saftey_enable_list_baby[1] !=0):
output.append("not enabled")
else:
output.append("Enabled")
safety_enable_print = ("Time: "+str(saftey_enable_list_baby[0])+", Enable - " +output)
safety_enable_json = {"Time":str(saftey_enable_list_baby[0]),"Enable":output}
safety_enable_json = json.dumps(safety_enable_json)
print(safety_enable_print)
log.write(safety_enable_print)
log.close()
log1.write(fault_codes_json)
log1.close()
CAN_Data = CANReader()
port.reset_input_buffer()
receive = port.read(26)
CAN_Data.CAN_Parser(receive)