Skip to content

Commit

Permalink
support quirk to reply to broadcast reads only.
Browse files Browse the repository at this point in the history
Strict addressing bans replies to broadcast, and disabling it allows
replies to all broadcast operations.  This is helpful to find a single
device of unknown address via broadcast reads.  However, it's
problematic when trying to use broadcast writes, where you do _not_ want
to get a reply from each device.

Defaults to off, for maximum out of the box compliance.
define MB_QUIRK_REPLY_BROADCAST_READ 1 to enable this feature.

Don't try and combine strict=0 and this quirk at the same time.

Signed-off-by: Karl Palsson <[email protected]>
  • Loading branch information
karlp committed Jun 9, 2022
1 parent 5f6be12 commit 4ea6112
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modbus/include/mbconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ PR_BEGIN_EXTERN_C
#define MB_STRICT_ADDRESSING ( 1 )
#endif

/*! \brief Quirk mode to allow replies to broadcast reads, but not writes.
* This is an alternative to relaxing MB_STRICT_ADDRESSING, which would
* also allow replies to broadcast writes.
*/
#ifndef MB_QUIRK_REPLY_BROADCAST_READ
#define MB_QUIRK_REPLY_BROADCAST_READ ( 0 )
#endif

/*! \brief provide a notification hook in the rx path.
* Must provide a hook function of the form:
* vMBPortNotifyRX(UCHAR *rxdata, USHORT length)
Expand Down
18 changes: 18 additions & 0 deletions modbus/mb.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,20 @@ eMBPoll( void )
if( ucRcvAddress != MB_ADDRESS_BROADCAST )
{
#endif

#if MB_QUIRK_REPLY_BROADCAST_READ > 0
/*
* reply to any non-broadcast, and also broadcasts for these
* few functions used for discovering devices.
*/
if (!(ucRcvAddress == MB_ADDRESS_BROADCAST) ||
((ucRcvAddress == MB_ADDRESS_BROADCAST) && (
(ucFunctionCode == MB_FUNC_READ_INPUT_REGISTER) ||
(ucFunctionCode == MB_FUNC_READ_HOLDING_REGISTER) ||
(ucFunctionCode == MB_FUNC_OTHER_REPORT_SLAVEID))
)) {
#endif

if( eException != MB_EX_NONE )
{
/* An exception occured. Build an error frame. */
Expand All @@ -411,6 +425,10 @@ eMBPoll( void )
#endif
/* Receive address == our address, or broadcast, if allowed */
eStatus = peMBFrameSendCur( ucRcvAddress, ucMBFrame, usLength );
#if MB_QUIRK_REPLY_BROADCAST_READ > 0
}
#endif

#if MB_STRICT_ADDRESSING > 0
}
#endif
Expand Down

0 comments on commit 4ea6112

Please sign in to comment.