⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ioctltest.c

📁 盛博can驱动程序功能很全
💻 C
字号:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "sbscan.h"

int main(void)
{
 int fd;

 filter_struct *filter; 
 enum bau baud;
 int status;
 int err_count,arb_count;
 err_cap_struct *err_capture;
 arb_cap_struct *arb_capture;
 enum fil afm;
 enum sle sm;
 enum lis lom;
 enum tes stm;
 int trig=3;
 int mode;

/*****************************************************
 *          open device file 
 *****************************************************/
 
 fd=open("/dev/can0",O_RDWR);
 
 if (fd<0)  printf("open failed\n");

/*****************************************************
 *        reset the can device
 *****************************************************/

// ioctl(fd,CAN_RESET);

 /****************************************************
  *           set singal/double filter
  ****************************************************/
 
 afm=DOUBLE;
 ioctl(fd,CAN_SET_AFM,&afm);  //double

 afm=SINGLE;
 ioctl(fd,CAN_SET_AFM,&afm);  //singal


 /****************************************************
  *           set listen/normal mode
  ****************************************************/
 
 lom=LON;
 ioctl(fd,CAN_SET_LOM,&lom);  //listen only
 lom=LOFF;
 ioctl(fd,CAN_SET_LOM,&lom);  //normal

 
/****************************************************
  *           set selftest/normal mode
  ****************************************************/
 
 stm=TON;
 ioctl(fd,CAN_SET_STM,&stm);  //selftest mode
 stm=TOFF;
 ioctl(fd,CAN_SET_STM,&stm);  //normal


/****************************************************
  *           set sleep/normal
  ****************************************************/
 
 sm=SON;
 ioctl(fd,CAN_SET_SM,&sm);  //sleep mode
 sm=SOFF;
 ioctl(fd,CAN_SET_SM,&sm);  //normal


 /***************************************************
  *             set filter
  ***************************************************/
 
 filter=(filter_struct *)malloc(sizeof(filter_struct));
 memset(filter,0,sizeof(filter_struct));
 
 filter->code1=11;
 filter->code2=22;
 filter->code3=3;
 filter->code4=4;

 filter->mask1=0;
 filter->mask2=0;
 filter->mask3=0;
 filter->mask4=0xff;


 ioctl(fd,CAN_SET_FILTER,filter);
 
 memset(filter,0,sizeof(filter_struct));
 
/****************************************************
 *                get filter
 ****************************************************/
 
 ioctl(fd,CAN_GET_FILTER,filter);

 //printf("filter->code1=%d\n",filter->code1);

 /***************************************************
  *              set  baud rate 
  ***************************************************/

 baud=B1000;

 ioctl(fd,CAN_SET_BAUDRATE,&baud);

 /***************************************************
  *              get baud rate
  ***************************************************/
 
  ioctl(fd,CAN_GET_BAUDRATE,&baud);


 /***************************************************
  *     Get the CAN device status
  ***************************************************/

 ioctl(fd,CAN_GET_STATUS,&status);

/****************************************************
 *      get the error counter,
 *      and the error capture 
 ****************************************************/

 ioctl(fd,CAN_GET_ERRCOUNT,&err_count);

 err_capture=(err_cap_struct *)malloc(sizeof(err_cap_struct));
 err_capture->pos=err_count;

 ioctl(fd,CAN_GET_ERRCAPTURE,err_capture);

 //printf("the current error is 0x%x\n",err_capture->code);

/****************************************************
 *      get the arbitration counter,
 *      and the arbitration lost capture
 ****************************************************/

 ioctl(fd,CAN_GET_ARBCOUNT,&arb_count);

 arb_capture=(arb_cap_struct *)malloc(sizeof(arb_cap_struct));
 arb_capture->pos=arb_count;

 ioctl(fd,CAN_GET_ARBCAPTURE,arb_capture);

 printf("the current arbitration is 0x%x\n",arb_capture->code);

 
 /*****************************************************
  * set receive interrupt triggerlevel                *
  *****************************************************/

 ioctl(fd,CAN_SET_TRIGLEVEL,&trig);

 /********************************************************
  *                 get work mode 
  * bit4=1: sleep    mode
  * bit3=1: single   filter
  * bit2=1: selftest mode
  * bit1=1: listen   only
  * bit0=0: reset    mode
  *********************************************************/

 ioctl(fd,CAN_GET_MODE,&mode);

 printf("mode=0x%x\n",mode);

/***********************************************************
 *             close device file
 ***********************************************************/

 if (close(fd)) 
   printf("close failed\n"); 

return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -