espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.36k stars 7.21k forks source link

ESP32 RS485 Communication Max485 (IDFGH-9412) #10781

Closed maialbe2823 closed 5 months ago

maialbe2823 commented 1 year ago

Answers checklist.

IDF version.

4.4

Operating System used.

Windows

How did you build your project?

Command line with Make

If you are using Windows, please specify command line type.

None

What is the expected behavior?

I am using a MAX485 in order to achieve RS485 communication for ESP32 and other devices. However, ESP32 doesn't see any data present but there is. It keeps saying that it has no data on the receiving end while in truth there is. I have check lots of samples regarding rs485 but I haven't been able to make any of them work. I have a an exact setup as the following: ESP32-DOIT-DevKit-v1-rs458-max3485-module-connection_bb

Any other information need it please let me know. Thanks

What is the actual behavior?

It should read all the data available and send as well.

Steps to reproduce.

ESP32-DOIT-DevKit-v1-rs458-max3485-module-connection_bb

Build or installation Logs.

Log monitor keeps saying that there is no data available.

More Information.

No response

alisitsyn commented 1 year ago

Hello @maialbe2823 ,

The RS485 communication mode is supported by esp-idf driver and tested. Let us start to check from your connection diagram first then we can go ahead. I see on your connection diagram that your blue board which is RS485 transceiver is connected incorrectly to your board. For example: RO - receiver output is connected to TX2 - transmitter output pin, DI - data (transmitter) input is connected to RX2 - receiver input pin, which is opposite direction. Please check the connection and add your code with the configuration of UART pins you are using to check the functionality of the RS485 interface. Also I would suggest you to use the RS485 echo example to check the communication interface.

Please also refer to: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/uart.html#overview-of-rs485-specific-communication-0ptions

Once the communication issue is resolved we can follow with your Bacnet MS/TP issue. Please also take a look to Modbus serial port driver which can be useful for the Bacnet MS/TP port.

maialbe2823 commented 1 year ago

I’ll start changing the connection and checking everything else. I’ll let you know. Thanks.On Feb 17, 2023, at 4:04 AM, Alex Lisitsyn @.***> wrote: Hello @maialbe2823 , The RS485 communication mode is supported by esp-idf driver and tested. Let us start to check from your connection diagram first then we can go ahead. I see on your connection diagram that your blue board which is RS485 transceiver is connected incorrectly to your board. For example: RO - receiver output is connected to TX2, DI - data (transmitter) input is connected to RX2 which is opposite direction. Please check the connection and add your code with the configuration of UART pins your are using to check the functionality of the RS485 interface. Also I would suggest you to use the RS485 echo example to check the communication interface. Please also refer to: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/uart.html#overview-of-rs485-specific-communication-0ptions Once the communication issue is resolved we can follow with your Bacnet MS/TP issue. Please also take a look to Modbus serial port driver which can be useful for the Bacnet MS/TP port.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

maialbe2823 commented 1 year ago

@alisitsyn I have made the changes you mentioned but it did not work. Below is the UART config that I am using:

define UART0_TXD (17)

define UART0_RXD (16)

define UART0_RTS (18)

define BUF_SIZE (1024)

define RD_BUF_SIZE (BUF_SIZE)

define PACKET_READ_TICS (500 / portTICK_RATE_MS)

static void rs485_baud_rate_configure(void) { uart_config_t uart0_config = { .baud_rate = Baud_Rate, .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, .source_clk = UART_SCLK_APB, }; ESP_LOGI(TAG, "RS485 start and configure UART."); uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 20, &uart_queue, 0); // Configure UART parameters uart_param_config(UART_NUM_1, &uart0_config); uart_set_pin(UART_NUM_1, UART0_TXD, UART0_RXD, UART0_RTS, UART_PIN_NO_CHANGE); uart_set_mode(UART_NUM_1, UART_MODE_RS485_HALF_DUPLEX);

//Reads uart and puts data to FIFO Buffer
xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);

}

One thing that I am checking is that ESP32 is not switching my RTS pin which is GPIO 18 from 0 to 3.3v(0-1) so it just gets stuck receiving data. However; even that data I get it is not the full data I am missing a bit and sometimes even more. How can I tackle this? Thanks.

alisitsyn commented 1 year ago

Hi @maialbe2823,

Depending on your ESP32 module you are using the pins 16, 17 can be used for other purpose and are not available for UART. In the ESP32-WROVER modules Pin 16 is used as the chip select for PSRAM, and 17 is used for the clock signal. Please use the original rs485 echo example with the default pins first then you can experiment with the code. The pin 18 should not be a problem if it is not configured for other purpose (peripheral). Please try again with default pins and then prepare the logic analyzer or oscilloscope diagram of RTS, TXD, RXD pins to find the issue on your side.

The ESP32 wrover information.

4 Peripherals and Sensors
Please refer to Section Peripherals and Sensors in ESP32 Datasheet.
Note:
1. GPIO12 is internally pulled high in the module and is not recommended for use as a touch pin.
2. External connections can be made to any GPIO except for GPIOs in the range 6-11, 16, or 17. GPIOs 6-11 are
connected to the module’s integrated SPI flash and PSRAM. GPIOs 16 and 17 are connected to the module’s
integrated PSRAM. For details, please see Chapter 6 Schematics.

