Skip to content

Commit ad8b948

Browse files
authored
socketcan: Make find_available_interfaces() find slcanX interfaces (hardbyte#1369)
* socketcan: Make find_available_interfaces() find slcanX interfaces * doc: socketcan: Fix external link to can-utils on github * socketcan: Extend name interface pattern to match vxcan\d+
1 parent 9d9cb16 commit ad8b948

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

can/interfaces/socketcan/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def pack_filters(can_filters: Optional[typechecking.CanFilters] = None) -> bytes
3838
return struct.pack(can_filter_fmt, *filter_data)
3939

4040

41-
_PATTERN_CAN_INTERFACE = re.compile(r"v?can\d+")
41+
_PATTERN_CAN_INTERFACE = re.compile(r"(sl|v|vx)?can\d+")
4242

4343

4444
def find_available_interfaces() -> Iterable[str]:

doc/interfaces/socketcan.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ existing ``can0`` interface with a bitrate of 1MB:
5757
5858
sudo ip link set can0 up type can bitrate 1000000
5959
60+
CAN over Serial / SLCAN
61+
~~~~~~~~~~~~~~~~~~~~~~~
62+
63+
SLCAN adapters can be used directly via :doc:`/interfaces/slcan`, or
64+
via :doc:`/interfaces/socketcan` with some help from the ``slcand`` utility
65+
which can be found in the `can-utils <https://github.com/linux-can/can-utils>`_ package.
66+
67+
To create a socketcan interface for an SLCAN adapter run the following:
68+
69+
.. code-block:: bash
70+
71+
slcand -f -o -c -s5 /dev/ttyAMA0
72+
ip link set up slcan0
73+
74+
Names of the interfaces created by ``slcand`` match the ``slcan\d+`` regex.
75+
If a custom name is required, it can be specified as the last argument. E.g.:
76+
77+
.. code-block:: bash
78+
79+
slcand -f -o -c -s5 /dev/ttyAMA0 can0
80+
ip link set up can0
81+
6082
.. _socketcan-pcan:
6183

6284
PCAN

test/open_vcan.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
modprobe vcan
66
ip link add dev vcan0 type vcan
77
ip link set up vcan0 mtu 72
8+
ip link add dev vxcan0 type vcan
9+
ip link set up vxcan0 mtu 72
10+
ip link add dev slcan0 type vcan
11+
ip link set up slcan0 mtu 72

test/test_socketcan_helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ def test_find_available_interfaces(self):
3131
result = list(find_available_interfaces())
3232
self.assertGreaterEqual(len(result), 0)
3333
for entry in result:
34-
self.assertRegex(entry, r"v?can\d+")
34+
self.assertRegex(entry, r"(sl|v|vx)?can\d+")
3535
if TEST_INTERFACE_SOCKETCAN:
36-
self.assertGreaterEqual(len(result), 1)
36+
self.assertGreaterEqual(len(result), 3)
3737
self.assertIn("vcan0", result)
38+
self.assertIn("vxcan0", result)
39+
self.assertIn("slcan0", result)
3840

3941

4042
if __name__ == "__main__":

0 commit comments

Comments
 (0)