# SocketCAN (Linux) SocketCAN lets a Linux PC, Jetson, or Raspberry Pi control CAN and CAN FD devices directly. After the board is connected through USB, Linux exposes the four CAN/CAN FD channels as four `canX` network interfaces. You can use `can-utils`, `python-can`, or other SocketCAN software. SocketCAN does not use SANPO `ST`, `ET`, `SF`, or `EF` headers. RS485 is not accessed through SocketCAN. ## Connect and Identify the Board 1. Connect the board to the Linux host through USB. 2. Connect and power the motor on a board CAN interface. 3. Install tools and load the driver: ```bash sudo apt update sudo apt install can-utils sudo modprobe gs_usb ``` List CAN interfaces: ```bash ip link show type can ``` A complete board normally exposes four `canX` interfaces. Linux assigns numbers by enumeration order, so `can0` to `can3` do not identify physical board connectors by themselves. For first use, connect one motor and send a query on each interface to identify the mapping. You can also inspect the USB path: ```bash readlink -f /sys/class/net/can0/device udevadm info -q property -p "$(readlink -f /sys/class/net/can0/device)" ``` For long-term use, create `udev` rules based on the USB physical port or device serial number to assign stable interface names. ## Classic CAN Configure `can0` for 1 Mbps: ```bash sudo ip link set can0 down sudo ip link set can0 type can bitrate 1000000 sudo ip link set can0 up ``` Monitor the bus: ```bash candump can0 ``` Send a standard frame: ```bash cansend can0 123#11223344 ``` Send an extended frame: ```bash cansend can0 0000FD01#0102030405060708 ``` `cansend` normally prints nothing on success. A reply depends on the motor protocol, ID, and current state. ## CAN FD Configure `can0` for 1 Mbps arbitration, 5 Mbps data, and CAN FD: ```bash sudo ip link set can0 down sudo ip link set can0 type can bitrate 1000000 dbitrate 5000000 fd on sudo ip link set can0 up ``` Send a CAN FD standard frame with BRS: ```bash cansend can0 123##11122334455667788 ``` Send a CAN FD extended frame with BRS: ```bash cansend can0 0000FD01##11122334455667788 ``` After `##`, the first hexadecimal digit contains CAN FD flags. `1` enables BRS; the remaining digits are data. ## Check Interface State ```bash ip -s -details link show can0 ``` With CAN FD enabled, the output should show `fd on`, `bitrate`, and `dbitrate`. If error counters continue increasing, stop transmission and check bitrate, termination, wiring, and device power. SocketCAN settings are not saved to the board. Reapply `ip link` settings after a power cycle, USB reconnect, or re-enumeration. ## CyberGear Example Example: [SocketCAN CyberGear](https://gitcode.com/sanpo/robot/blob/unified-main/products/spine/v6/demo/socketcan/socketcan_cybergear_demo_v6.py) Control motor ID 1 on `can0`: ```bash sudo python3 socketcan_cybergear_demo_v6.py \ --setup-links \ --motor-map "can0:1" ``` Multiple interfaces: ```text --motor-map "can0:1,2,3;can1:4,5,6" ``` Update interface names and motor IDs to match the actual enumeration and wiring. ## Troubleshooting | Symptom | Action | | --- | --- | | No `canX` interface | Confirm both MCU devices have V6 firmware, run `sudo modprobe gs_usb`, and reconnect USB | | `Network is down` | Run `sudo ip link set can0 up` for the selected interface | | `Device or resource busy` | Set the interface down before configuring it again | | `No buffer space available` | Check wiring, bitrate, and termination; after fixing the bus, set the interface down and up again | | Frames transmit but the motor does not reply | Check `canX` mapping, motor ID, protocol, power, and bus settings |