当前位置: 首页 > news >正文

使用 select 进行 UART 通信的注意事项

文章目录

    • 引言
    • UART 通信中的 `select` 函数
    • `select` 函数的工作原理
    • 使用 `select` 进行 UART 通信的注意事项
    • 示例代码

引言

UART(Universal Asynchronous Receiver/Transmitter)是一种用于异步串行通信的硬件协议,常用于计算机和外设之间的数据交换。在嵌入式系统中,UART 通信非常常见,用于连接传感器、微控制器、调制解调器等设备。为了实现高效的 UART 通信,通常需要使用非阻塞式 I/O 操作,这时候 select 函数就派上用场了。

UART 通信中的 select 函数

select 函数允许我们同时监控多个文件描述符,并在这些文件描述符变为可读、可写或发生错误时通知程序。这样可以避免程序在等待 I/O 操作时被阻塞,提高系统的响应速度和效率。

select 函数的工作原理

  • 监控文件描述符select 接受一组文件描述符,并监控这些文件描述符的状态。
  • 阻塞等待:可以设置阻塞等待或非阻塞等待,直到文件描述符变为可读、可写或发生错误,或者达到超时时间。
  • 返回事件select 返回后,程序可以检查哪些文件描述符变为可读、可写或发生错误,并进行相应的处理。

使用 select 进行 UART 通信的注意事项

在使用 select 进行 UART 通信时,有几个关键点需要注意:

  1. 正确初始化文件描述符集

每次调用 select 之前,需要重新初始化文件描述符集(fd_set),因为 select 调用会修改这个集合:

FD_ZERO(&rfds);
FD_SET(fd, &rfds);
  1. 设置超时值

在每次调用 select 之前,需要设置超时值(struct timeval)。同样,select 会修改这个结构体,因此每次调用前都需要重新设置:

struct timeval tv;
tv.tv_sec = timeout_us / 1000000;
tv.tv_usec = timeout_us % 1000000;
  1. 处理 select 的返回值

检查 select 的返回值以确定是否有文件描述符变为可读、可写或出错:

int ret = select(fd + 1, &rfds, NULL, NULL, &tv);
if (ret == -1) {// Handle error
} else if (ret == 0) {// Handle timeout
} else {if (FD_ISSET(fd, &rfds)) {// Handle readable data}
}
  1. 避免文件描述符泄漏

确保在程序结束或不再需要文件描述符时关闭它们,防止资源泄漏:

close(fd);
  1. 多线程环境下的文件描述符使用

如果在多线程环境下使用文件描述符,需要确保对文件描述符的操作是线程安全的。使用互斥锁(mutex)保护对文件描述符的操作,避免竞态条件。

  1. 检查文件描述符的有效性

在调用 select 之前检查文件描述符是否有效,确保文件描述符在整个生命周期中是有效的:

int check_fd_valid(int fd) {return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}
  1. 处理边界情况

处理 read 返回 0 的情况,这通常表示文件结束或没有数据可用:

ssize_t bytesRead = read(fd, buff, len);
if (bytesRead == -1) {// Handle read error
} else if (bytesRead == 0) {// Handle EOF or no data
}

示例代码

以下是综合了上述注意事项的改进代码示例:

#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include <stdarg.h>
#include <fcntl.h>
#include <pthread.h>void ota_info(const char *fmt, ...) {va_list args;va_start(args, fmt);vprintf(fmt, args);printf("\n");va_end(args);
}int check_fd_valid(int fd) {return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}int uart_read(int fd, uint8_t *buff, int len, int timeout_us)
{int ret;struct timeval tv;fd_set rfds;memset(buff, 0, len);// Debug log to print fd value before selectota_info("uart_read: fd=%d, timeout_us=%d, thread_id=%ld", fd, timeout_us, pthread_self());// Check if fd is validif (!check_fd_valid(fd)) {ota_info("Invalid file descriptor: %d", fd);return -1;}FD_ZERO(&rfds);FD_SET(fd, &rfds);memset(&tv, 0, sizeof(tv));tv.tv_sec = timeout_us / 1000000;tv.tv_usec = timeout_us % 1000000;ret = select(fd + 1, &rfds, NULL, NULL, &tv);if (ret == -1) {// Select itself failedota_info("select() failed. ret %d, errno %d, %m", ret, errno);return -1;} else if (ret == 0) {// Timeout occurred without any file descriptor becoming readyota_info("select() timed out.");return 0; // Indicate timeout}if (FD_ISSET(fd, &rfds)) {ssize_t bytesRead = read(fd, buff, len);if (bytesRead == -1) {// Error during readota_info("read() failed. errno %d, %m", errno);return -1;} else if (bytesRead == 0) {// EOF reached or no data availableota_info("read() returned 0, no data available or EOF reached.");return 0;}// Return actual bytes readota_info("read() succeeded. bytesRead=%ld, thread_id=%ld", bytesRead, pthread_self());return bytesRead;} else {// This branch should not be reachable given the checks aboveota_info("select() returned with no file descriptor ready.");return 0;}
}int main() {// Example usageint fd = 0; // Example file descriptor, should be initialized properlyuint8_t buffer[256];int len = 256;int timeout_us = 5000000; // 5 secondsint result = uart_read(fd, buffer, len, timeout_us);if (result > 0) {printf("Read %d bytes\n", result);} else if (result == 0) {printf("Timeout or no data available\n");} else {printf("Error occurred\n");}return 0;
}

