Linux UART通信实战入门教程

linux uart demo

时间:2024-11-30 02:49


探索Linux UART通信:构建高效串口通信Demo 在嵌入式系统和物联网(IoT)领域,UART(通用异步收发传输器)作为一种简单而高效的串行通信协议,扮演着至关重要的角色

    它不仅能够实现设备间的低速数据传输,还因其低硬件要求和广泛的兼容性,成为连接微控制器、传感器、计算机等多种设备的桥梁

    本文将深入探讨如何在Linux环境下构建一个UART通信Demo,通过实践展示UART通信的基本原理、配置步骤以及代码实现,旨在帮助读者掌握这一基础而强大的通信技术

     一、UART通信基础 UART通信基于异步串行传输机制,通过两根线(TX发送、RX接收)实现全双工通信

    其核心特点包括: - 异步性:不需要时钟信号同步,通过起始位、数据位、校验位和停止位来界定数据帧

     - 灵活性:波特率(数据传输速率)、数据位长度、停止位数量及校验方式均可配置

     - 低成本:硬件实现简单,仅需少量引脚即可建立通信链路

     在Linux系统中,UART设备通常被映射为文件系统中的设备文件(如`/dev/ttyS0`、`/dev/ttyUSB0`等),这使得通过标准文件I/O操作即可实现数据的读写

     二、Linux UART配置 在Linux环境下配置UART通信,主要涉及以下几个步骤: 1.识别UART设备: 使用`dmesg`命令查看系统日志,识别连接的UART设备及其对应的设备文件

     bash dmesg | grep tty 2.设置串口参数: 通过`stty`命令配置串口参数,包括波特率、数据位、停止位和校验方式

    例如,将`/dev/ttyS0`设置为9600波特率、8数据位、无校验、1停止位: bash stty -F /dev/ttyS0 9600 cs8 -cstopb -parenb 3.检查并调整设备权限: 确保当前用户有权访问UART设备文件

    必要时,使用`chmod`或`chown`命令调整权限

     bash sudo chmod 666 /dev/ttyS0 三、构建UART通信Demo 接下来,我们将通过C语言编写一个简单的UART通信Demo,包括发送和接收数据的功能

     3.1 发送数据 首先,编写发送数据的函数

    该函数将打开UART设备文件,配置必要的参数,然后发送指定长度的数据

     include include include include include include int uart_send(constchar device, const char data, size_tlength){ int fd =open(device,O_WRONLY |O_NOCTTY |O_SYNC); if(fd < { perror(open); return -1; } struct termios tty; memset(&tty, 0, sizeof tty); if(tcgetattr(fd, &tty) != 0) { perror(tcgetattr); close(fd); return -1; } cfsetospeed(&tty, B9600); cfsetispeed(&tty, B9600); tty.c_cflag= (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars tty.c_iflag &= ~IGNBRK; // disable break processing tty.c_lflag = 0; // no signaling chars, no echo, // no canonical processing tty.c_oflag = 0; // no remapping, no delays tty.c_cc【VMIN】 = 0; // read doesnt block tty.c_cc【VTIME】 = 5; // 0.5 seconds read timeout tty.c_iflag &=~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl tty.c_cflag|= (CLOCAL | CREAD); // ignore modem controls, // enable reading tty.c_cflag&= ~(PARENB | PARODD); // shut off parity tty.c_cflag |= 0; tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CRTSCTS; if(tcsetattr(fd, TCSANOW, &tty) != 0) { perror(tcsetattr); close(fd); return -1; } ssize_t written =write(fd, data,length); if(written < { perror(write); close(fd); return -1; } close(fd); return 0; } 3.2 接收数据 接着,编写接收数据的函数

    该函数同样打开UART设备文件,配置参数,并读取指定长度的数据

     int uart_receive(constchar device, char buffer, size_t length) { int fd =open(device,O_RDONLY |O_NOCTTY |O_SYNC); if(fd < { perror(open); return -1; } struct termios tty; memset(&tty, 0, sizeof tty); if(tcgetattr(fd, &tty) != 0) { perror(tcgetattr); close(fd); return -1; } cfsetospeed(&tty, B9600); cfsetispeed(&tty, B9600); tty.c_cflag= (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars tty.c_iflag &= ~IGNBRK; // disable break processing tty.c_lflag = 0; // no signaling chars, no echo, // no canonical processing tty.c_oflag = 0; // no remapping, no delays tty.c_cc【VMIN】 = 1; // read blocks tty.c_cc【VTIME】 = 5; // 0.5 seconds read timeout tty.c_iflag &=~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl tty.c_cflag|= (CLOCAL | CREAD); // ignore modem controls, // enable reading tty.c_cflag&= ~(PARENB | PARODD); // shut off parity tty.c_cflag |= 0;