-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPyLinDriver.py
138 lines (103 loc) · 2.75 KB
/
PyLinDriver.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
#This Driver file contain functions which is used to send header frame, master frame to target ECU.
import serial
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(24,GPIO.OUT)
ser = serial.Serial("/dev/ttyS0",
baudrate=19231,
timeout=0.1,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS)
SyncBreak = 0x55
def SendHeader(ID):
GPIO.output(24,0)
time.sleep(0.000677)
GPIO.output(24,1)
time.sleep(0.000322)
# serial expects a bytearray and not a string in python3.
ser.write(chr(SyncBreak).encode()) #This is convert string into a byte array for serial transfer
ser.write(chr(ID).encode())
def SendData(msg):
print(msg)
tmpBuffer= bytearray()
for i in range(len(msg)):
tmpBuffer.append(msg[i])
Cks = CalcChecksum(tmpBuffer)
tmpBuffer.append(Cks)
ser.write(tmpBuffer)
#Function to calculate checksum
def CalcChecksum(msg):
"""check sum calculation
The checksums are computed according to the following formula:
Checksum = INV (data byte 1 ⊕ data byte 2 ⊕ ... ⊕ data byte 8)"""
data =0
for i in range(len(msg)):
data = data+msg[i]
if data >255 :
data = data -255
data = ~data
return ( data & 0xFF)
def SendMasterFrame(ID,msg):
SendHeader(ID) #Master ID
SendData(msg) #Request Frame
def SendHeaderFrame(ID):
x=[]
x1=[]
ser.flushOutput()
ser.flushInput()
SendHeader(ID)
time.sleep(0.010)
a = ser.read(13)
x = a.hex()
print(x)
ser.flushOutput()
ser.flushInput()
'''
if x[0:2] =="55":
x1 = x[4:22]
elif x[0:2] =="7d":
x1 = x[2:20]
else:
x1 = x[0:18]
'''
if x[6:8] =="7d":
x1 = x[8:]
return x1
def SendHeaderFrame_HexResp(ID):
x=[]
x1=[]
ser.flushOutput()
ser.flushInput()
SendHeader(ID)
time.sleep(0.010)
a = ser.read(20)
x = a.hex()
print(x)
ser.flushOutput()
ser.flushInput()
'''
if x[0:2] =="55":
x1 = x[4:22]
elif x[0:2] =="7d":
x1 = x[2:20]
else:
x1 = x[0:18]
'''
if x[6:8] =="7d":
x1 = x[8:]
else:
return None
print(x1)
if x1=="":
return None
digit = []
#print("Function is Called")
for i in range(int(len(x1) / 2)):
HighDigit = int(x1[(i * 2)], 16) * 16
LowDigit = int(x1[(i * 2) + 1], 16)
digit.append(HighDigit + LowDigit)
#print(digit)
return digit