Skip to content

Commit 215b4fa

Browse files
committed
New API functions to access constructed response frame
1 parent bb13443 commit 215b4fa

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/modbus.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -773,22 +773,21 @@ static int response_exception(modbus_t *ctx,
773773
return rsp_length;
774774
}
775775

776-
/* Send a response to the received request.
777-
Analyses the request and constructs a response.
776+
/* Analyses the request and constructs a response.
778777
779-
If an error occurs, this function construct the response
778+
If an error occurs, this function constructs the response
780779
accordingly.
781780
*/
782-
int modbus_reply(modbus_t *ctx,
783-
const uint8_t *req,
784-
int req_length,
785-
modbus_mapping_t *mb_mapping)
781+
int modbus_reply_construct(modbus_t *ctx,
782+
const uint8_t *req,
783+
int req_length,
784+
modbus_mapping_t *mb_mapping,
785+
uint8_t *rsp)
786786
{
787787
unsigned int offset;
788788
int slave;
789789
int function;
790790
uint16_t address;
791-
uint8_t rsp[MAX_MESSAGE_LENGTH];
792791
int rsp_length = 0;
793792
sft_t sft;
794793

@@ -1189,6 +1188,29 @@ int modbus_reply(modbus_t *ctx,
11891188
!(ctx->quirks & MODBUS_QUIRK_REPLY_TO_BROADCAST)) {
11901189
return 0;
11911190
}
1191+
return rsp_length;
1192+
}
1193+
1194+
int modbus_reply_send(modbus_t *ctx, uint8_t *rsp, int rsp_length)
1195+
{
1196+
return send_msg(ctx, rsp, rsp_length);
1197+
}
1198+
1199+
/* Send a response to the received request.
1200+
Analyses the request and constructs a response.
1201+
1202+
If an error occurs, this function construct the response
1203+
accordingly.
1204+
*/
1205+
int modbus_reply(modbus_t *ctx,
1206+
const uint8_t *req,
1207+
int req_length,
1208+
modbus_mapping_t *mb_mapping)
1209+
{
1210+
uint8_t rsp[MAX_MESSAGE_LENGTH];
1211+
int rsp_length;
1212+
1213+
rsp_length = modbus_reply_construct(ctx, req, req_length, mb_mapping, rsp);
11921214
return send_msg(ctx, rsp, rsp_length);
11931215
}
11941216

src/modbus.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ MODBUS_API int modbus_reply(modbus_t *ctx,
275275
int req_length,
276276
modbus_mapping_t *mb_mapping);
277277
MODBUS_API int
278+
modbus_reply_construct(modbus_t *ctx, const uint8_t *req,
279+
int req_length, modbus_mapping_t *mb_mapping, uint8_t *rsp);
280+
MODBUS_API int modbus_reply_send(modbus_t *ctx, uint8_t *rsp, int rsp_length);
281+
MODBUS_API int
278282
modbus_reply_exception(modbus_t *ctx, const uint8_t *req, unsigned int exception_code);
279283
MODBUS_API int modbus_enable_quirks(modbus_t *ctx, unsigned int quirks_mask);
280284
MODBUS_API int modbus_disable_quirks(modbus_t *ctx, unsigned int quirks_mask);

0 commit comments

Comments
 (0)