Skip to content

Commit b446ead

Browse files
committed
Update socketcan ctypes and cyclic example to use new api
1 parent f119490 commit b446ead

2 files changed

Lines changed: 36 additions & 31 deletions

File tree

can/interfaces/socketcan/socketcan_ctypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ctypes.util import find_library
99

1010
import can
11-
from can.broadcastmanager import CyclicSendTaskABC
11+
from can.broadcastmanager import CyclicSendTaskABC, RestartableCyclicTaskABC, ModifiableCyclicTaskABC
1212
from can.bus import BusABC
1313
from can.message import Message
1414
from can.interfaces.socketcan.socketcan_constants import * # CAN_RAW
@@ -413,7 +413,7 @@ def __init__(self, channel, *args, **kwargs):
413413
super(SocketCanCtypesBCMBase, self).__init__(*args, **kwargs)
414414

415415

416-
class CyclicSendTask(SocketCanCtypesBCMBase, CyclicSendTaskABC):
416+
class CyclicSendTask(SocketCanCtypesBCMBase, RestartableCyclicTaskABC, ModifiableCyclicTaskABC):
417417

418418
def __init__(self, channel, message, period):
419419
"""

examples/cyclic.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def simple_periodic_send(bus):
2323
print("Starting to send a message every 200ms for 2s")
2424
msg = can.Message(arbitration_id=0x123, data=[1, 2, 3, 4, 5, 6], extended_id=False)
2525
task = bus.send_periodic(msg, 0.20)
26+
assert isinstance(task, can.CyclicSendTaskABC)
2627
time.sleep(2)
2728
task.stop()
2829
print("stopped cyclic send")
@@ -31,35 +32,39 @@ def simple_periodic_send(bus):
3132
def limited_periodic_send(bus):
3233
print("Starting to send a message every 200ms for 1s")
3334
msg = can.Message(arbitration_id=0x12345678, data=[0, 0, 0, 0, 0, 0], extended_id=True)
34-
bus.send_periodic(msg, 0.20, 1)
35-
time.sleep(3)
36-
# task.stop()
37-
# print("stopped cyclic send")
35+
task = bus.send_periodic(msg, 0.20, 1)
36+
if not isinstance(task, can.LimitedDurationCyclicSendTaskABC):
37+
print("This interface doesn't seem to support a ")
38+
task.stop()
3839

40+
print("stopped cyclic send")
3941

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-
#
42+
43+
def test_periodic_send_with_modifying_data():
44+
print("Starting to send a message every 200ms. Initial data is ones")
45+
msg = can.Message(arbitration_id=0x0cf02200, data=[1, 1, 1, 1])
46+
task = can.send_periodic('vcan0', msg, 0.20)
47+
time.sleep(2)
48+
print("Changing data of running task to begin with 99")
49+
msg.data[0] = 0x99
50+
task.modify_data(msg)
51+
time.sleep(2)
52+
53+
task.stop()
54+
print("stopped cyclic send")
55+
print("Changing data of stopped task to single ff byte")
56+
msg.data = bytearray([0xff])
57+
task.modify_data(msg)
58+
time.sleep(1)
59+
print("starting again")
60+
task.start()
61+
time.sleep(1)
62+
task.stop()
63+
print("done")
64+
65+
66+
# Will have to consider how to expose items like this. The socketcan
67+
# interfaces will continue to support it... but the top level api won't.
6368
# def test_dual_rate_periodic_send():
6469
# """Send a message 10 times at 1ms intervals, then continue to send every 500ms"""
6570
# msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5])
@@ -92,7 +97,7 @@ def limited_periodic_send(bus):
9297

9398
for interface in {
9499
'socketcan_ctypes',
95-
#'socketcan_native'
100+
'socketcan_native'
96101
}:
97102
print("Carrying out cyclic tests with {} interface".format(interface))
98103
can.rc['interface'] = interface
@@ -107,7 +112,7 @@ def limited_periodic_send(bus):
107112

108113
limited_periodic_send(bus)
109114

110-
#test_periodic_send_with_modifying_data()
115+
test_periodic_send_with_modifying_data()
111116

112117
#print("Carrying out multirate cyclic test for {} interface".format(interface))
113118
#can.rc['interface'] = interface

0 commit comments

Comments
 (0)