From 767a87b1b658ec8a45a87b100bf0c9c22d7f1eb6 Mon Sep 17 00:00:00 2001 From: Johann Fischer Date: Sat, 8 Jan 2022 12:34:58 +0100 Subject: [PATCH] modbus: keep transaction and protocol IDs in gateway mode Serial line interface does not use transaction and protocol IDs. Continue to use memcpy() for the whole frame as it is safer and more efficient but temporarily store transaction and protocol IDs, and write it back if the transfer was successful. Signed-off-by: Johann Fischer --- subsys/modbus/modbus_raw.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/subsys/modbus/modbus_raw.c b/subsys/modbus/modbus_raw.c index e4e330f5ad8..665e9187f28 100644 --- a/subsys/modbus/modbus_raw.c +++ b/subsys/modbus/modbus_raw.c @@ -147,7 +147,17 @@ int modbus_raw_backend_txn(const int iface, struct modbus_adu *adu) err = modbus_tx_wait_rx_adu(ctx); if (err == 0) { + /* + * Serial line does not use transaction and protocol IDs. + * Temporarily store transaction and protocol IDs, and write it + * back if the transfer was successful. + */ + uint16_t trans_id = adu->trans_id; + uint16_t proto_id = adu->proto_id; + memcpy(adu, &ctx->rx_adu, sizeof(struct modbus_adu)); + adu->trans_id = trans_id; + adu->proto_id = proto_id; } else { modbus_set_exception(adu, MODBUS_EXC_GW_TARGET_FAILED_TO_RESP); }