-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_command.py
640 lines (337 loc) · 7.86 KB
/
send_command.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
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
import struct
import socket
import time
def send(arb_id, data, device='can0'):
"""
id: Can id in hex format
data: Can data in hex format
"""
if not hasattr(socket, 'PF_CAN') or not hasattr(socket, 'CAN_RAW'):
print("Python 3.3 or later is needed for native SocketCan")
raise SystemExit(1)
sckt = socket.socket(socket.PF_CAN, socket.SOCK_RAW,
socket.CAN_RAW)
sckt.bind((device,))
# get data, padded to 8 bytes
data = data + [0] * (8 - len(data))
frame_format = "=IB3xBBBBBBBB"
dlc = len(data)
packet = struct.pack(frame_format, arb_id, dlc,
data[0], data[1], data[2], data[3],
data[4], data[5], data[6], data[7])
sckt.send(packet)
def parse(frame):
"""
Receives a string and returns id, data.
frame: String input (e.g. 415#19E6D0F907FA07FA
)
Output
id (hex), data ([hex])
"""
str_id, str_data = frame.split("#")
assert len(str_data) == 16
list_data = [str_data[0:2], str_data[2:4],
str_data[4:6], str_data[6:8],
str_data[8:10], str_data[10:12],
str_data[12:14], str_data[14:16]]
data = [int(item, 16) for item in list_data]
return int(str_id, 16), data
def send_frames(frames, delay=1, device='can0'):
"""Receives a list of frames and sends it via a socket.
"""
for frame in frames.split('\n'):
frame = frame.strip()
# Ignore blank lines
if len(frame) != 20:
continue
arb_id, data = parse(frame)
send(arb_id, data, device=device)
time.sleep(delay)
if __name__=="__main__":
frames="""
415#19DBC80608030803
415#19DDCC0308020802
415#19E1D0FE08010801
415#19E0D4FE08000800
415#19E6D8F708000800
415#19E8DCF407FF07FF
415#19E1E0FA07FE07FE
415#19E4E4F607FD07FD
415#19EAE8EF07FC07FC
415#19E7ECF107FB07FB
415#19E7F0F007FA07FA
415#19E6F4F007FA07FA
415#19E6F8EF07F907F9
415#19E5FCEF07F907F9
415#19E6C0FD07F907F9
415#19E4C4FE07F907F9
415#19E2C8FF07F907F9
415#19DECC0207F907F9
415#19DFD00007F907F9
415#19E1D4FD07F907F9
415#19DFD8FE07FA07FA
415#19DFDCFD07FA07FA
415#19DCE0FF07FA07FA
415#19DEE4FC07FA07FA
415#19E0E8F907FA07FA
415#19E4ECF407FA07FA
415#19E0F0F707FA07FA
415#19E3F4F307FA07FA
415#19DFF8F607FA07FA
415#19E5FCEF07FA07FA
415#19E8C0FB07FA07FA
415#19E4C4FE07FA07FA
415#19E7C8FA07FA07FA
415#19EACCF607FA07FA
415#19E6D0F907FA07FA
415#19ECD4F207FA07FA
415#19F3D8EA07FA07FA
415#19E8DCF407FA07FA
415#19EDE0EE07FB07FB
415#19EAE4F007FB07FB
415#19ECE8ED07FB07FB
415#19EEECEA07FB07FB
415#19EDF0EA07FB07FB
415#19E9F4ED07FB07FB
415#19EAF8EB07FB07FB
415#19E3FCF107FB07FB
415#19E6C0FD07FA07FA
415#19E8C4FA07FA07FA
415#19E3C8FE07FA07FA
415#19E9CCF707FA07FA
415#19E1D0FE07F907F9
415#19E1D4FD07F907F9
415#19E6D8F707F907F9
415#19E5DCF707F907F9
415#19E5E0F607F907F9
415#19E5E4F507F907F9
415#19EBE8EE07F907F9
415#19F4ECE407F907F9
415#19F2F0E507F907F9
415#19F5F4E107F907F9
415#19F5F8E007FA07FA
415#19F1FCE307FA07FA
415#19F1C0F207FA07FA
415#19F1C4F107FA07FA
415#19ECC8F507FA07FA
415#19E7CCF907FA07FA
415#19E7D0F807F907F9
415#19DFD4FF07F907F9
415#19E1D8FC07F907F9
415#19E4DCF807F907F9
415#19DEE0FD07F907F9
415#19E4E4F607F907F9
415#19E2E8F707F907F9
415#19E7ECF107F907F9
415#19E9F0EE07FA07FA
415#19ECF4EA07FA07FA
415#19EBF8EA07FB07FB
415#19EDFCE707FB07FB
415#19EFC0F407FC07FC
415#19F3C4EF07FC07FC
415#19F4C8ED07FD07FD
415#19F3CCED07FD07FD
415#19F6D0E907FE07FE
415#19F4D4EA07FE07FE
415#19F6D8E707FE07FE
415#19F7DCE507FF07FF
415#19F8E0E307FF07FF
415#19F6E4E407FF07FF
415#19F5E8E408000800
415#19F5ECE308000800
415#19FBF0DC08000800
415#19FCF4DA08000800
415#19F7F8DE08000800
415#19F8FCDC08000800
415#19F4C0EF08010801
415#19F2C4F008010801
415#19F6C8EB08010801
415#19F4CCEC08020802
415#19F2D0ED08020802
415#19F0D4EE08030803
415#19EED8EF08030803
415#19F0DCEC08050805
415#19F0E0EB08060806
415#19ECE4EE08070807
415#19F2E8E708090809
415#19EEECEA080A080A
415#19F0F0E7080C080C
415#19F6F4E0080E080E
415#19F5F8E008100810
415#19F6FCDE08110811
415#19FCC0E708130813
415#19FDC4E508140814
415#1A01C8DF08140814
415#1A04CCDB08150815
415#1A03D0DB08160816
415#1A03D4DA08160816
415#1A01D8DB08160816
415#1A03DCD808160816
415#1A06E0D408160816
415#1A02E4D708150815
415#1A01E8D708150815
415#19FFECD908150815
415#19FCF0DB08140814
415#19FDF4D908140814
415#19FCF8D908140814
415#19F3FCE108130813
415#19F7C0EC08120812
415#19F5C4ED08110811
415#19F6C8EB08110811
415#19F3CCED08100810
415#19F4D0EB080F080F
415#19F3D4EB080E080E
415#19FAD8E3080D080D
415#19F6DCE6080C080C
415#19FCE0DF080B080B
415#19FAE4E0080A080A
415#19FBE8DE08090809
415#19FEECDA08090809
415#19FBF0DC08080808
415#19FBF4DB08080808
415#19FFF8D608070807
415#1A01FCD208070807
415#19FAC0E908060806
415#19FDC4E508060806
415#19FBC8E608050805
415#19FFCCE108050805
415#1A01D0DD08050805
415#19FBD4E308040804
415#19FFD8DE08040804
415#19FCDCE008030803
415#19FFE0DC08030803
415#19FEE4DC08020802
415#1A00E8D808020802
415#19FBECDD08010801
415#19FDF0DA08010801
415#19FBF4DB08000800
415#1A02F8D208000800
415#19FEFCD608000800
415#19FFC0E408000800
415#1A04C4DD08000800
415#1A00C8E008000800
415#1A01CCDE07FF07FF
415#1A02D0DC07FF07FF
415#1A01D4DC07FF07FF
415#1A00D8DC07FE07FE
415#1A02DCD907FE07FE
415#1A04E0D607FD07FD
415#1A00E4D907FD07FD
415#1A04E8D407FD07FD
415#1A08DCD307FF07FF
415#1A0AE0D007FF07FF
415#1A09E4D007FF07FF
415#1A08E8D007FF07FF
415#1A07ECD007FF07FF
415#1A0CF0CA07FF07FF
415#1A0BF4CA07FF07FF
415#1A08F8CC07FF07FF
415#1A08FCCB07FF07FF
415#1A06C0DC07FE07FE
415#1A07C4DA07FE07FE
415#1A08C8D807FE07FE
415#1A05CCDA07FE07FE
415#1A06D0D807FE07FE
415#1A03D4DA07FE07FE
415#1A05D8D707FE07FE
415#1A09DCD207FD07FD
415#1A07E0D307FD07FD
415#1A08E4D107FD07FD
415#1A0AE8CE07FD07FD
415#1A08ECCF07FD07FD
415#1A0CF0CA07FC07FC
415#1A0FF4C607FC07FC
415#1A10F8C407FC07FC
415#1A0FFCC407FC07FC
415#1A12C0D007FC07FC
415#1A11C4D007FC07FC
415#1A0EC8D207FB07FB
415#1A0ECCD107FB07FB
415#1A0CD0D207FB07FB
415#1A11D4CC07FB07FB
415#1A0DD8CF07FB07FB
415#1A0FDCCC07FB07FB
415#1A10E0CA07FB07FB
415#1A12E4C707FA07FA
415#1A0DE8CB07FA07FA
415#1A12ECC507FA07FA
415#1A12F0C407FA07FA
415#1A13F4C207FA07FA
415#1A15F8BF07FA07FA
415#1A11FCC207FB07FB
415#1A12C0D007FB07FB
415#1A11C4D007FB07FB
415#1A13C8CD07FB07FB
415#1A10CCCF07FB07FB
415#1A14D0CA07FB07FB
415#1A11D4CC07FB07FB
415#1A11D8CB07FB07FB
415#1A10DCCB07FB07FB
415#1A0FE0CB07FB07FB
415#1A10E4C907FB07FB
415#1A0EE8CA07FB07FB
415#1A16ECC107FB07FB
415#1A14F0C207FB07FB
415#1A12F4C307FB07FB
415#1A15F8BF07FB07FB
415#1A12FCC107FB07FB
415#1A14C0CE07FB07FB
415#1A16C4CB07FB07FB
415#1A14C8CC07FB07FB
415#1A16CCC907FB07FB
415#1A12D0CC07FB07FB
415#1A0DD4D007FB07FB
415#1A0ED8CE07FB07FB
415#1A0CDCCF07FB07FB
415#1A0FE0CB07FC07FC
415#1A0DE4CC07FC07FC
415#1A0EE8CA07FC07FC
415#1A0CECCB07FD07FD
415#1A0EF0C807FD07FD
415#1A0EF4C707FE07FE
415#1A0DF8C707FE07FE
415#1A10FCC307FE07FE
415#1A0CC0D607FE07FE
415#1A13C4CE07FF07FF
415#1A0FC8D107FF07FF
415#1A0ECCD107FF07FF
415#1A0ED0D007FF07FF
415#1A0DD4D007FF07FF
415#1A0FD8CD07FF07FF
415#1A0EDCCD07FF07FF
415#1A0EE0CC07FF07FF
415#1A0EE4CB07FF07FF
415#1A0CE8CC07FF07FF
415#1A0AECCD07FF07FF
415#1A12F0C407FF07FF
415#1A12F4C307FF07FF
415#1A12F8C207FF07FF
415#1A13FCC007FF07FF
415#1A15C0CD07FF07FF
415#1A11C4D008000800
415#1A13C8CD08000800
415#1A11CCCE08000800
415#1A11D0CD08000800
415#1A10D4CD08000800
415#1A0CD8D008000800
415#1A10DCCB08000800
415#1A10E0CA08000800
415#1A0EE4CB08000800
415#1A0FE8C908000800
415#1A14ECC308000800
415#1A0EF0C808000800
415#1A12F4C308000800
415#1A11F8C308000800
415#1A12FCC108000800
415#1A17C0CB08000800
415#1A10C4D107FF07FF
415#1A15C8CB07FF07FF
415#1A12CCCD07FF07FF
415#1A15D0C907FF07FF
415#1A14D4C907FF07FF
415#1A14D8C807FF07FF
415#1A15DCC607FE07FE
415#1A12E0C807FE07FE
"""
for i in range(2):
send_frames(frames, delay=0.35 )