I have Arduino Uno R3 sending every 1s 'test' string at 115200 8N1 from TX pin to RX2 pin of DOIT ESP32 Devkit.
I'm using this code to initialize UART2 and receive bytes:
Code: Select all
uart_config_t uart_config;
uart_config.baud_rate = 115200;
uart_config.data_bits = UART_DATA_8_BITS;
uart_config.parity = UART_PARITY_DISABLE;
uart_config.stop_bits = UART_STOP_BITS_1;
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
if (uart_param_config(UART_NUM_2, &uart_config) != ESP_OK)
printf("uart_param_config error\n");
if (uart_set_pin(UART_NUM_2, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE) != ESP_OK)
printf("uart_set_pin error\n");
if (uart_driver_install(UART_NUM_2, 1024 * 2, 0, 0, NULL, 0) != ESP_OK)
printf("uart_driver_install error\n");
uint8_t * data = (uint8_t*) malloc(1024);
while (1) {
const int rxBytes = uart_read_bytes(UART_NUM_2, data, 1024, 100 / portTICK_RATE_MS);
if (rxBytes > 0) {
data[rxBytes] = 0;
printf("read %d bytes: '%s' [", rxBytes, data);
for (int i = 0; i < rxBytes-1; ++i)
{
printf("0x%02hhx,", data[i]);
}
printf("0x%02hhx]\n", data[rxBytes-1]);
} else {
uart_write_bytes(UART_NUM_2, "test", 4);
vTaskDelay(500/portTICK_PERIOD_MS);
}
}
Code: Select all
read 3 bytes: '!' [0xbf,0x21,0x98]
read 3 bytes: '!' [0xbf,0x21,0x98]
read 3 bytes: '!' [0xbf,0x21,0x98]
read 3 bytes: '!' [0xbf,0x21,0x98]
Code: Select all
read 4 bytes: 'test' [0x74,0x65,0x73,0x74]
read 4 bytes: 'test' [0x74,0x65,0x73,0x74]
read 4 bytes: 'test' [0x74,0x65,0x73,0x74]
read 4 bytes: 'test' [0x74,0x65,0x73,0x74]