ESP32学习(外设-UART)
/**
* @brief UART configuration parameters for uart_param_config function
*/
typedef struct {
int baud_rate; /*!< UART baud rate*/
uart_word_length_t data_bits; /*!< UART byte size*/
uart_parity_t parity; /*!< UART parity mode*/
uart_stop_bits_t stop_bits; /*!< UART stop bits*/
uart_hw_flowcontrol_t flow_ctrl; /*!< UART HW flow control mode (cts/rts)*/
uint8_t rx_flow_ctrl_thresh; /*!< UART HW RTS threshold*/
union {
uart_sclk_t source_clk; /*!< UART source clock selection */
bool use_ref_tick __attribute__((deprecated)); /*!< Deprecated method to select ref tick clock source, set source_clk field instead */
};
} uart_config_t;
ESP32总共有三个串口分别是UART_NUM0,UART_NUM1,UART_NUM2。串口0作为软件日志打印接口,其他串口可以为我们所用,比较灵活的是
所有串口的IO都可以自由设置。上面结构体是串口配置结构体包含了串口的波特率,数据长度,奇偶检验,停止位,硬件流控制,还有时钟源。
esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, QueueHandle_t* uart_queue, int intr_alloc_flags);
串口驱动安装函数,参数有串口号,接收缓存大小,发送缓存大小,队列大小,队列句柄,中断标志位
uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config);
串口参数配置函数,参数有串口号,以及串口参数配置结构体
uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num)
串口引脚设置,可以设置串口4个引脚,一般设置TX RX即可。
int uart_read_bytes(uart_port_t uart_num, void *buf, uint32_t length, TickType_t ticks_to_wait)
int uart_write_bytes(uart_port_t uart_num, const void *src, size_t size)
第一个函数是读函数,参数有串口号,缓存地址,缓存长度,等待时间
第二个函数是写函数,也就是发送函数,参数有串口号,数据源首地址,发送数据长度
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)