Last active
February 10, 2016 02:39
-
-
Save chintanp/2251dff8f0a0664fc02f to your computer and use it in GitHub Desktop.
Working C code that sends data over CANBus to an ELCON charger and corresponding node implementation (that doesnt work yet)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* $Id$ | |
*/ | |
/* | |
* cansend.c - simple command line tool to send CAN-frames via CAN_RAW sockets | |
* | |
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* 1. Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* 2. Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* 3. Neither the name of Volkswagen nor the names of its contributors | |
* may be used to endorse or promote products derived from this software | |
* without specific prior written permission. | |
* | |
* Alternatively, provided that this notice is retained in full, this | |
* software may be distributed under the terms of the GNU General | |
* Public License ("GPL") version 2, in which case the provisions of the | |
* GPL apply INSTEAD OF those given above. | |
* | |
* The provided data structures and external interfaces from this code | |
* are not restricted to be used by modules with a GPL compatible license. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
* DAMAGE. | |
* | |
* Send feedback to <linux-can@vger.kernel.org> | |
* | |
*/ | |
/* 20-05-15 Modified to sent a message out in code rather then from parameter. | |
www.skpang.co.uk | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <time.h> | |
#include <net/if.h> | |
#include <sys/ioctl.h> | |
#include <linux/can.h> | |
#include <linux/can/raw.h> | |
#include "lib.h" | |
int main(int argc, char **argv) | |
{ | |
int s; /* can raw socket */ | |
int nbytes; | |
struct sockaddr_can addr; | |
struct can_frame frame; | |
struct ifreq ifr; | |
int i; | |
/* CAN message to be sent out */ | |
unsigned char buff[] = "1806E5F4#0118006400000000"; | |
fprintf(stderr,"CAN testing\n"); | |
/* parse CAN frame */ | |
if (parse_canframe(buff, &frame)){ | |
fprintf(stderr, "\nWrong CAN-frame format!\n\n"); | |
fprintf(stderr, "Try: <can_id>#{R|data}\n"); | |
fprintf(stderr, "can_id can have 3 (SFF) or 8 (EFF) hex chars\n"); | |
fprintf(stderr, "data has 0 to 8 hex-values that can (optionally)"); | |
fprintf(stderr, " be seperated by '.'\n\n"); | |
fprintf(stderr, "e.g. 5A1#11.2233.44556677.88 / 123#DEADBEEF / "); | |
fprintf(stderr, "5AA# /\n 1F334455#1122334455667788 / 123#R "); | |
fprintf(stderr, "for remote transmission request.\n\n"); | |
return 1; | |
} | |
18 | |
/* open socket */ | |
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { | |
perror("socket"); | |
return 1; | |
} | |
addr.can_family = AF_CAN; | |
strcpy(ifr.ifr_name, "can0"); | |
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { | |
perror("SIOCGIFINDEX"); | |
return 1; | |
} | |
addr.can_ifindex = ifr.ifr_ifindex; | |
/* disable default receive filter on this RAW socket */ | |
/* This is obsolete as we do not read from the socket at all, but for */ | |
/* this reason we can remove the receive list in the Kernel to save a */ | |
/* little (really a very little!) CPU usage. */ | |
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0); | |
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { | |
perror("bind"); | |
return 1; | |
} | |
/* send frame */ | |
for(i=0;i<1000;i++) | |
{ | |
if ((nbytes = write(s, &frame, sizeof(frame))) != sizeof(frame)) { | |
perror("write"); | |
return 1; | |
} | |
fprintf(stderr, "%d \n", i); | |
usleep(1000000); /* Delay before next loop */ | |
} | |
close(s); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment