⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c_can_readburst.c

📁 HMS30C7202下的CAN驱动
💻 C
字号:
/*Usage: *call c_can_readburst [device] [message id] [max messages] {[acceptance mask]} *for example to receive through /dev/can0 up to 100 *messages with the id 55 (decimal): * "./c_can_readburst /dev/can0 55 100" */#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/canb0"#ifndef TRUE# define TRUE  1#endif#ifndef FALSE# define FALSE 0#endif/* Prototypes */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 );}/** * main */int main( int argc, char **argv ){        long n,ret, max;        long mask;        int umask = 0;        char c;        unsigned long i=0;        char *szdevice/*[ 256 ]*/ = DEFAULT_DEV;        int bVerbose = FALSE;        int bDebug = FALSE;        int infinite = 0;        struct canmsg_t readmsg = { 0,5,0,0,0,{0,} };        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 recieves CAN-messages with the specified ID\n\n");        if (argc < 4) {                printf("Error: Call \"c_can_readburst [device] [message id] [max messages] {[acceptance mask]}\"\n");                return 1;        } else {                szdevice = argv[1];                readmsg.id = atol(argv[2]);                max = atol(argv[3]);        }        if (argc > 4) {                umask = 1;                mask = atol(argv[4]);        }        printf("Openig %s\n", szdevice);        if ( ( fd = open( szdevice, O_RDONLY ) ) < 0 ) {                perror("open");                printf("Error opening %s\n", szdevice);                exit(1);        }        printf("Setting arbitration mask to id=%d\n", readmsg.id);        ioctl( fd, IOCTL_CAN_SET_READ_MODE, readmsg.id );        if (umask) {                printf("Setting acceptance mask to mask=%d\n", mask);                ioctl( fd, IOCTL_CAN_SET_FILTER, mask );        }        if (max == -1) {                infinite = 1;                printf("Receiving messages:\n");        } else                printf("Receiving up to %d messages:\n", max);        while ( infinite || i < max ) {                readmsg.flags=0;                ret = read( fd, &readmsg, sizeof( struct canmsg_t ) );                if ( ret < 0 ) {                        printf( "Error reading message\n" );                } else {                        printf("Received message #%lu: id=%X dlc=%u", i, readmsg.id, readmsg.length );                        for( n=0; n < readmsg.length ; n++ )                                fprintf( stdout, " %.2X", ( unsigned char ) readmsg.data[ n ] );                        fprintf( stdout, " timestamp: %lds %ldus\n", readmsg.timestamp_sec, readmsg.timestamp_usec);                        i++;                }        }        printf("Max message number reached (%d)\n", max);        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 + -