嵌入式Linux下CAN接口socket发送调试

1、简介

最近在做嵌入式linux can通讯的调试,需要用到扩展帧,参照示例代码和linux库改为扩展帧格式发送。
参考链接 链接: https://blog.csdn.net/toradexsh/article/details/72639660 .

2、扩展帧与数据帧的不同

// linux/can.h
/*

  • Controller Area Network Identifier structure
  • bit 0-28 : CAN identifier (11/29 bit)
  • bit 29 : error message frame flag (0 = data frame, 1 = error message)
  • bit 30 : remote transmission request flag (1 = rtr frame)
  • bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
    */
    typedef __u32 canid_t;

#define CAN_SFF_ID_BITS 11
#define CAN_EFF_ID_BITS 29

标准帧的ID长度为11位,扩展帧的ID长度为29位,对于复杂的多点应用可以更灵活的设计优先级设置滤波器,帧ID是32位数据结构,想要发送扩展帧,可以根据以上的定义,对帧ID的bit31置1即可发送标准帧,置零则是标准帧,远程帧和错误帧同理,

3、附上测试代码

// 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 main(int argc, char *argv[])
{
	int s, nbytes;
	struct sockaddr_can addr;
	struct ifreq ifr;
	struct can_frame frame;
	struct can_filter rfilter[1];

	/* handle (optional) flags first */
	if (argc != 2)
	{
		fprintf(stderr, "Usage:   <can interface name> for sending\n");
		exit(1);
	}
	/* create socket */
	if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
	{
		perror("Create socket failed");
		exit(-1);
	}

	/* set up can interface */
	strcpy(ifr.ifr_name, argv[1]);
	printf("can port is %s\n", ifr.ifr_name);
	/* assign can device */
	ioctl(s, SIOCGIFINDEX, &ifr);
	addr.can_family = AF_CAN;
	addr.can_ifindex = ifr.ifr_ifindex;
	/* bind can device */
	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
	{
		perror("Bind can device failed\n");
		close(s);
		exit(-2);
	}

	/* configure sending */

	/* configure can_id and can data length */
	//frame.can_id = 0x1F;			 //标准帧
	frame.can_id = 0x1F | (1 << 31); //扩展帧
	frame.can_dlc = 8;
	printf("%s ID=%#x data length=%d\n", ifr.ifr_name, frame.can_id, frame.can_dlc);
	/* prepare data for sending: 0x11,0x22...0x88 */
	for (int i = 0; i < 8; i++)
	{
		frame.data[i] = ((i + 1) << 4) | (i + 1);
		printf("%#x ", frame.data[i]);
	}
	printf("Sent out\n");
	/* Sending data */
	if (write(s, &frame, sizeof(frame)) < 0)
	{
		perror("Send failed");
		close(s);
		exit(-4);
	}

	close(s);
	return 0;
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值