版权声明:本文为博主原创文章,未经博主允许不得转载。
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
在编写一个小程序candump-client,配合candump程序一同测试:
- #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;
- charchar *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 canbusload\
- candump.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
执行结果:
也可以直接结婚can-utils目录下的candump和cansend程序一同测试:
发送端:
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 5A1#11.2233.44556677.88
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#DEADBEEF
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 5AA#
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123##1
- CAN interface ist not CAN FD capable - sorry.
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 1F334455#1122334455667788
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 1F334455#112233445566778899
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#R
接收端:
- [fulinux@ubuntu can-utils]$ ./candump vcan0
- vcan0 5A1 [8] 11 22 33 44 55 66 77 88
- vcan0 123 [4] DE AD BE EF
- vcan0 5AA [0]
- vcan0 1F334455 [8] 11 22 33 44 55 66 77 88
- vcan0 1F334455 [8] 11 22 33 44 55 66 77 88
- vcan0 123 [0] remote request
第一个小游戏:
第一个终端中:
- [fulinux@ubuntu can-utils]$./candump -b vcan0 vcan0,123:C00007FF
第二个终端中:
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#DEADBEEF
你会看到第一个终端中不停地打印如下数据:
- vcan0 123 [4] DE AD BE EF
- vcan0 123 [4] DE AD BE EF
- vcan0 123 [4] DE AD BE EF
因为vcan0这时候充当了一个网关的角色,将vcan0接收到的数据在转发给vcan0,于是就形成了一个循环。
第二个小游戏:
创建一个vcan1设备:
- fulinux ~$ sudo ip link add dev vcan1 type vcan
- fulinux ~$ sudo ip link set up vcan1
- fulinux ~$ ip link show vcan1
- 3: vcan1: <NOARP,UP,LOWER_UP> mtu 16 qdisc noqueue state UNKNOWN
- link/can
第一个终端中:
- [fulinux@ubuntu can-utils]$./candump -b vcan1 vcan0,123:C00007FF
第二个终端中:
- [fulinux@ubuntu can-utils]$./candump -b vcan0 vcan1,123:C00007FF
第三个终端中:
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#DEADBEEF
于是你就会看的第一二两个终端上不停的打印信息,原因是,vcan0接收到的数据转发给vcan1,vcan1接收到的数据再转发给vcan0,于是就形成了一个循环。
第三个小游戏:
第一个终端接收:
- fulinux@ubuntu can-utils]$ ./candump -c -ta vcan0,123:7FF,400:700,#000000FF vcan1,400~7F0
- (1406170861.311895) vcan0 123 [4] DE AD BE EF
- (1406170896.189825) vcan0 123 [0] remote request
- (1406170917.890993) vcan1 123 [0] remote request
- (1406170960.689413) vcan0 400 [0] remote request
- (1406170999.256695) vcan1 111 [0] remote request
第二个终端发送:
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#DEADBEEF
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#DEADBEEF
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#DEADBEEF
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#DEADBEEF
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#DEADBEEF
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#DEADBEEF
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 123#R
- [fulinux@ubuntu can-utils]$ ./cansend vcan1 123#R
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 400#R
- [fulinux@ubuntu can-utils]$ ./cansend vcan0 111#R
- [fulinux@ubuntu can-utils]$ ./cansend vcan1 111#R
- [fulinux@ubuntu can-utils]$ ./cansend vcan1 400#R
第四个小游戏:
延迟转发:
- [fulinux@ubuntu can-utils]$ ./candump -u 3000000 -b vcan0 vcan0,123:C00007FF
- vcan0 123 [5] DE AD BE EF 33
- vcan0 123 [5] DE AD BE EF 33
- vcan0 123 [5] DE AD BE EF 33
- vcan0 123 [5] DE AD BE EF 33
by fulinux
email:fulinux@sina.com
blog: blog.csdn.net/fulinus