首页 > 代码库 > CAN bus的移植
CAN bus的移植
CAN bus的移植
一、首先要阅读相关的文档了解can bus协议;
二、驱动移植,内核中一般是自带相关的can驱动,如果没有可以自行加入;
三、测试,首先下载can bus套件:
fulinux ~$git clone https://git.gitorious.org/linux-can/can-utils.git fulinux ~$cd can-utils fulinux ~$ls**** candump.c ***fulinux ~$make
make后有candump执行程序。如果没有can设备,可以通过下面的linux命令来创建一个虚拟的can设备:
fulinux ~$ modprobe can fulinux ~$ modprobe can_raw fulinux ~$ modprobe vcan fulinux ~$ sudo ip link add dev vcan0 type vcan fulinux ~$ sudo ip link set up vcan0 fulinux ~$ ip link show vcan0 3: vcan0: <NOARP,UP,LOWER_UP> mtu 16 qdisc noqueue state UNKNOWN link/can
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <net/if.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <linux/can.h> #include <linux/can/raw.h> int main(void) { int s; int nbytes; struct sockaddr_can addr; struct can_frame frame; struct ifreq ifr; char *ifname = "vcan0"; if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("Error while opening socket"); return -1; } strcpy(ifr.ifr_name, ifname); ioctl(s, SIOCGIFINDEX, &ifr); addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; printf("%s at index %d\n", ifname, ifr.ifr_ifindex); if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("Error in socket bind"); return -2; } frame.can_id = 0x123; frame.can_dlc = 2; frame.data[0] = 0x11; frame.data[1] = 0x22; nbytes = write(s, &frame, sizeof(struct can_frame)); printf("Wrote %d bytes\n", nbytes); return 0; }
同时修改makefile文件,将candump-client程序加入到makefile文件中一起编译:
PROGRAMS = can-calc-bit-timing candump candump-client cansniffer cansend canplayer cangen canbusloadcandump.o: lib.h candump-client.o: lib.h candump: candump.o lib.o candump-client: candump-client.o lib.o
fulinux ~$make
在两个终端上执行:
fulinux ~$./candump vcan0 fulinux ~$./candump-client
执行结果:
by fulinux
blog: blog.csdn.net/fulinus
CAN bus的移植
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。