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

📄 canconfig.c.svn-base

📁 canpie 一个can bus的协议栈 - CAN interface for embedded control - CAN interface for PC (without local
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
//****************************************************************************//
// File:          canconfig.c                                                 //
// Description:   Test program for writing CAN messages to a socket           //
// Author:        Uwe Koppe                                                   //
// e-mail:        koppe@microcontrol.net                                      //
//                                                                            //
//============================================================================//
// This program is free software; you can redistribute it and/or modify       //
// it under the terms of the GNU Lesser General Public License as published   //
// by the Free Software Foundation; either version 2.1 of the License, or     //
// (at your option) any later version.                                        //
//============================================================================//
//                                                                            //
// Date        History                                                        //
// ----------  -------------------------------------------------------------- //
// 02.01.2004  Initial version                                                //
//                                                                            //
//****************************************************************************//


//------------------------------------------------------------------------------
// CVS version information:
// $Id: canconfig.c,v 1.3 2005/08/15 16:36:33 microcontrol Exp $
//------------------------------------------------------------------------------


/*----------------------------------------------------------------------------*\
** Include files                                                              **
**                                                                            **
\*----------------------------------------------------------------------------*/

#include <sys/socket.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <can/can.h>

#include <getopt.h>

#define CAN_PORT_MAX 15

/* Ugh.  But libc5 doesn't provide POSIX types.  */
#include <asm/types.h>

/*----------------------------------------------------------------------------*\
** Definitions                                                                **
**                                                                            **
\*----------------------------------------------------------------------------*/


int  runProg = 1;

//----------------------------------------------------------------------
// quit()
// exit this program gracefully
//----------------------------------------------------------------------
void quit(int signal)
{
   runProg = 0;
}

//----------------------------------------------------------------------
// exit_prog()
// close the filedescriptor and exit this program 
//----------------------------------------------------------------------
void exit_prog(int fd, int err_number) {
   close(fd);
   exit (err_number);
}

//----------------------------------------------------------------------
// usage()
// shows the usage informations
//----------------------------------------------------------------------
void usage(void)
{
   printf("Usage: canconfig <interface> <options>\n\n");
   printf(" interface : can0...canN\n\n");
   printf(" options   : [up|down] [bitrate=<Value> | btr0 bt1 sjw] [ ] \n");
   printf("           : [-?|-h|--help] \n");
}


//----------------------------------------------------------------------
// get_can_state()
// gets state of a socket
//----------------------------------------------------------------------
int get_can_state(int sockfd, int slSockOptT) {
   int opt = -1;
   opt = ioctl(sockfd, SIOC_CAN_GET_STATE, &slSockOptT);
   return opt;
}

//----------------------------------------------------------------------
// connect_can()
// connects a can_port to a socket
//----------------------------------------------------------------------
int connect_can (int can_port, int sockfd) {
   
   struct sockaddr_can can_sock;
   
   can_sock.can_family = AF_CAN;
   can_sock.can_if     = can_port;
   can_sock.can_id     = 0x00FFFFFF;

   if(connect(sockfd, &can_sock, sizeof(can_sock)) < 0)
   {
      return (1);
   }
   else
      return (0);

}

//----------------------------------------------------------------------
// get_can_nr()
// returns errors or entered user CAN-Number
//----------------------------------------------------------------------
int get_can_nr (char *str_name) {
   int char_index = 3;
   char scan_number[]= "   ";
   if (str_name[0] == 'c' & str_name[1] == 'a' & str_name[2] == 'n') {  
      if (str_name[char_index] == '\0') //the can number is not available
         return -1;
      else {  
         while (str_name[char_index] != '\0') {
            scan_number[char_index-3] = str_name[char_index];  
            char_index++;
         }
      }
   } 
   else {
      // The argument is not adress family name
      return -2;
   }
   return atoi(scan_number);
}

//----------------------------------------------------------------------
// get_can_opt()
// returns a value for an input:
// -1 for Unknown entry
//  1 for "up"
//  2 for "down"
//  3 for "btr0"
//  4 for "btr1"
//  5 for "sjw"
//  6 for "bitrate="
//  7 for help
//----------------------------------------------------------------------
int get_can_opt(char *str_name) {
  
  if (str_name[0] == 'u' & str_name[1] == 'p')
     return 1;
  else if (str_name[0] == 'd' & str_name[1] == 'o' & str_name[2] == 'w' & str_name[3] == 'n')
     return 2;
  else if (str_name[0] == 'b' & str_name[1] == 't' & str_name[2] == 'r' & str_name[3] == '0')
     return 3;
  else if (str_name[0] == 'b' & str_name[1] == 't' & str_name[2] == 'r' & str_name[3] == '1')
     return 4;
  else if (str_name[0] == 's' & str_name[1] == 'j' & str_name[2] == 'w')
     return 5;
  else if (str_name[0] == 'b' & str_name[1] == 'i' & str_name[2] == 't' & str_name[3] == 'r' &
           str_name[4] == 'a' & str_name[5] == 't' & str_name[6] == 'e' & str_name[7] == '=')
     return 6;
  else if ((str_name[0] == '-' & (str_name[1] == 'h' || str_name[1] == '?')) || 
           (str_name[0] == '-' & str_name[1] == '-' & str_name[2] == 'h' & str_name[3] == 'e' &
            str_name[4] == 'l' & str_name[5] == 'p'))
     return 7;
  else return -1;
}

//----------------------------------------------------------------------
// get_bitrate()
// returns an error or entered user bitrate
//----------------------------------------------------------------------
int get_bitrate(char *str_name) {
   char can_bitrate[]= "     ";
   // char_index has the Value 8, because the bitrate should be on this position: "bitrate=XYZ"
   int char_index = 8;
   
   if (str_name[char_index] == '\0')
      return -1;  //No value for bitrate was entered
   else {  
      while (str_name[char_index] != '\0') {
         can_bitrate[char_index-3] = str_name[char_index];  
         char_index++;
      }
      return atoi(can_bitrate);
   }
}

//----------------------------------------------------------------------
// can_info()
// shows all information of a CAN on the screen
//----------------------------------------------------------------------
int can_info (int index, int sockfd, int slSockOptT) {
   printf("can%i\tstate: %i\n", index, ioctl(sockfd, SIOC_CAN_GET_STATE, &slSockOptT));
   printf("\t---Hier wird nun die Information zum can%i angezeigt!---\n", index);
   return 0;
}


int set_can_bitrate(int sockfd, int can_port, int bitrate) {
   int bitrate_value;
   int opt;
   //Muss ergäntzt und korigiert werden!!!!--------------------------------------------------!!!!
   switch (bitrate) {
   case 10 : bitrate_value = 1;
   break;
   case 20 : bitrate_value = 2;
   break;
   case 50 : bitrate_value = 3;
   break;
   case 125 : bitrate_value = 4;
   break;
   case 250 : bitrate_value = 5;
   break;
   case 500 : bitrate_value = 6;
   break;
   case 1000 : bitrate_value = 7;
   break;
   case 101 : bitrate_value = 8;  //
   break;
   case 102 : bitrate_value = 9;  //
   break;

⌨️ 快捷键说明

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