日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux CAN通信

發布時間:2023/12/20 linux 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 #endiftypedef enum {CAN_PORT_0 = 0, // can0CAN_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); }


總結

以上是生活随笔為你收集整理的Linux CAN通信的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。