#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#define UART_PORT UART_NUM_0
#define BUF_SIZE 128
void uart_init() {
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(UART_PORT, &uart_config);
uart_set_pin(UART_PORT, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(UART_PORT, BUF_SIZE * 2, 0, 0, NULL, 0);
}
void uart_task(void *pvParameters) {
char data[BUF_SIZE]; // Changed from uint8_t to char
char buffer[BUF_SIZE];
int index = 0;
int bufferIndex = 0;
while (1) {
int len = uart_read_bytes(UART_PORT, data + index, 1, 20 / portTICK_PERIOD_MS);
if (len > 0){
for (int i = 0; i < len; i++) {
buffer[bufferIndex++] = data[i];
// Check for newline character
if (data[i] == '\n') {
// Null-terminate the string and print
buffer[bufferIndex] = '\0';
printf("received %s\n", buffer);
// Reset buffer index for next message
bufferIndex = 0;
}
}
}
}
}
//uart_write_bytes(UART_PORT, (const char *)(data + index), len);
void app_main() {
uart_init();
xTaskCreate(uart_task, "uart_task", 2048, NULL, 10, NULL);
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4