-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot get the RS485 communication on ESP32C3's UART0 working using auto switching RS485 transmitter/receiver. (IDFGH-10362) #11620
Comments
Hello DicoderZ, Could you make clear some aspects of your project:
|
Hi,
Now, there is a weird problem. It works as long as the device is connected to PC with USB in monitor mode. As soon as I take the cable out. The RS485 reading part does not read. ESP32C3 device continues to work, and reads other sensors such as board temperature. |
Hi, I can recommend you to use the galvanic isolation. The Schematic variant C supports the auto switching of direction and galvanic isolation as well it can solve many problems in your project and verified as reliable. Also please take a look to your switching schematic. I expect in some conditions it may cause the unreliable behavior. |
Do you have any update on this issue? Thanks. |
No, the problem still exists while using UART0. Reads actual values from RS485 as long the board is powered by USB data cable. Returns empty values, when restarting the board with USB power only cable. Checked the character lengths, for TX and RX, Board is sending correctly to the RS485 device, but the RX is empty. Cable's are thicker, and the supply is 5V 2.4A capable. Thinking of ditching the UART0 completely and move to using UART1. |
It is hard to check your issue with the same environment on my side. However, my board with different driver, suggested schematic in similar conditions works just fine. Please check and share the oscillogram for the communication and check the logic levels at the output of driver. Also check the configuration to use separate UARTs for console and for Modbus communication. |
Hi @diwakrZha , |
Answers checklist.
General issue report
I have a custom board to read an RS485 device using UART0 of the ESP32C3, GPIO 20 for RX, 21 for TX. The board has auto switching RS485 transmitter/receiver built using SP3485. I have now spent days trying to troublshoot it with no success. So any hints will be really really helpful.
Here is the schematic.
this is adapted from the Espressif's documentation,
The circuit C: auto switching of transmitter/receiver:
https://docs.espressif.com/projects/esp-idf/en/v3.3.6/api-reference/peripherals/uart.html
I am using ESP-IDF v4.4.4
I couldn't manage to get any data. Same schematic with SP3485 works fine with RPI PICO w based board. I tested them side by side.
Here is the code:
#include "uart_handler.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"
const uart_port_t uart_num = UART_NUM_0;
uart_config_t uart_config = {
.baud_rate = 38400,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
//.rx_flow_ctrl_thresh = 122,
};
// Configure UART parameters
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
// Set UART pins(TX: IO4, RX: IO5, RTS: IO18, CTS: IO19)
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_0, 20, 21, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
// Setup UART in rs485 mode
ESP_ERROR_CHECK(uart_set_mode(uart_num, UART_MODE_RS485_APP_CTRL));
// ESP_ERROR_CHECK(uart_set_mode(uart_num, UART_RS485_CONF_REG.UART_RS485RXBY_TX_EN = 1));
// ESP_ERROR_CHECK(uart_set_mode(uart_num, , UART_RS485_CONF_REG.UART_RS485TX_RX_EN = 0));
// Setup UART buffered IO with event queue
const int uart_buffer_size = (1024 * 2);
QueueHandle_t uart_queue;
// Install UART driver using an event queue here
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0, uart_buffer_size,
uart_buffer_size, 10, &uart_queue, 0));
void tx_task() {
// Write data to UART.
uart_flush(uart_num);
// uart_flush_input(uart_num);
char* test_str = "getData\n\r";
uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str));
// Write data to UART, end with a break signal.
// uart_write_bytes_with_break(uart_num, const char*)test_str,strlen(test_str), 100);
vTaskDelay(pdMS_TO_TICKS(500));
}
void rx_task(char rxdata[RX_BUF_SIZE]) {
tx_task();
// Read data from UART.
const uart_port_t uart_num = UART_NUM_0;
uint8_t data[128];
int length = 0;
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
length = uart_read_bytes(uart_num, data, length, 100);
ESP_LOGI("UART HANDLER", "Read %d bytes: '%s'", length, (char*) data);
uart_flush(uart_num);
The text was updated successfully, but these errors were encountered: