我想使用RX中断从我的ESP2866通过UART接收数据,所以我不需要轮询数据。
代码工作正常,我可以在调试时看到rx_buffer中的响应,但是如何检查我的ESP何时完成发送?
ESP发送的最后一个字符是\ r \ n但是它在传输过程中也会这样做几次,所以我不能真正依赖它。
我知道我应该以某种方式检查缓冲区中的'\ 0'终止符,但处理程序在收到最后一个字符时停止。因此在处理程序中检查'\ 0'不起作用。
这可能是我想念的一些简单但我希望有人可以帮助我。
int main(void)
{
/* USER CODE BEGIN 1 */
char* msg = "AT+GMR\r\n";
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_USART1_UART_Init();
/* Initialize interrupts */
MX_NVIC_Init();
/* USER CODE BEGIN 2 */
// Send AT+GMR to ESP module
HAL_UART_Transmit(&huart1, (uint8_t *)msg, strlen(msg) + 1, HAL_MAX_DELAY);
// Receive character (1 byte)
HAL_UART_Receive_IT(&huart1, &rx_data, 1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
GETCHAR_PROTOTYPE
{
/* Place your implementation of fgetc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
char ch;
HAL_UART_Receive(&huart2,(uint8_t*)&ch,1, HAL_MAX_DELAY);
return ch;
}
/**
* @brief Rx Transfer completed callback
* @param UartHandle: UART handle
* @note This example shows a simple way to report end of DMA Rx transfer, and
* you can add your own implementation.
* @retval None
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1){
if(rx_index == 0){
memset(&rx_buffer, 0, sizeof(rx_buffer));
}
rx_buffer[rx_index] = rx_data;
if(rx_buffer[rx_index] == '\0'){
printf("%s", rx_buffer);
}
rx_index++;
// Receive next character (1 byte)
HAL_UART_Receive_IT(&huart1, &rx_data, 1);
}
}
答案 0 :(得分:0)
当您考虑 size of
和 char* msg = "AT+GMR\r\n";
时,它自己将 \0
视为一个字符。
因此您无需在 +1
函数中添加 Transmit
。