串口透传
- “透传”通常指的是数据的透明传输,意思是在不对数据进行任何处理或修改的情况下,将数据从一个接口转发到另一个接口。
- 值得注意的是要避免串口之间无限制的透明,可以采用互斥锁的方式进行限制
- 使用方法
- 对USART1和USART3(用他俩举例)的模式都是设置为Asynchronous,并开启对应的中断。
- RCC的High SPeed CLock模式设置为Crystal/Ceramic
- 配置对应的时钟为64Mhz
- 在main函数中启动串口1和串口3的空闲中断模式,接收数据 HAL_UARTEx_ReceiveToIdle_IT(&huart1, rxbuf1, sizeof(rxbuf1));
HAL_UARTEx_ReceiveToIdle_IT(&huart3, rxbuf3, sizeof(rxbuf3)); - 在
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef* huart, uint16_t Size)
写对应的透传即可
#include "main.h"
#include <string.h>
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart3;
char rxbuf1[128] = {0};
char rxbuf3[128] = {0};
uint8_t uart1_to_uart3_enable = 1;
uint8_t uart3_to_uart1_enable = 1;
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef* huart, uint16_t Size)
{
printf("HAL_UARTEx_RxEventCallback triggered\n");
if (huart == &huart1 && uart1_to_uart3_enable)
{
uart3_to_uart1_enable = 0;
HAL_UART_Transmit(&huart3, (uint8_t*)rxbuf1, Size, HAL_MAX_DELAY);
memset(rxbuf1, 0, sizeof(rxbuf1));
HAL_UARTEx_ReceiveToIdle_IT(&huart1, rxbuf1, sizeof(rxbuf1));
uart3_to_uart1_enable = 1;
printf("port1 sent to port 3\n");
}
else if (huart == &huart3 && uart3_to_uart1_enable)
{
uart1_to_uart3_enable = 0;
HAL_UART_Transmit(&huart1, (uint8_t*)rxbuf3, Size, HAL_MAX_DELAY);
memset(rxbuf3, 0, sizeof(rxbuf3));
HAL_UARTEx_ReceiveToIdle_IT(&huart3, rxbuf3, sizeof(rxbuf3));
uart1_to_uart3_enable = 1;
printf("port3 sent to port 1\n");
}
return;
}
int __io_putchar(int ch)
{
HAL_UART_Transmit(&huart1, (unsigned char*)&ch, 1, HAL_MAX_DELAY);
return ch;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
HAL_UARTEx_ReceiveToIdle_IT(&huart1, rxbuf1, sizeof(rxbuf1));
HAL_UARTEx_ReceiveToIdle_IT(&huart3, rxbuf3, sizeof(rxbuf3));
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
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_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART3_UART_Init(void)
{
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_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
ESP8266与STM32
- ESP8266 是一款集成了Wi-Fi通信功能的低功耗微控制器,广泛应用于物联网(IoT)设备中。
- 使用AT指令进行通信
- STM32与ESP8266通常通过UART(串行通信接口)进行通信。STM32发送AT指令到ESP8266模块,模块根据指令进行网络连接、数据传输等操作,并通过串口返回执行结果。
- 常见的AT指令
AT 指令 | 说明 |
---|
AT | 测试连接,返回“OK”表示模块工作正常。 |
AT+RST | 重启ESP8266。 |
AT+CWMODE | 设置ESP8266的工作模式(STA、AP或STA+AP)。 |
AT+CWJAP | 连接Wi-Fi网络,提供SSID和密码。 |
AT+CIPSTART | 启动TCP/UDP连接,常用于连接服务器。 |
AT+CIPSEND | 发送数据到服务器。 |
AT+CIPCLOSE | 关闭网络连接。 |
- ESP8266执行命令后,会返回执行结果,通常为“OK”或“ERROR”,我们需要通过UART接收这些数据
void Connect_To_Server(char *ip, uint16_t port) {
char cmd[100];
sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", ip, port);
UART_Send_AT_Command(cmd);
}
- 有三种模式
- Station 模式:可以像普通的 Wi-Fi 设备一样连接到现有的无线网络。
- AP 模式:可以创建自己的 Wi-Fi 热点,让其他设备连接到 ESP8266。
- Station + Access Point 混合模式(STA + AP 模式):ESP8266 同时充当客户端和热点,既可以连接到现有的 Wi-Fi 网络(作为 STA),又可以作为热点允许其他设备连接到它(作为 AP)。
AT+CWMODE=1:设置为 Station 模式。
AT+CWMODE=2:设置为 AP 模式。
AT+CWMODE=3:设置为 STA + AP 混合模式。
www.daxia.com
STATION模式
- 配置uart1- printf,uart3-esp上网(因为WiFi芯片在串口3)
使用方法
- 新建stm32工程中添加WiFi-ops.c和WiFi-ops.h
- 开启时钟频率为64Mhz和RCC的Crystal
- 开启UART1和UART3串口为异步通信,且开启中断。
- 并配置LED灯管脚留作测试
- 代码示例
#include "main.h"
#include "stdio.h"
#include "string.h"
#include "wifi-ops.h"
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart3;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART3_UART_Init(void);
int log_success_flags = 0;
int esp_station_callback(char *data, int len)
{
printf("recv from serv: %s\r\n", data);
if (strstr(data, "log success")) {
log_success_flags = 1;
} else if (strstr(data, "log failed")) {
log_success_flags = 2;
}
return 0;
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
wifi_uart_prepare_idle(huart);
}
int __io_putchar(int ch)
{
HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, 1);
return ch;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
int ret;
char sendbuf[128] = {0};
wifi_uart_prepare_idle(&huart3);
ret = wifi_station_init(&huart3, esp_station_callback);
if (ret < 0) {
printf("%s-%d wifi_station_init err\r\n", __func__, __LINE__);
return -35;
}
ret = wifi_station_join_ap(&huart3, "xiaomimobile", "12345600");
if (ret < 0) {
printf("%s-%d wifi_station_join_ap err\r\n", __func__, __LINE__);
return -35;
}
ret = wifi_station_tcp_connect(&huart3, "107.148.201.156", 10005);
if (ret < 0) {
printf("%s-%d wifi_station_tcp_connect err\r\n", __func__, __LINE__);
return -35;
}
snprintf(sendbuf, sizeof(sendbuf), "toServ:action=log,usrname=%s,passwd=%s,devname=sml001;", "xiaowang", "123456");
ret = wifi_station_tcp_send_data(&huart3, sendbuf, strlen(sendbuf));
if (ret < 0) {
printf("%s-%d wifi_station_tcp_send_data err\r\n", __func__, __LINE__);
}
for (int i = 0; i < 50; i++) {
if (log_success_flags == 2) {
printf("%s-%d log failed \r\n", __func__, __LINE__);
return -23;
} else if (log_success_flags == 1) {
printf("%s-%d log success \r\n", __func__, __LINE__);
break;
}
HAL_Delay(10);
}
strcpy(sendbuf, "toServ:action=gettime;");
while (1)
{
ret = wifi_station_tcp_send_data(&huart3, sendbuf, strlen(sendbuf));
if (ret < 0) {
printf("%s-%d wifi_station_tcp_send_data err\r\n", __func__, __LINE__);
}
HAL_Delay(500);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
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_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK) {
Error_Handler();
}
}
static void MX_USART3_UART_Init(void)
{
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_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK) {
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1) {
}
}
void assert_failed(uint8_t *file, uint32_t line)
{
}
混合模式
- 默认情况下,ESP8266 的 IP 地址在 AP 模式下是 192.168.4.1,以便其他设备连接到该 AP 时可以进行通信。
- 代码
#include "main.h"
#include "stdio.h"
#include "string.h"
#include "wifi-ops.h"
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart3;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART3_UART_Init(void);
#ifdef ESP_STATION
int log_success_flags = 0;
int esp_station_callback (char *data,int len)
{
printf("recv from serv: %s\r\n",data);
if( strstr(data,"log success") ){
log_success_flags = 1;
}else if(strstr( data, "log failed")){
log_success_flags = 2;
}
}
#endif
int esp_ap_callback(char *data,int len)
{
printf("recv from phone: %s\r\n",data);
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
wifi_uart_prepare_idle(huart);
}
int __io_putchar(int ch)
{
HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, 1);
return ch;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
int ret;
int linkid;
char sendbuf[128]={0};
#ifdef ESP_STATION
wifi_uart_prepare_idle(&huart3);
ret = wifi_station_init(&huart3, esp_station_callback );
if(ret < 0 ){ printf("%s-%d wifi_station_init err\r\n",__func__,__LINE__); return -35;}
ret = wifi_station_join_ap(&huart3,"WANGQINGFA","1234567890");
if(ret < 0 ){ printf("%s-%d wifi_station_join_ap err\r\n",__func__,__LINE__); return -35;}
ret = wifi_station_tcp_connect(&huart3,"107.148.201.156",10001);
if(ret < 0 ){ printf("%s-%d wifi_station_tcp_connect err\r\n",__func__,__LINE__); return -35;}
snprintf(sendbuf,sizeof(sendbuf), "toServ:action=log,usrname=%s,passwd=%s,devname=sml001;","xiaowang","123456" );
ret = wifi_station_tcp_send_data(&huart3,sendbuf,strlen(sendbuf));
if(ret < 0 ){ printf("%s-%d wifi_station_tcp_connect err\r\n",__func__,__LINE__); }
for(int i=0;i<50;i++){
if(log_success_flags == 2){
printf("%s-%d log failed \r\n",__func__,__LINE__);
return -23;
}else if(log_success_flags == 1){
printf("%s-%d log success \r\n",__func__,__LINE__);
break;
}
HAL_Delay(10);
}
#endif
wifi_uart_prepare_idle(&huart3);
ret = wifi_ap_init(&huart3, esp_ap_callback);
if(ret < 0 ){ printf("%s-%d wifi_ap_init err\r\n",__func__,__LINE__); return -35;}
ret = wifi_ap_set_args(&huart3, "zhangsan","12345678");
if(ret < 0 ){ printf("%s-%d wifi_ap_set_args err\r\n",__func__,__LINE__); return -35;}
linkid = wifi_ap_tcp_listen_and_wait_connect_timeout(&huart3,10001, 5*60*1000);
if(linkid < 0 ){ printf("%s-%d wifi_ap_tcp_listen_and_wait_connect_timeout err\r\n",__func__,__LINE__); return -35;}
strcpy(sendbuf,"cmd:hello phone,I have got you messgae,please reset it;");
while (1)
{
wifi_ap_tcp_send_data(&huart3, linkid , sendbuf,strlen(sendbuf) );
#ifdef ESP_STATION
ret = wifi_station_tcp_send_data(&huart3,sendbuf,strlen(sendbuf));
if(ret < 0 ){ printf("%s-%d wifi_station_tcp_connect err\r\n",__func__,__LINE__); }
#endif
HAL_Delay(500);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
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_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART3_UART_Init(void)
{
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_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
- 如图所示,用的TCP/IP调试软件