Skip to main content

Basic SocketCAN Operations

This chapter explains the basic Linux SocketCAN commands used after starting the MicroPeckerX SocketCAN driver.
Because MicroPeckerX is handled as a CAN network interface on Linux, can-utils commands such as candump, cansend, and cangen can be used.

note

This chapter uses can0 / can1 for explanation. The actual interface names may differ depending on the environment. Check them with ip link or ./mpdrv_run status.

Checking the Interface Status​

Check the list of CAN interfaces.

ip link

To check detailed settings, run the following commands.

ip -details link show can0
ip -details link show can1

If you are using mpdrv_run, you can also check with the following command.

./mpdrv_run status

Check items such as bitrate, dbitrate, fd on/off, and state ERROR-ACTIVE.

Receiving (candump)​

Use candump to receive CAN frames.

candump can0

To receive with can1:

candump can1

To monitor multiple interfaces at the same time:

candump can0 can1

To exit, press Ctrl + C.

Receiving with timestamps​

To display the reception time, specify -ta.

candump -ta can0

To check the hardware timestamp, specify enable_hwts=1 when loading the driver and run the command with -H.

candump -H -ta can0
note

This driver supports reading receive hardware timestamps. TX hardware timestamps and hardware timestamp setting changes are not supported.

Sending Classical CAN (cansend)​

Use cansend to send a Classical CAN frame once.

cansend can0 123#1122334455667788
ElementMeaning
can0CAN interface to send from
123CAN ID. Specify it in hexadecimal.
#Separator for Classical CAN frames
1122334455667788Transmit data. Two digits are 1 byte, up to 8 bytes.

The data length can also be shortened.

cansend can0 123#11223344

Example of sending a frame with no data:

cansend can0 123#

To send an Extended ID, specify the CAN ID within the extended ID range.

cansend can0 12345678#11223344

Sending CAN FD (cansend)​

To send CAN FD frames, start the interface with CAN FD settings in advance.

MPX_CANFD=1 MPX_BITRATE=500000 MPX_DBITRATE=2000000 ./mpdrv_run start-all

CAN FD frames are specified using ##.

cansend can0 123##1112233445566778899AABBCCDDEEFF

In CAN FD, the first character immediately after ## is the CAN FD flags. The transmit data starts from the next character.

ElementMeaning
can0CAN interface to send from
123CAN ID. Specify it in hexadecimal.
##Separator for CAN FD frames
1CAN FD flags. 0 means without BRS, and 1 means with BRS.
112233...Transmit data. Two digits are 1 byte, up to 64 bytes.

In other words, in the following example, the 0 immediately after ## is the flags, and the transmit data is 1122334455667788.

cansend can0 123##01122334455667788

In the following example, the 1 immediately after ## is the flags, and the transmit data is 1122334455667788.

cansend can0 123##11122334455667788
Command exampleBRSTransmit data
cansend can0 123##01122334455667788No1122334455667788
cansend can0 123##11122334455667788Yes1122334455667788
note

When using CAN FD, start both the transmit side and the receive side with CAN FD settings. CAN FD frames cannot be sent or received while using Classical CAN settings.

Periodic Transmission (cangen)​

Use cangen to periodically send CAN frames.

Example of sending 8-byte data with ID 123 at a 100 ms period using Classical CAN:

cangen can0 -I 123 -L 8 -D 1122334455667788 -g 100
OptionMeaning
-I 123Fixes the CAN ID.
-L 8Fixes the data length to 8 bytes.
-D 1122334455667788Fixes the transmit data.
-g 100Sets the transmission interval to 100 ms.

Example of periodic transmission with CAN FD:

cangen can0 -f -b -I 123 -L 64 -g 100

Because the above command does not fix the data content, the transmit data is generated by cangen. If you also want to fix the transmit data, specify the data with -D.

cangen can0 -f -b -I 123 -L 64 -D 00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF -g 100
OptionMeaning
-fSends CAN FD frames.
-bEnables CAN FD BRS.
-I 123Fixes the CAN ID.
-L 64Fixes the data length to 64 bytes.
-D ...Fixes the transmit data. Two digits are 1 byte, and the example above specifies 64 bytes.
-g 100Sets the transmission interval to 100 ms.

To stop transmission, press Ctrl + C.

caution

If the period is too short, the load on the PC, USB, and CAN bus increases. Start by checking with a period of about 100 ms, and adjust as necessary.

Frequently Used Commands​

PurposeCommand example
Check interface listip link
Check detailed CAN settingsip -details link show can0
Check driver status./mpdrv_run status
Receive with can0candump can0
Receive with timestampcandump -ta can0
Receive with hardware timestampcandump -H -ta can0
Send Classical CANcansend can0 123#1122334455667788
Send CAN FDcansend can0 123##11122334455667788
Periodic Classical CAN transmissioncangen can0 -I 123 -L 8 -D 1122334455667788 -g 100
Periodic CAN FD transmissioncangen can0 -f -b -I 123 -L 64 -D 00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF -g 100