Skip to content

Commit 7bb760e

Browse files
committed
New API functions to access constructed response frame
1 parent bb13443 commit 7bb760e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/modbus.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,32 @@ int modbus_reply(modbus_t *ctx,
783783
const uint8_t *req,
784784
int req_length,
785785
modbus_mapping_t *mb_mapping)
786+
{
787+
uint8_t rsp[MAX_MESSAGE_LENGTH];
788+
int rsp_length;
789+
790+
rsp_length = modbus_reply_construct(ctx, req, req_length, mb_mapping, rsp);
791+
if (rsp_length > 0) {
792+
return send_msg(ctx, rsp, rsp_length);
793+
}
794+
return rsp_length;
795+
}
796+
797+
/* Analyses the request and constructs a response.
798+
799+
If an error occurs, this function constructs the response
800+
accordingly.
801+
*/
802+
int modbus_reply_construct(modbus_t *ctx,
803+
const uint8_t *req,
804+
int req_length,
805+
modbus_mapping_t *mb_mapping,
806+
uint8_t *rsp)
786807
{
787808
unsigned int offset;
788809
int slave;
789810
int function;
790811
uint16_t address;
791-
uint8_t rsp[MAX_MESSAGE_LENGTH];
792812
int rsp_length = 0;
793813
sft_t sft;
794814

@@ -1189,7 +1209,15 @@ int modbus_reply(modbus_t *ctx,
11891209
!(ctx->quirks & MODBUS_QUIRK_REPLY_TO_BROADCAST)) {
11901210
return 0;
11911211
}
1192-
return send_msg(ctx, rsp, rsp_length);
1212+
return rsp_length;
1213+
}
1214+
1215+
int modbus_reply_send(modbus_t *ctx, uint8_t *rsp, int rsp_length)
1216+
{
1217+
if (rsp_length > 0) {
1218+
return send_msg(ctx, rsp, rsp_length);
1219+
}
1220+
return rsp_length;
11931221
}
11941222

11951223
int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, unsigned int exception_code)

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)