📄 c_can_sendburst.c
字号:
#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <signal.h>#include "../include/can.h"#define DEFAULT_DEV "/dev/cana0"#ifndef TRUE# define TRUE 1#endif#ifndef FALSE# define FALSE 0#endif// Prototypesvoid sortie (int sig );int fd;/*--- handler on SIGINT signal : the program quit with CTL-C ---*/void sortie (int sig ){ close(fd); printf("Terminated by user.\n"); exit(0);}int main( int argc, char **argv ){ struct canmsg_t sendmsg = { 0,5,0,0,8,{1,2,3,4,5,6,7,8 } }; int fd, ret; long i,j; long nmessages; long delay; char c; char *szdevice = DEFAULT_DEV; char buf[ 256 ]; int bVerbose = FALSE; int bDebug = FALSE; struct sigaction act; /*------- register handler on SIGINT signal -------*/ act.sa_handler=sortie; sigemptyset(&act.sa_mask); act.sa_flags=0; sigaction(SIGINT,&act,0); /*-------------------------------------------------*/ printf("\n This program sends a specified number of message-packets of specified size over the given can device\n"); if (argc < 5){ printf("Error: Call \"c_can_sendburst [device] [number of messages] [message id] [delay us]\"\n"); return 1; } else { szdevice = argv[1]; nmessages = atol(argv[2]); sendmsg.id = atol(argv[3]); delay = atol(argv[4]); } printf("Try to open %s\n", szdevice); if ( ( fd = open( szdevice, O_WRONLY) ) < 0 ) { perror("open"); printf("Error opening %s\n", szdevice); exit(1); } j=0; while ( nmessages == -1 || j < nmessages ) { sendmsg.data[ 0 ] = i; sendmsg.data[ 1 ] = ( j & 0xff ); sendmsg.length = 8; if ( ( ret = write( fd, &sendmsg, sizeof( struct canmsg_t ) ) ) < 0) { perror("write"); printf("Error sending message\n"); break; } if ( bDebug ) { printf("Message written. Return value for write: ret=: %u\n", ret ); } j++; usleep( delay ); } printf("Sent %d messages\n", nmessages); printf("flushing...\n"); sleep( 1 ); /* Allow buffers to empty before closing */ printf("Printing statistics of message object\n"); sleep( 1 ); ioctl( fd, IOCTL_CAN_PRINT_STATISTICS ); close(fd); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -