Skip to content

Commit 6b53ef4

Browse files
committed
Implement CanProtocol for SocketcanBus
1 parent 6b59513 commit 6b53ef4

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

can/interfaces/socketcan/socketcan.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
import can
33-
from can import BusABC, Message
33+
from can import BusABC, CanProtocol, Message
3434
from can.broadcastmanager import (
3535
LimitedDurationCyclicSendTaskABC,
3636
ModifiableCyclicTaskABC,
@@ -710,7 +710,12 @@ def __init__(
710710
"local_loopback": local_loopback,
711711
}
712712
)
713-
super().__init__(channel=channel, can_filters=can_filters, **kwargs)
713+
super().__init__(
714+
channel=channel,
715+
can_filters=can_filters,
716+
protocol=CanProtocol.CAN_FD if fd else CanProtocol.CAN_20,
717+
**kwargs,
718+
)
714719

715720
def shutdown(self) -> None:
716721
"""Stops all active periodic tasks and closes the socket."""

test/test_socketcan.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import warnings
1010
from unittest.mock import patch
1111

12+
from .config import TEST_INTERFACE_SOCKETCAN
13+
1214
import can
1315
from can.interfaces.socketcan.constants import (
1416
CAN_BCM_TX_DELETE,
@@ -357,6 +359,16 @@ def test_build_bcm_update_header(self):
357359
self.assertEqual(can_id, result.can_id)
358360
self.assertEqual(1, result.nframes)
359361

362+
@unittest.skipUnless(TEST_INTERFACE_SOCKETCAN, "Only run when vcan0 is available")
363+
def test_bus_creation_can(self):
364+
bus = can.Bus(interface="socketcan", channel="vcan0", fd=False)
365+
self.assertEqual(bus.protocol, can.CanProtocol.CAN_20)
366+
367+
@unittest.skipUnless(TEST_INTERFACE_SOCKETCAN, "Only run when vcan0 is available")
368+
def test_bus_creation_can_fd(self):
369+
bus = can.Bus(interface="socketcan", channel="vcan0", fd=True)
370+
self.assertEqual(bus.protocol, can.CanProtocol.CAN_FD)
371+
360372
@unittest.skipUnless(IS_LINUX and IS_PYPY, "Only test when run on Linux with PyPy")
361373
def test_pypy_socketcan_support(self):
362374
"""Wait for PyPy raw CAN socket support

0 commit comments

Comments
 (0)