This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF51822 Processing of received UART data

Hi everyone.

I have a problem with processing with recived UART data. I'm using software UART from example > peripheral > uart.

I use nRF to comunicate with GSM module. When I send AT command need to check recived data. Example shows how react to one symbol i.e.:

    while (true)
    {
        uint8_t cr;
        while (app_uart_get(&cr) != NRF_SUCCESS);
        while (app_uart_put(cr) != NRF_SUCCESS);

        if (cr == 'q' || cr == 'Q')
        {
            //do something
        }

I want to check if recived data contain words i.e.

check if recived data contain "+CGNSPWR=0

On the begining I tried to store recived data in array and then check it. Probably I do something wrong because my stored data contains message, but from few earlier receivs.

Thats my function:

void UART(void)   
{
	unsigned char data[100];
	unsigned char finale[100];
	uint32_t count = 0;
	uint32_t cr;
	uint32_t clean;
	
	while (1)
	{
		while (app_uart_get(&cr) != NRF_SUCCESS) ;
		if (cr != '\r')
		{
			data[count] = cr;
			count++;
		}
		else
		{
			for (uint32_t i = 0; i < clean; i++)
			{
				finale[i] = 0;
			}
			for (uint32_t i = 0; i < count; i++)
			{
				finale[i] = data[i];	
			}
			clean = count + 1;
			count = 0;
			break;
		}
	}
}

I send message using app_uart_put() then use this UART(); function then Im checking finale[] if it contain what I need.

Can someone help me with that function?

Or any sugestion  how to build neew one?

  • Hi Rafaello

    If I understand you correctly the issue is not to find the +CGNSPWR=0 string, but that the data in the finale buffer is not correct after reception?

    The way you 'clean' the buffer seems overly complicated in my opinion. It should be sufficient either to just clear all the 100 bytes before you copy data to finale, or to clear the remaining bytes of finale after you have copied the data. Also, I would expect you to want to clean the bytes that you don't overwrite with data, there is no point clearing the bytes that you are about to overwrite anyway.

    I would expect it to look something like this:

    for (uint32_t i = 0; i < count; i++)
    {
      finale[i] = data[i];
    }

    for(uint32_t i = count; i < 100; i++)
    {
      finale[i] = 0;
    }

    And if you still have issues I would suggest putting some logging into the function so that you can see exactly which bytes are received at any one time. 

    Best regards
    Torbjørn

  • Hi, Overbekk.

    Yes, you understand it correct. I can find the string but was thinking if is any better way than mine.

    And the main problem is with with data.

    Your way to clean buffer is much better so thanks for it.

    Nevertheless problem is still here.

    I checked it with "simple LED debuger"

    I was sending "AT" command in the loop. GSM module should respond with OK. I set:

    If get OK toggle 1st LED.

    else toggle 2nd LED.

    And i found something like that:

    After first AT command nRF toggled second LED (something different than OK), at second AT command toggled 1 LED (OK) . And it was repeated. First 2nd LED then 1st LED and again 2nd LED again 1st LED and again and again.

    It looks like something is wrong with command sending or with reciving function.

    To send command I used:

    app_uart_put('A');

    app_uart_put('T');

    app_uart_put('\r');

    app_uart_put('\n');

    So sending should be fine.

    Thanks in advance

    Rafaello

  • I got the solution!
    My GSM module had enabled echo function. When I send AT command it repeat after me then send answer.
    When I disable echo function everything work fine.

Related