-
Notifications
You must be signed in to change notification settings - Fork 1
/
T5LiveData.bak
executable file
·1074 lines (869 loc) · 38.9 KB
/
T5LiveData.bak
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import socket
import struct
import sys
import time
import signal
import subprocess
from random import randint
from threading import Thread, Lock
from tkinter import *
from tkinter import ttk
import queue
import os.path
TIMEOUT = "TIMEOUT"
CR = 13
NL = 10
SYM_FILE = "T5_SymTable"
SWVER_FILE = "T5_SWVersion"
BG_COLOR = "#F0F0ED"
class GuiApp:
def __init__(self, master, queue, endCommand):
# class variables
self.bMeasureMode = False
self.kylTempMax = 0
self.luftTempMax = 0
self.rpmMax = 0
self.speedMax = 0
self.boost = 0
self.boostMax = 0
self.tqMax = 0
self.powerMax = 0
self.queue = queue
self.main = master
self.vKyl = StringVar()
self.vKylPeak = StringVar()
self.vLuft = StringVar()
self.vLuftPeak = StringVar()
self.vRpm = StringVar()
self.vRpmPeak = StringVar()
self.vSpeed = StringVar()
self.vSpeedPeak = StringVar()
self.vBoost = StringVar()
self.vBoostPeak = StringVar()
self.vTQ = StringVar()
self.vTQPeak = StringVar()
self.vPower = StringVar()
self.vPowerPeak = StringVar()
self.vAFR = StringVar()
self.vKyl.set('0')
self.vLuft.set('0')
self.vKylPeak.set('0')
self.vLuftPeak.set('0')
self.vRpm.set('0')
self.vRpmPeak.set('0')
self.vSpeed.set('0')
self.vSpeedPeak.set('0')
self.vBoost.set('0')
self.vBoostPeak.set('0')
self.vAFR.set('14.7')
self.vTQ.set('0')
self.vTQPeak.set('0')
self.vPower.set('0')
self.vPowerPeak.set('0')
stest = ttk.Style()
stest.theme_use('clam')
stest.configure("red.test", foreground='red', background='red')
gui_style = ttk.Style()
gui_style.configure("My.TFrame", background= BG_COLOR)
gui_style.configure("My.TNotebook", background= BG_COLOR)
self.nbook = ttk.Notebook(self.main, style = "My.TNotebook")
self.f1 = ttk.Frame(self.nbook, style = "My.TFrame")
self.f2 = ttk.Frame(self.nbook, style = "My.TFrame")
self.f3 = ttk.Frame(self.nbook, style = "My.TFrame")
self.f4 = ttk.Frame(self.nbook, style = "My.TFrame")
self.nbook.add(self.f1, text='Sensoren 1')
self.nbook.add(self.f2, text='Sensoren 2')
self.nbook.add(self.f3, text='Zeit')
self.nbook.add(self.f4, text='Status')
#self.nbook.grid()
self.main.bind('<Button-1>', self.mclick)
# Set up the GUI
self.main.wm_title("Trionic Live-Daten")
# frame 1
Label(self.f1,text = "Sensor", padx=5, pady=5).grid(row=0, column=0)
Label(self.f1,text = "aktueller Wert", padx=5, pady=5).grid(row=0, column=1)
ttk.Separator(self.f1, orient=VERTICAL).grid(column=2, row=0, rowspan=7, sticky="ns")
Label(self.f1,text = "Peak", padx=5, pady=5).grid(row=0, column=3)
ttk.Separator(self.f1, orient=HORIZONTAL).grid(row=1, columnspan=4, sticky="ew")
Label(self.f1, text = "Kühltemp.", padx=25, pady=10).grid(row=3, column=0)
Label(self.f1, text = "Einlasstemp.", padx=5, pady=10).grid(row=4, column=0)
Label(self.f1, text = "Geschwindigkeit", padx=5, pady=10).grid(row=5, column=0)
Label(self.f1, text = "Drehzahl", padx=5, pady=10).grid(row=6, column=0)
Label(self.f1, textvariable = self.vKyl, padx=5, pady=10).grid(row=3, column=1)
Label(self.f1, textvariable = self.vKylPeak, padx=5, pady=10).grid(row=3, column=3)
Label(self.f1, textvariable = self.vLuft, padx=5, pady=10).grid(row=4, column=1)
Label(self.f1, textvariable = self.vLuftPeak, padx=5, pady=10).grid(row=4, column=3)
Label(self.f1, textvariable = self.vSpeed, padx=5, pady=10).grid(row=5, column=1)
Label(self.f1, textvariable = self.vSpeedPeak, padx=5, pady=10).grid(row=5, column=3)
Label(self.f1, textvariable = self.vRpm, padx=5, pady=10).grid(row=6, column=1)
Label(self.f1, textvariable = self.vRpmPeak, padx=5, pady=10).grid(row=6, column=3)
ttk.Separator(self.f1, orient=HORIZONTAL).grid(row=7, columnspan=4, sticky="ew")
Label(self.f1, text = "U/min", padx=0, pady=5).grid(row=8, column=0)
sRed = ttk.Style()
sRed.theme_use('clam')
sRed.configure("red.Horizontal.TProgressbar", foreground='red', background='red')
sGreen = ttk.Style()
sGreen.theme_use('clam')
sGreen.configure("green.Horizontal.TProgressbar", foreground='green', background='green')
sYellow = ttk.Style()
sYellow.theme_use('clam')
sYellow.configure("yellow.Horizontal.TProgressbar", foreground='yellow', background='yellow')
self.pbRPM = ttk.Progressbar(self.f1, style="green.Horizontal.TProgressbar", orient="horizontal", length=175, mode="determinate")
self.pbRPM["value"] = 0
self.pbRPM["maximum"] = 6200
self.pbRPM.grid(row=8, column=1, columnspan=3, padx=1, pady=5)
# frame 2
Label(self.f2,text = "Sensor", padx=5, pady=5).grid(row=0, column=0)
Label(self.f2,text = "aktueller Wert", padx=5, pady=5).grid(row=0, column=1)
ttk.Separator(self.f2, orient=VERTICAL).grid(column=2, row=0, rowspan=7,sticky="ns")
Label(self.f2,text = "Peak", padx=5, pady=5).grid(row=0, column=3)
ttk.Separator(self.f2, orient=HORIZONTAL).grid(row=1, columnspan=4, sticky="ew")
Label(self.f2, text = "A/F Ratio", padx=31, pady=10).grid(row=3, column=0)
Label(self.f2, text = "Boost", padx=5, pady=10).grid(row=4, column=0)
Label(self.f2, text = "Drehmoment", padx=5, pady=10).grid(row=5, column=0)
Label(self.f2, text = "Leistung", padx=5, pady=10).grid(row=6, column=0)
Label(self.f2, textvariable = self.vAFR, padx=5, pady=10).grid(row=3, column=1)
Label(self.f2, text = "------", padx=5, pady=10).grid(row=3, column=3)
Label(self.f2, textvariable = self.vBoost, padx=5, pady=10).grid(row=4, column=1)
Label(self.f2, textvariable = self.vBoostPeak, padx=5, pady=10).grid(row=4, column=3)
Label(self.f2, textvariable = self.vTQ, padx=5, pady=10).grid(row=5, column=1)
Label(self.f2, textvariable = self.vTQPeak, padx=5, pady=10).grid(row=5, column=3)
Label(self.f2, textvariable = self.vPower, padx=5, pady=10).grid(row=6, column=1)
Label(self.f2, textvariable = self.vPowerPeak, padx=5, pady=10).grid(row=6, column=3)
ttk.Separator(self.f2, orient=HORIZONTAL).grid(row=7, columnspan=4, sticky="ew")
Label(self.f2, text = "Boost", padx=0, pady=5).grid(row=8, column=0)
self.pbBoost = ttk.Progressbar(self.f2, orient="horizontal", length=170, mode="determinate")
# boost from -1 to 2 bar --> from 0 to 3
self.pbBoost["value"] = 0
self.pbBoost["maximum"] = 2
self.pbBoost.grid(row=8, column=1, columnspan=3, padx=1, pady=5)
# frame 3
self.vStart = IntVar()
Label(self.f3, text = "Start bei (km/h)", pady=10).grid(row=0, column=2)
Radiobutton(self.f3, text= "60", variable = self.vStart, value = 60, padx=1).grid(row = 1, column = 0)
Radiobutton(self.f3, text= "80", variable = self.vStart, value = 80, padx=1).grid(row = 1, column = 1)
Radiobutton(self.f3, text= "100", variable = self.vStart, value = 100, padx=1).grid(row = 1, column = 2)
Radiobutton(self.f3, text= "120", variable = self.vStart, value = 120, padx=1).grid(row = 1, column = 3)
Radiobutton(self.f3, text= "140", variable = self.vStart, value = 140, padx=1).grid(row = 1, column = 4)
self.vStart.set(100)
Label(self.f3, text = "Stop bei (km/h)", pady=10).grid(row=2, column=2)
self.vStop = IntVar()
Radiobutton(self.f3, text= "120", variable = self.vStop, value = 120).grid(row = 3, column = 0)
Radiobutton(self.f3, text= "140", variable = self.vStop, value = 140).grid(row = 3, column = 1)
Radiobutton(self.f3, text= "160", variable = self.vStop, value = 160).grid(row = 3, column = 2)
Radiobutton(self.f3, text= "180", variable = self.vStop, value = 180).grid(row = 3, column = 3)
Radiobutton(self.f3, text= "200", variable = self.vStop, value = 200, pady=5).grid(row = 3, column = 4)
self.vStop.set(160)
self.buttStart = Button(self.f3, text = "Start", command=self.startPressed, pady=5)
self.buttStart.grid(row=4, column=2)
self.vTime = StringVar()
self.lTime = Label(self.f3, textvariable = self.vTime, font = "Verdana 10 bold", padx = 5, pady=10).grid(row=5, column=2)
# frame 4
Label(self.f4, text = "Motorstatus:", padx=10, pady=10).grid(row=0, column=0)
self.cbIdle = Checkbutton(self.f4, text="Idle", padx=10, pady=5)
self.cbIdle.grid(row=1, column=0, sticky=W)
self.cbCL = Checkbutton(self.f4, text="Closed Loop", padx=10, pady=5)
self.cbCL.grid(row=2, column=0, sticky=W)
self.cbKnock = Checkbutton(self.f4, text="Knock map", padx=10, pady=5)
self.cbKnock.grid(row=3, column=0, sticky=W)
self.cbWarm = Checkbutton(self.f4, text="Warmup", padx=10, pady=5)
self.cbWarm.grid(row=4, column=0, sticky=W)
self.cbThr = Checkbutton(self.f4, text="Throttle closed", padx=10, pady=5)
self.cbThr.grid(row=5, column=0, sticky=W)
self.cbCheck6 = Checkbutton(self.f4, text="Check6")
self.cbCheck6.grid(row=1, column=1, sticky=W)
self.cbCheck7 = Checkbutton(self.f4, text="Check7")
self.cbCheck7.grid(row=2, column=1, sticky=W)
self.cbCheck8 = Checkbutton(self.f4, text="Check8")
self.cbCheck8.grid(row=3, column=1, sticky=W)
self.cbCheck9 = Checkbutton(self.f4, text="Check9")
self.cbCheck9.grid(row=4, column=1, sticky=W)
self.cbCheck10 = Checkbutton(self.f4, text="Check10")
self.cbCheck10.grid(row=5, column=1, sticky=W)
# GUI stuff end
def mclick(self, event):
#self.pbRPM.focus_set()
xr = self.main.winfo_pointerx()
yr = self.main.winfo_pointery()
#print("Mouse clicked at", event.x, event.y, xr, yr)
frameID = self.nbook.index(self.nbook.select())
#print ("frame", x)
def startShowData(self):
self.InfoBox.destroy()
self.nbook.grid()
def showInit(self):
ShowStr = '\n\n Reading Symbol Table...'
self.InfoBox = Label(self.main, text=ShowStr)
self.InfoBox.grid()
def startPressed(self):
print ("start pressed...", self.vStart.get(), self.vStop.get())
lock.acquire()
self.queue.queue.clear()
self.bMeasureMode = True
self.bCleanupQueue = True
self.buttStart.config(state=DISABLED)
lock.release()
def getFrameID(self):
return self.nbook.index(self.nbook.select())
def processIncoming(self):
"""
Handle all the messages currently in the queue (if any).
"""
kylTemp = 0
luftTemp = 0
rpm = 0
speed = 0
boost = 0
afr = 0
pgm = 0
tq = 0
power = 0
while self.queue.qsize():
if (self.queue.qsize() > 2): # queue too full ?
print ("Queue items: %i\n" % self.queue.qsize())
try:
msg = self.queue.get(0)
# Test on ##
#print (msg)
# Test off ##
frameID = self.nbook.index(self.nbook.select())+1
#print ("FrameID: "+ str(frameID))
if (frameID == 3):
# only update frame 3
for item in msg:
if item == "txt":
self.vTime.set(msg[item])
if item == "speed":
speed = msg[item]
self.main.update_idletasks()
else:
for item in msg:
if item == "kylTemp":
kylTemp = msg[item]
if item == "luftTemp":
luftTemp = msg[item]
if item == "rpm":
rpm = msg[item]
if item == "speed":
speed = msg[item]
if item == "boost":
boost = msg[item]
if item == "afr":
afr = msg[item]
if item == "pgm":
pgm = msg[item]
if item == "tq":
tq = msg[item]
power = tq*rpm/7121
if item == "pgm":
pgm = msg[item]
# calculate status bar
if (frameID == 4):
# idle --> byte 3 & 0x40
if (pgm & 0x0000000040000000) > 0:
self.cbIdle.select()
else:
self.cbIdle.deselect()
# closed loop --> byte 3 & 0x02
if (pgm & 0x0000000002000000) > 0:
self.cbCL.select()
else:
self.cbCL.deselect()
# knock (only T5.5) --> byte 1 & 0x02
if (pgm & 0x0000000000000200) > 0:
self.cbKnock.select()
print ("Knock detected!")
else:
self.cbKnock.deselect()
# warmup --> byte 0 & 0x10
if (pgm & 0x0000000000000010) > 0:
self.cbWarm.select()
else:
self.cbWarm.deselect()
# calculate max values
if (kylTemp > self.kylTempMax):
self.kylTempMax = kylTemp
if (rpm > self.rpmMax):
self.rpmMax = rpm
if (luftTemp > self.luftTempMax):
self.luftTempMax = luftTemp
if (speed > self.speedMax):
self.speedMax = speed
if (boost > self.boostMax):
self.boostMax = boost
if (tq > self.tqMax):
self.tqMax = tq
if (power > self.powerMax):
self.powerMax = power
self.vKyl.set(str(kylTemp) + " °C")
self.vKylPeak.set(str(self.kylTempMax) + " °C")
self.vKyl.set(str(kylTemp) + " °C")
self.vKylPeak.set(str(self.kylTempMax) + " °C")
self.vLuft.set(str(luftTemp) + " °C")
self.vLuftPeak.set(str(self.luftTempMax) + " °C")
self.vRpm.set(str(rpm) + " U/min")
self.vRpmPeak.set(str(self.rpmMax) + " U/min")
self.pbRPM["value"] = rpm
self.vSpeed.set(str(speed) + " km/h")
self.vSpeedPeak.set(str(self.speedMax) + " km/h")
self.vAFR.set("%.2f" % afr)
self.vBoost.set("%.2f bar" % boost)
self.vBoostPeak.set("%.2f bar" % (self.boostMax))
self.pbBoost["value"] = (boost + 1) # convert from (-1 to 2) to (0 to 3)
if (rpm < 5000):
self.pbRPM.configure(style="green.Horizontal.TProgressbar")
elif (rpm >= 5000 and rpm < 6000):
self.pbRPM.configure(style="yellow.Horizontal.TProgressbar")
else:
self.pbRPM.configure(style="red.Horizontal.TProgressbar")
if (boost < 0.65):
self.pbBoost.configure(style="green.Horizontal.TProgressbar")
elif (boost >= 0.65 and boost < 0.8):
self.pbBoost.configure(style="yellow.Horizontal.TProgressbar")
elif (boost >= 0.8):
self.pbBoost.configure(style="red.Horizontal.TProgressbar")
self.vTQ.set(str(tq) + " Nm")
self.vTQPeak.set(str(self.tqMax) + " Nm")
self.vPower.set("%3.0f PS" % power)
self.vPowerPeak.set("%3.0f PS" % (self.powerMax))
# calculate status bar
self.main.update_idletasks()
except Queue.Empty:
pass
class CanClient:
# class variables
# frame format: (16 bytes)
# "<" --> little endian
# "I" --> unsigned int (4 bytes) --> frame ID
# "B" --> unsigned char (1 byte) --> frame length
# "3x" --> 3x pad byte --> padding
# "Q" --> unsigned long long (8 bytes) --> frame data
#
# example: b"\x0c\x00\x00\x00\x08\xff\xff\xff\xc7\x00\x30\x25\x80\x00\x5c\x00"
# ---------------- ID : 0000000c
# ---- : length = 8 bytes
# ------------ : padding
# -------------------------------- : data bytes
can_frame_fmt = "<IB3xQ"
speedStart = 0
speedStop = 0
bMeasureMode = False
bRecording = False
count = 0
### here are the symbol adresses
kyltemp_adr = 0x102E # kyl_temp (1 byte)
lufttemp_adr = 0x1033 # luft_temp (1 byte)
rpm_adr = 0x1066 # rpm (2 bytes)
speed_adr = 0x101C # bil_hast (1 byte)
boost_adr = 0x3356 # p_medel (1 byte)
ad_sond_adr = 0x1012 # AD_sond (1 byte)
pgm_adr = 0x1039 # pgm_status (6 bytes)
tq_adr = 0x1074 # TQ (2 bytes)
demoMode = False
dumpMode = False
symMode = False
swMode = False
symbolTable = []
def __init__(self, master):
for i in range(len(sys.argv)):
if (sys.argv[i].upper() == "DEMO"):
self.demoMode = True
print("starting in Demo mode....")
if (sys.argv[i].upper() == "DUMP"):
self.dumpMode = True
if (sys.argv[i].upper() == "SYMBOL"):
self.symMode = True
if (sys.argv[i].upper() == "SW"):
self.swMode = True
# test ########## !!!!!!!!!!!!!!
self.symMode = True
#self.swMode = True
#self.dumpMode = True
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# check if CANUSB is attached. If not --> demo mode
cmd = "ip addr | grep slcan0"
try:
p = subprocess.check_output([cmd], shell=True)
print("CanUSB/slcan0 device found !")
except:
self.demoMode = True
print("No slcan0 device found. Demo mode active")
self.master = master
# Create the queue
self.queue = queue.Queue()
# Set up the CAN connection, of no demo mode
if (not self.demoMode):
self.cansocket = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
self.cansocket.bind(('slcan0',))
if self.symMode:
# force reading from ECU and writing into file
self.getT5SymbolTable(True)
time.sleep(1)
self.sendC2Cmd()
print ("finish App")
exit(1)
elif self.swMode:
self.getT5SWVersion()
time.sleep(1)
self.sendC2Cmd()
print ("finish App")
exit(1)
else:
# normal main thread with GUI
# Set up the thread to do asynchronous I/O
# Set up the GUI part
self.gui = GuiApp(master, self.queue, self.endApplication)
master.protocol("WM_DELETE_WINDOW", self.endApplication)
self.running = 1
self.thread1 = Thread(target=self.getT5Values)
self.thread1.start()
# Start the periodic call in the GUI to check if the queue contains
# anything
self.periodicCall()
# cleanup stuff
def endApplication(self):
if (self.dumpMode):
print("endApplication called - CleanUp")
self.running = 0
self.bRecording = False
self.sendC2Cmd()
if (not self.demoMode):
self.cansocket.close()
###################
# can frame stuff #
###################
def build_can_frame(self, can_id, data):
_s = struct.pack(self.can_frame_fmt, can_id, 8, data)
#print ("build_can_frame:",_s)
return _s
def dissect_can_frame(self, can_frame):
can_id, can_dlc, data = struct.unpack(self.can_frame_fmt, can_frame) # check
return (can_id, data)
def sendReadSRAMCmd(self, address):
address += 5 # 5 bytes are read
cmd = 0x00000000000000C7;
cmd |= (address & 0x000000FF) << 4 * 8;
cmd |= ((address & 0x0000FF00) >> 8) << 3 * 8;
cmd |= ((address & 0x00FF0000) >> 2 * 8) << 2 * 8;
cmd |= ((address & 0xFF000000) >> 3 * 8) << 8;
x = self.build_can_frame(0x05, cmd)
if (self.sendFrame(x)):
return True
else:
return False
def sendCmdByte(self, cmdbyte):
cmd = 0x0000000000000000
cmd |= cmdbyte
cmd <<= 8
cmd |= 0xC4
cmd |= 0xFFFFFFFFFFFF0000
x = self.build_can_frame(0x05, cmd)
if (self.sendFrame(x)):
return True
else:
return False
def waitForResponse(self):
returnStr = ""
recv = self.readFrame()
values = bytearray(recv)
#### only for test #####
if self.demoMode:
values[8] = 0xc6 # C6 must be content of byte 7
###########
if ((values[8]) != 0xc6): # 0xC6 is always in the response code in byte 7 (0 - 7)
print ("Wrong response received!")
return TIMEOUT
id,data = self.dissect_can_frame(recv)
#print ("waitForResponse -> Data received: %#.16x" % data)
returnStr += chr((data >> 16) & 0xFF)
print ("waitForResponse:", (data >> 16) & 0xFF)
self.sendAck()
return returnStr
def sendAck(self):
cmd = 0x00000000000000C6
x = self.build_can_frame(0x06, cmd)
if (self.sendFrame(x)):
return True
else:
return False
def sendCmdByteN(self, cmdbyte, nrOfBytes):
retStr = ""
self.sendCmdByte(cmdbyte)
for i in range(nrOfBytes):
str = self.waitForResponse()
if (str == TIMEOUT):
return TIMEOUT
retStr += str
#print ("sendCmdByteN returns:",retStr)
return retStr
def sendCmdByteE(self, cmdbyte, endString):
retStr = ""
self.sendCmdByte(cmdbyte)
finished = False
while not finished:
str = self.waitForResponse()
if (str == TIMEOUT):
return TIMEOUT
retStr += str
print ("String read:", str)
if not endString.endswith(chr(CR)+chr(NL)):
if len(retStr) > len(endString):
print ("sendCmdByteE break, returns:",retStr)
return retStr
#print ("sendCmdByteE returns:",retStr)
finished = (retStr.endswith(endString))
return retStr
def sendCmdByteEMax(self, cmdbyte, endString, maxLength):
retStr = ""
self.sendCmdByte(cmdbyte)
finished = False
cnt = 0
while not finished:
str = self.waitForResponse()
if (str == TIMEOUT):
return TIMEOUT
retStr += str
if not endString.endswith(chr(CR)+chr(NL)):
if len(retStr) > len(endString):
print ("sendCmdByteEMax break, returns:",retStr)
return retStr
#print ("sendCmdByteEMax returns:",retStr)
cnt += 1
finished = ((retStr.endswith(endString)) or (cnt >= maxLength))
return retStr
def sendC2Cmd(self):
cmd = 0x00000000000000C2
x = self.build_can_frame(0x05, cmd)
if (not self.sendFrame(x)):
return False
# Trionic answers, hence read one frame
rcv = self.readFrame()
return True
def sendFrame(self, can_frame):
if (self.dumpMode):
self.printFrame(can_frame, True)
if (self.demoMode):
return True
try:
self.cansocket.send(can_frame)
return True
except socket.error:
if (self.dumpMode):
print('Error sending CAN frame')
return False
def readFrame(self):
self.count += 1
if (self.demoMode):
recv = b"\x0c\x00\x00\x00\x08\xff\xff\xff\xc7\x00\x30\x25\x80\x00\x5c\x01", ('slcan0', 29)
else:
recv = (self.cansocket).recvfrom(16)
# only for test #
if (self.symMode and self.demoMode):
if self.count < 5:
recv = b"\x0c\x00\x00\x00\x08\xff\xff\xff\xc7\x00\x30\x25\x80\x00\x5c\x00", ('slcan0', 29)
elif self.count == 6:
recv = b"\x0c\x00\x00\x00\x08\xff\xff\xff\xc7\x00E\x25\x80\x00\x5c\x00", ('slcan0', 29)
elif self.count == 7:
recv = b"\x0c\x00\x00\x00\x08\xff\xff\xff\xc7\x00N\x25\x80\x00\x5c\x00", ('slcan0', 29)
elif self.count == 8:
recv = b"\x0c\x00\x00\x00\x08\xff\xff\xff\xc7\x00D\x25\x80\x00\x5c\x00", ('slcan0', 29)
elif self.count == 9:
recv = b"\x0c\x00\x00\x00\x08\xff\xff\xff\xc7\x00\x0d\x25\x80\x00\x5c\x00", ('slcan0', 29)
elif self.count == 10:
recv = b"\x0c\x00\x00\x00\x08\xff\xff\xff\xc7\x00\x0a\x25\x80\x00\x5c\x00", ('slcan0', 29)
self.count = 0
if (self.count > 10):
self.count = 0
# test end ###
self.printFrame(recv[0], False)
return recv[0] # only 1st element (canframe) is interesting
def getT5SymbolTable(self, bForce = False):
# check, if Symboltable already exists
bSymFile = os.path.isfile(SYM_FILE)
# read Symtable from Trionic and store it in file
if bForce or not bSymFile:
self.sendCmdByteN(ord('S'), 1) # capital "S" for T5.5
symTableRaw = self.sendCmdByteE(CR,"END"+chr(CR)+chr(NL))
### test start
# symTableRaw = ">A554X24L.18C\r\n48700001STSWFS\r\n38600001VSS_status\r\n3AC60002TWFS1PU\nEND"
### test end
print ("\nTrionic has symbol table (read from ECU):\n" + symTableRaw)
f = open(SYM_FILE, 'w')
f.write(symTableRaw)
f.close
print ("Symbol table written in file")
else: # read from file
f = open(SYM_FILE, 'r')
symTableRaw = f.read() # whole file into a string
f.close
print ("\nTrionic has symbol table (read from file):\n" + symTableRaw)
# store raw date into a list of tupels
_symTbl = symTableRaw.split("\n")
#print (_symTbl, len(_symTbl))
for i in range(1, len(_symTbl)-1): # w/o SW version and last line
x = _symTbl[i]
if "END" not in x:
element = (x[8:], hex(int(x[0:4],16)), int(int(x[4:8],16)))
#print (element)
self.symbolTable.append(element)
if (self.dumpMode):
print ("SymbolTable:\n",self.symbolTable)
def getT5SWVersion(self):
# small "s" for T5.5
T5typ = "5.5"
self.sendCmdByteN(ord('s'), 1)
version = self.sendCmdByteEMax(CR,chr(CR)+chr(NL),20)
#version = ">>A554X24L.18C\r\n"
version = version.lstrip('>')
version = version.rstrip('\n')
version = version.rstrip('\r')
version = version.replace(">", "")
n = len(version)
if n < 10: # trionic 5.2 --> send capital "S", version is 1st string of symtable
self.sendCmdByteN(ord('S'), 1)
version = self.sendCmdByteE(CR, chr(CR)+chr(NL))
version = version.lstrip('>')
version = version.rstrip('\n')
version = version.rstrip('\r')
T5typ = "5.2"
if n > 12:
version = version[n-12:n]
print ("\nTrionic (type: %s) has SW version: %s\n" % (T5typ,version))
if not os.path.isfile(SWVER_FILE):
f = open(SWVER_FILE, 'w')
f.write(version)
f.close
print ("SW version written in file")
# gets the 6 data bytes out of the frame (bytes 0 - 5)
def getSRAMReadData(self, recvframe):
rid, rdata = self.dissect_can_frame(recvframe)
if (rid != 12): # 0x0C is always ID for received messages
print("Error frame received")
return -1
# data byte array contains all 6 received data bytes
databytes = bytearray(recvframe)[10:]
databytes.reverse()
return databytes
def getT5Data(self, address, length, specCalc = False):
self.sendReadSRAMCmd(address)
recvframe = self.readFrame()
#print("getT5Data, readFrame:", recvframe)
datalist = self.getSRAMReadData(recvframe)
# for some special states like pgm_data, special calculation needed
if (specCalc):
return self.ConvertDataToDouble2(datalist, length)
return self.ConvertDataToDouble(datalist, length)
def ConvertDataToDouble(self, datalist, length):
if length == 1:
retval = datalist[0]
if length == 2:
retval = datalist[0] * 256
retval += datalist[1]
if length == 4:
retval = datalist[0] *256*256*256
retval += datalist[1] *256*256
retval += datalist[2] *256
retval += datalist[3]
#data = 0
#for i in range(length):
# data = data * (256 ** i)
# data += datalist[i]
# print (i, datalist[i], chr(datalist[i]), hex(datalist[i]), data)
return retval
def ConvertDataToDouble2(self, datalist, length):
retval = 0
# to check:simpler algorithm possible ?
if length == 4:
retval = datalist[3] *256*256*256
retval += datalist[2] *256*256
retval += datalist[1] *256
retval += datalist[0]
if length == 5:
retval = datalist[4] *256*256*256*256
retval += datalist[3] *256*256*256
retval += datalist[2] *256*256
retval += datalist[1] *256
retval += datalist[0]
if length == 6:
retval = datalist[5] *256*256*256*256*256
retval += datalist[4] *256*256*256*256
retval += datalist[3] *256*256*256
retval += datalist[2] *256*256
retval += datalist[1] *256
retval += datalist[0]
return retval
def printFrame(self, can_frame, bTX):
id,data = self.dissect_can_frame(can_frame)
if bTX:
s="TX"
else:
s="RX"
if (self.dumpMode):
print("--- %s: Frame ID:%.3x Data: %#.16x" % (s, id, data))
#######################
# can frame stuff end #
#######################
def periodicCall(self):
"""
Check every 100 ms if there is something new in the queue.
"""
self.gui.processIncoming()
if not self.running:
# This is the brutal stop of the system. You may want to do
# some cleanup before actually shutting it down.
time.sleep(1)
print ("Programm terminated.")
sys.exit(1)
self.master.after(100, self.periodicCall)
def getAdrforSymbol(self, sym):
for elem in self.symbolTable:
if elem[0] == sym:
print ("Symbol found:", elem)
return int(elem[1],16)
def getT5Values(self):
# init phase, show dialog
self.gui.showInit()
self.getT5SymbolTable()
# read symbol address from table
self.kyltemp_adr = self.getAdrforSymbol("Kyl_temp")
self.lufttemp_adr = self.getAdrforSymbol("Lufttemp")
self.rpm_adr = self.getAdrforSymbol("Rpm")
self.speed_adr = self.getAdrforSymbol("Bil_hast")
self.boost_adr = self.getAdrforSymbol("P_medel")
self.ad_sond_adr = self.getAdrforSymbol("AD_sond")
self.pgm_adr = self.getAdrforSymbol("Pgm_status")
self.tq_adr = self.getAdrforSymbol("TQ")
# run phase
self.gui.startShowData()
_speed = 0
_bUp = True
values = {}
while self.running:
try:
fID = (self.gui.getFrameID()) + 1
if (fID == 1): #frame 1
# read coolant temp
kylTemp = self.getT5Data(self.kyltemp_adr, 1)
if (kylTemp > 128):
kylTemp = -(256 - kylTemp)
time.sleep(0.05)
# read lufttemp
luftTemp = self.getT5Data(self.lufttemp_adr, 1)
if (luftTemp > 128):
luftTemp = -(256 - luftTemp)
if (self.demoMode):
luftTemp = randint(10, 30)
time.sleep(0.05)
# read rpm
rpm = self.getT5Data(self.rpm_adr, 2)
rpm *= 10
#if (self.demoMode):
# rpm = randint(0,6500)
time.sleep(0.05)
# read speed (bil_hast)
speed = self.getT5Data(self.speed_adr, 1)
if (self.demoMode):
speed = randint(0,200)
time.sleep(0.05)
values = {"rpm":rpm,"speed":speed,"kylTemp":kylTemp, "luftTemp":luftTemp}
self.queue.put(values)
#values = {}
elif (fID == 2): # frame 2
# read boost (p_medel)
boost = self.getT5Data(self.boost_adr, 1)
if (self.demoMode):
boost = randint(10, 250)
boost *= 0.01
boost -= 1
# read afr(ad_sond)
afr = self.getT5Data(self.ad_sond_adr, 1)
if (self.demoMode):
afr = 40
afr = abs(125 - afr) # return value is between -12 and -75
afr /= 100
afr *= 14.7
time.sleep(0.05)
# read torque
tq = self.getT5Data(self.tq_adr, 2)
time.sleep(0.05)
values = {"boost":boost,"afr":afr, "tq":tq}
self.queue.put(values)
elif (fID == 3): # frame 3
self.speedStart = self.gui.vStart.get()
self.speedStop = self.gui.vStop.get()
self.bRecording = self.gui.bMeasureMode
speed = self.getT5Data(self.speed_adr, 1)
if (self.demoMode):
if (_bUp):
_speed += 1
if (_speed >= 200): _bUp = False
else :
_speed -= 1
if (_speed <= 0): _bUp = True
speed = _speed
time.sleep(0.05)
if ((speed > self.speedStart) and not self.bRecording):
textstr = "Warte..." + str('\u2193')
values = {"txt":textstr, "speed":speed}
self.queue.put(values)
if ((speed < self.speedStart) and not self.bRecording):
textstr = "Warte..." + str('\u2191')
values = {"txt":textstr, "speed":speed}
self.queue.put(values)
if ((speed == self.speedStart) and not self.bRecording):
print ("Start set at:", speed)
self.bRecording = True