cancel
Showing results for 
Search instead for 
Did you mean: 

How to take input from keyboard in UART on putty

MG1
Associate II

I need to help for take input from keyboard in UART on putty.
board name is: STM32F407G Discovery
Here is code for reference to you.

This is the message which i want to transmit.

"char msg1[]="Press 1 for Input capture:\n\r"

"Press 2 for Duty Cycle:\n\r"

"Press 3 for Exit or Stop:\n\r";"

I create this function:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

if (huart->Instance == USART2)

{

// Process received data

if (msg1[0] == '\n') // Check for newline character indicating end of the string

{

// Null-terminate the received string

msg1[0] = '\0';

 

// Transmit the received string back to the console

HAL_UART_Transmit(&huart2, (uint8_t *)msg1, strlen(msg1), HAL_MAX_DELAY);

 

// Clear the buffer for the next reception

//memset(msg1, 0, sizeof(msg1));

 

// Continue to listen for more data

HAL_UART_Receive_IT(&huart2, (uint8_t *)msg1, 1);

}

else

{

// Continue to receive more characters

HAL_UART_Receive_IT(&huart2, (uint8_t *)msg1 + 1, 1);

}

}

}

 

And call this function in int main() function in while(1) loop 

int main(void)

{

/* USER CODE BEGIN 1 */

 

/* 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_DMA_Init();

MX_USART2_UART_Init();

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

HAL_UART_RxCpltCallback(&huart2);

}

/* USER CODE END 3 */

} 

 

transmit this message successfully on putty. Now i need to press 1/ 2/ 3 any number for further calculation so according that it will process that calculation and this input is give from keyboard. 
How it will take input from putty?
My concern is that which parameters need to taking care of that.
I also put reference code for you.
Please guide me for that.

 

 

3 REPLIES 3
MG1
Associate II

Or if you have any example code then you also provide that for my reference. 

TDK
Guru

> msg1[0] = '\0';

This sets the string to null. After that, there is nothing to transmit as it's an empty string.

If you feel a post has answered your question, please click "Accept as Solution".
Andrew Neil
Evangelist III

Use this button to properly post source code:

AndrewNeil_0-1707230402821.png

 

To get that extra row of icons, press this button:

AndrewNeil_1-1707230402823.png