📄 c_can_sendburst2.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/* Prototypes */void usage( char *pname );void 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 } }; long idl, idh, id; int fd, ret, infinite=0, info=0; long i,j; long nbursts, nburstsize; 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); /*-------------------------------------------------*/ //nbursts = 5000; // One burst as a default //nburstsize = 50; // Size for each message burst printf("\n This program sends a specified number of message-packets of specified size over can0.\n"); printf("The message-ids of the sent messages will lie in the given area\n\n"); if (argc < 7) { printf("Error: Call \"c_can_sendburst2 [device] [number of blocks] [blocksize] [id low] [id high] [delay us]\"\n"); return 1; } else { szdevice = argv[1]; nbursts = atol(argv[2]); nburstsize = atol(argv[3]); idl = atol(argv[4]); idh = atol(argv[5]); delay = atol(argv[6]); } if (argc>7) info =1; //exchange values if needed if (idl > idh) { id = idl; idl = idh; idh = id; } if ( ( fd = open( szdevice, O_WRONLY) ) < 0 ) { perror("open"); printf("Error opening %s\n", szdevice); exit(1); } id = idl; j=0; if (delay == -1) infinite = 1; while ( infinite || nbursts-- ) { for( i=0; i<nburstsize; i++ ) { if (id > idh) id = idl; sendmsg.id = id++; //printf("%ld\n", sendmsg.id); 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 ); } } if (info) printf("Sent block of %d messages (#%u)\n", nburstsize, j ); j++; if (delay > 0) usleep( delay ); } printf("Sent %d blocks a %d messages\n", nbursts, nburstsize); 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;}/////////////////////////////////////////////////////////////////////////////////////////////////////////// usage//void usage( char *pname ){ fprintf(stderr, "usage: %s options [id [ byte ..]]\n", pname ); fprintf(stderr, "-h\t Prints this message.\n"); fprintf(stderr, "-n\t The number of bursts to send [1].\n"); fprintf(stderr, "-s\t The number of messages to send in each burts [10].\n"); fprintf(stderr, "-d\t Enables debug mode [off].\n"); fprintf(stderr, "-\t Enables verbose mode [off].\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -