Go to the documentation of this file.00001
00002
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007
00008 #include <net/if.h>
00009 #include <sys/ioctl.h>
00010
00011 #include <linux/can.h>
00012 #include <linux/can/raw.h>
00013
00014 #define CANINTERFACE "can0"
00015
00016 int main (void) {
00017 int left_pwm = 100;
00018 char left_dir = 0;
00019 char left_brake = 0;
00020 int right_pwm = 100;
00021 char right_dir = 0;
00022 char right_brake = 0;
00023
00024 char left_dir_brake = (left_dir << 1) + left_brake;
00025 char right_dir_brake = (right_dir << 1) + right_brake;
00026
00027 int i;
00028
00029
00030 int s;
00031 int nbytes;
00032 struct sockaddr_can addr;
00033 struct can_frame frame;
00034 struct ifreq ifr;
00035
00036 frame.can_id = 1;
00037 frame.can_dlc = 8;
00038
00039 frame.data[0] = 0 >> 8;
00040 frame.data[1] = 0;
00041 frame.data[2] = (left_dir_brake);
00042 frame.data[3] = (left_pwm >> 8);
00043 frame.data[4] = (left_pwm);
00044 frame.data[5] = (right_dir_brake);
00045 frame.data[6] = (right_pwm >> 8);
00046 frame.data[7] = (right_pwm);
00047
00048 printf("%s ", CANINTERFACE);
00049 printf("%X#", frame.can_id);
00050
00051 for (i = 0; i < 8; i++) {
00052 printf("%X", frame.data[i]);
00053 }
00054
00055 printf("\n");
00056
00057
00058
00059 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
00060 perror("socket");
00061 return 1;
00062 }
00063
00064 addr.can_family = AF_CAN;
00065
00066 strcpy(ifr.ifr_name, CANINTERFACE);
00067 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
00068 perror("SIOCGIFINDEX");
00069 return 1;
00070 }
00071 addr.can_ifindex = ifr.ifr_ifindex;
00072
00073
00074
00075
00076
00077 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
00078
00079 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
00080 perror("bind");
00081 return 1;
00082 }
00083
00084
00085 if ((nbytes = write(s, &frame, sizeof(frame))) != sizeof(frame)) {
00086 perror("write");
00087 return 1;
00088 }
00089
00090
00091 close(s);
00092 return 0;
00093 }