RS485 UART returns incorrect messages

jaspernorth
Posts: 1
Joined: Tue Feb 04, 2020 7:01 pm

RS485 UART returns incorrect messages

Postby jaspernorth » Tue Feb 04, 2020 7:14 pm

Hello everyone,

I'm trying to communicate with an external control unit with a bunch of locks and sensors over RS485 but am unable to receive the data in the correct format. It's pretty much just a bunch of jibberish I receive.

The format should be 02 xx xx xx xx 03 SUM but I receive all kinds of random bytes. Maybe the PIN is incorrect or I should use a different method, maybe the modbus master one?

Sending commands to the board works perfectly which makes me think the connection is pretty solid.

I'm using the ESP32 devkit c with an esp32-wroom-32D

My pin layout is as follows:

RX: 22 (connected to R0 on MAX485)
TX: 23 (connected to DI on MAX485)
RTS: 18 (connected to RE&DE on MAX485)

My code is:

Code: Select all

void control_unit_task(void *arg) {
    ESP_LOGI(TAG, "Initialising lock control unit connection...");

    // Install UART driver (we don't need an event queue here)
    uart_driver_install(uart_num, 127 * 2, 0, 0, NULL, 0);

    // Configure UART parameters
    uart_param_config(uart_num, &uart_config);

    ESP_LOGI(TAG, "UART set pins, mode and install driver.");
    // Set UART1 pins(TX: IO23, RX: I022, RTS: IO18, CTS: IO19)
    uart_set_pin(uart_num, TXD_PIN, RXD_PIN, CU_RTS_PIN, CU_CTS_PIN);

    // Set RS485 half duplex mode
    uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX);

    ESP_LOGI(TAG, "Starting lock control unit read loop...");

    // Allocate buffers for UART
    int responseBytesProcessed = 0;
    uint8_t data[128];
    uint8_t response[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

    // Read data from UART
    while (1) {
        int len = uart_read_bytes(uart_num, data, 127, 100);
        if (len > 0) {
            // ESP_LOGI(TAG, "Received %u bytes:", len);

            for (int i = 0; i < len; i++) {
            	// Print Received Byte in HEX
                printf("0x%.2X ", data[i]);

                if ((data[i] == 0x02 && responseBytesProcessed == 0) || responseBytesProcessed > 0) {
                    response[responseBytesProcessed] = data[i];
                    responseBytesProcessed++;
                }

                if (responseBytesProcessed == 9) {
                    // Reset count
                    responseBytesProcessed = 0;

                    // Process control unit response
                    process_message(response);
                }
            }

            printf("\n");
        }

        // Not entirely sure if this is necessary
        // uart_flush(uart_num);
    }
}
After some more research on this I'm starting to think the protocol is maybe using modbus and I should use the modbus utilities by esp-idf. I've been experimenting a bit with that but I'm not entirely sure how that works...

I've attached the protocol documentation I've received with the board in case anyone with experience in RS485/modbus would want to have a look at it.
Attachments
Kerong CU-Protocol.pdf
Protocol documentation
(93.72 KiB) Downloaded 358 times

ESP_alisitsyn
Posts: 203
Joined: Fri Feb 01, 2019 4:02 pm
Contact:

Re: RS485 UART returns incorrect messages

Postby ESP_alisitsyn » Wed Mar 11, 2020 10:21 am

Hi @jaspernorth,

The protocol you provided is not the Modbus protocol but some custom protocol. ESP-IDF does not support this protocol.

--
Alex

ESP_houwenxiang
Posts: 118
Joined: Tue Jun 26, 2018 3:09 am

Re: RS485 UART returns incorrect messages

Postby ESP_houwenxiang » Mon Mar 16, 2020 10:14 am

RS485 is half-duplex communication, the data sent is also received, so when you finish sending data, you need to call `uart_flush_input`.
Not sure if it solves your issue

thanks !!
wookooho

Who is online

Users browsing this forum: Bing [Bot] and 114 guests