Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API functions to access constructed response frame #786 #787

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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