2023年 beagle bone black教程5-CAN的使用


友情提示:本教程述基于 Debian 9.x 版本或者10.x。它已经包含了 CANBUS 和 CAN-Tools 集合的内核驱动程序。 但重要的是,beaglebone的ubboot-Bootloader也更新到该版本的当前版本。如何更新 uboot.)当安装了旧版本的"uboot"时,"配置针"命令不起作用。

1 引脚配置

首先,查看引脚功能

cd /lib/firmware
ls BB-CAN*

在这里插入图片描述
我们查看CAN0

cat BB-CAN0-00A0.dtbo

在这里插入图片描述
如图所示,P9.19和P9.20为我们所用的CAN0。

但是这里只是描述,我们需要真正地把该引脚配置为相应功能(其他外设也如此)

config-pin <pin> <filename> #配置引脚为对应filename的功能
config-pin -l <pin># 查询引脚支持的模式
config-pin -q <pin> #查看引脚目前配置模式

在这里插入图片描述

我们配置一下。

sudo config-pin p9.19 can	#配置p9.19为can
config-pin -q p9.19			#查询p9.19

在这里插入图片描述
可以看到成功配置。值得一提的是,这个配置,每次上电都需要重新配置

下面介绍一劳永逸的办法。

通过 systemd 在引导时启动的配置脚本来完成上电初始化

配置脚本是通过以下方式生成的:
在此脚本中,可以添加所有必需的 IO 引脚。对于本节需求,只有p9.19和p9.20需要配置(后边想加什么自己加),所以新建并打开脚本:

sudo nano /usr/bin/config_pins.sh

添加下边语句:

#!/bin/bash
config-pin p9.19 can
config-pin p9.20 can

通过以下方式使脚本具备可执行权限:sudo chmod a+x /usr/bin/config_pins.sh
在这里插入图片描述

设置服务配置:

sudo nano /lib/systemd/system/config_pins.service

输入下边语句。

[Unit]
Description=Enable pin configuration at startup
After=generic-board-startup.service
[Service]
Type=simple
ExecStart=/usr/bin/config_pins.sh
[Install]
WantedBy=multi-user.target

激活新服务:

sudo systemctl daemon-reload
sudo systemctl enable config_pins.service

在这里插入图片描述重启系统

sudo reboot #重启

重新启动后,检查服务的状态:

sudo systemctl status config_pins.service

在这里插入图片描述
如图,说明服务成功运行。

为了保险,我们再查看一下引脚配置。

config-pin -q p9.19
config-pin -q p9.20

在这里插入图片描述

nice!!!!

2 终端启动can

将CAN0总线速度配置为1000kbps(用其他也行,保持一致即可)。

sudo ip link set can0 up type can bitrate 1000000

启动CAN0

sudo ifconfig can0 up

在这里插入图片描述

没有提示,我们用ifconfig 来看一下。
在这里插入图片描述ok,没有问题。

为了让can服务直接开机启动,可以去配置**/etc/network/interfaces**

sudo vim /etc/network/interfaces

添加如下语句

allow-hotplug can0 
 iface can0 can static   
    bitrate 1000000

在这里插入图片描述

3 CAN通信测试

需要说明的是,beaglebone 的板子上的CAN引脚,需要外部添加CAN电平转换芯片才真正变成CAN信号。
CAN通信可以使用 can-utils 集合中的 CANSend 和 CanDump函数。

CAN0发送数据:设置地址为0x2AA,数据为0x01 0x02 0x03 0x04 0x05(最多8个)。如下:

cansend can0 2AA#00.01.02.03.04.05

要在 CAN 总线上接收消息,使用下边命令,它会阻塞地接收数据

candump -ta can0 #可以不要-ta

如图为用一块板子发送,使用电脑的上位机进行接收(CAN上位机很多,CANalyst、CANtest之类的)
在这里插入图片描述
测试接收,使用电脑发送、

candump -ta can0 #可以不要-ta,就不显示时间

在这里插入图片描述
ok,nice!!

接收如果没有-ta,则为下图的样子。
在这里插入图片描述

4 CAN通信编程

直接看代码

#include <stdlib.h>
#include <stdio.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>
#include <poll.h>
#include <pthread.h>
int sockfd;//can的文件描述符

static void can0_init(void)
{
    //下边若权限不够则开头添加sudo
    system("sudo ifconfig can0 down ");//先关闭 can0 设备
    system("sudo ip link set can0 up type can bitrate 1000000");//设置波特率为 1000000
    system("sudo ifconfig can0 up");//启动can0
    sleep(1);

    sockfd=socket(PF_CAN,SOCK_RAW,CAN_RAW);
    if(sockfd<0) perror("socket error");

 /******************将套接字与 CAN 设备进行绑定********************************/
    struct  ifreq ifr={0};
    struct sockaddr_can can_addr={};
    

    //指定can0设备
    strcpy(ifr.ifr_name,"can0");
    ioctl(sockfd,SIOCGIFINDEX,&ifr);
    
    can_addr.can_family=AF_CAN;//填充数据
    can_addr.can_ifindex=ifr.ifr_ifindex;
       
    int ret=bind(sockfd,(struct sockaddr *)&can_addr,sizeof(can_addr)); //绑定 
    if(ret<0) perror("bind error");

}

static void thread_can_start(void)
{
    can0_init();
    sleep(1);
    while(1){
        struct can_frame frame;
        int ret=read(sockfd,&frame,sizeof(struct can_frame));//这里好像有堵塞效果
        if(ret==-1) perror("read error");
        if(frame.can_id&CAN_ERR_FLAG) printf("Error frame!!\r\n");
        if(frame.can_id&CAN_SFF_MASK)  //CAN接收到了数据,并且是标准帧
        {
        printf("标准帧 <0x%03x>\r\n",frame.can_id&CAN_SFF_MASK);
        printf("len of frame is %d\r\n",frame.can_dlc);

        for(int i = 0; i < frame.can_dlc; i++)
            printf("%02x ",frame.data[i]);
        printf("\r\n");
        } 
          
    }

}

int main (void) 
{
    pthread_t tid_can;
    pthread_create(&tid_can, NULL, (void *)thread_can_start, NULL);//创建CAN接收线程


    while(1){

/**************************数据接受***************************************/
     sleep(1);
     struct can_frame frame;
    frame.can_id =123;//id 
    frame.can_dlc=4;//长度
    frame.data[0]=0xA0;//数据
    frame.data[1]=0xB0;
    frame.data[2]=0xC0;

    int ret=write(sockfd,&frame,sizeof(frame));//CAN每隔1s发送一次 
    if(ret!=sizeof(frame)) perror("write error");

    }   
}
gcc -o can_thread can_thread.c -lpthread %编译
 ./can_thread

在这里插入图片描述
在这里插入图片描述
只使用线程的话,目前有两个问题:偶尔死掉,并且开头会接收一帧乱码。因此下边试试添加POLL

没有问题

5 CAN通信编程+POLL

#include <stdlib.h>
#include <stdio.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>
#include <poll.h>
#include <pthread.h>
int sockfd;//can的文件描述符

static void can0_init(void)
{
    //下边若权限不够则开头添加sudo
    system("sudo ifconfig can0 down ");//先关闭 can0 设备
    system("sudo ip link set can0 up type can bitrate 1000000");//设置波特率为 1000000
    system("sudo ifconfig can0 up");//启动can0
    sleep(1);

    sockfd=socket(PF_CAN,SOCK_RAW,CAN_RAW);
    if(sockfd<0) perror("socket error");

 /******************将套接字与 CAN 设备进行绑定********************************/
    struct  ifreq ifr={0};
    struct sockaddr_can can_addr={};
    
    //指定can0设备
    strcpy(ifr.ifr_name,"can0");
    ioctl(sockfd,SIOCGIFINDEX,&ifr);
    
    can_addr.can_family=AF_CAN;//填充数据
    can_addr.can_ifindex=ifr.ifr_ifindex;
       
    int ret=bind(sockfd,(struct sockaddr *)&can_addr,sizeof(can_addr)); //绑定 
    if(ret<0) perror("bind error");

}

static void thread_can_start(void)
{

    struct pollfd fds;  
    fds.fd=sockfd;     //can
    fds.events=POLLIN;  
    fds.revents=0;//
    while(1){
        int ret=poll(&fds,1,-1);//2 表示fds里元素个数为1 -1为一直阻塞
        if(ret<0) perror("poll error");
    
        if(fds.revents & POLLIN){//有数据
            struct can_frame frame;
            int ret=read(sockfd,&frame,sizeof( struct can_frame));//这里也有阻塞效果
            if(ret==-1) perror("read error");
            if(frame.can_id&CAN_ERR_FLAG) printf("Error frame!!\r\n");
            if(frame.can_id&CAN_SFF_MASK)   
                printf("标准帧 <0x%03x>\r\n",frame.can_id&CAN_SFF_MASK);
            printf("len of frame is %d\r\n",frame.can_dlc);
    
            for(int i = 0; i < frame.can_dlc; i++)
                printf("%02x ",frame.data[i]);
            printf("\r\n");
        }
    }
}
int main (void) 
{
    can0_init();
    pthread_t tid_can;
    pthread_create(&tid_can, NULL, (void *)thread_can_start, NULL);//创建can接收线程 

    while(1){

/**************************数据接受***************************************/
     sleep(1);
     struct can_frame frame;
    frame.can_id =0x123;//id of frame
    frame.can_dlc=3;//len of frame is 3 bytes
    frame.data[0]=0xA0;//data
    frame.data[1]=0xB0;
    frame.data[2]=0xC0;

    int ret=write(sockfd,&frame,sizeof(frame));//write frame
    if(ret!=sizeof(frame)) perror("write error");

    }   

}
//编译使用:gcc -o can_poll can_poll.c -lpthread

运行:程序往can发送A0 B0 C0 ,然后程序收到can数据到终端里。如图,没有问题。
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值