From 9a3a505612c07dcd2a90d2ceec9f26edac63cf00 Mon Sep 17 00:00:00 2001 From: ericQiang Date: Wed, 22 Aug 2018 06:55:58 +0800 Subject: [PATCH 1/3] fix the mistake of calling modbus API before T35 timer complate initialization --- FreeModbus/modbus/include/mb_m.h | 12 ++++++++++++ FreeModbus/modbus/mb_m.c | 22 +++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/FreeModbus/modbus/include/mb_m.h b/FreeModbus/modbus/include/mb_m.h index f6b25e2..6ae8878 100644 --- a/FreeModbus/modbus/include/mb_m.h +++ b/FreeModbus/modbus/include/mb_m.h @@ -172,6 +172,18 @@ eMBErrorCode eMBMasterEnable( void ); */ eMBErrorCode eMBMasterDisable( void ); +/*! \ingroup modbus + * \brief Check the Modbus Master protocol stack has established or not. + * + * This function must be called and check the return value before calling + * any other functions. + * + * \return If the protocol stack has been established or not + * TRUE. the protocol stack has established + * FALSE. the protocol stack hasn't established + */ +BOOL eMBMasterIsEstablished( void ); + /*! \ingroup modbus * \brief The main pooling loop of the Modbus Master protocol stack. * diff --git a/FreeModbus/modbus/mb_m.c b/FreeModbus/modbus/mb_m.c index 4e7c4b2..efcc584 100644 --- a/FreeModbus/modbus/mb_m.c +++ b/FreeModbus/modbus/mb_m.c @@ -71,7 +71,8 @@ static enum { STATE_ENABLED, STATE_DISABLED, - STATE_NOT_INITIALIZED + STATE_NOT_INITIALIZED, + STATE_ESTABLISHED, } eMBState = STATE_NOT_INITIALIZED; /* Functions pointer which are initialized in eMBInit( ). Depending on the @@ -233,7 +234,7 @@ eMBMasterDisable( void ) { eMBErrorCode eStatus; - if( eMBState == STATE_ENABLED ) + if(( eMBState == STATE_ENABLED ) || ( eMBState == STATE_ESTABLISHED)) { pvMBMasterFrameStopCur( ); eMBState = STATE_DISABLED; @@ -250,6 +251,20 @@ eMBMasterDisable( void ) return eStatus; } +BOOL +eMBMasterIsEstablished( void ) +{ + if(eMBState == STATE_ESTABLISHED) + { + return TRUE; + } + else + { + return FALSE; + } +} + + eMBErrorCode eMBMasterPoll( void ) { @@ -265,7 +280,7 @@ eMBMasterPoll( void ) eMBMasterErrorEventType errorType; /* Check if the protocol stack is ready. */ - if( eMBState != STATE_ENABLED ) + if(( eMBState != STATE_ENABLED ) && ( eMBState != STATE_ESTABLISHED)) { return MB_EILLSTATE; } @@ -277,6 +292,7 @@ eMBMasterPoll( void ) switch ( eEvent ) { case EV_MASTER_READY: + eMBState = STATE_ESTABLISHED; break; case EV_MASTER_FRAME_RECEIVED: From 0ecdf2720f736d906f9d65720b470113d5235ab7 Mon Sep 17 00:00:00 2001 From: nq Date: Wed, 22 Aug 2018 10:05:25 +0800 Subject: [PATCH 2/3] tabs to space --- FreeModbus/modbus/mb_m.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/FreeModbus/modbus/mb_m.c b/FreeModbus/modbus/mb_m.c index efcc584..a276306 100644 --- a/FreeModbus/modbus/mb_m.c +++ b/FreeModbus/modbus/mb_m.c @@ -72,7 +72,7 @@ static enum STATE_ENABLED, STATE_DISABLED, STATE_NOT_INITIALIZED, - STATE_ESTABLISHED, + STATE_ESTABLISHED, } eMBState = STATE_NOT_INITIALIZED; /* Functions pointer which are initialized in eMBInit( ). Depending on the @@ -292,22 +292,22 @@ eMBMasterPoll( void ) switch ( eEvent ) { case EV_MASTER_READY: - eMBState = STATE_ESTABLISHED; + eMBState = STATE_ESTABLISHED; break; case EV_MASTER_FRAME_RECEIVED: eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); /* Check if the frame is for us. If not ,send an error process event. */ - if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) ) - { - ( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE ); - } - else - { - vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); - ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); - } - break; + if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) ) + { + ( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE ); + } + else + { + vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); + ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); + } + break; case EV_MASTER_EXECUTE: ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; From ab2403888e6c3f8686fce1b67db6003803b62e0c Mon Sep 17 00:00:00 2001 From: nq Date: Wed, 22 Aug 2018 10:15:13 +0800 Subject: [PATCH 3/3] tabs to space 2 --- FreeModbus/modbus/mb_m.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FreeModbus/modbus/mb_m.c b/FreeModbus/modbus/mb_m.c index a276306..f42cd00 100644 --- a/FreeModbus/modbus/mb_m.c +++ b/FreeModbus/modbus/mb_m.c @@ -296,8 +296,8 @@ eMBMasterPoll( void ) break; case EV_MASTER_FRAME_RECEIVED: - eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); - /* Check if the frame is for us. If not ,send an error process event. */ + eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); + /* Check if the frame is for us. If not ,send an error process event. */ if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) ) { ( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );