RS-485 support

hwmaier
Posts: 31
Joined: Sun May 28, 2017 7:30 am

Re: RS-485 support

Postby hwmaier » Fri Jun 16, 2017 7:43 am

@aaquilina Today I started working on the RS-485 code again, and I also have this phantom null character at the end of each transmission.

I investigated this further with different line driver circuits.

Circuit A)

Code: Select all

                                +---------------+
                     RX <-------| R             |
                                |              B|----
                     TX ------->| D    MAX485   |
             ESP32              |               |     RS-485 side
                     RTS ------>| DE            |
                                |              A|----
                           +----| /RE           |
                           |    +---------------+
                          GND
This circuit is preferred as it allows collision detection. Echo suppression is done by the ESP32 chip.

- rs485_conf.en = 1
- rs485_conf.tx_rx_en must be set to 0 to remove echo. (This makes sense and would be expected.)
- If rs485_conf.rx_busy_tx_en is set to 1 then we receive a single null character per packet sent :?:
- If rs485_conf.rx_busy_tx_en is set to 0 then we receive a null character for every character sent :?:

Very strange behavior. I did not manage to get rid of the 00 characters. I also confirmed that the 00 characters are not generated by wrong timing of the DE signal. They are always generated when sending with above flags set, regardless of the state of driver enable.

Circuit B)

Code: Select all

                                +---------------+
                     RX <-------| R             |
                                |              B|----
                     TX ------->| D    MAX485   |
             ESP32              |               |     RS-485 side
                     RTS --+--->| DE            |
                           |    |              A|----
                           +----| /RE           |
                                +---------------+

This circuit does not allow collision detection. It suppresses the echo by hardware.

- rs485_conf.en = 1
- rs485_conf.tx_rx_en must be set to 1 to not receive any echo (if 0 we would receive a single null char per packet sent otherwise) :?:
- rs485_conf.rx_busy_tx_en can be either value. (Makes sense and is to be expected)

I have no idea how to configure this RS-485 mode of the ESP32 to not generate these phantom 00 characters.

Clearing the FIFO as suggested by aaquilina is a workaround, but does not seem right and should not be necessary.

fri.sch
Posts: 3
Joined: Tue Apr 11, 2017 7:16 pm

Re: RS-485 support

Postby fri.sch » Fri Aug 04, 2017 1:06 pm

Hi,

I'm trying RS485 on the ESP32 at the moment, using hwmaier's PR with the current esp-idf 2.1 Release.
I have two ESPs connected via MAX13433 transceivers.

Sending on one side and receiving on the other works fine, but I'm having problems with receiving and sending on the same side.
I'm using one ESP as a sender (sending strings using uart_write_bytes()) and on the receiver I get the correct data with uart_read_bytes().
As soon as I add some kind of sending routine (e.g. to echo the incoming data) in between the calls to uart_read_bytes(), I only receive garbage.

I tried playing, with the tx_rx_en and rx_busy_tx_en bits, but didn't get anywhere.
Can someone help with this?

Thanks and regards
Frieder

hwmaier
Posts: 31
Joined: Sun May 28, 2017 7:30 am

Re: RS-485 support

Postby hwmaier » Sun Aug 06, 2017 11:41 pm

@fri.sch Most of the support I added is for half-duplex operation, so you must not send while receiving and you must wait until the other sender is quiet before your side starts sending. Otherwise you get collisions and the data will be garbled.

marek_h
Posts: 1
Joined: Fri Aug 11, 2017 7:41 am

Re: RS-485 support

Postby marek_h » Fri Aug 11, 2017 7:48 am

Hi,
I’am also trying the RS485 communication using hwmaier’s PR. My ESPs are connected according to the Circuit A described above (via MAX485 transceiver). Unfortunately, I only get garbage data (if any).

If I try to communicate with the ESP from linux (Raspi with USB-RS485 converter and simple code sending 8 bytes modbus query and waiting for response – BTW working fine with my temperature sensor) some data are read by ESP, but content is “garbage“ (and data length received is wrong and differs call by call).
If I connect two ESPs together (one only in sending mode, second only listening/reading) I even do not receive anything.

To figure it out I played a lot with the baud-rates (synchronizing UART and ESP, trying different rates but always same for sender-receiver, …), timeouts and thresholds by with no success ☹ As the available documentation is very limited I get stuck here.

Am I missing something? Could somebody help me – point me out what’s wrong/missing. Thank you very much in advance.
Marek

P.S: Here is relevant code (based on UART_echo example from ESP-IDF)

Code: Select all

#define ECHO_TEST_TXD  (18) // Connects to DI pin on the MAX485
#define ECHO_TEST_RXD  (21) // Connects to R0 pin on the MAX485
#define ECHO_TEST_RTS  (23) // Connects to DE pin on the MAX485
#define ECHO_TEST_CTS  (5) // GND is connected to RE pin on the MAX485 (according to the schema) 

#define BUF_SIZE (1024)

static void echo_task()
{
    const int uart_num = UART_NUM_1;
    uart_config_t uart_config = {
        .baud_rate = 19200, //115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, //UART_HW_FLOWCTRL_DISABLE, //UART_HW_FLOWCTRL_CTS_RTS,
        .rx_flow_ctrl_thresh = 122,
    };
    //Configure UART1 parameters
    uart_param_config(uart_num, &uart_config);
    uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
	uart_set_rs485_hd_mode(uart_num, 1);
    uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);

    uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
	char request[] = {0x01, 0x03, 0x01, 0x07, 0x00, 0x01, 0x34, 0x37};
    while(1) {
        //Read data from UART
        int len = uart_read_bytes(uart_num, data, BUF_SIZE,  100/portTICK_RATE_MS);
		//Write data to console
		if (len) {
			//printf("Data length received is: %d \n", len); //Fails on STACK OVERFLOW Error
			ESP_LOGI("DEBUG", "Data length received is: %d", len);
			int a;
			for  (a=0; a < len; a++) { 
				ESP_LOGI("DEBUG", "Data [%d]: %x", a, data[a]);
			}
		}
		//Write data to UART
		/*
		vTaskDelay(2000 / portTICK_PERIOD_MS);
		uart_write_bytes(uart_num, (const char*) request, sizeof(request));
		ESP_LOGI("DEBUG", "Data sent.");
		*/
		uart_write_bytes(uart_num, (const char*) data, len);
    }
}

void app_main()
{
    xTaskCreate(echo_task, "uart_echo_task", 2048, NULL, 10, NULL);
}


fri.sch
Posts: 3
Joined: Tue Apr 11, 2017 7:16 pm

Re: RS-485 support

Postby fri.sch » Thu Aug 17, 2017 9:10 pm

hwmaier wrote:@fri.sch Most of the support I added is for half-duplex operation, so you must not send while receiving and you must wait until the other sender is quiet before your side starts sending. Otherwise you get collisions and the data will be garbled.
I'm using half-duplex and I paid attention to avoid any collisions on the bus by separating the single transmissions by large delays, but I still have the problem that as soon I have sent something on one side the receiving on this side stops working properly.
I will try to do some more research...

EndlessDelirium
Posts: 8
Joined: Wed May 10, 2017 1:12 pm

Re: RS-485 support

Postby EndlessDelirium » Sun Sep 17, 2017 10:44 am

Thanks to pctj101, there is now some example code available: https://github.com/espressif/esp-idf/pull/1006

rohit269
Posts: 24
Joined: Mon Sep 11, 2017 4:22 am

Re: RS-485 support

Postby rohit269 » Fri Oct 13, 2017 8:52 pm

aaquilina wrote:@hwmaier Excellent, that worked perfectly. I just had to switch the levels in the code because my TX is active high.
Hi, can you mention which levels did you switch? My RS485 driver also works with an active high TX.

Thanks.

jas-esp
Posts: 1
Joined: Mon Apr 23, 2018 5:18 pm

Re: RS-485 support

Postby jas-esp » Mon Apr 23, 2018 5:26 pm

I have implemented a cleaner fix for adding RS485 half duplex support along with a code example. The patches are here and a description is on my blog.

Who is online

Users browsing this forum: MikeMyhre and 109 guests