1

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);

    }
}       
4

1 に答える 1