Skip to main content

Application Development Tips

When using the MicroPeckerX SocketCAN driver, MicroPeckerX can be handled as a CAN network interface on Linux.
Applications send and receive CAN / CAN FD frames using the Linux SocketCAN API instead of a dedicated API.

Check Procedure Before Creating an Application​

Before creating an application, first check that sending and receiving work with can-utils.

  1. Start the driver.
cd 01_drv
./mpdrv_run load
./mpdrv_run start-all
  1. Check reception.
candump can0
  1. Send from another terminal.
cansend can0 123#1122334455667788

If this check succeeds, the application can also send and receive by specifying the same can0 / can1.

Example of Sending Classical CAN in C​

The following is a minimal example that sends one Classical CAN frame from can0.

#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>

int main(void) {
int s;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;

s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (s < 0) {
perror("socket");
return 1;
}

strcpy(ifr.ifr_name, "can0");
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
perror("ioctl");
close(s);
return 1;
}

memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
close(s);
return 1;
}

frame.can_id = 0x123;
frame.can_dlc = 8;
frame.data[0] = 0x11;
frame.data[1] = 0x22;
frame.data[2] = 0x33;
frame.data[3] = 0x44;
frame.data[4] = 0x55;
frame.data[5] = 0x66;
frame.data[6] = 0x77;
frame.data[7] = 0x88;

if (write(s, &frame, sizeof(frame)) != sizeof(frame)) {
perror("write");
close(s);
return 1;
}

close(s);
return 0;
}

Build example:

gcc -o can_send can_send.c

Execution example:

./can_send

Example of Receiving Classical CAN in C​

The following is a minimal example that receives one Classical CAN frame with can0.

#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>

int main(void) {
int s;
int nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;

s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (s < 0) {
perror("socket");
return 1;
}

strcpy(ifr.ifr_name, "can0");
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
perror("ioctl");
close(s);
return 1;
}

memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
close(s);
return 1;
}

nbytes = read(s, &frame, sizeof(frame));
if (nbytes < 0) {
perror("read");
close(s);
return 1;
}

printf("ID=0x%03X DLC=%d DATA=", frame.can_id & CAN_EFF_MASK, frame.can_dlc);
for (int i = 0; i < frame.can_dlc; i++) {
printf("%02X ", frame.data[i]);
}
printf("\n");

close(s);
return 0;
}

When Creating a CAN FD Application​

When handling CAN FD, start the interface with CAN FD settings in advance.

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

In applications, the main differences from Classical CAN are as follows.

Classical CANCAN FD
struct can_framestruct canfd_frame
Up to 8 bytesUp to 64 bytes
Send and receive with sizeof(struct can_frame)Send and receive with sizeof(struct canfd_frame)
No CAN FD flagsFlags such as CANFD_BRS can be used

Example frame setting for sending a CAN FD frame:

struct canfd_frame frame;

memset(&frame, 0, sizeof(frame));
frame.can_id = 0x123;
frame.len = 64;
frame.flags = CANFD_BRS;

for (int i = 0; i < frame.len; i++) {
frame.data[i] = i;
}

write(s, &frame, sizeof(frame));

Using from Python​

In Python, you can use SocketCAN-compatible libraries such as python-can.
Follow the documentation for the library you use for installation and detailed APIs.

Image of transmit processing:

import can

bus = can.interface.Bus(channel="can0", interface="socketcan")
msg = can.Message(arbitration_id=0x123, data=[0x11, 0x22, 0x33, 0x44], is_extended_id=False)
bus.send(msg)

When using CAN FD, start the interface with CAN FD settings and then use CAN FD-compatible message settings.

Notes When Creating Applications​

  • Match the interface name used by the application to the name in the actual environment.
  • Before sending and receiving, check the bitrate and CAN FD settings with ip -details link show can0.
  • When using CAN FD, match the bitrate / dbitrate settings on the transmit side and receive side.
  • When performing high-cycle or high-load transmission, first check with a low transmission frequency.
  • To use receive hardware timestamps, specify enable_hwts=1 when loading the driver.