can_write.c

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

C
73
字号
/* * 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"int main(int argc, char *argv[]){    if (argc < 2) {	printf("Useage: %s dev\n", argv[0]);	exit(0);    }        struct can_frame frame;        int device = open(argv[1], O_RDWR);    if (device == -1){	perror("open:device");	exit(-1);    }            int i,can_id = 1;    int rate;    ioctl(device, CAN_IOCSRATE, 250000);    ioctl(device, CAN_IOCGRATE, &rate);        printf("%s Work Rate: %dbps\n", argv[1], rate);            while(1)    {		memset(&frame, 0, sizeof(struct can_frame));	sprintf(frame.data, "%d", can_id);	frame.header.id = can_id;	frame.header.dlc = strlen(frame.data);	if ((can_id % 2) == 0) {	    frame.header.ide = 1;	    frame.header.eid = can_id * 2 + 1;	}else{	    frame.header.ide = 0;	    frame.header.eid = 0;	}	can_id++;	if (can_id > 2031) {	    can_id = 1;	}			int ret = write(device, &frame, sizeof(struct can_frame));	if (ret < 0)	    perror("write");		sleep(3);	    }        close(device);        return 0;    }

⌨️ 快捷键说明

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