nanoMODBUS icon indicating copy to clipboard operation
nanoMODBUS copied to clipboard

Exception Sent for Ignored Messages in handle_req_fc Function

Open simonbarreaueq opened this issue 9 months ago • 1 comments

Hello,

When the address doesn't match our configuration, the function is checked anyway, and the modbus replies an exception_msg if the function is not recognized.

static nmbs_error recv_req_header(nmbs_t *nmbs, bool *first_byte_received) {
  nmbs_error err = recv_msg_header(nmbs, first_byte_received);
  if (err != NMBS_ERROR_NONE)
    return err;

  if (nmbs->platform.transport == NMBS_TRANSPORT_RTU) {
    // Check if request is for us
    if (nmbs->msg.unit_id == NMBS_BROADCAST_ADDRESS)
      nmbs->msg.broadcast = true;
    else if (nmbs->msg.unit_id != nmbs->address_rtu)
      nmbs->msg.ignored = true;
    else
      nmbs->msg.ignored = false;
  }

Here, the message is detected as ignored, which is correct. However, in the handle_req_fc function, an exception is sent even if the message is ignored:

static nmbs_error handle_req_fc(nmbs_t *nmbs) {
  NMBS_DEBUG_PRINT("fc %d\t", nmbs->msg.fc);

  nmbs_error err = NMBS_ERROR_NONE;
  switch (nmbs->msg.fc) {
#ifndef NMBS_SERVER_READ_COILS_DISABLED
  case 1:
    err = handle_read_coils(nmbs);
    break;
#endif

#ifndef NMBS_SERVER_READ_DISCRETE_INPUTS_DISABLED
  case 2:
    err = handle_read_discrete_inputs(nmbs);
    break;
#endif

[...]

#ifndef NMBS_SERVER_READ_DEVICE_IDENTIFICATION_DISABLED
  case 43:
    err = handle_read_device_identification(nmbs);
    break;
#endif
  default:
     err = send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_FUNCTION);
  }

  return err;
}

If I go to "default" I send an exception msg even if the message is detected as ignored. I added the following fix, which works for me

default:
	  if (!nmbs->msg.ignored){
		  err = send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_FUNCTION);
	  }
	  break;
  }

However, I think the polling will restart even if the frame is completely finished by the server.

simonbarreaueq avatar Mar 20 '25 15:03 simonbarreaueq

This was fixed awhile ago.

See #78

pseudotronics avatar Apr 01 '25 02:04 pseudotronics