UART rx/tx incorrect data

playmean
Posts: 2
Joined: Wed Oct 11, 2017 11:18 am

UART rx/tx incorrect data

Postby playmean » Tue Feb 20, 2018 10:20 pm

Hello,
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);
		}
	}
In a console out I have this data:

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]
But all going well if I connect RX2 pin to TX2 pin of ESP32 ('uart_write_bytes' part in a code):

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]
I need to receive 'test' string from Arduino. Can't understand what I'm doing wrong.

playmean
Posts: 2
Joined: Wed Oct 11, 2017 11:18 am

Re: UART rx/tx incorrect data

Postby playmean » Wed Feb 21, 2018 11:04 am

With Arduino IDE code below works correctly:

Code: Select all

#include <HardwareSerial.h>

HardwareSerial Serial2(2);

void setup()
{
	Serial.begin(115200, SERIAL_8N1);
	Serial2.begin(115200, SERIAL_8N1);
}

void loop()
{
	if (Serial2.available() > 0) {
		Serial.write(Serial2.read());
	}
}
But I need it works in ESP-IDF.
I have tried to change CPU freq from 240 MHz to 80 MHz and it doesn't helped.

Also I have tried to use Arduino's esp32-hal-uart component to implement Arduino's uart function to ESP-IDF and it works also bad as native function with 'driver/uart.h':

Code: Select all

	uart_t * com = uartBegin(2, 115200, SERIAL_8N1, 16, 17, 256, false);

	while (1) {
		uint32_t rc = uartAvailable(com);
		if (rc > 0) {
			printf("read %d [", rc);
			for (int i = 0; i < rc-1; ++i)
			{
				printf("%d,", uartRead(com));
			}
			printf("%d]\n", uartRead(com));
		} else {
			uartWriteBuf(com, (uint8_t *)"test", 5);
			vTaskDelay(500 / portTICK_PERIOD_MS);
		}
	}
Help :?

Who is online

Users browsing this forum: Bing [Bot] and 144 guests