diff --git a/FreeModbus 主机分析图.vsd b/FreeModbus 主机分析图.vsd index 51b6809..6e6c68b 100644 Binary files a/FreeModbus 主机分析图.vsd and b/FreeModbus 主机分析图.vsd differ diff --git a/FreeModbus/modbus/functions/mbfunccoils_m.c b/FreeModbus/modbus/functions/mbfunccoils_m.c index c78ad10..324bed5 100644 --- a/FreeModbus/modbus/functions/mbfunccoils_m.c +++ b/FreeModbus/modbus/functions/mbfunccoils_m.c @@ -120,7 +120,12 @@ eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen ) eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; - if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) + /* If this request is broadcast, and it's read mode. This request don't need execute. */ + if ( xMBMasterRequestIsBroadcast() ) + { + eStatus = MB_EX_NONE; + } + else if ( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) { vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); diff --git a/FreeModbus/modbus/functions/mbfuncdisc_m.c b/FreeModbus/modbus/functions/mbfuncdisc_m.c index 5cc35e2..ed865ed 100644 --- a/FreeModbus/modbus/functions/mbfuncdisc_m.c +++ b/FreeModbus/modbus/functions/mbfuncdisc_m.c @@ -104,7 +104,12 @@ eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen ) eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; - if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) + /* If this request is broadcast, and it's read mode. This request don't need execute. */ + if ( xMBMasterRequestIsBroadcast() ) + { + eStatus = MB_EX_NONE; + } + else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) { vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); diff --git a/FreeModbus/modbus/functions/mbfuncholding_m.c b/FreeModbus/modbus/functions/mbfuncholding_m.c index ffbe294..ef81062 100644 --- a/FreeModbus/modbus/functions/mbfuncholding_m.c +++ b/FreeModbus/modbus/functions/mbfuncholding_m.c @@ -294,7 +294,12 @@ eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen ) eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; - if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) + /* If this request is broadcast, and it's read mode. This request don't need execute. */ + if ( xMBMasterRequestIsBroadcast() ) + { + eStatus = MB_EX_NONE; + } + else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) { vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); @@ -397,7 +402,12 @@ eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; - if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN ) + /* If this request is broadcast, and it's read mode. This request don't need execute. */ + if ( xMBMasterRequestIsBroadcast() ) + { + eStatus = MB_EX_NONE; + } + else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN ) { vMBMasterGetPDUSndBuf(&ucMBFrame); usRegReadAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] << 8U ); diff --git a/FreeModbus/modbus/functions/mbfuncinput_m.c b/FreeModbus/modbus/functions/mbfuncinput_m.c index 728ecc0..653414f 100644 --- a/FreeModbus/modbus/functions/mbfuncinput_m.c +++ b/FreeModbus/modbus/functions/mbfuncinput_m.c @@ -103,7 +103,12 @@ eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen ) eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; - if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) + /* If this request is broadcast, and it's read mode. This request don't need execute. */ + if ( xMBMasterRequestIsBroadcast() ) + { + eStatus = MB_EX_NONE; + } + else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) { vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); diff --git a/FreeModbus/modbus/include/mb_m.h b/FreeModbus/modbus/include/mb_m.h index abe4889..15ebb0e 100644 --- a/FreeModbus/modbus/include/mb_m.h +++ b/FreeModbus/modbus/include/mb_m.h @@ -389,6 +389,7 @@ void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode ); USHORT usMBMasterGetPDUSndLength( void ); void vMBMasterSetPDUSndLength( USHORT SendPDULength ); void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode ); +BOOL xMBMasterRequestIsBroadcast( void ); eMBMasterErrorEventType eMBMasterGetErrorType( void ); void vMBMasterSetErrorType( eMBMasterErrorEventType errorType ); eMBMasterReqErrCode vMBMasterWaitRequestFinish( void ); diff --git a/FreeModbus/modbus/include/mbconfig.h b/FreeModbus/modbus/include/mbconfig.h index 0862c70..89030b7 100644 --- a/FreeModbus/modbus/include/mbconfig.h +++ b/FreeModbus/modbus/include/mbconfig.h @@ -116,8 +116,8 @@ PR_BEGIN_EXTERN_C * And if slave is not respond in this time,the master will process this timeout error. * Then master can send other frame */ #define MB_MASTER_TIMEOUT_MS_RESPOND (100 ) -/*! \brief The total slaves in Modbus Master system.Default 16. - * Note : The slave ID must be continuous from 0.*/ +/*! \brief The total slaves in Modbus Master system. Default 16. + * \note : The slave ID must be continuous from 1.*/ #define MB_MASTER_TOTAL_SLAVE_NUM ( 16 ) #endif diff --git a/FreeModbus/modbus/mb_m.c b/FreeModbus/modbus/mb_m.c index 962bb13..4e7c4b2 100644 --- a/FreeModbus/modbus/mb_m.c +++ b/FreeModbus/modbus/mb_m.c @@ -259,7 +259,7 @@ eMBMasterPoll( void ) static USHORT usLength; static eMBException eException; - int i; + int i , j; eMBErrorCode eStatus = MB_ENOERR; eMBMasterEventType eEvent; eMBMasterErrorEventType errorType; @@ -305,16 +305,24 @@ eMBMasterPoll( void ) for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) { /* No more function handlers registered. Abort. */ - if (xMasterFuncHandlers[i].ucFunctionCode == 0) - { + if (xMasterFuncHandlers[i].ucFunctionCode == 0) { break; } - else if (xMasterFuncHandlers[i].ucFunctionCode - == ucFunctionCode) - { + else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) { vMBMasterSetCBRunInMasterMode(TRUE); - eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, - &usLength); + /* If master request is broadcast, + * the master need execute function for all slave. + */ + if ( xMBMasterRequestIsBroadcast() ) { + usLength = usMBMasterGetPDUSndLength(); + for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){ + vMBMasterSetDestAddress(j); + eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); + } + } + else { + eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); + } vMBMasterSetCBRunInMasterMode(FALSE); break; } diff --git a/FreeModbus/modbus/rtu/mbrtu_m.c b/FreeModbus/modbus/rtu/mbrtu_m.c index 55ff739..4756f57 100644 --- a/FreeModbus/modbus/rtu/mbrtu_m.c +++ b/FreeModbus/modbus/rtu/mbrtu_m.c @@ -395,10 +395,9 @@ xMBMasterRTUTimerExpired(void) eSndState = STATE_M_TX_IDLE; vMBMasterPortTimersDisable( ); - /* If timer mode is convert delay ,then Master is idel now. */ + /* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */ if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) { - vMBMasterCBRequestScuuess( ); - vMBMasterRunResRelease( ); + xNeedPoll = xMBMasterPortEventPost( EV_MASTER_EXECUTE ); } return xNeedPoll; @@ -433,5 +432,10 @@ void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode ) { eMasterCurTimerMode = eMBTimerMode; } + +/* The master request is broadcast? */ +BOOL xMBMasterRequestIsBroadcast( void ){ + return xFrameIsBroadcast; +} #endif