记几个调试SocketCAN的命令

设置波特率:

echo 125000 > /sys/class/net/can0/can_bittiming/bitrate


启动can接口:

ifconfig can0 up


查看统计:

cat /proc/net/can/stats


查看can设备的中断统计:

cat /proc/interrupts


另附一段简单的测试代码:

#include 
#include 
#include 
#include 
#include 
 
#include 
#include 
#include 
 
/* 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;
}
 
  

你可能感兴趣的:(工作流水账)