2020-12-04 01:22 PM
I wish to print "String recieved" if /r or /n is detecetd
otherwise i wish to print "Error" .The problem is after detecting the first string i receive an output "String Received" After which i also receive error message. I have doubt if i have implemented the __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE) and enabled the HAL_UART_IRQ handler correctly,
I have used __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE) function after initialising all the configured peripherals in CubeMx
<
/* USER CODE BEGIN PV */
uint8_t g_rx_receive_complete=0;
uint8_t g_rx_received_bytes=0;
char g_rx_buffer [10]={0};
char rx_buffer [10]={0};
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
void check (void)
{
g_rx_buffer[g_rx_received_bytes]=(uint8_t)USART3->DR;
if((g_rx_buffer[g_rx_received_bytes]=='\n')||(g_rx_buffer[g_rx_received_bytes]=='\r'))
{
g_rx_buffer[g_rx_received_bytes]=='\0';
g_rx_receive_complete= 1;
}
else
{
if(g_rx_received_bytes<10)
g_rx_received_bytes++;
}
}
/* USER CODE END PFP */
while (1)
{
if (g_rx_receive_complete)
{
memcpy(rx_buffer, g_rx_buffer,sizeof(rx_buffer));
g_rx_receive_complete=0;
runCommand(rx_buffer);
/* USER CODE END WHILE */
}
>
In STM32f2xx_it.c I am calling the check function
<
void USART3_IRQHandler(void)
{
/* USER CODE BEGIN USART3_IRQn 0 */
check();
/* USER CODE END USART3_IRQn 0 */
HAL_UART_IRQHandler(&huart3);
/* USER CODE BEGIN USART3_IRQn 1 */
/* USER CODE END USART3_IRQn 1 */
}
>
Solved! Go to Solution.
2020-12-04 04:03 PM
hello
> "I wish to print "String recieved" if /r or /n is detecetd
otherwise i wish to print "Error" ."
What you mean with "otherwise"? Timeout generaly? timemout with some bytes already received? after a predetermined number of bytes?
What if receive \r and this is not the last byte of the packet?
These questions and maybe more must answered before you write the code
The code you published have issues as TDK mentioned.
Additionaly, code reads DR register and the HAL_UART_IRQHandler reads it again later.
The code that would started receiving again after the first time, is missing.
The macro __HAL_UART_ENABLE_IT enables interrupts only, no need to call it, if you call HAL_UART_Receive_IT(...).
To start RX, there are 3 functions HAL_UART_Receive, HAL_UART_Receive_IT, HAL_UART_Receive_DMA. Call one of them every time you want to receive.
There are working UART examples in the MXCube repository, inside your User folder. IMO are a good starting point to study Serial comm.
2020-12-04 02:31 PM
There's a lot of stuff here that doesn't make a lot of sense.
You have two variables defined which differ by a single character, and you seem to use them interchangeably:
> uint8_t g_rx_receive_complete;
> uint8_t g_rx_receive_completed=0;
You make a comparison statement instead of an assignment.
> g_rx_buffer[g_rx_received_bytes]=='\0';
You assign a variable one value, then immediately assign it something else.
> g_rx_receive_complete= 1;
> g_rx_receive_complete= 0;
You look up the value on an uninitialized variable
> if (g_rx_receive_complete)
then appear to turn the flag off, but assign a value to another variable instead.
> g_rx_receive_completed=0;
A lot of these issues should have given warnings in the IDE. A lot of them should have been caught when you were debugging the program.
I still think you should follow the advice in your last post on this exact topic.
2020-12-04 04:03 PM
hello
> "I wish to print "String recieved" if /r or /n is detecetd
otherwise i wish to print "Error" ."
What you mean with "otherwise"? Timeout generaly? timemout with some bytes already received? after a predetermined number of bytes?
What if receive \r and this is not the last byte of the packet?
These questions and maybe more must answered before you write the code
The code you published have issues as TDK mentioned.
Additionaly, code reads DR register and the HAL_UART_IRQHandler reads it again later.
The code that would started receiving again after the first time, is missing.
The macro __HAL_UART_ENABLE_IT enables interrupts only, no need to call it, if you call HAL_UART_Receive_IT(...).
To start RX, there are 3 functions HAL_UART_Receive, HAL_UART_Receive_IT, HAL_UART_Receive_DMA. Call one of them every time you want to receive.
There are working UART examples in the MXCube repository, inside your User folder. IMO are a good starting point to study Serial comm.
2020-12-05 07:16 AM
Hi since I dint have CubeMx while posting the question i made few mistakes i typed the program in editor to post the question. I am facing difficulty after eanbling the HAL_UART_IRQHandler(&huart3) do i have to call it in main.c or do i need to call HAL_UART_Receive in the main function ?