通过遵循这些注意事项,可以确保在使用 select 函数进行 UART 通信时,代码更加健壮和可靠,减少出现难以复现的错误的可能性。希望这篇博客能帮助你在项目中更好地使用 select 函数进行 UART 通信。

相关文章:

  • godot所有2D节点介绍
  • 【C语言】--- 常见调试信息预处理器宏
  • 全面解析:C# 委托的实质性应用与优势
  • windows11子系统Ubuntu 22.04.4子安装图形化界面
  • 在Linux中如何解决程序崩溃的问题
  • linux高级编程(1)
  • 华为Atlas NPU ffmpeg 编译安装
  • cuda与cudnn下载(tensorflow-gpu)
  • 爬虫经典案例之爬取豆瓣电影Top250(方法一)
  • 学习前台开发主要掌握的技能
  • SpringCloud和K8s的区别
  • python从入门到精通4:变量赋值
  • Linux开发讲课7---Linux sysfs文件系统
  • 【设计模式之迭代器模式 -- C++】
  • web安全渗透测试十大常规项(一):web渗透测试之PHP反序列化
  • 【翻译】babel对TC39装饰器草案的实现
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • Javascript基础之Array数组API
  • Linux下的乱码问题
  • Mocha测试初探
  • MQ框架的比较
  • spark本地环境的搭建到运行第一个spark程序
  • webpack+react项目初体验——记录我的webpack环境配置
  • Zepto.js源码学习之二
  • 不用申请服务号就可以开发微信支付/支付宝/QQ钱包支付!附:直接可用的代码+demo...
  • 从tcpdump抓包看TCP/IP协议
  • 前端_面试
  • 强力优化Rancher k8s中国区的使用体验
  • 我这样减少了26.5M Java内存!
  • 线上 python http server profile 实践
  • 用Canvas画一棵二叉树
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • 做一名精致的JavaScripter 01:JavaScript简介
  • NLPIR智能语义技术让大数据挖掘更简单
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (代码示例)使用setTimeout来延迟加载JS脚本文件
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (算法)Travel Information Center
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • (原创)可支持最大高度的NestedScrollView
  • (转载)Google Chrome调试JS
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET C# 使用 iText 生成PDF
  • .net core 调用c dll_用C++生成一个简单的DLL文件VS2008
  • .NET gRPC 和RESTful简单对比
  • .NET 服务 ServiceController
  • .NET 使用配置文件
  • .net6+aspose.words导出word并转pdf
  • /etc/X11/xorg.conf 文件被误改后进不了图形化界面
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • [ Linux ] git工具的基本使用(仓库的构建,提交)
  • [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945