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

串口、终端应用程序 API termios

UART简介

串口全称为串行接口,也称为COM接口,串行接口指的是比特一位位顺序传输,通信线路简单。使用两根线就可以实现双向通信,一条为TX,一个为RX。串口通信距离远,但速度相对慢,是一种常用的工业接口。
UART全称为Universal Asynchronous Receiver/Trasmitter,也是异步串行接收器。
USART全称为Universal Synchronous/Asynchronous Receiver/Transmitter,也就是同步/异步串行收发器。相对于UART多了一个同步功能,在硬件上多出一条时钟线。USART可以用作UART。

通讯和通信格式

UART接口与外界通信时,至少用三根线:TXD(发送)、RXD(接收)、GND(地线)。
UART通信格式:
在这里插入图片描述

UART位含义
空闲位数据线在空闲状态的时候,为逻辑1,高电平,没有数据传输。
起始位当要传输数据时,先传输一个0,也就是将数据线拉低,表示开始传输数据。
数据位为实际传输的数据,数据位可选5~8位,一般为1个字节8位传输。低位(LSB)先传,高位最后传输。
奇偶校验位1启动奇偶校验,0不启动
停止位数据传输完成标志位,停止位的位数可选1或2位,常用1位
波特率波特率就是UART数据传输的速率,每秒传输的数据位数。

介绍

Linux 为上层用户做了一层封装,将这些 ioctl()操作封装成了一套标准的 API,我们就直接使用这一套标准 API 编写自己的串口应用程序。termios 函数描述了一个通用的终端接口,提供了控制异步通讯端口。

termios API

大纲

#include <termiso.h>
#include <unistd.h>int tcgetattr(int fd,struct termios *termios_p);
int tcsetattr(int fd,int potional_actions,struct termios *termios_p);
int tcsendbreak(int fd,int duration);
int tcdrain(int fd);
int tcflush(int fd, int queue_selector);
int tcflow(int fd, int action);
void cfmakeraw(struct termios *termios_p);
speed_t cfgetispeed(const struct termios *termios_p);
speed_t cfgetospeed(const struct termios *termios_p);
int cfsetispeed(struct termios *termios_p, speed_t speed);
int cfsetospeed(struct termios *termios_p, speed_t speed);
int cfsetspeed(struct termios *termios_p, speed_t speed);

termios 结构体
许多函数描述有一个参数termios_p,是一个指针指向了termios结构体。结构体至少包括了以下成员。

tcflag_t c_iflag; //输入模式
tcflag_t c_oflag; //输出模式
tcflag_t c_cflag; //控制模式
tcflag_t c_lflag; //本地模式
cc_t c_cc[NCCS];  //特定字符

这些值被分配到

http://www.lryc.cn/news/24556.html

相关文章:

  • 【服务器搭建】教程七:如何为自己的网站添加运行时间?
  • 【消息中间件】Apache Kafka 教程
  • ARM基础
  • Python排序 -- 内附蓝桥题:错误票据,奖学金
  • 容器化部署是什么意思?有什么优势?
  • 1.设计模式简介
  • 【算法题解】实现一个包含“正负数和括号”的基本计算器
  • 网站服务器如何防护攻击?网站服务器被挂马如何检测
  • JavaSE16-面向对象-接口
  • 安卓设备蓝牙键盘快捷键
  • Puppeteer项目结构梳理
  • (02)Unity HDRP Volume 详解
  • 拒绝B站邀约,从月薪3k到年薪47W,我的经验值得每一个测试人借鉴
  • 分享一种实用redis原子锁的方式
  • 【华为OD机试】 字符串解密(C++ Java JavaScript Python)
  • 金三银四,助力你的大厂梦,2023年软件测试经典面试真题(1)(共3篇)
  • 假如面试官要你手写一个promise
  • 【leetcode】寻找重复数
  • LeetCode 1247. Minimum Swaps to Make Strings Equal【数学,贪心,字符串】
  • pid控制加热算法,附代码仓库
  • 一文看懂预训练和自训练模型
  • (五十四)大白话索引的页存储物理结构,是如何用B+树来实现的?.md
  • 前端Vue代码风格指南
  • 「TCG 规范解读」基础设施架构和协议 (2)
  • NodeJs 中的 HTML 模板
  • 3.ffmpeg命令行环境搭建、ffmpeg命令行初步了解
  • Kubernetes初始化容器
  • leetcode: Swapping Nodes in a Linked List
  • Nydus 在约苗平台的容器镜像加速实践
  • 企业对不同形态CRM系统价格需求不同