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

📄 canwrite.c.svn-base

📁 canpie 一个can bus的协议栈 - CAN interface for embedded control - CAN interface for PC (without local
💻 SVN-BASE
字号:
//****************************************************************************//
// File:          canwrite.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: canwrite.c,v 1.1 2005/06/01 21:36:15 microcontrol Exp $
//------------------------------------------------------------------------------


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

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

#include <can/can.h>



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


int  runProg = 1;

//----------------------------------------------------------------------
// quit()
// exit this program gracefully

void quit(int signal)
{
	runProg = 0;
}

int main(int argc, char **argv)
{
	int   opt;
   int   sockfd;
   int   can_port = 0;
   int   can_id   = 0x0100;
   
   struct sockaddr_can	can_sock;
   struct can_frame     frame;

   signal(SIGINT, quit);
      
   while((opt = getopt(argc, argv, "i:p:")) != -1)
   {
   	switch(opt)
      {
      	case 'i':
   			can_id = atoi(optarg);
            break;
            
         case 'p':
         	can_port = atoi(optarg);
            break;
            
         case '?':
            printf("Unknown option: %c\n", optopt);
            break;      
      }
   }
   
   sockfd = socket(AF_CAN, SOCK_STREAM, 23);
   if(sockfd <0)
   {
      printf("Can't open socket AF_CAN\n");
		exit(0)   ;
   }

   can_sock.can_family = AF_CAN;
   can_sock.can_if     = can_port;
   can_sock.can_id     = can_id;
   
   if(connect(sockfd, &can_sock, sizeof(can_sock)) < 0)
   {
   	printf("Can't connect\n");
      exit(0);
   }
   
   //-------------------------------------------------
   // setup the frame
   //
   frame.id.uwStd  = can_id;
   frame.dlc = 3;
   frame.data.aubByte[0] = 0x01;
   frame.data.aubByte[1] = 0x20;
   frame.data.aubByte[2] = 0x33;
   
   while(runProg)
   {
   	printf("Send CAN message %04X\n", frame.id.uwStd);
   	write(sockfd, &frame, sizeof(struct can_frame));
   	frame.data.aubByte[0]++;
      sleep(1);
   }
   
   
   close(sockfd);
}

⌨️ 快捷键说明

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