AP05/uart_utils/uart_utils.h
2025-04-06 14:41:47 +08:00

117 lines
4.3 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef _UART_INIT_H_
#define _UART_INIT_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <termios.h>
typedef struct {
int uart_fd; //串口设备文件描述符
int saved;
struct termios option_back; //串口属性结构体
}uart_utils_t;
extern int uart_open(uart_utils_t *uart, char *dev_name);
extern void uart_print_attr(struct termios *options);
/*************************************************************
* 功能: 设置串口属性结构体
* 参数:
speed串口波特率可选2400、4800、9600、115200。可自己酌情修改程序添加支持
data_bits数据位宽可选5、6、7、8
stop_bits停止位可选1、2
check奇偶校验位可选'N'、'O'、'E';分别应着无奇偶校验、奇校验、偶校验)
flow_ctrl硬件流控制0为OFF1为ON
options串口属结构体
* 返回值: 串口属性结构体
**************************************************************/
extern struct termios *uart_set_attr(
int speed,
int data_bits,
int stop_bits,
int check,
int flow_ctrl,
struct termios *options);
/*************************************************************
* 功能: 设置回显
* 参数: 串口设备文件名
echo回显0为关1为开
* 返回值: 无
**************************************************************/
extern void uart_set_echo(uart_utils_t *uart, int echo);
/*************************************************************
* 功能: 设置阻塞
* 参数: 串口设备文件名
block阻塞0为不阻塞1为阻塞
* 返回值: 无
**************************************************************/
extern void uart_set_block(uart_utils_t *uart, int block);
/*************************************************************
* 功能: 串口初始化程序
* 参数: 串口设备
speed串口波特率可选2400、4800、9600、115200。可自己酌情修改程序添加支持
data_bits数据位宽可选5、6、7、8
stop_bits停止位可选1、2
check奇偶校验位可选'N'、'O'、'E';分别应着无奇偶校验、奇校验、偶校验)
flow_ctrl硬件流控制0为OFF1为ON
* 返回值: 串口设备文件描述符
**************************************************************/
extern int uart_init(uart_utils_t *uart,
int speed,
int data_bits,
int stop_bits,
int check,
int flow_ctrl);
/*************************************************************
* 功能: 串口反初始化程序
* 参数: 串口设备文件描述符
* 返回值: 无
**************************************************************/
extern void uart_uninit(uart_utils_t *uart);
/*************************************************************
* 功能: 串口发送字符串
* 参数: uart_fd串口设备文件描述符
str待发送的字符
* 返回值: 无
**************************************************************/
extern void uart_send_str(int uart_fd, char *str);
/*************************************************************
* 功能: 读一串至截止字符的字符串,在设定的时间内读不到数据则函数返回
* 参数: uart_fd串口设备文件描述符
buffer存放数据的内存的首地址
len存放数据的内存空间的大小
until截止字符
timeout_ms超时时间(单位ms)
* 返回值:
成功:实际读到的字符数
失败:-1
**************************************************************/
extern int uart_read_until_char(int uart_fd, char *buffer, int len, unsigned char until, int timeout_ms);
/*************************************************************
* 功能: 设定的时间内读取数据
* 参数: uart_fd串口设备文件描述符
buffer存放数据的内存的首地址
len存放数据的内存空间的大小
timeout_ms超时时间(单位ms)
* 返回值:
成功:实际读到的字符数
失败:-1
**************************************************************/
extern int uart_read_until_time(int uart_fd, char *buffer, int len, int timeout_first, int timeout_interval);
#ifdef __cplusplus
}
#endif
#endif