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

Libuarte send and receive String

Hi,

I am trying to send and receive strings using examples\peripheral\libuarte. I am trying to send a string like this but not receiving it on another board. Also how can I check coming data? Means how can I perform some action based on coming data?

Thanks!



Parents
  • Also, I have sent one command and getting data in a loop like this. 

    /**
     * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
     *
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     *
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     *
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     *
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     *
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     *
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    /** @file
     * @defgroup libuarte_example_main main.c
     * @{
     * @ingroup libuarte_example
     * @brief Libuarte Example Application main file.
     *
     * This file contains the source code for a sample application using libuarte.
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "nrf_libuarte_async.h"
    #include "nrf_drv_clock.h"
    #include <bsp.h>
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #include "nrf_queue.h"
    
    NRF_LIBUARTE_ASYNC_DEFINE(libuarte, 0, 0, 0, NRF_LIBUARTE_PERIPHERAL_NOT_USED, 255, 3);
    
    static uint8_t text[] = "UART example started5.\r\nLoopback:\r\n";
    static uint8_t text_size = sizeof(text);
    static volatile bool m_loopback_phase;
    
    typedef struct {
        uint8_t * p_data;
        uint32_t length;
    } buffer_t;
    
    NRF_QUEUE_DEF(buffer_t, m_buf_queue, 10, NRF_QUEUE_MODE_NO_OVERFLOW);
    
    void uart_event_handler(void * context, nrf_libuarte_async_evt_t * p_evt)
    {
        nrf_libuarte_async_t * p_libuarte = (nrf_libuarte_async_t *)context;
        ret_code_t ret;
    
        switch (p_evt->type)
        {
            case NRF_LIBUARTE_ASYNC_EVT_ERROR:
                bsp_board_led_invert(0);
                break;
            case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
                ret = nrf_libuarte_async_tx(p_libuarte,p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                if (ret == NRF_ERROR_BUSY)
                {
                    buffer_t buf = {
                        .p_data = p_evt->data.rxtx.p_data,
                        .length = p_evt->data.rxtx.length,
                    };
    
                    ret = nrf_queue_push(&m_buf_queue, &buf);
                    APP_ERROR_CHECK(ret);
                }
                else
                {
                    APP_ERROR_CHECK(ret);
                }
                bsp_board_led_invert(1);
                m_loopback_phase = true;
                printf("Rx Done");
                break;
            case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
                if (m_loopback_phase)
                {
                    nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                    if (!nrf_queue_is_empty(&m_buf_queue))
                    {
                        buffer_t buf;
                        ret = nrf_queue_pop(&m_buf_queue, &buf);
                        APP_ERROR_CHECK(ret);
                        UNUSED_RETURN_VALUE(nrf_libuarte_async_tx(p_libuarte, buf.p_data, buf.length));
                    }
                }
                bsp_board_led_invert(2);
                printf("Tx Done");
                break;
            default:
                break;
        }
    }
    
    /**
     * @brief Function for main application entry.
     */
    
    
    
    
    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
        
        ret_code_t ret = nrf_drv_clock_init();
        APP_ERROR_CHECK(ret);
      
        nrf_drv_clock_lfclk_request(NULL);
    
        ret_code_t err_code = NRF_LOG_INIT(app_timer_cnt_get);
        APP_ERROR_CHECK(err_code);
    
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        nrf_libuarte_async_config_t nrf_libuarte_async_config = {
                .tx_pin     = TX_PIN_NUMBER,
                .rx_pin     = RX_PIN_NUMBER,
                .baudrate   = NRF_UARTE_BAUDRATE_9600,
                .parity     = NRF_UARTE_PARITY_EXCLUDED,
                .hwfc       = NRF_UARTE_HWFC_DISABLED,
                .timeout_us = 100,
                .int_prio   = APP_IRQ_PRIORITY_LOW
        };
    
        err_code = nrf_libuarte_async_init(&libuarte, &nrf_libuarte_async_config, uart_event_handler, (void *)&libuarte);
    
        APP_ERROR_CHECK(err_code);
    
        nrf_libuarte_async_enable(&libuarte);
    
        //err_code = nrf_libuarte_async_tx(&libuarte, text, sizeof(text));
        static uint8_t command[]="AT+GSN\r\n";
        err_code = nrf_libuarte_async_tx(&libuarte, command,sizeof(command) );
        APP_ERROR_CHECK(err_code);
    
    
        while (true)
        {
            NRF_LOG_FLUSH();
        }
    }
    
    
    /** @} */
    

  • Make sure that the string you want to send is defined as a variable, e.g. static uint8_t command[] should work, since this will cause the string to exist in RAM. You should not point to a string that is placed in FLASH, which is the case in the picture in your original case desription.

    Also, I suggest that you check the return code from nrf_libuarte_async_tx(), if it's NRF_ERROR_BUSY you should retry until NRF_SUCCESS.

    I do believe the \nRF5_SDK_17.0.2_d674dde\examples\peripheral\libuarte example show both TX and RX.

    If it still fails you need to connect a logic analyzer to measure the RX and TX pins to see what is received and sent during an transaction.

    Kenneth

  • Thanks

    Make sure that the string you want to send is defined as a variable, e.g. static uint8_t command[]

    Is there any other way as I have to send many AT commands?

     

    Also, I suggest that you check the return code from nrf_libuarte_async_tx(), if it's NRF_ERROR_BUSY you should retry until NRF_SUCCESS.

    I am trying to get complete data but receive like one byte. How can receive complete string?

            case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
                ret = nrf_libuarte_async_tx(p_libuarte,p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                //if (ret == NRF_ERROR_BUSY)
                //{
                //    buffer_t buf = {
                //        .p_data = p_evt->data.rxtx.p_data,
                //        .length = p_evt->data.rxtx.length,
                //    };
    
                //    ret = nrf_queue_push(&m_buf_queue, &buf);
                //    APP_ERROR_CHECK(ret);
                //}
                //else
                //{
                //    APP_ERROR_CHECK(ret);
                //}
                while(ret==NRF_ERROR_BUSY)
                {           
                printf("Data : %s\n",p_evt->data.rxtx.p_data);
                }
                ;
    
                printf("Data1 : %s\n",p_evt->data.rxtx.p_data);
    
                if(p_evt->data.rxtx.p_data=="Hello")
                  printf("Data Matched");
    
                bsp_board_led_invert(1);
                m_loopback_phase = true;
                printf("Rx Called\n");
                break;
            case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
                if (m_loopback_phase)
                {
                    nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                    if (!nrf_queue_is_empty(&m_buf_queue))
                    {
                        buffer_t buf;
                        ret = nrf_queue_pop(&m_buf_queue, &buf);
                        APP_ERROR_CHECK(ret);
                        UNUSED_RETURN_VALUE(nrf_libuarte_async_tx(p_libuarte, buf.p_data, buf.length));
                    }
                }
                bsp_board_led_invert(2);
                printf("Tx Called\n");
                break;
    

  • Muqarrab said:
    Is there any other way as I have to send many AT commands?

    You don't need many variables, instead update the command data once a transfer is finished.

    Muqarrab said:
    I am trying to get complete data but receive like one byte. How can receive complete string?

    You might need to assemble the received data, so if you receive 1byte, you may need to copy it to a variable until the entire string is received.

    Best regards,
    Kenneth

  • You might need to assemble the received data, so if you receive 1byte, you may need to copy it to a variable until the entire string is received.

    How can I check that complete data is received?

Reply Children
  • I have made this function to send string(successfully received on another controller) but I have to put a 1-sec delay after one command.
    Is there any callback check which I can add in this function to wait until sending data?

    Thanks

    bool send_command(char cmd[],int size_cmd)
    {
    
        ret_code_t err_code;
        printf("Sending : ");
        printf(cmd);
        printf("\n");
    
        memset(command, 0, sizeof(command));
        
        for (int i=0;i<size_cmd;i++)
        {
          command[i]=cmd[i];   
        }
    
        err_code = nrf_libuarte_async_tx(&libuarte, command,sizeof(command) );
        APP_ERROR_CHECK(err_code);
        while(err_code==NRF_ERROR_BUSY); // Is this correct??
    
    }
    


        send_command("Muqarrab",sizeof("Muqarrab"));
        nrf_delay_ms(1000);
        send_command("Rahman",sizeof("Rahman"));
    

  • Looking at: https://sites.google.com/site/vmacgpsgsm/understanding-at-commands

    I can find that "Responses start and end with <CR><LF>."

    So I assume you could check if you receive <CR><LF>? 

    CR= carriage return
    LF= line feed

    See ascii table for hex values: https://www.asciitable.com/ 

  • I have read this post.


    I also don't want to save the data in the queue and resend on tx. I want to send AT commands and want to check the response immediately. I am not getting how to do this. Can you please help?


            case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
    
                ret = nrf_libuarte_async_tx(p_libuarte,p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                if (ret == NRF_ERROR_BUSY)
                {
                    buffer_t buf = {
                        .p_data = p_evt->data.rxtx.p_data,
                        .length = p_evt->data.rxtx.length,
                    };
    
                    ret = nrf_queue_push(&m_buf_queue, &buf);
                    APP_ERROR_CHECK(ret);
                }
                else
                {
                    APP_ERROR_CHECK(ret);
                }
                //while(ret==NRF_ERROR_BUSY)
                //{           
                //printf("Data : %s\n",p_evt->data.rxtx.p_data);
                //}
    
                printf("Data1 : %s\n",p_evt->data.rxtx.p_data);
    
                if(p_evt->data.rxtx.p_data==0x0D  || p_evt->data.rxtx.p_data=='\r')
                  printf("Completed\n");
    
                bsp_board_led_invert(1);
                m_loopback_phase = true;
                printf("Rx Called\n");
                break;
            case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
                if (m_loopback_phase)
                {
                    nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
                    if (!nrf_queue_is_empty(&m_buf_queue))
                    {
                        buffer_t buf;
                        ret = nrf_queue_pop(&m_buf_queue, &buf);
                        APP_ERROR_CHECK(ret);
                        UNUSED_RETURN_VALUE(nrf_libuarte_async_tx(p_libuarte, buf.p_data, buf.length));
                    }
                }
                bsp_board_led_invert(2);
                printf("Tx Called\n");
                break;
    

Related