zoukankan      html  css  js  c++  java
  • QT 环境下开发socketCan接口程序

    使用头文件

    #include <QMainWindow>
    #include "QTimer"
    #include "QTime"
    #include "QMessageBox"
    #include <QSocketNotifier>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <sys/types.h>
    #include "stdio.h"
    #include "stdlib.h"
    #include "unistd.h"
    #include "fcntl.h"
    #include "sys/ioctl.h"
    #include "sys/stat.h"
    #include <net/if.h>

    #include <sys/ioctl.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <sys/uio.h>
    #include <linux/can.h>
    #include <linux/can/raw.h>
    #define PF_CAN 29

    定义相关变量

    struct sockaddr_can addr;
    struct ifreq ifr;
    struct can_frame frame;
    int canfd;

    接口初始化

    canfd=socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if(canfd==-1)
    {
    perror("socket");
    exit(1);
    }
    strcpy(ifr.ifr_name, "can0" );
    if(ioctl(canfd, SIOCGIFINDEX, &ifr))
    {
    qDebug("ioctl");

    }
    addr.can_family = PF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;
    if(bind(canfd, (struct sockaddr *)&addr, sizeof(addr))<0)
    {
    qDebug("bind");

    }

    发送数据

    memcpy(frame.data,buf,8);
    frame.can_dlc=8;
    frame.can_id=0x32;
    write(canfd, &frame, sizeof(struct can_frame));

    接收数据

    read(canfd, &frame, sizeof(struct can_frame));

  • 相关阅读:
    06
    05
    继承
    0713作业
    0712作业
    0711作业
    福彩双色球作业
    0709作业
    选择语句+循环语句作业
    0706作业
  • 原文地址:https://www.cnblogs.com/luxiaolai/p/3727943.html
Copyright © 2011-2022 走看看