Thanks.

maialbe2823 commented 1 year ago

I made such changes, however it still works the same as it did with pins 16 and 17. I can read data over and over but whenever it is processing such data it just says there's none. Also, for the transmitting part it just sends separates bits instead of sending a whole 8 bit frame as I have it. It is like its not capable of sending more than one bit in one single frame. What is that for?

alisitsyn commented 1 year ago

@maialbe2823,

I just checked this again in order to check if some update made a regression but it works just fine with the v4.4 and later. Again, please send the following artifacts:

  1. oscillogram or logic analyzer picture of your signals, RXD, TXD, RTS, A, B on RS485 transceiver,
  2. your connection diagram or a clear picture of connections,
  3. the log for the rs485 echo example.
  4. Check the VCC pin of the RS485 transceiver = 3.3V and the transceiver is 3.3V compatible. It can work properly if your RS485 transceiver board works correctly. However, it could be broken after your incorrect connection in the previous step. Change the transceiver to new one and check it again later.

The RS485 echo example should receive and show the data received from other side and then sends it back.

Thanks.

alisitsyn commented 1 year ago

Hi @maialbe2823,

Could you give some update for this issue? Is this possible to send some picture of the signals from the TXD, RXD, RTS, D+, D- pins with the rs485_echo example?

maialbe2823 commented 1 year ago

The trials I am doing are using a basic oscilloscope and I am getting ESP to send data however it is like if the 8 bytes frame is too slow for other BACnet devices. Does the RS485_echo sample supports uart events in order to read data into a buffer and while also have it transmit 8 bytes? Also, which RS485 transceiver do you recommend? Since my Bacnet devices are RS485 5v not 3.3v. This is the one I am using :https://www.amazon.com/MAX485-Conversion-Transceiver-Development-Accessories/dp/B0B3M456QG/ref=sr_1_5?crid=1P6KU1I7UO9MS&keywords=rs485+transceiver+esp32&qid=1680025014&sprefix=rs485+transceiver+esp32%2Caps%2C107&sr=8-5 Thank you.

alisitsyn commented 1 year ago

The trials I am doing are using a basic oscilloscope and I am getting ESP to send data however it is like if the 8 bytes frame is too slow for other BACnet devices.

The simple and cheap logic analyzer can help to debug what happened during communication on your side. The RS485 communication has been tested up to 1Mbit. However, the RS485 direction control is performed in the driver by software to mitigate the HW issues and can cause some delays. This usually is not a problem for communication. I propose to test your communication interface with simple USB-RS485 adapter + PC first then you can check the communication with your devices.

Does the RS485_echo sample supports uart events in order to read data into a buffer and while also have it transmit 8 bytes?

The RS485_echo is very simple and just simply receives the data then sends it back to external interface as a strings. It uses the UART driver in HALF DUPLEX mode and its events and the ring buffers for RX/TX, in some conditions in can include gaps in the transmitted frame which is not a problem for this example. The uart events example can be used to monitor what happened, can be changed to work in HALF DUPLEX mode as well.

Also, which RS485 transceiver do you recommend? Since my Bacnet devices are RS485 5v not 3.3v. This is the one I am using

The transceiver you are using is 5V based. However it can work normally with VCC = 3.3V for short distance with your BACnet devices. However it is not recommended to use it with 5V VCC without level shifter with conjunction with ESP32. For better result I would recommend you to use the RS485 driver with galvanic isolation like ADM2484 which will provide correct level shifting on ESP32 side and the same time will allow to solve many communication problems with your BACnet devices with 5V transceivers. There are many equivalent IC and design options like here.

maialbe2823 commented 1 year ago

Got it. I'll try the above for the next few days and will let you know. Thanks again.

maialbe2823 commented 1 year ago

I have achieve a correct level shifter. Since this now my Bacnet devices do see sometimes a whole frame package. However, I am not able to keep esp uart working completely right while transmitting. I think that the problem is within the initialization of the uart itself. The receiving is good though as I see the other device's data. But the ESP is not responding in the way is it supposed to as the uart is not working well for transmitting and after sometime it just keeps transmitting and shuts up the tx of the bacnet device. Any suggestion on how can I fix the uart configuration in order to have the esp uart working correctly for rs485 protocol? Thank you.

alisitsyn commented 1 year ago

@maialbe2823 ,

You can refer to initialization of serial port for modbus for more information on how to transmit and receive data from your RS485 based devices. Here is the simple RS485 driver based on HAL which supports the collision detection.

Hope this can help to resolve your issues.

maialbe2823 commented 1 year ago

Got it, I'll try it out and see what happens. Thank you.

preveen-stack commented 1 year ago

@maialbe2823 can you update the status. I have been using ESP32 MODBUS-RS485 with Texas Instruments SN65HVD72DR and SN65HVD1785 transceiver from 2020 and had never had any issues

alisitsyn commented 5 months ago

@maialbe2823,

The issue will be closed because no any update for a long time. Feel free to reopen the issue if you have some update.