项目复刻之前的准备
USB转TTL(ch340) 4G模块(这个取决于自己的需要查看是否需要GPS)

串口5和6的相互收发(其实只需要能够把串口5收到的数据转发至串口6即可)
board.h(串口的引脚选择以及基础配置)
#define BSP_USING_UART5c
#define BSP_UART5_TX_PIN "PC12"
#define BSP_UART5_RX_PIN "PD2"
#define BSP_UART5_RX_USING_DMA //开启 UART DMA 功能
#define BSP_USING_UART6
#define BSP_UART6_TX_PIN "PC6"
#define BSP_UART6_RX_PIN "PC7"
#define BSP_UART6_RX_USING_DMA //开启 UART DMA 功能
uart5_app.c
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-11-21 leader the first version
* 202x-xx-xx leader support communicate with uart6
*/
#include <uart5_app.h>
#define UART_NAME "uart5"
// 串口5设备句柄(区分串口6)
rt_device_t uart5_handle;
// 串口5接收信号量(区分串口6)
static struct rt_semaphore rx5_sem;
// 串口5接收回调函数
static rt_err_t uart5_input(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx5_sem);
return RT_EOK;
}
// 串口5任务:接收数据并转发到串口6
static void uart5_task(void *parameter)
{
char ch;
while(1)
{
// 永久等待接收信号量(阻塞式)
rt_sem_take(&rx5_sem, RT_WAITING_FOREVER);
// 读取串口5接收到的字节
while(rt_device_read(uart5_handle, -1, &ch, 1) == 1)
{
// 转发到串口6
rt_device_write(uart6_handle, 0, &ch, 1);
}
}
}
static int uart5_init(void)
{
// 查找串口5设备
uart5_handle = rt_device_find(UART_NAME);
if (uart5_handle == RT_NULL)
{
rt_kprintf("uart5 device find failed!\\n");
return -RT_ERROR;
}
// 打开串口5(中断接收模式)
if (rt_device_open(uart5_handle, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
{
rt_kprintf("uart5 device open failed!\\n");
return -RT_ERROR;
}
// 初始化接收信号量
rt_sem_init(&rx5_sem, "rx5_sem", 0, RT_IPC_FLAG_FIFO);
// 设置接收回调函数
rt_device_set_rx_indicate(uart5_handle, uart5_input);
// 创建串口5任务(命名唯一,避免冲突)
rt_thread_t uart5_thread = rt_thread_create("uart5_task",
uart5_task,
RT_NULL,
1024, // 栈大小
25, // 优先级
10); // 时间片
if (uart5_thread != RT_NULL)
{
rt_thread_startup(uart5_thread);
}
else
{
rt_kprintf("uart5 thread create failed!\\n");
return -RT_ERROR;
}
rt_kprintf("uart5 init success!\\n");
return 0;
}
INIT_APP_EXPORT(uart5_init);
uart5_app.h
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2026-01-14 leader the first version
*/
#ifndef APPLICATIONS_UART5_APP_H_
#define APPLICATIONS_UART5_APP_H_
#include <bsp_system.h>
// 声明串口5句柄,供串口6调用
extern rt_device_t uart5_handle;
#endif /* APPLICATIONS_UART5_APP_H_ */
uart6_app.c
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2026-01-14 leader the first version
* 202x-xx-xx leader support communicate with uart5
*/
#include <uart6_app.h>
#define UART_NAME "uart6"
// 串口6设备句柄(区分串口5)
rt_device_t uart6_handle;
// 串口6接收信号量(区分串口5)
static struct rt_semaphore rx6_sem;
// 串口6接收回调函数
static rt_err_t uart6_input(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx6_sem);
return RT_EOK;
}
// 串口6任务:接收数据并转发到串口5
static void uart6_task(void *parameter)
{
char ch;
while(1)
{
// 永久等待接收信号量(阻塞式)
rt_sem_take(&rx6_sem, RT_WAITING_FOREVER);
// 读取串口6接收到的字节
while(rt_device_read(uart6_handle, -1, &ch, 1) == 1)
{
// 转发到串口5
rt_device_write(uart5_handle, 0, &ch, 1);
}
}
}
static int uart6_init(void)
{
// 查找串口6设备
uart6_handle = rt_device_find(UART_NAME);
if (uart6_handle == RT_NULL)
{
rt_kprintf("uart6 device find failed!\\n");
return -RT_ERROR;
}
// 打开串口6(中断接收模式)
if (rt_device_open(uart6_handle, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
{
rt_kprintf("uart6 device open failed!\\n");
return -RT_ERROR;
}
// 初始化接收信号量
rt_sem_init(&rx6_sem, "rx6_sem", 0, RT_IPC_FLAG_FIFO);
// 设置接收回调函数
rt_device_set_rx_indicate(uart6_handle, uart6_input);
// 创建串口6任务(命名唯一,避免冲突)
rt_thread_t uart6_thread = rt_thread_create("uart6_task",
uart6_task,
RT_NULL,
1024, // 栈大小
25, // 优先级
10); // 时间片
if (uart6_thread != RT_NULL)
{
rt_thread_startup(uart6_thread);
}
else
{
rt_kprintf("uart6 thread create failed!\\n");
return -RT_ERROR;
}
rt_kprintf("uart6 init success!\\n");
return 0;
}
INIT_APP_EXPORT(uart6_init);
uart6_app.h
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2026-01-14 leader the first version
*/
#ifndef APPLICATIONS_UART6_APP_H_
#define APPLICATIONS_UART6_APP_H_
#include <bsp_system.h>
// 声明串口6句柄,供串口5调用
extern rt_device_t uart6_handle;
#endif /* APPLICATIONS_UART6_APP_H_ */
两个串口设置默认为波特率115200
连线说明(在进行连线之前,请一定要测试两个串口是否能进行相互转发)
串口6:(可以先不急着进行接线)
TX(PC6)-RX(4G模块) RX(PC7)-TX(4G模块) 5V GND-注意与单片机共地
串口相互收发数据结果展示




