Linux CAN通信
来源:互联网 发布:淘宝运营浏览器插件 编辑:程序博客网 时间:2024/06/07 02:44
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#endiftypedef 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(¤t->send_thread, NULL, can_send_thread, (void *)current); pthread_create(¤t->recv_thread, NULL, can_recv_thread, (void *)current);}
阅读全文
0 0
- Linux CAN通信
- STM32 CAN通信问题
- CAN 通信引脚
- stm32的CAN通信
- STM32 CAN通信
- CAN FD安全通信
- CAN总线通信
- STM32F429 CAN通信不成功
- CAN通信详解
- stm32 CAN通信 TJA1040
- CAN通信机制
- stm32 can中断通信
- TX2 CAN通信配置
- Linux CAN
- linux can
- CAN总线通信原理分析
- CAN总线通信原理分析
- STM32F103C8开发CAN通信(四)
- Codeforces Round #385 (Div. 2) B
- 解决分布式定时任务执行多次问题
- log4j配置示例
- 对于crudrepository怎么解决update的问题
- LINUX--hadoop部署
- Linux CAN通信
- 第二章 模型构建与选择
- 仿照django的urls风格和模块化结构的flask项目(Django-Style URL Patterns for Flask)
- hibernate之一对多|多对多案例|关联映射(03)
- 什么是对象,为什么要面向对象,怎么才能面向对象?
- 如何利用charles迅速定位bug
- Android应用访问文本文件
- 跳表SkipList
- window下编译qt5.6.1含QWebengine模块开启MP4