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

📄 receive.c

📁 linux下PCI_CAN 的驱动程序
💻 C
字号:
#include "adv_can.h"	/* Library function declaration	*/#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <sys/ioctl.h>#include <string.h>/*-------------------------------------------------------------------------- * parameter: * 	1:port "0" for port 0, "1" for port 1 * 	2:protocol "a" for 2.0A, "b" for 2.0B  --------------------------------------------------------------------------*//* define can controller interrupt connect to PC irq number */int main(int argv, char **arg){  int fd=0;  /* declare the can card segment address */  CAN_STRUCT can;  can_msg_t msg;  char name[20];  int	i,j;  memset(name, 0 , 20);  if(argv != 3) {    printf("argument error!\n");    return 0;  }  sprintf(name, "/dev/can%c", *arg[1]);  if((fd = open(name, O_RDWR))<=0)  {	printf("cannot open %s\n", name);        return -1;  }  /*---- reset can controller	*/  ioctl(fd, IOCTL_RESET_CAN, NULL);    if(*arg[2] =='a') can.protocol=CAN_PROTOCOL_20A;  else if(*arg[2] =='b') can.protocol=CAN_PROTOCOL_20B;  else {printf("arg 2 error\n"); return -1;}  can.accode[0]=0x0;  can.accode[1]=48;        can.accode[2]=0x0;  can.accode[3]=80;  can.accmask[0]=0xff;      can.accmask[1]=0xff;      can.accmask[2]=0xff;     can.accmask[3]=0xff;      can.speed= CAN_125K;  can.interruptmask=0xff;  can.filtertype = 1; //0 for single filter, 1 for double filter   if(ioctl(fd,IOCTL_SET_CAN,&can) <= 0) {    printf("\n\n can controller port configure is error\n");    close(fd);    return 0;  }  j=0; printf("port open successful, begin to receive data:\n");  while(1) {    if(read(fd, &msg, CAN_MSG_LEN)) {     if(can.protocol == CAN_PROTOCOL_20A)     printf("Recieve:ID=%d rtr=%1d Length=%1d ",	     msg.id, msg.rtr, msg.dlen);     else     printf("Recieve:ID=%d ff=%1d rtr=%1d Length=%1d ",	     msg.id, msg.ff, msg.rtr, msg.dlen);      for(i=0 ; i< 8 ; i++)	printf(" %2X",msg.data[i]);        printf("  %d\n", j);	      j++;    }    if(j==100)	break;    /*else {      j++;      if(j%1000 == 0)	printf("%d\n", j);    }*/  }    printf("total %d\n", j);  printf("receive data successful!\n");  close(fd);  return 1;} 

⌨️ 快捷键说明

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