From 3ea5343f0df729eae2bed5f5c0cb5c31e1415001 Mon Sep 17 00:00:00 2001 From: Marius Scholtz Date: Fri, 21 Jan 2022 09:03:09 -0800 Subject: [PATCH] modbus: serial: Fix incomplete transmission issue This patch is to fix an issue where the transceiver chip is disabled before it has transmitted all data. This causes the message to be corrupted because the last few bytes are missing. The fix adds a check to make sure the transmission is completed before disabling the transceiver. Signed-off-by: Marius Scholtz --- subsys/modbus/modbus_serial.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/subsys/modbus/modbus_serial.c b/subsys/modbus/modbus_serial.c index 32aa525ad8d..0289c001bc6 100644 --- a/subsys/modbus/modbus_serial.c +++ b/subsys/modbus/modbus_serial.c @@ -362,7 +362,14 @@ static void cb_handler_tx(struct modbus_context *ctx) cfg->uart_buf_ctr); cfg->uart_buf_ctr -= n; cfg->uart_buf_ptr += n; - } else { + return; + } + + /* Must wait till the transmission is complete or + * RS-485 tranceiver could be disabled before all data has + * been transmitted and message will be corrupted. + */ + if (uart_irq_tx_complete(cfg->dev)) { /* Disable transmission */ cfg->uart_buf_ptr = &cfg->uart_buf[0]; modbus_serial_tx_off(ctx);