#include #include #include #include #include #include #include #include #include #include #include int sockfd_can0; // 全局变量,用于线程间通信 static int can_sockfd = -1; static pthread_t can_thread; static volatile int can_thread_running = 1; // 线程运行标志 int socket_can_init(char* can_dev) { int sockfd_can; struct ifreq ifr; struct sockaddr_can addr; sockfd_can = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (sockfd_can < 0) { perror("Error while opening socket"); return -1; } strcpy(ifr.ifr_name, can_dev); ioctl(sockfd_can, SIOCGIFINDEX, &ifr); // 绑定 CAN 套接字到接口 addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; if (bind(sockfd_can, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("Error in socket bind"); return -2; } return sockfd_can; } int socket_can_send(int sockfd,uint32_t can_id,uint8_t data[8]) { struct can_frame frame; // 准备发送的 CAN 帧 frame.can_id = can_id; // 11位标准帧ID frame.can_dlc = 8; // 数据长度为8字节 // 使用循环赋值 CAN 数据帧 for (int i = 0; i < frame.can_dlc; i++) { frame.data[i] = data[i]; // 将数据从0x01到0x08依次赋值 } if (write(sockfd, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) { perror("Write error"); return -3; } return 0; } /** * 从 CAN 套接字中读取数据 * @param sockfd CAN 套接字文件描述符 * @param data 存储接收到的 CAN 数据 * @return 成功返回读取的字节数,失败返回 -1 */ int socket_can_receive(int sockfd, uint8_t *data) { struct can_frame frame; int nbytes; // 读取 CAN 数据帧 nbytes = read(sockfd, &frame, sizeof(struct can_frame)); if (nbytes < 0) { perror("Error reading from CAN socket"); return -1; } // 检查读取的字节数是否正确 if (nbytes != sizeof(struct can_frame)) { fprintf(stderr, "Incomplete CAN frame read\n"); return -1; } // 将 CAN 数据帧的内容复制到输出缓冲区 memcpy(data, frame.data, frame.can_dlc); // frame.can_dlc 是数据长度 return frame.can_dlc; // 返回实际读取的数据长度 } int socket_can_close(int sockfd) { // 关闭套接字 close(sockfd); return -1; } #include // 添加 select 头文件 void *can_receive_task(void *arg) { int sockfd = *(int *)arg; // 从参数中获取 sockfd struct can_frame frame; fd_set read_fds; while (can_thread_running) { // 初始化 fd_set FD_ZERO(&read_fds); FD_SET(sockfd, &read_fds); // 设置超时时间(100ms) struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 100000; // 等待 CAN 数据 int ret = select(sockfd + 1, &read_fds, NULL, NULL, &timeout); if (ret < 0) { perror("Error in select"); break; } else if (ret == 0) { // 超时,继续循环 continue; } // 检查是否有数据可读 if (FD_ISSET(sockfd, &read_fds)) { int nbytes = read(sockfd, &frame, sizeof(struct can_frame)); if (nbytes < 0) { perror("Error reading from CAN socket"); break; } // 检查读取的字节数是否正确 if (nbytes != sizeof(struct can_frame)) { fprintf(stderr, "Incomplete CAN frame read\n"); continue; } // 打印接收到的 CAN 数据 printf("Received CAN frame: ID=0x%X, Data=", frame.can_id); for (int i = 0; i < frame.can_dlc; i++) { printf("%02X ", frame.data[i]); } printf("\n"); } } // 线程退出 printf("CAN receive task stopped.\n"); return NULL; } int start_can_receive_task(int sockfd) { if(sockfd > 0) { can_thread_running = 1; } // 创建线程 if (pthread_create(&can_thread, NULL, can_receive_task, &sockfd) != 0) { perror("Error creating CAN receive thread"); return -1; } printf("CAN receive task started.\n"); return 0; } /** * 停止 CAN 数据读取任务 */ void stop_can_receive_task() { // 设置线程运行标志为 0 can_thread_running = 0; // 等待线程退出 pthread_join(can_thread, NULL); printf("CAN receive task stopped.\n"); } int test() { int sockfd; struct sockaddr_can addr; struct ifreq ifr; struct can_frame frame; // 创建 CAN 套接字 sockfd = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (sockfd < 0) { perror("Error while opening socket"); return -1; } // 指定 CAN 接口(假设为 can0) strcpy(ifr.ifr_name, "can0"); ioctl(sockfd, SIOCGIFINDEX, &ifr); // 绑定 CAN 套接字到接口 addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("Error in socket bind"); return -2; } // 准备发送的 CAN 帧 frame.can_id = 0x123; // 11位标准帧ID frame.can_dlc = 8; // 数据长度为8字节 // 使用循环赋值 CAN 数据帧 for (int i = 0; i < frame.can_dlc; i++) { frame.data[i] = i + 1; // 将数据从0x01到0x08依次赋值 } // 发送 CAN 帧 for(int i=0;i<3;i++) { if (write(sockfd, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) { perror("Write error"); return -3; } } printf("CAN message sent on can0\n"); // 关闭套接字 close(sockfd); return 0; }