From 215b4fa7e162d78621c2072beed4306fc9cb9870 Mon Sep 17 00:00:00 2001 From: Ana Rodriguez Date: Thu, 9 Jan 2025 10:43:37 +0100 Subject: [PATCH] New API functions to access constructed response frame --- src/modbus.c | 38 ++++++++++++++++++++++++++++++-------- src/modbus.h | 4 ++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/modbus.c b/src/modbus.c index e3737bb2..b94114f0 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -773,22 +773,21 @@ static int response_exception(modbus_t *ctx, return rsp_length; } -/* Send a response to the received request. - Analyses the request and constructs a response. +/* Analyses the request and constructs a response. - If an error occurs, this function construct the response + If an error occurs, this function constructs the response accordingly. */ -int modbus_reply(modbus_t *ctx, - const uint8_t *req, - int req_length, - modbus_mapping_t *mb_mapping) +int modbus_reply_construct(modbus_t *ctx, + const uint8_t *req, + int req_length, + modbus_mapping_t *mb_mapping, + uint8_t *rsp) { unsigned int offset; int slave; int function; uint16_t address; - uint8_t rsp[MAX_MESSAGE_LENGTH]; int rsp_length = 0; sft_t sft; @@ -1189,6 +1188,29 @@ int modbus_reply(modbus_t *ctx, !(ctx->quirks & MODBUS_QUIRK_REPLY_TO_BROADCAST)) { return 0; } + return rsp_length; +} + +int modbus_reply_send(modbus_t *ctx, uint8_t *rsp, int rsp_length) +{ + return send_msg(ctx, rsp, rsp_length); +} + +/* Send a response to the received request. + Analyses the request and constructs a response. + + If an error occurs, this function construct the response + accordingly. +*/ +int modbus_reply(modbus_t *ctx, + const uint8_t *req, + int req_length, + modbus_mapping_t *mb_mapping) +{ + uint8_t rsp[MAX_MESSAGE_LENGTH]; + int rsp_length; + + rsp_length = modbus_reply_construct(ctx, req, req_length, mb_mapping, rsp); return send_msg(ctx, rsp, rsp_length); } diff --git a/src/modbus.h b/src/modbus.h index fa7ec4a2..e594a82e 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -275,6 +275,10 @@ MODBUS_API int modbus_reply(modbus_t *ctx, int req_length, modbus_mapping_t *mb_mapping); MODBUS_API int +modbus_reply_construct(modbus_t *ctx, const uint8_t *req, + int req_length, modbus_mapping_t *mb_mapping, uint8_t *rsp); +MODBUS_API int modbus_reply_send(modbus_t *ctx, uint8_t *rsp, int rsp_length); +MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, unsigned int exception_code); MODBUS_API int modbus_enable_quirks(modbus_t *ctx, unsigned int quirks_mask); MODBUS_API int modbus_disable_quirks(modbus_t *ctx, unsigned int quirks_mask);