Use serial port to read data but never get timeout

Hi,

   I use serial port example to do out UART exchange data function. But I get a problem, I don't know why? Below is my source code.

static void serial_event_handler(struct nrf_serial_s const *p_serial, nrf_serial_event_t event)
{
    static uint8_t buffer[MAX_RX_BUFFER_SIZE] = {0};
    static uint8_t length = 0;

    switch (event)
    {
        case NRF_SERIAL_EVENT_TX_DONE:
            break;
        case NRF_SERIAL_EVENT_RX_DATA:
        {
             nrf_serial_read(&serial_uart, &buffer, 2, NULL, 100);

When I send 2 bytes data to our device from PC terminal. I can get a return and real data as i sent. But if I only send one data in from PC. I think I will get timeout return after 100ms. But I never get. Why?

Thank you.

John. 

Parents Reply Children
  • Hi.

       I use SDK 14 and I use examples\peripheral\serial  

    I try to fix it. If I move nrf_serial_read(&serial_uart, &buffer, 2, NULL, 100); to main loop. It work very well.

    Below is my source code

    I want to get 80 bytes data. if I only send 30 Bytes data from PC to our device. I will get timeout error code. This is what I want.

    static void serial_event_handler(struct nrf_serial_s const *p_serial, nrf_serial_event_t event)
    {
        switch (event)
        {
            case NRF_SERIAL_EVENT_TX_DONE:
                break;
            case NRF_SERIAL_EVENT_RX_DATA:
            {
                radar_raw_data_get = 1;
            }
            break;
            case NRF_SERIAL_EVENT_DRV_ERR:
            break;
            case NRF_SERIAL_EVENT_FIFO_ERR:
            break;
            default:
            break;
        }
    }
    
    void main()
    {
     for (;;)
        {
            receive_radar_raw_data();
            nrf_pwr_mgmt_run();
        }
        
     }
     
     void receive_radar_raw_data()
    {
        uint32_t err_code = 0;
        if(radar_raw_data_get == 1)
        {
            if(radar_packet_select_number == 0)
            {
                err_code = nrf_serial_read(&serial_uart, &radar_packet_0, 80, NULL, 20);
            }
            else if(radar_packet_select_number == 1)
            {
                err_code = nrf_serial_read(&serial_uart, &radar_packet_1, 80, NULL, 20);
            }
            else if(radar_packet_select_number == 2)
            {
                err_code = nrf_serial_read(&serial_uart, &radar_packet_2, 80, NULL, 20);
            }
            
            
            if(err_code == NRF_ERROR_TIMEOUT)
            {
            }
            else
            {
                radar_packet_select_number++;
                if(radar_packet_select_number == 2)
                    radar_packet_select_number = 0;
            }
            (void)nrf_serial_flush(&serial_uart, 0);
            radar_raw_data_get = 0;
        }
    }

  • Hi,

    Could you please provide more information on what you actually want to do? 
    In addition, could you elaborate on your choice for values 20, 30 and 80?

    Best regards,
    Dejan

Related