// RS-485-SP3485EEN.h #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "nvs_flash.h" #include "driver/uart.h" #include "freertos/queue.h" #include "esp_log.h" #include "driver/gpio.h" typedef struct { uart_port_t uart_num; gpio_num_t tx_pin; gpio_num_t rx_pin; gpio_num_t de_re_pin; char name[20]; } rs485_channel_t; /** * @brief RS-485-SP3485EEN.h * DE和RE引脚接在一起使用,默认是下拉,通过控制DE_RE引脚的高低电平来控制发送和接收 * DE_RE = HIGH 发送数据,接收器使能,低电平有效。低电平时使能接收器,高电平时禁用接收器 * DE_RE = LOW 接收数据,驱动器(发送器)使能,高电平有效。高电平时使能发送器,低电平时禁用发送器 * * 所以默认是接收状态,发送数据时需要先将DE_RE引脚拉高,发送完数据后再拉低 */ // 第一个模块的引脚配置 #define RS_485_SP3485EEN_UART_PORT (UART_NUM_0) #define RS_485_SP3485EEN_RO_PIN (GPIO_NUM_41) // 接收器输出。将总线上的差分信号转换为TTL电平,输出给 单片机RX #define RS_485_SP3485EEN_DE_RE_PIN (GPIO_NUM_42) // 数据使能,接收器使能。控制发送器和接收器是否工作 #define RS_485_SP3485EEN_DI_PIN (GPIO_NUM_44) // 驱动器输入。单片机TX 发送的TTL电平信号,转换为总线上的差分信号 // 第二个模块的引脚配置 #define RS_485_SP3485EEN_2_UART_PORT (UART_NUM_2) #define RS_485_SP3485EEN_2_RO_PIN (GPIO_NUM_43) // 接收器输出。将总线上的差分信号转换为TTL电平,输出给 单片机RX #define RS_485_SP3485EEN_2_DE_RE_PIN (GPIO_NUM_2) // 数据使能,接收器使能。控制发送器和接收器是否工作 #define RS_485_SP3485EEN_2_DI_PIN (GPIO_NUM_1) // 驱动器输入。单片机TX 发送的TTL电平信号,转换为总线上的差分信号 // 公共配置 #define BUF_SIZE 256 #define BAUD_RATE 115200 // 通道数量常量 #define RS485_NUM_CHANNELS 2 // ---------------------------- // 通道数组 // ---------------------------- extern rs485_channel_t rs485_channels[]; #define NUM_CHANNELS RS485_NUM_CHANNELS void RS_485_init(uart_port_t uart_num, int tx_pin, int rx_pin, int de_re_pin); void init_specific_rs485_channel(int channel_num); // 新增函数声明 void init_all_rs485_channels(void); void rs485_send(uart_port_t uart_num, const uint8_t *data, size_t len); int rs485_receive(uart_port_t uart_num, uint8_t *buffer, size_t buf_size, uint32_t timeout_ms); BaseType_t start_rs485_rx_task_for_channel(int channel_num, UBaseType_t priority, uint32_t stack_size);