sudo apt-get update
sudo apt-get install can-utils libsocketcan-dev
can數(shù)據(jù)發(fā)送這個采用來自網(wǎng)上的一段代碼進行測試:文章來源:http://www.zghlxwxcb.cn/news/detail-677614.html
can_send.c代碼內(nèi)容如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-677614.html
/* 1. 報文發(fā)送程序 */
#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 s, nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame[2] = {
{0}};
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);//創(chuàng)建套接字
strcpy(ifr.ifr_name, "can0" );
ioctl(s, SIOCGIFINDEX, &ifr); //指定 can0 設(shè)備
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind(s, (struct sockaddr *)&addr, sizeof(addr));//將套接字與 can0 綁定
//禁用過濾規(guī)則,本進程不接收報文,只負責發(fā)送
setsockopt(s, SOL_CAN_RAW, CAN_RA
到了這里,關(guān)于ubuntu can應用開發(fā)環(huán)境搭建指南的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!