Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
litesom:devicetree:can [2017/06/09 21:50] filug [CAN bus load test] |
litesom:devicetree:can [2017/06/09 22:00] filug [SocketCAN API] |
||
---|---|---|---|
Line 220: | Line 220: | ||
< | < | ||
+ | int s; | ||
+ | struct sockaddr_can addr; | ||
+ | struct ifreq ifr; | ||
+ | s = socket(PF_CAN, | ||
+ | |||
+ | strcpy(ifr.ifr_name, | ||
+ | ioctl(s, SIOCGIFINDEX, | ||
+ | |||
+ | addr.can_family = AF_CAN; | ||
+ | addr.can_ifindex = ifr.ifr_ifindex; | ||
+ | |||
+ | bind(s, (struct sockaddr *)& | ||
+ | |||
+ | // build CAN data frame | ||
+ | struct can_frame frame; | ||
+ | frame.can_id = node_id | CAN_EFF_FLAG; | ||
+ | frame.can_dlc = 6; // data frame length | ||
+ | frame.data[0] = 0x11; // data frame first byte | ||
+ | frame.data[1] = 0x22; | ||
+ | frame.data[2] = 0x33; | ||
+ | frame.data[3] = 0x44; | ||
+ | frame.data[4] = 0x55; | ||
+ | frame.data[5] = 0x66; // data frame last byte | ||
+ | |||
+ | // send data frame via CAN bus | ||
+ | write(s, &frame, sizeof(frame)); | ||
</ | </ |