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

📄 sendburst.c

📁 在linux操作系统下CAN收发程序的示例源代码。
💻 C
字号:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include "can.h"

#define DEFAULT_DEV "/dev/can0"

#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,0,5,0,8,{1,2,3,4,5,6,7,8 } };
  int fd, ret,i,j;
  int nbursts, nburstsize;
  char c;
  char szdevice[ 256 ] = DEFAULT_DEV;
  int bVerbose = FALSE;
  int bDebug = FALSE;
  struct canmsg_t readmsg = { 0,0,5,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);
  /*-------------------------------------------------*/	
  
  nbursts = 1;       // One burst as a default
  nburstsize = 10;   // Size for each message burst  

  while ( ( c = getopt( argc, argv, "vhdD:n:s:")) != EOF) {
    
    switch (c) {
      
    case 'v':
      bVerbose = TRUE;
      break;

    case 'D':
      sprintf( szdevice, "/dev/%s", optarg );
      break;

    case 'n':
      nbursts = atoi( optarg );
      break;
      
    case 's':
      nburstsize = atoi( optarg );
      break;
      
    case 'd':
      bDebug = TRUE;
      break;
      
    case 'h':
    default: 
      usage( argv[ 0 ] ); 
      exit (0 );      
    }
  }
  
  if ( ( fd = open( szdevice, O_WRONLY) ) < 0 )  {
    perror("open");
    printf("Error opening %s\n", szdevice);
    exit(1);	
  }

  j=0;
  while ( nbursts-- ) {
    for( i=0; i<nburstsize; i++ ) {
      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 );
      }

    }

    printf("Sent block of %d messages #: %u\n", nburstsize, j );

    j++;
    //usleep( 500000 );
    }

  printf("flushing...\n");
  sleep( 2 ); // Allow buffers to empty before closing
  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, "-d\t Enables verbose mode [off].\n");
}
	












⌨️ 快捷键说明

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