?? receive.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 + -