can_read.c

来自「ATEML AT91SAM9260测试程序。包括 can测试」· C语言 代码 · 共 87 行

C
87
字号
/* * can_test.c * * Copyright (C) 2007 Mengrz */#include <sys/types.h>#include <sys/unistd.h>#include <sys/stat.h>#include <linux/fcntl.h>#include <linux/ioctl.h>#include "can.h"void can_show_header(struct can_frame_header *header){    printf("Header: id=%d srr=%d ide=%d eid=%d rtr=%d rb1=%d rb0=%d dlc=%d\n",	   header->id,	   header->srr,	   header->ide,	   header->eid,	   header->rtr,	   header->rb1,	   header->rb0,	   header->dlc);}int main(int argc, char *argv[]){    if (argc < 2) {	printf("Useage: %s dev\n", argv[0]);	exit(0);    }        struct can_frame frame_recv;        struct can_filter filter = {	.fid = {	    {3,0,0,1},	    {4,1,9,1},	    {5,0,0,1},	    {6,1,13,1},	    {7,0,0,1},	    {8,1,17,1},	},	.sidmask = 0x0f,	.eidmask = 0x00,	.mode = 1,    };        int device = open(argv[1], O_RDWR);    if (device == -1){	perror("open:device");	exit(-1);    }        int i;    int rate, ret;    ioctl(device, CAN_IOCSRATE, 250000);    ioctl(device, CAN_IOCGRATE, &rate);    ioctl(device, CAN_IOCSFILTER, &filter);        printf("%s Work Rate: %dbps\n", argv[1], rate);        while(1)    {		memset(&frame_recv, 0, sizeof(struct can_frame));		ret = read(device, &frame_recv, sizeof(struct can_frame));	if (ret < 0)	    perror("read");		can_show_header(&frame_recv.header);	printf("%s RECV: ", argv[1]);	for (i=0; i<frame_recv.header.dlc; i++)	    printf("%c", frame_recv.data[i]);	printf("\n\n");	    }        close(device);        return 0;    }

⌨️ 快捷键说明

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