日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

CAN 接口测试

發布時間:2023/12/2 综合教程 31 生活家
生活随笔 收集整理的這篇文章主要介紹了 CAN 接口测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CAN 接口測試

一、命令測試

系統下測試會用到candump與cansend

備注:如果沒有can命令可以通過編譯can-utils獲得,can-utils源碼:http://sources.buildroot.net/can-utils/
交叉編譯:make ARCH=XXXX CC=XXXX-linux-gcc -j4 CFLAGS=-static

測試腳本cantest.sh如下

#!/bin/sh
ifconfig can0 down
ifconfig can1 down
echo cantest recv can0 id
echo cantest send can0 id times data0 data1
echo id -1 means receive all
set -x
/sbin/ip link set can0 type can bitrate 100000
/sbin/ip link set can1 type can bitrate 100000
ifconfig can0 up
ifconfig can1 up
candump can0  &
cansend  can1 123#11223344556677

1.sudo modprobe vcan
加載虛擬can模塊
2.sudo ip link add dev vcan0 type vcan
添加vcan0網卡
3.ifconfig -a
可以查到當前can網絡 can0 can1,包括收發包數量、是否有錯誤等等
4.ip link set can0 up type can bitrate 800000
//ip link set can0 type can --help
設置can0的波特率為800kbps,CAN網絡波特率最大值為1Mbps
5.ip link set can0 up type can bitrate 800000 loopback on
設置回環模式,自發自收,用于測試是硬件是否正常,loopback不一定支持
6. ip link set can0 down
關閉can0 網絡
7.cansend can0 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
發送默認ID為0x1的can標準幀,數據為0x11 22 33 44 55 66 77 88 每次最大8個byte
8.cansend can0 -i 0x800 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 -e
-e 表示擴展幀,CAN_ID最大29bit,標準幀CAN_ID最大11bit
-i表示CAN_ID
9. cansend can0 -i 0x02 0x11 0x12 --loop=20
–loop 表示發送20個包
10.candump can0
接收CAN0數據

二、應用程序測試

1、can發送測試

#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>void CANTestMenu(void)
{printf("************************************************** \n");printf("*  Select CAN bitrate menu \n");printf("*  1  5Kbps \n");printf("*  2  10Kbps \n");printf("*  3  20Kbps \n");printf("*  4  50Kbps \n");printf("*  5  100Kbps \n");printf("*  6  125Kbps \n");printf("*  7  250Kbps \n");printf("*  8  500Kbps \n");printf("*  9  800Kbps \n"); printf("*  10 1000Kbps \n");    printf("*  11 exit \n");printf("************************************************** \n");printf("*  please input CAN bitrate Int Number,press enter end \n");printf("************************************************** \n");
}int CanInit(unsigned int id, unsigned int baud)
{int s;int ret;char dev[8] = {0};char cmd[128] = {0};struct sockaddr_can addr = {0};struct ifreq ifr = {0};sprintf(dev, "can%d", id);printf("can dev : %s \n", dev);//關閉can設備sprintf(cmd, "ifconfig %s down", dev);printf(cmd);printf("\n");if(system(cmd) < 0){printf("can device shut down failed  \n");return -1;}//設置can設備波特率bzero(cmd, sizeof(cmd));sprintf(cmd, "/sbin/ip link set %s type can bitrate %d ", dev, baud);printf(cmd);printf("\n");if(system(cmd) < 0){printf("set can device baud rate failed  \n");return -1;}//打開can設備bzero(cmd, sizeof(cmd));sprintf(cmd, "ifconfig %s up", dev);printf(cmd);printf("\n");   if(system(cmd) < 0){printf("can device open failed  \n");return -1;}//創建套接字s = socket(PF_CAN, SOCK_RAW, CAN_RAW);if(s < 0){perror("can socket");return -1;}strcpy(ifr.ifr_name, "can0" );//指定can0 設備ret = ioctl(s, SIOCGIFINDEX, &ifr);if(ret < 0){perror("can ioctl");return -1;}addr.can_family = AF_CAN;addr.can_ifindex = ifr.ifr_ifindex;//將套接字與can0 綁定ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));if(ret < 0){perror("can bind");return -1;}return s;   
}int main()
{int s, nbytes, n;long bitrate;struct sockaddr_can addr;struct ifreq ifr;struct can_frame frame;//CANTestMenu();bitrate=100000;s = CanInit(0, bitrate);if(s < 0){printf("CanInit failed \n");sleep(1);close(s);return -1;}printf("CanInit success\n");frame.can_id = 0x123ab|CAN_EFF_FLAG;frame.can_dlc = 8;frame.data[0] = 0x11;frame.data[1] = 0x22;frame.data[2] = 0x33;frame.data[3] = 0xaa;frame.data[4] = 0xbb;frame.data[5] = 0xcc;frame.data[6] = 0xdd;frame.data[7] = 0xee;//禁用過濾規則,本進程不接收報文,只負責發送setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);    while(1){nbytes = write(s, &frame, sizeof(frame)); //發送frameif(nbytes != sizeof(frame)){printf("Send Error frame\n!");
//            break; //發送錯誤,退出}else{printf("Send msg success!\n");}usleep(10000);}close(s);return 0;
}

2、can接收測試

#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>int CanInit(unsigned int id, unsigned int baud)
{int s;int ret;char dev[8] = {0};char cmd[128] = {0};struct sockaddr_can addr = {0};struct ifreq ifr = {0};sprintf(dev, "can%d", id);printf("can dev : %s \n", dev);//關閉can設備sprintf(cmd, "ifconfig %s down", dev);printf(cmd);printf("\n");if(system(cmd) < 0){printf("can device shut down failed  \n");return -1;}//設置can設備波特率bzero(cmd, sizeof(cmd));sprintf(cmd, "ip link set %s type can bitrate %d ", dev, baud);printf(cmd);printf("\n");if(system(cmd) < 0){printf("set can device baud rate failed  \n");return -1;}//打開can設備bzero(cmd, sizeof(cmd));sprintf(cmd, "ifconfig %s up", dev);printf(cmd);printf("\n");   if(system(cmd) < 0){printf("can device open failed  \n");return -1;}//創建套接字s = socket(PF_CAN, SOCK_RAW, CAN_RAW);if(s < 0){perror("can socket");return -1;}strcpy(ifr.ifr_name, "can0" );//指定can0 設備ret = ioctl(s, SIOCGIFINDEX, &ifr);if(ret < 0){perror("can ioctl");return -1;}addr.can_family = AF_CAN;addr.can_ifindex = ifr.ifr_ifindex;//將套接字與can0 綁定ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));if(ret < 0){perror("can bind");return -1;}return s;
}int main()
{int s, nbytes;struct can_frame frame;struct can_filter rfilter[1];s = CanInit(0, 100000);//定義接收規則,只接收表示符等于0x11 的報文rfilter[0].can_id = 0x11;rfilter[0].can_mask = CAN_SFF_MASK;//設置過濾規則setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));while(1){nbytes = read(s, &frame, sizeof(frame)); //接收報文//顯示報文if(nbytes > 0){printf("ID=0x%x, DLC=%d, data[0]=0x%x \n", frame.can_id, frame.can_dlc, frame.data[0]);}}close(s);return 0;
}

總結

以上是生活随笔為你收集整理的CAN 接口测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。