4 import csv, time, argparse;
7 from random import randrange
8 from GoodFETMCPCAN import GoodFETMCPCAN;
9 from GoodFETMCPCANCommunication import GoodFETMCPCANCommunication
10 from intelhex import IntelHex;
15 class FordExperiments(GoodFETMCPCANCommunication, dataLocation):
18 GoodFETMCPCANCommunication.__init__(self, dataLocation)
19 #super(FordExperiments,self).__init__(self) #initialize chip
25 def mimic1056(self,packetData,runTime):
28 self.spitSetup(self.freq)
29 #FIGURE out how to clear buffers
30 self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
31 packet1 = self.client.rxpacket();
33 packetParsed = self.client.packet2parsed(packet1);
34 #keep sniffing till we read a packet
35 while( packet1 == None or packetParsed.get('sID') != 1056 ):
36 packet1 = self.client.rxpacket()
38 packetParsed = self.client.packet2parsed(packet1)
39 recieveTime = time.time()
40 packetParsed = self.client.packet2parsed(packet1)
41 if( packetParsed['sID'] != 1056):
42 print "Sniffed wrong packet"
44 countInitial = ord(packetParsed['db3']) #initial count value
46 #set data packet to match what was sniffed or at least what was input
49 if(packetData.get(idx) == None):
50 packet.append(ord(packetParsed.get(idx)))
52 packet.append(packetData.get(idx))
54 #### split SID into different regs
55 SIDlow = (1056 & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
56 SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
57 packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
58 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
60 packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
62 self.client.txpacket(packet);
64 while( (time.time()-recieveTime) < runTime):
65 #care about db3 or packet[8] that we want to count at the rate that it is
66 dT = time.time()-tpast
68 db3 = (countInitial + math.floor((time.time()-recieveTime)/0.2))%255
70 self.client.txpacket(packet)
74 self.client.MCPrts(TXB0=True)
75 tpast = time.time() #update our transmit time on the one before
79 def cycledb1_1056(self,runTime):
83 #FIGURE out how to clear buffers
84 self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
85 packet1 = self.client.rxpacket();
87 packetParsed = self.client.packet2parsed(packet1);
88 #keep sniffing till we read a packet
89 while( packet1 == None or packetParsed.get('sID') != 1056 ):
91 packet1 = self.client.rxpacket()
93 packetParsed = self.client.packet2parsed(packet1)
94 recieveTime = time.time()
95 packetParsed = self.client.packet2parsed(packet1)
96 if( packetParsed['sID'] != 1056):
97 print "Sniffed wrong packet"
100 #set data packet to match what was sniffed or at least what was input
103 packet.append(ord(packetParsed.get(idx)))
105 packet[1] = packetValue;
108 #### split SID into different regs
109 SIDlow = (1056 & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
110 SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
111 packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
112 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
113 # lower nibble is DLC
114 packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
116 self.client.txpacket(packet);
118 while( (time.time()-recieveTime) < runTime):
119 #care about db3 or packet[8] that we want to count at the rate that it is
120 dT = time.time()-tpast
123 #temp = ((packetValue+1))%2
125 # pV = packetValue%255
131 self.client.txpacket(packet)
133 tpast = time.time() #update our transmit time on the one before
136 def getBackground(self,sId):
137 packet1 = self.client.rxpacket();
139 packetParsed = self.client.packet2parsed(packet1);
140 #keep sniffing till we read a packet
141 while( packet1 == None or packetParsed.get('sID') != sId ):
142 packet1 = self.client.rxpacket()
144 packetParsed = self.client.packet2parsed(packet1)
146 #recieveTime = time.time()
149 def cycle4packets1279(self):
150 self.client.serInit()
153 self.addFilter([1279, 1279, 1279, 1279, 1279, 1279], verbose = False)
154 packetParsed = self.getBackground(1279)
156 if (packetParsed[db0] == 16):
157 # if it's the first of the four packets, replace the value in db7 with 83
158 packetParsed[db7] = 83
159 # transmit new packet
160 self.client.txpacket(packetParsed)
162 # otherwise, leave it alone
163 # transmit same pakcet we read in
164 self.client.txpacket(packetParsed)
165 # print the packet we are transmitting
169 def oscillateTemperature(self,time):
171 self.client.serInit()
173 #FIGURE out how to clear buffers
174 self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
175 packetParsed = self.getBackground(1056)
177 #set data packet to match what was sniffed or at least what was input
180 packet.append(ord(packetParsed.get(idx)))
182 packet[1] = packetValue;
185 #### split SID into different regs
186 SIDlow = (1056 & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
187 SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
188 packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
189 0x08, # bit 6 must be set to 0 for data frame (1 for RTR)
190 # lower nibble is DLC
191 packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
193 self.client.txpacket(packet);
194 startTime = tT.time()
195 while( (tT.time()-startTime) < runTime):
196 dt = tT.time()-startTime
197 inputValue = ((2.0*math.pi)/20.0)*dt
198 value = 30*math.sin(inputValue)+130
200 #packet[5] = int(value)
207 self.client.txpacket(packet)
209 #tpast = time.time() #update our transmit time on the one before
213 if __name__ == "__main__":
214 fe = FordExperiments();
216 packetData['db4'] = 4;
218 #fe.mimic1056(packetData, runTime)
219 #fe.cycledb1_1056(runTime)
220 fe.oscillateTemperature(runTime)