Skip to content
Closed
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
16 changes: 16 additions & 0 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,22 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
if (length_to_read == 0) {
switch (step) {
case _STEP_FUNCTION:
/* HACK: derive the length from the Length field of the MBAP. */
if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_TCP) {
int mbap_length_field = (
(((int)(msg[4])) << 8) |
((int)(msg[5]))
);
if (mbap_length_field < 2) {
errno = EMBBADDATA;
_error_print(ctx, "need more data");
return -1;
}
length_to_read = mbap_length_field - 2 /* the unit identifier and function code. */;
step = _STEP_DATA;
break;
}

/* Function code position */
length_to_read = compute_meta_length_after_function(
msg[ctx->backend->header_length],
Expand Down