RK3568-c语言socketcan收发程序

发布于:2025-06-27 ⋅ 阅读:(17) ⋅ 点赞:(0)

socketcan无法配置波特率

ip link set can0 up type can bitrate 500000
ip link set can1 up type can bitrate 500000

socketcan发送程序代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>

int main(void)
{
	struct ifreq ifr = {0};				// 配置或获取网络接口信息,结合ioctl与网络接口交互
    struct sockaddr_can can_addr = {0};	// 专门为 CAN 网络协议定义的套接字地址结构体
    struct can_frame frame = {0};		// CAN数据帧
    int sockfd = -1;
    int ret;
    
    /* 打开套接字,协议族/地址族: CAN 总线协议、套接字类型:原始套接字、协议:CAN */
    sockfd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if(0 > sockfd) 
    {
        perror("socket error");
        exit(EXIT_FAILURE);
 	}
    
     /* 指定 can0 设备,获取 can0 接口的索引值 */
     strcpy(ifr.ifr_name, "can0");
     ioctl(sockfd, SIOCGIFINDEX, &ifr);
     /* 设置 CAN 地址族 */
     can_addr.can_family = AF_CAN;
     can_addr.can_ifindex = ifr.ifr_ifindex;
    
     /* 将 can0 与套接字进行绑定 */
     ret = bind(sockfd, (struct sockaddr *)&can_addr, sizeof(can_addr));
     if (0 > ret) 
     {
         perror("bind error");
         close(sockfd);
         exit(EXIT_FAILURE);
     }
    
     /* 设置过滤规则:不接受任何报文、仅发送数据 */
     setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
    
     /* 发送数据 */
     frame.data[0] = 0xA0;
     frame.data[1] = 0xB0;
     frame.data[2] = 0xC0;
     frame.data[3] = 0xD0;
     frame.data[4] = 0xE0;
     frame.data[5] = 0xF0;
     frame.can_dlc = 6; //一次发送 6 个字节数据
     frame.can_id = 0x123;//帧 ID 为 0x123,标准帧
     for ( ; ; ) 
     {
         ret = write(sockfd, &frame, sizeof(frame)); //发送数据
         if(sizeof(frame) != ret) 
         { 
             //如果 ret 不等于帧长度,就说明发送失败
         	perror("write error");
         	goto out;
     	 }
     	 sleep(1); //一秒钟发送一次
     }
    
    out:
         /* 关闭套接字 */
         close(sockfd);
         exit(EXIT_SUCCESS);
}

socketcan接收程序代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>

int main(void)
{
     struct ifreq ifr = {0};
     struct sockaddr_can can_addr = {0};
     struct can_frame frame = {0};
     int sockfd = -1;
     int i;
     int ret;
     
    /* 打开套接字 */
     sockfd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
     if(0 > sockfd) 
     {
         perror("socket error");
         exit(EXIT_FAILURE);
     }
     
     /* 指定 can1 设备 */
     strcpy(ifr.ifr_name, "can1");
     ioctl(sockfd, SIOCGIFINDEX, &ifr);
     can_addr.can_family = AF_CAN;
     can_addr.can_ifindex = ifr.ifr_ifindex;
      
     /* 将 can1 与套接字进行绑定 */
     ret = bind(sockfd, (struct sockaddr *)&can_addr, sizeof(can_addr));
     if (0 > ret) 
     {
         perror("bind error");
         close(sockfd);
         exit(EXIT_FAILURE);
     }
    
     /* 设置过滤规则 */
     //setsockopt(sockfd, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
    
     /* 接收数据 */
     for ( ; ; ) 
     {
         if (0 > read(sockfd, &frame, sizeof(struct can_frame))) 
         {
             perror("read error");
             break;
     	 }
         
         /* 校验是否接收到错误帧 */
         if (frame.can_id & CAN_ERR_FLAG) 
         {
             printf("Error frame!\n");
             break;
         }
     
         /* 校验帧格式 */
         if (frame.can_id & CAN_EFF_FLAG) 
         	printf("扩展帧 <0x%08x> ", frame.can_id & CAN_EFF_MASK);	//扩展帧
         else 
         	printf("标准帧 <0x%03x> ", frame.can_id & CAN_SFF_MASK);	//标准帧
         
         /* 校验帧类型:数据帧还是远程帧 */
         if (frame.can_id & CAN_RTR_FLAG) 
         {
             printf("remote request\n");
             continue;
         }
         /* 打印数据长度 */
         printf("[%d] ", frame.can_dlc);
         
         /* 打印数据 */
         for (i = 0; i < frame.can_dlc; i++)
         	printf("%02x ", frame.data[i]);
         
         printf("\n");
      }
    
     /* 关闭套接字 */
     close(sockfd);
     exit(EXIT_SUCCESS);
}

网站公告

今日签到

点亮在社区的每一天
去签到