Linux CAN通信

Linux CAN通信


        实现了Linux下的CAN通信——初始化,发两个送和接收(采用队列形式),使用两个线程,还有一个超时响应目前未写。接收部分使用select实现。


#ifndef _CAN_H_
#define _CAN_H_

#include <stdio.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/socket.h>
#include <linux/can.h>
#include <linux/can/error.h>
#include <linux/can/raw.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include "can_queue.h"
#include "type.h"

#ifndef AF_CAN
#define AF_CAN 29
#endif
#ifndef PF_CAN
#define PF_CAN AF_CAN
#endif

typedef enum
{
  CAN_PORT_0 = 0, // can0
  CAN_PORT_1,     // can1
}can_port_t ;

typedef struct
{
  char *name;
  int fd;
  fd_set fdsr;

  pthread_t send_thread;
  pthread_t recv_thread;
  pthread_t time_thread;  

  can_queue_t *send_queue; // 接受和发送的队列
  can_queue_t *recv_queue;
} can_t;

void *CanInit(int arg);

#endif /* _CAN_H_ */
#include "can.h"

static can_t *can_init(int name)
{
  int ret;
  struct sockaddr_can addr;
  struct ifreq ifr;
  struct can_filter rfilter[1];
  can_t *current = (can_t *)malloc(sizeof(can_t));

  current->fd = Socket(PF_CAN, SOCK_RAW, CAN_RAW);
  sprintf(ifr.ifr_name, "can%d", name);
  current->name = (char *)malloc(6);
  memset(current->name, 0, 6);
  sprintf(current->name, "can%d", name);

  ret = ioctl(current->fd, SIOCGIFINDEX, &ifr);

  if(ret < 0)
  {
    exit(0);
  }

  addr.can_family = AF_CAN;
  addr.can_ifindex = ifr.ifr_ifindex;
  Bind(current->fd, (struct sockaddr *)&addr, sizeof(addr));

  rfilter[0].can_id = 0x2;
  rfilter[0].can_mask = 0;
  Setsockopt(current->fd, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));

  return current;
}

static void *can_send_thread(void *arg)
{
  int ret;
  can_t *current = arg;
  can_frame_t frame;
  uint8_t read_ret = 0;

  while(1)
  {
    Write(current->fd, &frame, sizeof(frame));
    read_ret = current->send_queue->can_read(current->send_queue, &frame);
    if(CAN_OK == read_ret)
    {
      ret = Write(current->fd, &frame, sizeof(frame));
      usleep(1200);
    }
    usleep(100);
  }

  return NULL;
}

static void *can_recv_thread(void *arg)
{
  int ret, i;
  can_frame_t frame;
  struct timeval tv;
  fd_set rset;
  can_t *current = arg;

  while (1)
  {
    tv.tv_sec = 0;
    tv.tv_usec = 200;

    FD_ZERO(&rset);
    FD_SET(current->fd, &rset);
    ret = select(current->fd + 1, &rset, NULL, NULL, NULL);

    if (0 == ret)
    {
      return NULL;
    }

    ret = read(current->fd, &frame, sizeof(frame));

    if (ret < sizeof(frame))
    {
      return NULL;
    }

    if (current->recv_queue->can_write(current->recv_queue, &frame) == CAN_ERROR)
    {
     
    }
  }

  return NULL;
}

void *CanInit(int arg)
{
  can_t *current = can_init(arg);

  current->recv_queue = CanQueueInit(CAN_RECV_QUEUE_SIZE);
  current->send_queue = CanQueueInit(CAN_SEND_QUEUE_SIZE);

  pthread_create(&current->send_thread, NULL, can_send_thread, (void *)current);
  pthread_create(&current->recv_thread, NULL, can_recv_thread, (void *)current);
}



  • 2
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值