I am trying to get the ESP32 to process the UART when every character is received and am having difficulty.
The coms is running at a blistering 2400 BAUD so I do not think this should be a problem for the ESP32.
My code is currently searching for 0x01 followed by 0x03 and the response should be sent out the TX port.
When I send the following (in HEX): 0xFF 0x09 0x01 0x00 0x00 0x08 0x01 0x03, I get the correct response with a 4-10ms delay
However when I send 2 payloads: 0xFF 0x09 0x01 0x00 0x00 0x08 0x01 0x03 0xFF 0x09 0x01 0x00 0x00 0x08 0x01 0x03, I get two responses at the end of the sequence. There is no gap between 'payloads' in the data stream above. I would be expecting a response after the first 0x01 0x03, a gap while the other payload was being processed.
So it looks like the receive is gathering all the data and then only providing it to the uart_read_bytes() function when there is a timeout. I looked at the uart_set_rx_timeout() function with hope, but setting this to 0 stopped any processing (maybe I got this wrong).
I have tried to use an async task (examples/peripherals/uart/uart_async_rxtxtask) even setting the number of bytes to read down to 2. It did not help.
Code: Select all
for(;;) {
rxBytes = uart_read_bytes(UART_NUM_0, data, 2, 0);
if (rxBytes > 0){
processBytes(data, rxBytes);
}
vTaskDelay(4 / portTICK_PERIOD_MS);
}
Should I be using the pattern match system? The problem is that 0x01 0x03 needs to be proceeded by another packet (and this will have different values in it). It will start with an 0xFF 0x09 and have a total of 8 characters, not sure I can pattern match on this vague requirement.
Any direction would be wonderful.
As a final question.. Is there any way to get the TX data to be sent quicker (reducing the delay)? I enabled
uart_config.use_ref_tick = true
and that changed the delay from 30ms to 8ms. If I change the BAUD to 115200, the TX delay is less than 1ms.
Shaun