Skip to content

Commit

Permalink
New API functions to access constructed response frame
Browse files Browse the repository at this point in the history
  • Loading branch information
anrodlo committed Jan 9, 2025
1 parent bb13443 commit 215b4fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions src/modbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 215b4fa

Please sign in to comment.