Should a response be issued in broadcast mode when an exception occurs?
Hi there,
I think there may be a possible bug in the library where an exception response will be issued even if the message is a "broadcast" message.
Taking the static nmbs_error handle_write_single_coil(nmbs_t* nmbs) as an example (other write functions handle this situation the same) the exceptions are sent directly before the check to see if the message is a broadcast message.
if (nmbs->callbacks.write_single_coil) {
if (value != 0 && value != 0xFF00)
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_DATA_VALUE); // Instantly transmits response with no broadcast check!
err = nmbs->callbacks.write_single_coil(address, value == 0 ? false : true, nmbs->msg.unit_id,
nmbs->callbacks.arg);
if (err != NMBS_ERROR_NONE) {
if (nmbs_error_is_exception(err))
return send_exception_msg(nmbs, err); // Instantly transmits response with no broadcast check!
return send_exception_msg(nmbs, NMBS_EXCEPTION_SERVER_DEVICE_FAILURE); // Instantly transmits response with no broadcast check!
}
if (!nmbs->msg.broadcast) { // Broadcast check happens here :S
put_res_header(nmbs, 4);
put_2(nmbs, address);
put_2(nmbs, value);
NMBS_DEBUG_PRINT("a %d\tvalue %d", address, value);
err = send_msg(nmbs);
if (err != NMBS_ERROR_NONE)
return err;
}
}
The standard is pretty clear (I think) on this (from the V1.02 Serial spec)
"In unicast mode, the master addresses an individual slave. After receiving and processing the request, the slave returns a message (a 'reply') to the master .In that mode, a MODBUS transaction consists of 2 messages : a request from the master, and a reply from the slave"
"In broadcast mode, the master can send a request to all slaves. No response is returned to broadcast requests sent by the master"
This would imply no response at all should be given by a slave device when receiving a broadcast command? Figure 7 for slave then contradicts this by saying "[When processing the request] Different errors may occur : format error in the request, invalid action, … In case of error, a reply must be sent to the master."
I think personally that it makes sense for no response to be given as that'd be chaos. Freemodbus seems to take this approach (Line 452 here). But interested to hear your thoughts in if this is intended behavior or a bug.
This is definitely a bug. Fixed in b158a43d3c0cd1bdf39e4d3ca020024b341954a8. Thank you