UART 1 and 2 communication
Posted: Tue Oct 01, 2019 10:35 pm
Hi, I'm trying to set two UART port on my ESP32-WROOM-32D, so I can use it as a bridge to communicate with another device.
The thing is that I modified the UART_echo example code from Github and added the new port. when I'm sending data I recieve it correctly on the other side (the device side) but when the device answers I only read part of the data.
Here is the code I use:
This is what i get on the ESP side:
I suspect that there might be a problem with one of the following functions:
I want to know if anyone has an idea of what can be happening, Thanks.
The thing is that I modified the UART_echo example code from Github and added the new port. when I'm sending data I recieve it correctly on the other side (the device side) but when the device answers I only read part of the data.
Here is the code I use:
Code: Select all
/* UART Echo Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
/**
* This is an example which echos any data it receives on UART1 back to the sender,
* with hardware flow control turned off. It does not use UART driver event queue.
*
* - Port: UART1
* - Receive (Rx) buffer: on
* - Transmit (Tx) buffer: off
* - Flow control: off
* - Event queue: off
* - Pin assignment: see defines below
*/
//Definitions UART to PC
#define UART_NUM_PC UART_NUM_1
#define ECHO_TEST_TXD (GPIO_NUM_1)
#define ECHO_TEST_RXD (GPIO_NUM_3)
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
//Definitions UART to Device
#define UART_NUM_DEV UART_NUM_2
#define UART2_TEST_TXD (GPIO_NUM_26)
#define UART2_TEST_RXD (GPIO_NUM_25)
#define BUF_SIZE (5120)
static const char *TAG = "uart_echo_example_main.c";
static void echo_task(void *arg)
{
/* Configure parameters of an UART driver,
* communication pins and install the driver */
uart_config_t uart_config_pc = {
.baud_rate = 57600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_config_t uart_config_dev = {
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
//UART PC
uart_param_config(UART_NUM_PC, &uart_config_pc);
uart_set_pin(UART_NUM_PC, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
uart_driver_install(UART_NUM_PC, BUF_SIZE * 2, 0, 0, NULL, 0);
//UART DEV
uart_param_config(UART_NUM_DEV, &uart_config_dev);
uart_set_pin(UART_NUM_DEV, UART2_TEST_TXD, UART2_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
uart_driver_install(UART_NUM_DEV, BUF_SIZE * 2, 0, 0, NULL, 0);
// Configure a temporary buffer for the incoming data
uint8_t *data_pc = (uint8_t *) malloc(BUF_SIZE);
uint8_t *data_dev = (uint8_t *) malloc(BUF_SIZE);
while (1)
{
// Read data from the UART DEV
int len_dev = uart_read_bytes(UART_NUM_DEV, data_dev, BUF_SIZE, 1000 / portTICK_RATE_MS);
// Read data from the UART PC, more time more data that acumulates before sending
int len_pc = uart_read_bytes(UART_NUM_PC, data_pc, BUF_SIZE, 1000 / portTICK_RATE_MS);
// Write data back to the UART
if ( uart_write_bytes(UART_NUM_DEV, (const char *) data_pc, len_pc) >0)
{
uart_write_bytes(UART_NUM_DEV, "\r\n" , 2);
uart_write_bytes(UART_NUM_PC, "\r\n" , 2);
uart_write_bytes(UART_NUM_PC, "send to DEV", 10);
uart_write_bytes(UART_NUM_PC, "\r\n" , 2);
uart_flush(UART_NUM_PC);
}
// Write data back to the UART
if ( uart_write_bytes(UART_NUM_PC, (const char *) data_dev, len_dev) >0)
{
uart_write_bytes(UART_NUM_PC, "from dev" , 8);
uart_write_bytes(UART_NUM_PC, "\r\n" , 2);
uart_flush(UART_NUM_DEV);
}
}
}
void app_main(void)
{
/*static BaseType_t xTaskCreate(TaskFunction_t pvTaskCode, const char *const pcName, const uint32_t usStackDepth,
* void *const pvParameters, UBaseType_t uxPriority, TaskHandle_t *const pvCreatedTask)
*/
xTaskCreate(echo_task, "uart_echo_task", 5120, NULL, 10, NULL);
}
Code: Select all
send to DEV //here I send data that was recieved correctly on the other side
1236from dev //here I was supposed to recieve 123456
1236from dev //here I was supposed to recieve 123456
1236from dev //here I was supposed to recieve 123456
6541from dev //here I was supposed to recieve 654321
1233450from dev //here I was supposed to recieve 12345678901234567890
1from dev //here I was supposed to recieve 1
12from dev //here I was supposed to recieve 12
123from dev //here I was supposed to recieve 123
1234from dev //here I was supposed to recieve 1234
1235from dev //here I was supposed to recieve 12345
1111from dev //here I was supposed to recieve 11111
1110from dev //here I was supposed to recieve 11110
- uart_write_bytes(UART_NUM_PC, (const char *) data_dev, len_dev)
- int len_dev = uart_read_bytes(UART_NUM_DEV, data_dev, BUF_SIZE, 1000 / portTICK_RATE_MS);
I want to know if anyone has an idea of what can be happening, Thanks.