|
14 | 14 | import can |
15 | 15 | logging.basicConfig(level=logging.INFO) |
16 | 16 |
|
17 | | -channel = 'vcan0' |
18 | 17 |
|
19 | | - |
20 | | -def test_simple_periodic_send(): |
21 | | - print("Starting to send a message every 200ms. Initial data is zeros") |
22 | | - msg = can.Message(arbitration_id=0x123, data=[0, 0, 0, 0, 0, 0], extended_id=False) |
23 | | - task = can.send_periodic('vcan0', msg, 0.20) |
| 18 | +def simple_periodic_send(bus): |
| 19 | + """ |
| 20 | + Sends a message every 20ms with no explicit timeout |
| 21 | + Sleeps for 2 seconds then stops the task. |
| 22 | + """ |
| 23 | + print("Starting to send a message every 200ms for 2s") |
| 24 | + msg = can.Message(arbitration_id=0x123, data=[1, 2, 3, 4, 5, 6], extended_id=False) |
| 25 | + task = bus.send_periodic(msg, 0.20) |
24 | 26 | time.sleep(2) |
25 | 27 | task.stop() |
26 | 28 | print("stopped cyclic send") |
27 | 29 |
|
28 | 30 |
|
29 | | -def test_extended_periodic_send(): |
30 | | - print("Starting to send a message every 200ms. Initial data is zeros") |
| 31 | +def limited_periodic_send(bus): |
| 32 | + print("Starting to send a message every 200ms for 1s") |
31 | 33 | msg = can.Message(arbitration_id=0x12345678, data=[0, 0, 0, 0, 0, 0], extended_id=True) |
32 | | - task = can.send_periodic('vcan0', msg, 0.20) |
33 | | - time.sleep(2) |
34 | | - task.stop() |
35 | | - print("stopped cyclic send") |
| 34 | + bus.send_periodic(msg, 0.20, 1) |
| 35 | + time.sleep(3) |
| 36 | + # task.stop() |
| 37 | + # print("stopped cyclic send") |
| 38 | + |
| 39 | + |
| 40 | +# def test_periodic_send_with_modifying_data(): |
| 41 | +# print("Starting to send a message every 200ms. Initial data is ones") |
| 42 | +# msg = can.Message(arbitration_id=0x0cf02200, data=[1, 1, 1, 1]) |
| 43 | +# task = can.send_periodic('vcan0', msg, 0.20) |
| 44 | +# time.sleep(2) |
| 45 | +# print("Changing data of running task to begin with 99") |
| 46 | +# msg.data[0] = 0x99 |
| 47 | +# task.modify_data(msg) |
| 48 | +# time.sleep(2) |
| 49 | +# |
| 50 | +# task.stop() |
| 51 | +# print("stopped cyclic send") |
| 52 | +# print("Changing data of stopped task to single ff byte") |
| 53 | +# msg.data = bytearray([0xff]) |
| 54 | +# task.modify_data(msg) |
| 55 | +# time.sleep(1) |
| 56 | +# print("starting again") |
| 57 | +# task.start() |
| 58 | +# time.sleep(1) |
| 59 | +# task.stop() |
| 60 | +# print("done") |
| 61 | +# |
| 62 | +# |
| 63 | +# def test_dual_rate_periodic_send(): |
| 64 | +# """Send a message 10 times at 1ms intervals, then continue to send every 500ms""" |
| 65 | +# msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5]) |
| 66 | +# print("Creating cyclic task to send message 10 times at 1ms, then every 500ms") |
| 67 | +# task = can.interface.MultiRateCyclicSendTask('vcan0', msg, 10, 0.001, 0.50) |
| 68 | +# time.sleep(2) |
| 69 | +# |
| 70 | +# print("Changing data[0] = 0x42") |
| 71 | +# msg.data[0] = 0x42 |
| 72 | +# task.modify_data(msg) |
| 73 | +# time.sleep(2) |
| 74 | +# |
| 75 | +# task.stop() |
| 76 | +# print("stopped cyclic send") |
| 77 | +# |
| 78 | +# time.sleep(2) |
| 79 | +# |
| 80 | +# task.start() |
| 81 | +# print("starting again") |
| 82 | +# time.sleep(2) |
| 83 | +# task.stop() |
| 84 | +# print("done") |
36 | 85 |
|
37 | 86 |
|
38 | | -def test_periodic_send_with_modifying_data(): |
39 | | - print("Starting to send a message every 200ms. Initial data is ones") |
40 | | - msg = can.Message(arbitration_id=0x0cf02200, data=[1, 1, 1, 1]) |
41 | | - task = can.send_periodic('vcan0', msg, 0.20) |
42 | | - time.sleep(2) |
43 | | - print("Changing data of running task to begin with 99") |
44 | | - msg.data[0] = 0x99 |
45 | | - task.modify_data(msg) |
46 | | - time.sleep(2) |
47 | | - |
48 | | - task.stop() |
49 | | - print("stopped cyclic send") |
50 | | - print("Changing data of stopped task to single ff byte") |
51 | | - msg.data = bytearray([0xff]) |
52 | | - task.modify_data(msg) |
53 | | - time.sleep(1) |
54 | | - print("starting again") |
55 | | - task.start() |
56 | | - time.sleep(1) |
57 | | - task.stop() |
58 | | - print("done") |
59 | | - |
| 87 | +if __name__ == "__main__": |
60 | 88 |
|
61 | | -def test_dual_rate_periodic_send(): |
62 | | - """Send a message 10 times at 1ms intervals, then continue to send every 500ms""" |
63 | | - msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5]) |
64 | | - print("Creating cyclic task to send message 10 times at 1ms, then every 500ms") |
65 | | - task = can.interface.MultiRateCyclicSendTask('vcan0', msg, 10, 0.001, 0.50) |
66 | | - time.sleep(2) |
| 89 | + reset_msg = can.Message(arbitration_id=0x00, data=[0, 0, 0, 0, 0, 0], extended_id=False) |
67 | 90 |
|
68 | | - print("Changing data[0] = 0x42") |
69 | | - msg.data[0] = 0x42 |
70 | | - task.modify_data(msg) |
71 | | - time.sleep(2) |
72 | 91 |
|
73 | | - task.stop() |
74 | | - print("stopped cyclic send") |
75 | 92 |
|
76 | | - time.sleep(2) |
| 93 | + for interface in { |
| 94 | + 'socketcan_ctypes', |
| 95 | + #'socketcan_native' |
| 96 | + }: |
| 97 | + print("Carrying out cyclic tests with {} interface".format(interface)) |
| 98 | + can.rc['interface'] = interface |
77 | 99 |
|
78 | | - task.start() |
79 | | - print("starting again") |
80 | | - time.sleep(2) |
81 | | - task.stop() |
82 | | - print("done") |
| 100 | + channel = 'vcan0' |
| 101 | + bus = can.interface.Bus(channel=channel) |
| 102 | + bus.send(reset_msg) |
83 | 103 |
|
| 104 | + simple_periodic_send(bus) |
84 | 105 |
|
85 | | -if __name__ == "__main__": |
| 106 | + bus.send(reset_msg) |
86 | 107 |
|
87 | | - for interface in {'socketcan_ctypes', 'socketcan_native'}: |
88 | | - print("Carrying out cyclic tests with {} interface".format(interface)) |
89 | | - can.rc['interface'] = interface |
| 108 | + limited_periodic_send(bus) |
90 | 109 |
|
91 | | - test_simple_periodic_send() |
| 110 | + #test_periodic_send_with_modifying_data() |
92 | 111 |
|
93 | | - test_extended_periodic_send() |
| 112 | + #print("Carrying out multirate cyclic test for {} interface".format(interface)) |
| 113 | + #can.rc['interface'] = interface |
| 114 | + #test_dual_rate_periodic_send() |
94 | 115 |
|
95 | | - test_periodic_send_with_modifying_data() |
96 | 116 |
|
97 | | - print("Carrying out multirate cyclic test for {} interface".format(interface)) |
98 | | - can.rc['interface'] = interface |
99 | | - test_dual_rate_periodic_send() |
| 117 | + time.sleep(2) |
0 commit comments