设置波特率:
echo 125000 > /sys/class/net/can0/can_bittiming/bitrate
启动can接口:
ifconfig can0 up
查看统计:
cat /proc/net/can/stats
查看can设备的中断统计:
cat /proc/interrupts
另附一段简单的测试代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <string.h>
/* At time of writing, these constants are not defined in the headers */
#ifndef PF_CAN
#define PF_CAN 29
#endif
#ifndef AF_CAN
#define AF_CAN PF_CAN
#endif
/* ... */
/* Somewhere in your app */
int main()
{
/* Create the socket */
int skt = socket( PF_CAN, SOCK_RAW, CAN_RAW );
/* Locate the interface you wish to use */
struct ifreq ifr;
strcpy(ifr.ifr_name, "can0");
ioctl(skt, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled
* with that device's index */
/* Select that CAN interface, and bind the socket to it. */
struct sockaddr_can addr;
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind( skt, (struct sockaddr*)&addr, sizeof(addr) );
/* Send a message to the CAN bus */
struct can_frame frame;
frame.can_id = 0x123;
strcpy( frame.data, "foo" );
frame.can_dlc = strlen( frame.data );
int bytes_sent = write( skt, &frame, sizeof(frame) );
printf("CAN send %d bytes.\n", bytes_sent);
/* Read a message back from the CAN bus */
// int bytes_read = read( skt, &frame, sizeof(frame) );
// printf("CAN recv %d bytes.\n", bytes_read);
return 0;
}
本文介绍了几个用于调试SocketCAN的命令,包括设置波特率、启动CAN接口、查看统计信息和中断统计,帮助进行CAN网络的测试和故障排查。

487

被折叠的 条评论
为什么被折叠?



