基本信息
源码名称:树莓派CAN通讯(c语言)
源码大小:2.10KB
文件格式:.c
开发语言:C/C++
更新时间:2020-03-07
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
#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 ret;
int s, nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
memset(&frame, 0, sizeof(struct can_frame));
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 <指定can0设备>
strcpy(ifr.ifr_name, "can0");
ret = ioctl(s, SIOCGIFINDEX, &ifr);
if (ret < 0) {
perror("ioctl failed");
return 1;
}
//3.Bind the socket to can0 <将接口绑定到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.Set send data<设置发送数据>
frame.can_id = 0x123;
frame.can_dlc = 8;
frame.data[0] = 1;
frame.data[1] = 2;
frame.data[2] = 3;
frame.data[3] = 4;
frame.data[4] = 5;
frame.data[5] = 6;
frame.data[6] = 7;
frame.data[7] = 8;
printf("can_id = 0x%X\r\n", frame.can_id);
printf("can_dlc = %d\r\n", frame.can_dlc);
int i = 0;
for(i = 0; i < 8; i )
printf("data[%d] = %d\r\n", i, frame.data[i]);
//6.Send message<发送消息>
nbytes = write(s, &frame, sizeof(frame));
if(nbytes != sizeof(frame)) {
printf("Send Error frame[0]!\r\n");
system("sudo ifconfig can0 down");
}
//7.Close the socket and can0<关闭接口和can0>
close(s);
system("sudo ifconfig can0 down");
return 0;
}