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

BLE disconnects after sending a packet

HI,

i am using nrf51422  with sdk13.3.0 and soft device 130

i am receiving 69 byte of data through UART, i am compressing data in two packets. here after transmitting a packet  BLE is disconnected

i don't want BLE to be disconnected after sending a packet. can you please help me to overcome from this.

regards,

Bindushree

void removeChar(char *s, int c,int c_1,int c_2){

    int j, n = strlen(s);
    for (int i=j=0; i<n; i++)
       if ((s[i] != c)&&(s[i]!=c_1)&&(s[i]!=c_2))
          s[j++] = s[i];

    s[j] = '\0';
}

void my_method(char *s)
{
	uint32_t       err_code;
  char ANALOG_DATA[17];	
  removeChar(s, '.','/',':');
        
  char *start=strtok(s,",");
  char *HGS=strtok(NULL,",");
  char *AT_1=strtok(NULL,",");
  char *RT_1=strtok(NULL,",");
  char *EM=strtok(NULL,",");
  char *OUTPUT=strtok(NULL,",");
  char *INPUT=strtok(NULL,",");
  char *FAULT_DATA=strtok(NULL,",");
  char *MODE=strtok(NULL,",");
  char *DATE=strtok(NULL,",");
  char *TIME=strtok(NULL,",");
  char *AT_2=strtok(NULL,",");
  char *RT_2=strtok(NULL,",");
  sprintf(ANALOG_DATA,"%s%s%s%s%s%s%s","A",HGS,AT_1,RT_1,AT_2,RT_2,"#");
	
 err_code=ble_nus_string_send(&m_nus,(uint8_t*)ANALOG_DATA, 17);
  if (err_code != NRF_ERROR_INVALID_STATE)
    {
       APP_ERROR_CHECK(err_code);
    }


}
void uart_event_handle(app_uart_evt_t * p_event)
{
	    

	   uint16_t j;
     char my_data[72];
	   static uint8_t data_array[72];
    
    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
         UNUSED_VARIABLE(app_uart_get(&data_array[index]));
				 index++;
				for(j=0;j<72;j++)
				{
				my_data[j]=data_array[j];
				}
        if ((data_array[0] == '$') && (data_array[68] == '#'))
            {
							
							my_method((char*)my_data);
              							
              index = 0;
            }
           
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}

Parents Reply Children
  • Hello,

    The issue is that you spend too much time in the UART event handler, so it can't finish doing what it is doing before it receives a new byte.

    As you can see in the original ble_app_uart example, all it does in the UART event handler is to check whether the entire sting is received (either '\n' or the length of the buffer), and pass the string to the softdevice. So what you need to do is to make sure you exit the UART handler before the next character is received. 

    Copy the data_array to another buffer, and set a flag that this has to be processed in the application priority (lower than the UART priority).

    pseudo code:

    void uart_event_handle(app_uart_evt_t * p_event)
    {
        ...
        switch(p_event->evt_type)
        {
            case APP_UART_DATA_READY:
                if(condition1 && condition2)
                {
                    m_my_uart_buffer = data_array;
                    m_my_uart_flag = true;
                }
                break;
        }
        ...
    }
    
    int main(void)
    {
        ...
        for (;;)
        {
            if(m_my_uart_flag)
            {
                m_my_uart_flag = false;
                process_uart_function();
            }
            power_manage();
        }
    }

  • Hi,

    sorry for late reply

    my application is now working fine

    thank you so much for your suggestion

    regards

    shrushrutha

Related