1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#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> #define command "ip link set can0 type can bitrate 1000000" #define up "ifconfig can0 up" #define down "ifconfig can0 down"
int can_init() { system(down); system(command); system(up); return 0; }
int can_send(can_frame frame) { int s, nbytes; struct sockaddr_can addr; struct ifreq ifr; s = socket(PF_CAN, SOCK_RAW, CAN_RAW); strcpy(ifr.ifr_name, "can0" ); ioctl(s, SIOCGIFINDEX, &ifr); addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; bind(s, (struct sockaddr *)&addr, sizeof(addr)); nbytes = write(s, &frame, sizeof(frame)); if(nbytes != sizeof(frame)) { printf("Send Error frame[0]\n!"); } close(s); return 0; }
int can_receive(struct can_frame * r_frame,unsigned int filter_id) { int s, nbytes = 0; struct sockaddr_can addr; struct ifreq ifr; struct can_frame frame; struct can_filter rfilter;
memset(&frame,0,sizeof(can_frame)); s = socket(PF_CAN, SOCK_RAW, CAN_RAW); strcpy(ifr.ifr_name, "can0" ); ioctl(s, SIOCGIFINDEX, &ifr); addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; bind(s, (struct sockaddr *)&addr, sizeof(addr)); if (filter_id != 0) { rfilter.can_id = 0x123; rfilter.can_mask = CAN_SFF_MASK; setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); } while (nbytes == 0) { nbytes = read(s, &frame, sizeof(frame)); } *r_frame = frame; #ifdef MSG_DEBUG printf("the nbytes:%d\n", nbytes); printf("length:%d", sizeof(frame)); printf("ID=0x%X DLC=%d\n", frame.can_id, frame.can_dlc); printf("data0=0x%02x\n",frame.data[0]); printf("data1=0x%02x\n",frame.data[1]); printf("data2=0x%02x\n",frame.data[2]); printf("data3=0x%02x\n",frame.data[3]); printf("data4=0x%02x\n",frame.data[4]); printf("data5=0x%02x\n",frame.data[5]); printf("data6=0x%02x\n",frame.data[6]); printf("data7=0x%02x\n",frame.data[7]); #endif return 0; }
int led_ctl_on(void) { struct can_frame frame; memset(&frame, 0, sizeof(can_frame)); frame.can_id = 0x101; frame.can_dlc = 8; frame.data[0] = 1; can_send(frame); return 0; }
int led_ctl_off(void) { struct can_frame frame; memset(&frame, 0, sizeof(can_frame)); frame.can_id = 0x101; frame.can_dlc = 8; frame.data[0] = 2; can_send(frame); return 0; }
float can_get_vol(void) { float vol_vle = 0; struct can_frame frame; memset(&frame, 0, sizeof(can_frame)); can_receive(&frame,0); printf("###############################\n"); printf("length:%d", sizeof(frame)); printf("ID=0x%X DLC=%d\n", frame.can_id, frame.can_dlc); printf("data0=0x%02x\n",frame.data[0]); printf("data1=0x%02x\n",frame.data[1]); printf("data2=0x%02x\n",frame.data[2]); printf("data3=0x%02x\n",frame.data[3]); printf("data4=0x%02x\n",frame.data[4]); printf("data5=0x%02x\n",frame.data[5]); printf("data6=0x%02x\n",frame.data[6]); printf("data7=0x%02x\n",frame.data[7]);
vol_vle = (float)frame.data[0]/50; return vol_vle; }
int main(int argc, char* argv[]) { char control_str[15]; float vol_val = 0;
if (argc < 2) { printf("can_control service_type\n" " example: ./can_control led_off/led_on/get_vol\n" ); return 0; } strcpy(control_str,argv[1]);
printf("Argc : %d\n",argc); printf("Argv : %s\n , %s\n",argv[0], argv[1]);
if (strcmp(control_str,"led_off")==0) { led_ctl_off(); } else if (strcmp(control_str,"led_on")==0) { led_ctl_on(); } else if (strcmp(control_str,"get_vol")==0) { vol_val = can_get_vol(); printf("Voltage is : %5.2f V\n", vol_val); } else { } return 0; }
|