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

📄 cansupport.c

📁 can4linux-3.5.3.gz can4 linux
💻 C
字号:
#include <sys/types.h>#include <sys/stat.h>#include <sys/time.h>#include <fcntl.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <can4linux.h>int can_init(int port) {  char dev[10];  sprintf(dev,"/dev/can%d",port);      return open(dev,O_RDWR);}int can_close(int fd) {  return close(fd);}int can_send(int fd, int len,char *message) { char *token; canmsg_t tx; int j,sent=0;  if(( token = strtok(message,":") ) != NULL ) {   tx.flags=0;   if( token[0] == 'r' || token[0] == 'R' ) {     tx.flags = MSG_RTR;     tx.id = strtol(&token[1],NULL,0);     tx.length=len;   } else {     tx.id = strtol(token,NULL,0);     j=0;     while( (token = strtok(NULL,",")) != NULL ) {       /*printf("'%c' ",strtol(token,NULL,0) );*/       tx.data[j++] = strtol(token,NULL,0);     }     tx.length=(len>0 ? len : j );   }   sent = write(fd,&tx,1);   /*if(sent > 0 ) printf(" OK\n"); fflush(stdout);*/      return 1; } else {   return -1; }}#if 0int can_filter(int fd,char *fstring) {  char *token;  int i;  if(( token = strtok(fstring,",")) != NULL ) {    if( token[0] == '*' ) {      can_Config(fd, CONF_FILTER, 0 );  printf("\nfilter disabled");    } else {      can_Config(fd, CONF_FILTER, 1 );      can_Config(fd, CONF_FENABLE, strtol(token,NULL,0));printf("\naccept %d",strtol(token,NULL,0));    }    while((token=strtok(NULL,",")) != NULL ) {      can_Config(fd, CONF_FENABLE, strtol(token,NULL,0));printf("\naccept %d",strtol(token,NULL,0));    }    return 1;  }  return -1;}  #endifchar *can_read(int fd, int timeout) {fd_set   rfds;struct   timeval tv;int      got,i,j;canmsg_t rx[80];static char databuf[4096];char     *s;char     type;  FD_ZERO(&rfds);  FD_SET(fd,&rfds);  tv.tv_sec  = 0; /* wait 5 seconds before process terminates */  tv.tv_usec = timeout;  s = &databuf[0];  /* s += sprintf(s,"",fd); */  got=0;  if( select(FD_SETSIZE, &rfds, NULL, NULL, ( timeout > 0 ? &tv : NULL )  ) > 0 && FD_ISSET(fd,&rfds) ) {      got = read(fd, rx , 79 );      /*s += sprintf(s, "got=%d",got);*/      if( got > 0) {	/*s += sprintf(s, "\n");*/	for(i=0;i<got;i++) {	  rx[i].data[rx[i].length] = 0;		/* why ???, you are overwriting something if length = 8 */	  if(rx[i].flags & MSG_OVR) {	    type = 'O';	  } else if(rx[i].flags & MSG_EXT) {	    type = 'e';	  } else {	    type = '.';	  }	  if( rx[i].flags & MSG_RTR ) {	    s += sprintf(s, "%12lu.%06lu 0x%08lx R %c",		    rx[i].timestamp.tv_sec,		    rx[i].timestamp.tv_usec,		    rx[i].id, type) ;          }	  else {	    s += sprintf(s, "%12lu.%06lu 0x%08lx . %c %d ",		    rx[i].timestamp.tv_sec,		    rx[i].timestamp.tv_usec,		    rx[i].id, type, rx[i].length );      	    for(j=0;j<rx[i].length;j++)	      s += sprintf(s, " 0x%02x",rx[i].data[j] ); 	    for(;j<8;j++)	      s += sprintf(s, "  .  "); 	    s+=sprintf(s," '%s'",rx[i].data);          }	  s += sprintf(s, "\n");	}       }  }return databuf;}/***************************************************************************//**** Set CAN bitrate.** Stops the program when setting the bitrate fails.** \retval 0**/int set_bitrate(	int fd,		/**< device descriptor */	int baud	/**< bit rate, 10, 20, 50, 100, 125, 250, 500, 800, 1000 kBit/s */    ){int ret;volatile Config_par_t  cfg;    ret = can_stop(fd);        cfg.target = CONF_TIMING;     cfg.val1   = baud;    ret = ioctl(fd, CAN_IOCTL_CONFIG, &cfg);    ret = can_start(fd);    if (ret < 0) {	perror("set_bitrate");	exit(-1);    } else {	ret = 0;    }    return ret;}/***************************************************************************//**** Reset the CAN-Controller** \retval 0 success* \retval !=0 failure**/int can_reset(	int fd			/**< device descriptor */    ){int ret;volatile Command_par_t cmd;    cmd.cmd = CMD_RESET;    ret = ioctl(fd, CAN_IOCTL_COMMAND, &cmd);    return ret;}/***************************************************************************//**** Start the CAN-Controller** \retval 0 success* \retval !=0 failure**/int can_start(	int fd			/**< device descriptor */    ){int ret;volatile Command_par_t cmd;    cmd.cmd = CMD_CLEARBUFFERS;    ret = ioctl(fd, CAN_IOCTL_COMMAND, &cmd);    cmd.cmd = CMD_START;    ret = ioctl(fd, CAN_IOCTL_COMMAND, &cmd);    return ret;}/***************************************************************************//**** Stop the CAN-Controller** \retval 0 success* \retval !=0 failure**/int can_stop (	int fd			/**< device descriptor */    ){int ret;volatile Command_par_t cmd;    cmd.cmd = CMD_STOP;    ret = ioctl(fd, CAN_IOCTL_COMMAND, &cmd);    return ret;}/*______________________________________________________________________EOF_*/

⌨️ 快捷键说明

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