/** \file canreply.c Reply tool for round-trip measurement on CAN-bus. \author Cristiano Brudna \date 2003 University of Ulm, Germany This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #include #include "can.h" #define PING_ID 0x33 #define REPLY_ID 0x44 int done = 0; int verbose = 0; /* CAN-Bus */ int can; unsigned long baud_rate = B1000; void siginthandler(int egal) { done = 1; } int main(int argc, char **argv) { int ret; char opchr; canmsg ping_msg, reply_msg; int i = 0; int par; while((opchr = getopt(argc,argv, "hb:v")) != -1) switch(opchr) { case 'h': printf("\nCAN Ping help\n\n"); printf("usage: canping [-b baud] [-h] \n\n"); printf("-b baud : set baudrate [1000, 500, 250, 125, 20] kbit/s \n"); printf("-v : verbose, print received ping messages \n"); printf("-h : help \n"); printf("\n"); exit(0); case 'b': par = atoi(optarg); switch(par) { case 1000: baud_rate = B1000; break; case 500: baud_rate = B500; break; case 250: baud_rate = B250; break; case 125: baud_rate = B125; break; case 20: baud_rate = B20; break; default: printf("\nBaud rate not supported!!! Using 1Mbit/s instead\n"); } break; case 'v': verbose = 1; break; default: break; } can = open("/dev/can", O_RDWR); if(can == -1) { perror("Error opening /dev/can"); exit(0); } ioctl(can, CAN_IOCSBAUD, &baud_rate); reply_msg.id = REPLY_ID; ping_msg.type = EXTENDED; done = 0; signal(SIGINT,siginthandler); siginterrupt(SIGINT, 1); while(!done) { ret = read(can, &ping_msg, sizeof(canmsg)); if((ret > 0) && (ping_msg.id = PING_ID) ) { reply_msg.len = ping_msg.len; memcpy(&reply_msg.d, &ping_msg.d, 8); ret = write(can, &reply_msg, sizeof(canmsg)); if(ret < 0) done = 1; if(verbose) { printf("ping received: "); for(i=0; i