飞凌官方手册中关于CAN测试部分使用的是命令行进行的简单测试,实际开发过程中还是要使用C代码去操作CAN设备,本帖主要讲解怎么使用C代码对CAN设备进行读写的收发操作。
First of all ,先查看所有网卡信息,看看板上是否有CAN设备网络:
root@ok113i:/# ifconfig -acan0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 NOARP MTU:16 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:10 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)can1 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 NOARP MTU:16 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:10 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) Interrupt:1 eth0 Link encap:Ethernet HWaddr BA:E9:F2:1C:9D:87 inet addr:192.168.0.232 Bcast:0.0.0.0 Mask:255.255.255.0 inet6 addr: fe80::b8e9:f2ff:fe1c:9d87/64 Scope:Link UP BROADCAST RUNNING MUL
tiCAST MTU:1500 Metric:1 RX packets:421 errors:0 dropped:14 overruns:0 frame:0 TX packets:9 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:27272 (26.6 KiB) TX bytes:726 (726.0 B) Interrupt:39 ip6tnl0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 NOARP MTU:1452 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)sit0 Link encap:IPv6-in-IPv4 NOARP MTU:1480 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)wlan0 Link encap:Ethernet HWaddr 2C:C3:E6:67:4D:3B BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
1. CAN组网前的准备,设置CAN通信服务的通信速率。使用如下命令分别设置CAN0和CAN1的通信速率:
root@ok113i:/home/forlinx# ip link set can0 up type can bitrate 500000[ 4075.425084] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes readyroot@ok113i:/home/forlinx# ip link set can1 up type can bitrate 500000 [ 4086.037967] IPv6: ADDRCONF(NETDEV_CHANGE): can1: link becomes ready
以上设置can0 和 can1 通信速率均为500kbps,这个速率值务必记住,所有接入组网的的can节点都是以这个速率通信。
2. 使用C语言写一个CAN通信的发送接收测试程序,主要验证CAN发送和接收数据是否正常。#include #include #include #include #include #include #include "can_apply.h"#define MASTR_CAN1_ID 0x100#define MASTR_CAN2_ID 0x101#define RX_MSG_NUM 16#define MAX_BUF_LEN 64typedef enum{ CAN_1, CAN_2, CAN_RES}can_type;typedef struct{ int can_id; unsigned char can_dlc; unsigned char data[MAX_BUF_LEN];}can_msg_t;static int bexit = 0;static int can0_sock,can1_sock;can_msg_t can_rx_msg[RX_MSG_NUM];unsigned int can1_id = 0;unsigned int can2_id = 0;void signal_handler(int signo){ printf("signal %d(%s) receivedn", signo, strsignal(signo)); bexit = 1;}int can_send_data(can_type type, unsigned char *buf, int len){ int i, j; int cnt; int sock; int ret = 0; struct can_frame frame; if(type == CAN_1) { sock = can0_sock; frame.can_id = can1_id; } else { sock = can1_sock; frame.can_id = can2_id; } for(i = 0; i < len; i += 8) { cnt = len - i; if(cnt > 8) frame.can_dlc = 8; else frame.can_dlc = cnt; for(j=0; j