Created
November 19, 2023 17:05
-
-
Save cswiger/2580f4975c18c77e7c7917e00f709bf1 to your computer and use it in GitHub Desktop.
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
/* simple copy of can examples to filter on a specific ID and turn the light bits on | |
* this filters on the smart Car id 423:3FF and or's on the bits in the second byte: | |
* xx x3 where 01 is right, 02 is left | |
* which turn on the indicator lights for turn signal / emergency flasher on the dashboard instrument cluster | |
* doing this in compiled C as python3 is a bit slow for the fast can bus | |
* when the message id 423 is received, or the second byte with 0x03 and immediately re-send it - overwriting | |
* the original message | |
* this version has two length 50 for loops in the infinite loop to switch back and forth between the two lights | |
* that gives them about a second on each | |
* | |
* original source: https://github.com/craigpeacock/CAN-Examples | |
* | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <net/if.h> | |
#include <sys/ioctl.h> | |
#include <sys/socket.h> | |
#include <linux/can.h> | |
#include <linux/can/raw.h> | |
int main(int argc, char **argv) | |
{ | |
int s, i; | |
int nbytes; | |
struct sockaddr_can addr; | |
struct ifreq ifr; | |
struct can_frame rframe; | |
struct can_frame tframe; | |
printf("CAN control Demo\r\n"); | |
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { | |
perror("Socket"); | |
return 1; | |
} | |
strcpy(ifr.ifr_name, "can0" ); | |
ioctl(s, SIOCGIFINDEX, &ifr); | |
memset(&addr, 0, sizeof(addr)); | |
addr.can_family = AF_CAN; | |
addr.can_ifindex = ifr.ifr_ifindex; | |
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { | |
perror("Bind"); | |
return 1; | |
} | |
struct can_filter rfilter[1]; | |
rfilter[0].can_id = 0x423; | |
rfilter[0].can_mask = 0x7FF; | |
tframe.can_id = 0x423; | |
tframe.can_dlc = 8; | |
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); | |
while(1) { | |
for(int k=0; k<50; k++) { | |
nbytes = read(s, &rframe, sizeof(struct can_frame)); | |
if (nbytes < 0) { | |
perror("Read"); | |
return 1; | |
} | |
for (i = 0; i < rframe.can_dlc; i++) | |
tframe.data[i] = rframe.data[i]; | |
tframe.data[1] |= 0x01; | |
if (write(s, &tframe, sizeof(struct can_frame)) != sizeof(struct can_frame)) { | |
perror("Write"); | |
return 1; | |
} | |
} | |
for(int k=0; k<50; k++) { | |
nbytes = read(s, &rframe, sizeof(struct can_frame)); | |
if (nbytes < 0) { | |
perror("Read"); | |
return 1; | |
} | |
for (i = 0; i < rframe.can_dlc; i++) | |
tframe.data[i] = rframe.data[i]; | |
tframe.data[1] |= 0x02; | |
if (write(s, &tframe, sizeof(struct can_frame)) != sizeof(struct can_frame)) { | |
perror("Write"); | |
return 1; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment