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