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

📄 rxtx.c

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

#include "can.h"
#define MAXL 40

int main(void)
{
  int i=0, fd=0, ret=0, count=0;
  char loop=0;
  char ch, transmission[MAXL+1], specialfile[MAXL+1]="/dev/can0", emptystring[MAXL+1]="", buf[MAXL+1];
  char remote[MAXL+1];
  struct canmsg_t message;
  
  printf("\nThis program allows you to send or receive Can messages.\n");
  printf("Please answer the following questions:\n\n");
  
  while ( (*transmission!='s') && (*transmission!='r') ) {
    printf("Would you like to send or receive a message?\n");
    printf("send: <s> | receive: <r> ");
    strcpy(transmission,emptystring);
    count=0;
    while ( (ch=getchar()) != '\n' )
      transmission[count++]=ch;
    transmission[count]='\0';
  }
  
  while ( *remote!='y' && *remote!='n' ) {
    printf("Should the message be configured for Remote Transmission Requests?\n");
    printf("yes: <y> | no: <n> ");
    strcpy(remote,emptystring);
    count=0;
    while ( (ch=getchar()) != '\n' )
      remote[count++]=ch;
    remote[count]='\0';
  }
  if (remote[0]=='y')
    message.flags |= MSG_RTR;
  else
    message.flags = 0;
  //	message.flags |= MSG_EXT;  hard code EXT for now
  
  if (transmission[0]=='s') {
    printf("From wich device file would you like to send the message?\n");
    printf(specialfile);
    *buf='\0';
    fgets(buf,MAXL,stdin);
    buf[strcspn(buf,"\n")]='\0';
    if(*buf)
      strncpy(specialfile,buf,MAXL);
    specialfile[MAXL]='\0';
    specialfile[MAXL]='\0';
    printf("Enter the Message ID ");
    scanf("%lx",&message.id);
    printf("Enter the Message Length ");
    scanf("%d",&message.length);
    for (i=0; i<message.length; i++) {
      printf("Enter data byte [%d] ",i);
      scanf("%x",(int *)&message.data[i]);
    }
  }	
  if (*transmission=='r') {
    printf("At which device file would you like to receive the message?\n");
    printf(specialfile);
    *buf='\0';
    fgets(buf,MAXL,stdin);
    buf[strcspn(buf,"\n")]='\0';
    if(*buf)
      strncpy(specialfile,buf,MAXL);
    specialfile[MAXL]='\0';
    printf("Enter the Message ID ");
    scanf("%ld",&message.id);
    getchar();
  }
  
  fd=open(specialfile,O_RDWR);
  if (fd<0) {
    printf("Error opening %s\n",specialfile);
    return -1;
  }
  
  if (transmission[0]=='s') {
    printf("Press enter to send message\n");
    getchar();
    while (getchar() != '\n');
    ret=write(fd, &message, sizeof(struct canmsg_t));
    if (ret<0)
      printf("Error sending message from %s\n",specialfile);
    else
      printf("Message successfully sent from %s\n",specialfile);
  }
  
  if (*transmission=='r') {
    ioctl( fd, IOCTL_CAN_SET_FILTER, message. id );
    printf("Press enter to receive message or <l>oop\n");
    loop = 'l';
    while ( loop == 'l') {
      loop = getchar(); 
      ret=read(fd, &message, sizeof(struct canmsg_t));
      if (ret<0)
	printf("Error receiving message on %s\n",
	       specialfile);
      else {
	printf("Id	: %lx\n",message.id);
	printf("length	: %d\n",message.length);
	printf("flags	: 0x%02x\n", message.flags);
	printf("time	: %ld\n", message.timestamp);
	for (i=0; i<message.length; i++)
	  printf("data%d	: %02x\n",i,
		 message.data[i]);
      }
    }
  }
  
  if ( close( fd ) ) {
    printf("Error closing %s\n",specialfile);
    return -1;
  }
  
  return 0;
}	

⌨️ 快捷键说明

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