现使用芯片stm32f407,通过串口3接收到一串数据,暂存到数组avail_data中,现需要写一个函数能通过串口1将avail_data储存的数据发出一条出来
时间: 2024-05-15 16:15:02 浏览: 67
以下是一个参考代码,可以根据具体情况进行修改:
```c
#include "stm32f4xx_hal.h"
#define UART3_RX_BUFFER_SIZE 128 // 串口3接收缓存大小
#define UART1_TX_BUFFER_SIZE 128 // 串口1发送缓存大小
UART_HandleTypeDef huart3;
UART_HandleTypeDef huart1;
uint8_t uart3_rx_buffer[UART3_RX_BUFFER_SIZE]; // 串口3接收缓存
uint8_t uart1_tx_buffer[UART1_TX_BUFFER_SIZE]; // 串口1发送缓存
uint16_t uart3_rx_index = 0; // 串口3接收缓存索引
uint16_t uart1_tx_index = 0; // 串口1发送缓存索引
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart == &huart3) // 如果是串口3接收中断
{
// 将接收到的数据存入缓存
uart3_rx_buffer[uart3_rx_index++] = huart3.Instance->DR;
if (uart3_rx_index >= UART3_RX_BUFFER_SIZE) // 如果缓存已满
{
uart3_rx_index = 0;
}
// 继续接收
HAL_UART_Receive_IT(&huart3, &uart3_rx_buffer[uart3_rx_index], 1);
}
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart == &huart1) // 如果是串口1发送中断
{
// 发送完一条数据后继续发送下一条数据
if (uart1_tx_index > 0)
{
HAL_UART_Transmit_IT(&huart1, &uart1_tx_buffer[--uart1_tx_index], 1);
}
}
}
void send_data_via_uart1(uint8_t *data, uint16_t length)
{
// 将数据存入发送缓存
for (int i = 0; i < length; i++)
{
uart1_tx_buffer[uart1_tx_index++] = data[i];
if (uart1_tx_index >= UART1_TX_BUFFER_SIZE) // 如果缓存已满
{
uart1_tx_index = UART1_TX_BUFFER_SIZE - 1;
break;
}
}
// 如果串口1当前没有在发送数据,则立即开始发送
if (huart1.gState == HAL_UART_STATE_READY)
{
HAL_UART_Transmit_IT(&huart1, &uart1_tx_buffer[--uart1_tx_index], 1);
}
}
int main(void)
{
HAL_Init();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
// 配置串口3的GPIO引脚
GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// 配置串口1的GPIO引脚
GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// 配置串口3
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart3);
// 配置串口1
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart1);
// 启动串口3接收中断
HAL_UART_Receive_IT(&huart3, &uart3_rx_buffer[uart3_rx_index], 1);
while (1)
{
// 如果串口3接收到了数据,则将数据发送出去
if (uart3_rx_index > 0)
{
send_data_via_uart1(uart3_rx_buffer, uart3_rx_index);
uart3_rx_index = 0;
}
}
}
```
阅读全文