From 26160187b473929b4c7ed19792e0d858e6c74718 Mon Sep 17 00:00:00 2001 From: armink Date: Tue, 21 Jan 2014 16:27:47 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E3=80=90=E5=A2=9E=E5=8A=A0=E3=80=91F?= =?UTF-8?q?reeModbus=E4=B8=BB=E6=9C=BA=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E6=96=B9=E6=B3=95=EF=BC=8C=E4=BD=8D=E4=BA=8E?= =?UTF-8?q?port.c=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- FreeModbus/modbus/include/mb_m.h | 6 ++-- FreeModbus/modbus/include/mbconfig.h | 2 +- FreeModbus/modbus/include/mbport.h | 19 +++++++++++- FreeModbus/modbus/mb_m.c | 45 ++++++++++++++++++++++++---- FreeModbus/modbus/rtu/mbrtu_m.c | 15 ++++++---- FreeModbus/port/port.c | 14 +++++++++ 6 files changed, 86 insertions(+), 15 deletions(-) diff --git a/FreeModbus/modbus/include/mb_m.h b/FreeModbus/modbus/include/mb_m.h index a4c93a4..0dadb45 100644 --- a/FreeModbus/modbus/include/mb_m.h +++ b/FreeModbus/modbus/include/mb_m.h @@ -389,9 +389,11 @@ UCHAR ucMBMasterGetDestAddress( void ); void vMBMasterSetDestAddress( UCHAR Address ); BOOL xMBMasterGetCBRunInMasterMode( void ); void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode ); -UCHAR ucMBMasterGetPDUSndLength( void ); -void vMBMasterSetPDUSndLength( UCHAR SendPDULength ); +USHORT usMBMasterGetPDUSndLength( void ); +void vMBMasterSetPDUSndLength( USHORT SendPDULength ); void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode ); +eMBMasterErrorEventType eMBMasterGetErrorType( void ); +void vMBMasterSetErrorType( eMBMasterErrorEventType errorType ); /* ----------------------- Callback -----------------------------------------*/ diff --git a/FreeModbus/modbus/include/mbconfig.h b/FreeModbus/modbus/include/mbconfig.h index c5434de..0862c70 100644 --- a/FreeModbus/modbus/include/mbconfig.h +++ b/FreeModbus/modbus/include/mbconfig.h @@ -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. * 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 (2000) +#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.*/ #define MB_MASTER_TOTAL_SLAVE_NUM ( 16 ) diff --git a/FreeModbus/modbus/include/mbport.h b/FreeModbus/modbus/include/mbport.h index fb731c8..b465a94 100644 --- a/FreeModbus/modbus/include/mbport.h +++ b/FreeModbus/modbus/include/mbport.h @@ -41,7 +41,7 @@ PR_BEGIN_EXTERN_C /*! \ingroup modbus * \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_NO 0 /*!< Non-block. */ @@ -65,6 +65,13 @@ typedef enum EV_MASTER_ERROR_PROCESS /*!< Frame error process*/ } 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 * \brief Parity used for characters in serial mode. * @@ -147,6 +154,16 @@ INLINE void vMBMasterPortTimersRespondTimeoutEnable( 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 ------------------*/ /*! diff --git a/FreeModbus/modbus/mb_m.c b/FreeModbus/modbus/mb_m.c index 0d621c3..e0859a6 100644 --- a/FreeModbus/modbus/mb_m.c +++ b/FreeModbus/modbus/mb_m.c @@ -65,6 +65,7 @@ static UCHAR ucMBMasterDestAddress; static BOOL xMBRunInMasterMode = FALSE; +static eMBMasterErrorEventType eMBMasterCurErrorType; static enum { @@ -261,6 +262,7 @@ eMBMasterPoll( void ) int i; eMBErrorCode eStatus = MB_ENOERR; eMBMasterEventType eEvent; + eMBMasterErrorEventType errorType; /* Check if the protocol stack is ready. */ if( eMBState != STATE_ENABLED ) @@ -286,6 +288,7 @@ eMBMasterPoll( void ) } else { + vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); } break; @@ -318,18 +321,38 @@ eMBMasterPoll( void ) } } /* 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(); break; case EV_MASTER_FRAME_SENT: /* Master is busy now. */ vMBMasterGetPDUSndBuf( &ucMBFrame ); - eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, ucMBMasterGetPDUSndLength() ); + eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() ); break; 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; } } @@ -346,15 +369,27 @@ void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode ) { xMBRunInMasterMode = IsMasterMode; } -/* Get Modbus Master send destination address*/ +/* Get Modbus Master send destination address. */ UCHAR ucMBMasterGetDestAddress( void ) { return ucMBMasterDestAddress; } -/* Set Modbus Master send destination address*/ +/* Set Modbus Master send destination address. */ void vMBMasterSetDestAddress( UCHAR 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 diff --git a/FreeModbus/modbus/rtu/mbrtu_m.c b/FreeModbus/modbus/rtu/mbrtu_m.c index 6564d56..740e686 100644 --- a/FreeModbus/modbus/rtu/mbrtu_m.c +++ b/FreeModbus/modbus/rtu/mbrtu_m.c @@ -74,7 +74,7 @@ static volatile eMBMasterRcvState eRcvState; static volatile UCHAR ucMasterRTUSndBuf[MB_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 USHORT usMasterSndBufferCount; @@ -381,7 +381,10 @@ xMBMasterRTUTimerExpired(void) * If the frame is broadcast,The master will idle,and if the frame is not * broadcast.Notify the listener process error.*/ 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; /* Function called in an illegal state. */ default: @@ -411,15 +414,15 @@ void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame ) } /* 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.*/ -UCHAR ucMBMasterGetPDUSndLength( void ) +USHORT usMBMasterGetPDUSndLength( void ) { - return ucMasterSendPDULength; + return usMasterSendPDULength; } /* Set Modbus Master current timer mode.*/ diff --git a/FreeModbus/port/port.c b/FreeModbus/port/port.c index 7f58ba8..fb11c0f 100644 --- a/FreeModbus/port/port.c +++ b/FreeModbus/port/port.c @@ -43,3 +43,17 @@ void vMBDelay(ULONG 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) { + +}