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

append data before sending using ble_uart

hi, i want to append data before sending. i am using ble_uart example. here suppose the data is available on uart buffer at 1data/sec. i want to sent some timer/counter which appends if there will be more data will be available in uart buffer. say 5data/per sencond then. i should be able to append all the data in a single packet.

currently i am using timeout to send data. here when i set timeout 500 ms (since 1000<500). then it send it porperly. but when the data is more say 5data/sec then program get restarted.

how should i approch the problem with approporate data appending before sending when there is more data available in uart buffer.

thanks!!

my code :

static void timer_handler(void * p_context)
        {
        		preCount++;
        		uint32_t err_code;
                //SEGGER_RTT_WriteString (0, "--> in timeout handler\n");
            	//SEGGER_RTT_printf (0, "preCount : %d\tIndex:%d\n",preCount,index);
        		SEGGER_RTT_printf(0, "Time Out. \n"); 
        		err_code = app_timer_stop(wait_timer_id);
        		APP_ERROR_CHECK(err_code);
        	
        		if((datacnt%3==0)&(preCount!=datacnt/3))
        			//SEGGER_RTT_printf (0, "preCount : %d\tcount:%d\tdatacnt:%d\n",preCount,count,datacnt);
        			SEGGER_RTT_printf (0, "Add Data\n");
        		else
        			SEGGER_RTT_printf (0, "Send all Data\n");
        			SEGGER_RTT_printf (0, "preCount : %d\tcount:%d\tdatacnt:%d\n",preCount,count,datacnt/3);
        				
        }
        
        // Create timers
        static void init_timers()
        {
            uint32_t err_code;
        	
        		// Initialize the Application timer Library.
            APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
        		//SEGGER_RTT_printf(0, "APP_TIMER_INTI initialised. \n");
        
            // Create timers
            err_code = app_timer_create(&wait_timer_id,
                                        APP_TIMER_MODE_SINGLE_SHOT,
                                        timer_handler);
            APP_ERROR_CHECK(err_code);
        	
        		err_code = app_timer_start(wait_timer_id, APP_TIMER_TICKS(1500, APP_TIMER_PRESCALER), NULL);
        		APP_ERROR_CHECK(err_code);
        		//SEGGER_RTT_printf(0, "Timer Started \n");
        }
        
        
        /*@brief   Function for handling app_uart events.
         *
         * @details This function will receive a single character from the app_uart module and append it to 
         *          a string. The string will be be sent over BLE when the last character received was a 
         *          'new line' i.e '\n' (hex 0x0D) or if the string has reached a length of 
         *          @ref NUS_MAX_DATA_LENGTH.
         */
        /**@snippet [Handling the data received over UART] */
        void uart_event_handle(app_uart_evt_t * p_event)
        {
            static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
        		uint8_t data[5];
            static uint8_t index,push = 0;
        		//int index=0;
            uint32_t       err_code;
        
            switch (p_event->evt_type)
            {
                case APP_UART_DATA_READY:
                    UNUSED_VARIABLE(app_uart_get(&data_array[index]));
                    index++;datacnt++;
        						//preCount = index;
        						//SEGGER_RTT_printf(0, "index:%d\tprecnt:%d\n",index,preCount);
        						if(index%3 == 0)
        						{
        							count++;
        							//preCount = count;
        							init_timers();
        							
        							//preCount = count;
        							//SEGGER_RTT_printf(0, "index:%d\tprecnt:%d\tdatacnt:%d\n",index,preCount,datacnt);
        							//SEGGER_RTT_printf(0, "Application Timer Created. \n");
        							
        							
        							
        							//SEGGER_RTT_printf(0, "Data Count :%d\n",count);
        							//SEGGER_RTT_printf(0, "index :%d\n",index);
        							const uint8_t* midiData = parseMIDItoAppleBle(3, data_array);
        							for(int i=0;i<5;i++)
        							{
        								data[i] = midiData[i];
        							}
        							//SEGGER_RTT_printf(0, "%x\t%x\t%x\t%x\t%x\tCount:%d%tindex:%d\n",data[0], data[1],data[2],data[3], data[4],count, index);
        							
        								/*err_code = ble_nus_string_send(&m_nus, data, 5);
                        if (err_code != NRF_ERROR_INVALID_STATE)
                        {
                            APP_ERROR_CHECK(err_code);
                        }*/
        								//count++;
        								index = 0;
        						}						
        							
                    break;
    }
Parents
  • Hello.

    There is a limitation on how often you can send packets in BLE. This is decided by your connection interval and the output packet buffer. See this and this post.

    When you try to send more packets than what is possible, an error code is returned from ble_nus_string_send. Always check this error code, in fact always check ALL error codes returned to you by SDK functions. Read this devzone post and this blog post to learn how to understand error handling.

    If i understood your code correctly, you end up with data chunks of 5 bytes. You can combine 4 of these chunks (20 bytes in total), and send them at the same time using ble_nus_string_send. Simply create a uint8_t array with a length of 20, and put the 5 byte chunks in as they come. Then send the 20 byte "string" when it is full.

    I do not understand why you are using a timer here. Your timer_handler seems to only do some printing, no sending.

    -Anders

  • hi, i want to append data based on timer. suppose i am getting data from uart pin at 1 data per second. and if my time out is 2.5 sec the it should append 2 data as timer timeout is more that data coming rate.

    how should i do this.

    thanks!!

Reply Children
No Data
Related