SocketCAN

SocketCAN lets a Linux PC, Jetson, or Raspberry Pi control CAN devices directly. After the board is connected through USB, Linux exposes the four CAN channels as can0, can1, can2, and can3. You can use can-utils, python-can, CANgaroo, or other SocketCAN software.

Use firmware V4.6 or later. SocketCAN only accesses CAN devices. For RS485, see RS485 Raw Passthrough.

Connect and Identify the Board

  1. Connect the board to the Linux host through USB.

  2. Connect the motor to a CAN interface and supply motor power through the XT60 input.

  3. Install CAN tools and load the driver:

sudo apt update
sudo apt install can-utils
sudo modprobe gs_usb

List CAN interfaces:

ip link show type can

A complete board normally exposes four canX interfaces. Linux numbering depends on USB enumeration and may not follow the board silkscreen. Confirm the physical mapping before controlling a motor.

Configure a CAN Interface

Configure can0 for 1 Mbps:

sudo ip link set can0 down
sudo ip link set can0 type can bitrate 1000000
sudo ip link set can0 up

The bitrate must match the motor. This setting only applies to the current Linux interface and must be repeated after a power cycle, USB reconnect, or host restart.

Show the current state and bitrate:

ip -details link show can0

Configure all four interfaces for 1 Mbps:

for iface in can0 can1 can2 can3; do
  sudo ip link set "$iface" down
  sudo ip link set "$iface" type can bitrate 1000000
  sudo ip link set "$iface" up
done

Send and Receive CAN Frames

Monitor can0 in one terminal:

candump can0

Send a standard frame from another terminal:

cansend can0 123#11223344

Send an extended frame:

cansend can0 0000FD01#0102030405060708

cansend normally prints nothing on success. A reply depends on the device protocol, ID, and state; not every test frame produces a response.

View traffic and error counters:

ip -s -details link show can0

CyberGear Example

Download the CyberGear SocketCAN example, then control motor ID 1 on can0:

sudo python3 socketcan_cybergear_demo_v6.py \
  --setup-links \
  --motor-map "can0:1"

For multiple interfaces and motors:

--motor-map "can0:1,2,3;can1:4,5,6"

For example, can0:1,2,3 means that can0 is connected to motor IDs 1, 2, and 3. Update the interface names and motor IDs to match the actual wiring. --setup-links enables the listed interfaces at the default 1 Mbps bitrate.

Troubleshooting

Symptom

Action

No canX interface

Confirm firmware V4.6 or later, run sudo modprobe gs_usb, and reconnect USB

RTNETLINK answers: Device or resource busy

Run sudo ip link set can0 down before changing the bitrate

Network is down

Run sudo ip link set can0 up

Frames transmit but the motor does not reply

Check the canX mapping, motor ID, bitrate, wiring, termination, and motor power

No buffer space available or rising error counters

Check wiring, bitrate, and termination; after fixing the bus, set the interface down and up again