cancel
Showing results for 
Search instead for 
Did you mean: 

USART receive string STM32f429

mgieroba9
Associate II
Posted on October 15, 2015 at 14:06

Hi!

I have to receive string from USART and them interpret it and make other function(s) depends of string. I saw Clive's example but could someone tell me how can I interpret string ? Please help me :(

#include <
string.h
> // memcpy()
//**************************************************************************************
#define LINEMAX 100 // Maximal allowed/expected line length
volatile char line_buffer[LINEMAX + 1]; // Holding buffer with space for terminating NUL
volatile int line_valid = 0;
//**************************************************************************************
void USART1_IRQHandler(void)
{
static char rx_buffer[LINEMAX]; // Local holding buffer to build line
static int rx_index = 0;
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received character?
{
char rx = USART_ReceiveData(USART1);
if ((rx == '\r') || (rx == '\n')) // Is this an end-of-line condition, either will suffice?
{
if (rx_index != 0) // Line has some content?
{
memcpy((void *)line_buffer, rx_buffer, rx_index); // Copy to static line buffer from dynamic receive buffer
line_buffer[rx_index] = 0; // Add terminating NUL
line_valid = 1; // flag new line valid for processing
rx_index = 0; // Reset content pointer
}
}
else
{
if (rx_index == LINEMAX) // If overflows pull back to start
rx_index = 0;
rx_buffer[rx_index++] = rx; // Copy to buffer and increment
}
}
}
//**************************************************************************************
int main(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // Enable GPIO A bank clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); // Enable USART1 clock
/* Configure Alternate Function pin muxing fabric to escape USART1 Rx and Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Configure USART1 pins: Rx and Tx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable USART1 IRQ */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Configure USART1 settings */
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE); // Enable USART1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // Enable USART1 Receive Interrupt
while(1) // Don't want to exit
{
__WFI(); // Wait for an interrupt rather than grind endlessly
if (line_valid) // A new line has arrived
{
// ProcessLine(line_buffer); // Do something with the line
line_valid = 0; // clear pending flag
}
}
}

3 REPLIES 3
Posted on October 15, 2015 at 15:09

If it were on a PC how would you process the string?

The processing function is passed a string, you could deal with a character at a time, tokenize with strtok(), search for keyword with strstr(), strncmp(), strnicmp() or use things like sscanf() or atoi()

A generic tutorial on string processing and parsing should suffice.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mgieroba9
Associate II
Posted on October 15, 2015 at 15:19

Thank You ! I will try ! 🙂 

mgieroba9
Associate II
Posted on October 16, 2015 at 09:49

Ok, so I think I should use strstr()