mirror of
https://github.com/armink/FreeModbus_Slave-Master-RTT-STM32.git
synced 2024-09-01 08:09:25 +08:00
1、【增加】FreeModbus主机错误处理回调方法,位于port.c;
Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
parent
5d3bb1b383
commit
26160187b4
@ -389,9 +389,11 @@ UCHAR ucMBMasterGetDestAddress( void );
|
|||||||
void vMBMasterSetDestAddress( UCHAR Address );
|
void vMBMasterSetDestAddress( UCHAR Address );
|
||||||
BOOL xMBMasterGetCBRunInMasterMode( void );
|
BOOL xMBMasterGetCBRunInMasterMode( void );
|
||||||
void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode );
|
void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode );
|
||||||
UCHAR ucMBMasterGetPDUSndLength( void );
|
USHORT usMBMasterGetPDUSndLength( void );
|
||||||
void vMBMasterSetPDUSndLength( UCHAR SendPDULength );
|
void vMBMasterSetPDUSndLength( USHORT SendPDULength );
|
||||||
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode );
|
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode );
|
||||||
|
eMBMasterErrorEventType eMBMasterGetErrorType( void );
|
||||||
|
void vMBMasterSetErrorType( eMBMasterErrorEventType errorType );
|
||||||
|
|
||||||
/* ----------------------- Callback -----------------------------------------*/
|
/* ----------------------- Callback -----------------------------------------*/
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ PR_BEGIN_EXTERN_C
|
|||||||
/*! \brief If master send a frame which is not broadcast,the master will wait sometime for slave.
|
/*! \brief If master send a frame which is not broadcast,the master will wait sometime for slave.
|
||||||
* And if slave is not respond in this time,the master will process this timeout error.
|
* And if slave is not respond in this time,the master will process this timeout error.
|
||||||
* Then master can send other frame */
|
* Then master can send other frame */
|
||||||
#define MB_MASTER_TIMEOUT_MS_RESPOND (2000)
|
#define MB_MASTER_TIMEOUT_MS_RESPOND (100 )
|
||||||
/*! \brief The total slaves in Modbus Master system.Default 16.
|
/*! \brief The total slaves in Modbus Master system.Default 16.
|
||||||
* Note : The slave ID must be continuous from 0.*/
|
* Note : The slave ID must be continuous from 0.*/
|
||||||
#define MB_MASTER_TOTAL_SLAVE_NUM ( 16 )
|
#define MB_MASTER_TOTAL_SLAVE_NUM ( 16 )
|
||||||
|
@ -41,7 +41,7 @@ PR_BEGIN_EXTERN_C
|
|||||||
/*! \ingroup modbus
|
/*! \ingroup modbus
|
||||||
* \brief used for master mode.
|
* \brief used for master mode.
|
||||||
*
|
*
|
||||||
* the mxMBasterRunMutexLock() parameter definitions
|
* the xMBMasterRunResTake() parameter definitions
|
||||||
*/
|
*/
|
||||||
#define MB_WAITING_FOREVER -1 /*!< Block forever until get resource. */
|
#define MB_WAITING_FOREVER -1 /*!< Block forever until get resource. */
|
||||||
#define MB_WAITING_NO 0 /*!< Non-block. */
|
#define MB_WAITING_NO 0 /*!< Non-block. */
|
||||||
@ -65,6 +65,13 @@ typedef enum
|
|||||||
EV_MASTER_ERROR_PROCESS /*!< Frame error process*/
|
EV_MASTER_ERROR_PROCESS /*!< Frame error process*/
|
||||||
} eMBMasterEventType;
|
} eMBMasterEventType;
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
EV_ERROR_RESPOND_TIMEOUT, /*!< Slave respond timeout. */
|
||||||
|
EV_ERROR_RECEIVE_DATA, /*!< Receive frame data erroe. */
|
||||||
|
EV_ERROR_EXECUTE_FUNCTION, /*!< Execute function error. */
|
||||||
|
} eMBMasterErrorEventType;
|
||||||
|
|
||||||
/*! \ingroup modbus
|
/*! \ingroup modbus
|
||||||
* \brief Parity used for characters in serial mode.
|
* \brief Parity used for characters in serial mode.
|
||||||
*
|
*
|
||||||
@ -147,6 +154,16 @@ INLINE void vMBMasterPortTimersRespondTimeoutEnable( void );
|
|||||||
|
|
||||||
INLINE void vMBMasterPortTimersDisable( void );
|
INLINE void vMBMasterPortTimersDisable( void );
|
||||||
|
|
||||||
|
/* ----------------- Callback for the master error process ------------------*/
|
||||||
|
void vMBMasterErrorCBRespondTimeout( UCHAR ucDestAddress, const UCHAR* pucPDUData,
|
||||||
|
USHORT ucPDULength );
|
||||||
|
|
||||||
|
void vMBMasterErrorCBReceiveData( UCHAR ucDestAddress, const UCHAR* pucPDUData,
|
||||||
|
USHORT ucPDULength );
|
||||||
|
|
||||||
|
void vMBMasterErrorCBExecuteFunction( UCHAR ucDestAddress, const UCHAR* pucPDUData,
|
||||||
|
USHORT ucPDULength );
|
||||||
|
|
||||||
/* ----------------------- Callback for the protocol stack ------------------*/
|
/* ----------------------- Callback for the protocol stack ------------------*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -65,6 +65,7 @@
|
|||||||
|
|
||||||
static UCHAR ucMBMasterDestAddress;
|
static UCHAR ucMBMasterDestAddress;
|
||||||
static BOOL xMBRunInMasterMode = FALSE;
|
static BOOL xMBRunInMasterMode = FALSE;
|
||||||
|
static eMBMasterErrorEventType eMBMasterCurErrorType;
|
||||||
|
|
||||||
static enum
|
static enum
|
||||||
{
|
{
|
||||||
@ -261,6 +262,7 @@ eMBMasterPoll( void )
|
|||||||
int i;
|
int i;
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
eMBMasterEventType eEvent;
|
eMBMasterEventType eEvent;
|
||||||
|
eMBMasterErrorEventType errorType;
|
||||||
|
|
||||||
/* Check if the protocol stack is ready. */
|
/* Check if the protocol stack is ready. */
|
||||||
if( eMBState != STATE_ENABLED )
|
if( eMBState != STATE_ENABLED )
|
||||||
@ -286,6 +288,7 @@ eMBMasterPoll( void )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -318,18 +321,38 @@ eMBMasterPoll( void )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
|
/* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
|
||||||
if (eException != MB_EX_NONE) ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
if (eException != MB_EX_NONE) {
|
||||||
|
vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
|
||||||
|
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||||
|
}
|
||||||
else vMBMasterRunResRelease();
|
else vMBMasterRunResRelease();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case EV_MASTER_FRAME_SENT:
|
case EV_MASTER_FRAME_SENT:
|
||||||
/* Master is busy now. */
|
/* Master is busy now. */
|
||||||
vMBMasterGetPDUSndBuf( &ucMBFrame );
|
vMBMasterGetPDUSndBuf( &ucMBFrame );
|
||||||
eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, ucMBMasterGetPDUSndLength() );
|
eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case EV_MASTER_ERROR_PROCESS:
|
case EV_MASTER_ERROR_PROCESS:
|
||||||
vMBMasterRunResRelease();
|
/* Execute specified error process callback function. */
|
||||||
|
errorType = eMBMasterGetErrorType();
|
||||||
|
vMBMasterGetPDUSndBuf( &ucMBFrame );
|
||||||
|
switch (errorType) {
|
||||||
|
case EV_ERROR_RESPOND_TIMEOUT:
|
||||||
|
vMBMasterErrorCBRespondTimeout(ucMBMasterGetDestAddress(),
|
||||||
|
ucMBFrame, usMBMasterGetPDUSndLength());
|
||||||
|
break;
|
||||||
|
case EV_ERROR_RECEIVE_DATA:
|
||||||
|
vMBMasterErrorCBReceiveData(ucMBMasterGetDestAddress(),
|
||||||
|
ucMBFrame, usMBMasterGetPDUSndLength());
|
||||||
|
break;
|
||||||
|
case EV_ERROR_EXECUTE_FUNCTION:
|
||||||
|
vMBMasterErrorCBExecuteFunction(ucMBMasterGetDestAddress(),
|
||||||
|
ucMBFrame, usMBMasterGetPDUSndLength());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
vMBMasterRunResRelease();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -346,15 +369,27 @@ void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode )
|
|||||||
{
|
{
|
||||||
xMBRunInMasterMode = IsMasterMode;
|
xMBRunInMasterMode = IsMasterMode;
|
||||||
}
|
}
|
||||||
/* Get Modbus Master send destination address*/
|
/* Get Modbus Master send destination address. */
|
||||||
UCHAR ucMBMasterGetDestAddress( void )
|
UCHAR ucMBMasterGetDestAddress( void )
|
||||||
{
|
{
|
||||||
return ucMBMasterDestAddress;
|
return ucMBMasterDestAddress;
|
||||||
}
|
}
|
||||||
/* Set Modbus Master send destination address*/
|
/* Set Modbus Master send destination address. */
|
||||||
void vMBMasterSetDestAddress( UCHAR Address )
|
void vMBMasterSetDestAddress( UCHAR Address )
|
||||||
{
|
{
|
||||||
ucMBMasterDestAddress = Address;
|
ucMBMasterDestAddress = Address;
|
||||||
}
|
}
|
||||||
|
/* Get Modbus Master current error event type. */
|
||||||
|
eMBMasterErrorEventType eMBMasterGetErrorType( void )
|
||||||
|
{
|
||||||
|
return eMBMasterCurErrorType;
|
||||||
|
}
|
||||||
|
/* Set Modbus Master current error event type. */
|
||||||
|
void vMBMasterSetErrorType( eMBMasterErrorEventType errorType )
|
||||||
|
{
|
||||||
|
eMBMasterCurErrorType = errorType;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -74,7 +74,7 @@ static volatile eMBMasterRcvState eRcvState;
|
|||||||
|
|
||||||
static volatile UCHAR ucMasterRTUSndBuf[MB_PDU_SIZE_MAX];
|
static volatile UCHAR ucMasterRTUSndBuf[MB_PDU_SIZE_MAX];
|
||||||
static volatile UCHAR ucMasterRTURcvBuf[MB_SER_PDU_SIZE_MAX];
|
static volatile UCHAR ucMasterRTURcvBuf[MB_SER_PDU_SIZE_MAX];
|
||||||
static volatile UCHAR ucMasterSendPDULength;
|
static volatile USHORT usMasterSendPDULength;
|
||||||
|
|
||||||
static volatile UCHAR *pucMasterSndBufferCur;
|
static volatile UCHAR *pucMasterSndBufferCur;
|
||||||
static volatile USHORT usMasterSndBufferCount;
|
static volatile USHORT usMasterSndBufferCount;
|
||||||
@ -381,7 +381,10 @@ xMBMasterRTUTimerExpired(void)
|
|||||||
* If the frame is broadcast,The master will idle,and if the frame is not
|
* If the frame is broadcast,The master will idle,and if the frame is not
|
||||||
* broadcast.Notify the listener process error.*/
|
* broadcast.Notify the listener process error.*/
|
||||||
case STATE_M_TX_XFWR:
|
case STATE_M_TX_XFWR:
|
||||||
if ( xFrameIsBroadcast == FALSE ) xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
|
if ( xFrameIsBroadcast == FALSE ) {
|
||||||
|
vMBMasterSetErrorType(EV_ERROR_RESPOND_TIMEOUT);
|
||||||
|
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
/* Function called in an illegal state. */
|
/* Function called in an illegal state. */
|
||||||
default:
|
default:
|
||||||
@ -411,15 +414,15 @@ void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame )
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Set Modbus Master send PDU's buffer length.*/
|
/* Set Modbus Master send PDU's buffer length.*/
|
||||||
void vMBMasterSetPDUSndLength( UCHAR SendPDULength )
|
void vMBMasterSetPDUSndLength( USHORT SendPDULength )
|
||||||
{
|
{
|
||||||
ucMasterSendPDULength = SendPDULength;
|
usMasterSendPDULength = SendPDULength;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get Modbus Master send PDU's buffer length.*/
|
/* Get Modbus Master send PDU's buffer length.*/
|
||||||
UCHAR ucMBMasterGetPDUSndLength( void )
|
USHORT usMBMasterGetPDUSndLength( void )
|
||||||
{
|
{
|
||||||
return ucMasterSendPDULength;
|
return usMasterSendPDULength;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set Modbus Master current timer mode.*/
|
/* Set Modbus Master current timer mode.*/
|
||||||
|
@ -43,3 +43,17 @@ void vMBDelay(ULONG nCount)
|
|||||||
for(; nCount > 0;nCount--);
|
for(; nCount > 0;nCount--);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void vMBMasterErrorCBRespondTimeout(UCHAR ucDestAddress, const UCHAR* pucPDUData,
|
||||||
|
USHORT ucPDULength) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void vMBMasterErrorCBReceiveData(UCHAR ucDestAddress, const UCHAR* pucPDUData,
|
||||||
|
USHORT ucPDULength) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void vMBMasterErrorCBExecuteFunction(UCHAR ucDestAddress, const UCHAR* pucPDUData,
|
||||||
|
USHORT ucPDULength) {
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user