Skip to content
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

Closed
3 tasks done
diwakrZha opened this issue Jun 8, 2023 · 7 comments
Assignees
Labels
Awaiting Response awaiting a response from the author Resolution: Won't Do This will not be worked on Status: Done Issue is done internally

Comments

@diwakrZha
Copy link

diwakrZha commented Jun 8, 2023

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

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);

@espressif-bot espressif-bot added the Status: Opened Issue is new label Jun 8, 2023
@github-actions github-actions bot changed the title Cannot get the RS485 communication on ESP32C3's UART0 working using auto switching RS485 transmitter/receiver. Cannot get the RS485 communication on ESP32C3's UART0 working using auto switching RS485 transmitter/receiver. (IDFGH-10362) Jun 8, 2023
@alisitsyn
Copy link
Collaborator

alisitsyn commented Jun 9, 2023

Hello DicoderZ,

Could you make clear some aspects of your project:

  1. You are using the UART0 for the communication over RS485. Do you configure the ESP_CONSOLE_UART_NUM(=0) accordingly to use other port? The UART0 is used for console by default and usually connected to the usb-uart bridge.
  2. Did you try the RS485 auto switching circuit exactly as showed in the documentation?
  3. Please try to use the rs485 echo example with your peripheral to check the communication first. This example should be able to work without RTS pin connected.

@diwakrZha
Copy link
Author

Hi,
I am tried all three now.

  1. Changed UART0 to UART to ESP_CONSOLE_UART_NUM=1
  2. yes
  3. Tested with the example.
    It works now. Communicates with the RS485 device.

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.

@alisitsyn
Copy link
Collaborator

alisitsyn commented Jun 13, 2023

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.

@alisitsyn
Copy link
Collaborator

@diwakrZha ,

Do you have any update on this issue? Thanks.

@Alvin1Zhang Alvin1Zhang added the Awaiting Response awaiting a response from the author label Jun 27, 2023
@diwakrZha
Copy link
Author

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.

@alisitsyn
Copy link
Collaborator

alisitsyn commented Aug 21, 2023

@diwakrZha ,

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.

similar issue

@alisitsyn
Copy link
Collaborator

Hi @diwakrZha ,
Do you have any update on this issue? Thanks.

@espressif-bot espressif-bot added Status: In Progress Work is in progress and removed Status: Opened Issue is new labels Sep 14, 2023
@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: Won't Do This will not be worked on and removed Status: In Progress Work is in progress labels Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Response awaiting a response from the author Resolution: Won't Do This will not be worked on Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

5 participants