linux下socketcan代码编写


linux下socketcan代码编写



1.相关头文件

#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>

2.can设备的初始化

/**
 * function  :CAN设备初始化
 * parameter :void
 * return    :s (socketcan套接字)
 * */
int Can_Init(void)
{
    int s,ret;
    struct sockaddr_can addr;
    struct ifreq ifr;
    system("sudo ip link set can0 type can bitrate 100000");
    system("sudo ifconfig can0 up");
    printf("this is a can send demo\r\n");
//1.Create socket
    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if (s < 0) 
    {
        perror("socket PF_CAN failed");
        return -1;
    }
//2.Specify can0 device
    strcpy(ifr.ifr_name, "can0");
    ret = ioctl(s, SIOCGIFINDEX, &ifr);
    if (ret < 0) 
    {
        perror("ioctl failed");
        return -1;
    }
//3.Bind the socket to can0
    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;
    ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
    if (ret < 0) 
    {
        perror("bind failed");
        return -1;
    }
//4.Disable filtering rules, do not receive packets, only send
    setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);  
//5.return socket
    return s;
}


3.关闭can设备

/**
 * function  :关闭CAN设备
 * parameter :socketcan
 * return    :无
 * */
void Can_Close(int socketcan)
{
    close(socketcan);
    system("sudo ifconfig can0 down");
}


4.can发送函数

/**
 * function  :CAN发送函数
 * parameter :int socketcan,unsigned char *msg,unsigned char len,unsigned int id
 * return    :-1(发送失败) 
 * */
int CAN1_Send_Msg(int socketcan,unsigned char *msg,unsigned char len,unsigned int id)
{
     int i,nbytes;
     struct can_frame frame;
     frame.can_id  = id;
     frame.can_dlc = len;
     for(i=0;i<len;i++)
     frame.data[i]=msg[i];	
//Send message
     nbytes = write(socketcan, &frame, sizeof(frame)); 
     if(nbytes != sizeof(frame))
     {
        Can_Close(socketcan);
        return -1;
     }	
     return nbytes;	
}

5.can接收函数

/**
 * function  :CAN接收函数(只接收了一次,因为有个 return frame.can_dlc)
 * parameter :int socketcan,unsigned char *msg,unsigned char len,unsigned int id
 * return    :-1(接受失败)
 * */

unsigned char CAN_Receive_Msg(int socketcan,unsigned char *buf,unsigned int id)
{
//Define receive rules
	struct can_filter rfilter[1];
	rfilter[0].can_id = id;
	rfilter[0].can_mask = CAN_SFF_MASK;
	setsockopt(socketcan, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));

	struct can_frame frame;
	int nbytes=0; 
	int i;    

	while(1)
    {
        nbytes = read(socketcan, &frame, sizeof(frame));
        if(nbytes > 0)
        {
	        for(i=0;i<frame.can_dlc;i++)
            {			
		        buf[i]=frame.data[i];
		        printf("data[%d] = %d\r\n", i, frame.data[i]);
		    } 
		    return frame.can_dlc;
	    }
	    if(nbytes == -1)
        {
	        Can_Close(socketcan);
	        return -1;
	    }     
    }	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值