1、【增加】FreeModbus主机请求结果,包括请求处理成功、请求响应超时、接收数据出错及功能方法执行错误;

2、【增加】FreeModbus主机请求处理成功的回调接口,用户可以在里面增加自己的代码,统计成功率等等;
2、【修改】FreeModbus主机错误处理回调方法存放的位置,移动至/port/portevent_m.c。

Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
armink 2014-02-26 11:27:55 +08:00
parent e9d291a120
commit d9d5cae9bb
8 changed files with 164 additions and 58 deletions

View File

@ -38,7 +38,7 @@
#define RT_USING_MUTEX #define RT_USING_MUTEX
/* Using Event */ /* Using Event */
//#define RT_USING_EVENT #define RT_USING_EVENT
/* Using MailBox */ /* Using MailBox */
//#define RT_USING_MAILBOX //#define RT_USING_MAILBOX

View File

@ -49,15 +49,15 @@ void thread_entry_SysMonitor(void* parameter)
usModbusUserData[0] = (USHORT)(rt_tick_get()/10); usModbusUserData[0] = (USHORT)(rt_tick_get()/10);
usModbusUserData[1] = (USHORT)(rt_tick_get()%10); usModbusUserData[1] = (USHORT)(rt_tick_get()%10);
ucModbusUserData[0] = 0x1F; ucModbusUserData[0] = 0x1F;
eMBMasterReqReadDiscreteInputs(1,3,8,MB_WAITING_FOREVER); eMBMasterReqReadDiscreteInputs(1,3,8,RT_WAITING_FOREVER);
// eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData,MB_WAITING_FOREVER); // eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData,RT_WAITING_FOREVER);
// eMBMasterReqWriteCoil(1,8,0xFF00,MB_WAITING_FOREVER); // eMBMasterReqWriteCoil(1,8,0xFF00,RT_WAITING_FOREVER);
// eMBMasterReqReadCoils(1,3,8,MB_WAITING_FOREVER); // eMBMasterReqReadCoils(1,3,8,RT_WAITING_FOREVER);
// eMBMasterReqReadInputRegister(1,3,2,MB_WAITING_FOREVER); // eMBMasterReqReadInputRegister(1,3,2,RT_WAITING_FOREVER);
// eMBMasterReqWriteHoldingRegister(1,3,usModbusUserData[0],MB_WAITING_FOREVER); // eMBMasterReqWriteHoldingRegister(1,3,usModbusUserData[0],RT_WAITING_FOREVER);
// eMBMasterReqWriteMultipleHoldingRegister(1,3,2,usModbusUserData,MB_WAITING_FOREVER); // eMBMasterReqWriteMultipleHoldingRegister(1,3,2,usModbusUserData,RT_WAITING_FOREVER);
// eMBMasterReqReadHoldingRegister(1,3,2,MB_WAITING_FOREVER); // eMBMasterReqReadHoldingRegister(1,3,2,RT_WAITING_FOREVER);
// eMBMasterReqReadWriteMultipleHoldingRegister(1,3,2,usModbusUserData,5,2,MB_WAITING_FOREVER); // eMBMasterReqReadWriteMultipleHoldingRegister(1,3,2,usModbusUserData,5,2,RT_WAITING_FOREVER);
} }
} }

View File

@ -77,13 +77,10 @@ typedef enum
MB_MRE_NO_ERR, /*!< no error. */ MB_MRE_NO_ERR, /*!< no error. */
MB_MRE_NO_REG, /*!< illegal register address. */ MB_MRE_NO_REG, /*!< illegal register address. */
MB_MRE_ILL_ARG, /*!< illegal argument. */ MB_MRE_ILL_ARG, /*!< illegal argument. */
MB_MRE_PORT_ERR, /*!< porting layer error. */ MB_MRE_REV_DATA, /*!< receive data error. */
MB_MRE_NO_RES, /*!< insufficient resources. */
MB_MRE_IO, /*!< I/O error. */
MB_MRE_ILL_STATE, /*!< protocol stack in illegal state. */
MB_MRE_TIMEDOUT, /*!< timeout error occurred. */ MB_MRE_TIMEDOUT, /*!< timeout error occurred. */
MB_MRE_MASTER_BUSY, /*!< master is busy now. */ MB_MRE_MASTER_BUSY, /*!< master is busy now. */
MB_MRE_SLAVE_EXCE /*!< slave has exception. */ MB_MRE_EXE_FUN /*!< execute function error. */
} eMBMasterReqErrCode; } eMBMasterReqErrCode;
/*! \ingroup modbus /*! \ingroup modbus
* \brief TimerMode is Master 3 kind of Timer modes. * \brief TimerMode is Master 3 kind of Timer modes.
@ -394,6 +391,7 @@ void vMBMasterSetPDUSndLength( USHORT SendPDULength );
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode ); void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode );
eMBMasterErrorEventType eMBMasterGetErrorType( void ); eMBMasterErrorEventType eMBMasterGetErrorType( void );
void vMBMasterSetErrorType( eMBMasterErrorEventType errorType ); void vMBMasterSetErrorType( eMBMasterErrorEventType errorType );
eMBMasterReqErrCode vMBMasterWaitRequestFinish( void );
/* ----------------------- Callback -----------------------------------------*/ /* ----------------------- Callback -----------------------------------------*/

View File

@ -38,14 +38,6 @@ PR_BEGIN_EXTERN_C
/* ----------------------- Defines ------------------------------------------*/ /* ----------------------- Defines ------------------------------------------*/
/*! \ingroup modbus
* \brief used for master mode.
*
* the xMBMasterRunResTake() parameter definitions
*/
#define MB_WAITING_FOREVER -1 /*!< Block forever until get resource. */
#define MB_WAITING_NO 0 /*!< Non-block. */
/* ----------------------- Type definitions ---------------------------------*/ /* ----------------------- Type definitions ---------------------------------*/
typedef enum typedef enum
@ -99,7 +91,7 @@ BOOL xMBMasterPortEventPost( eMBMasterEventType eEvent );
BOOL xMBMasterPortEventGet( /*@out@ */ eMBMasterEventType * eEvent ); BOOL xMBMasterPortEventGet( /*@out@ */ eMBMasterEventType * eEvent );
void vMBMasterRunResInit( void ); void vMBMasterOsResInit( void );
BOOL xMBMasterRunResTake( int32_t time ); BOOL xMBMasterRunResTake( int32_t time );
@ -164,6 +156,8 @@ void vMBMasterErrorCBReceiveData( UCHAR ucDestAddress, const UCHAR* p
void vMBMasterErrorCBExecuteFunction( UCHAR ucDestAddress, const UCHAR* pucPDUData, void vMBMasterErrorCBExecuteFunction( UCHAR ucDestAddress, const UCHAR* pucPDUData,
USHORT ucPDULength ); USHORT ucPDULength );
void vMBMasterCBRequestScuuess( void );
/* ----------------------- Callback for the protocol stack ------------------*/ /* ----------------------- Callback for the protocol stack ------------------*/
/*! /*!

View File

@ -185,8 +185,8 @@ eMBMasterInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity
{ {
eMBState = STATE_DISABLED; eMBState = STATE_DISABLED;
} }
/* initialize the Mobus Master running resource. */ /* initialize the OS resource for modbus master. */
vMBMasterRunResInit(); vMBMasterOsResInit();
} }
return eStatus; return eStatus;
} }
@ -325,7 +325,10 @@ eMBMasterPoll( void )
vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION); vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
} }
else vMBMasterRunResRelease(); else {
vMBMasterCBRequestScuuess( );
vMBMasterRunResRelease( );
}
break; break;
case EV_MASTER_FRAME_SENT: case EV_MASTER_FRAME_SENT:

View File

@ -396,7 +396,10 @@ xMBMasterRTUTimerExpired(void)
vMBMasterPortTimersDisable( ); vMBMasterPortTimersDisable( );
/* If timer mode is convert delay ,then Master is idel now. */ /* If timer mode is convert delay ,then Master is idel now. */
if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) vMBMasterRunResRelease(); if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) {
vMBMasterCBRequestScuuess( );
vMBMasterRunResRelease( );
}
return xNeedPoll; return xNeedPoll;
} }

View File

@ -42,27 +42,3 @@ void vMBDelay(ULONG nCount)
{ {
for(; nCount > 0;nCount--); for(; nCount > 0;nCount--);
} }
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
/**
*
* Just use it for modbus master.
* @note There functions will block modbus master poll while execute OS waiting.
* So,for real-time of system.Do not execute too much waiting 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) {
}
#endif

View File

@ -21,15 +21,21 @@
/* ----------------------- Modbus includes ----------------------------------*/ /* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h" #include "mb.h"
#include "mb_m.h"
#include "mbport.h" #include "mbport.h"
#include "port.h" #include "port.h"
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
/* ----------------------- Defines ------------------------------------------*/
#define EVENT_MASTER_PROCESS_SUCESS 1<<0 //master request process success event
#define EVENT_MASTER_ERROR_RESPOND_TIMEOUT 1<<1 //master request respond timeout event
#define EVENT_MASTER_ERROR_RECEIVE_DATA 1<<2 //master request receive data error event
#define EVENT_MASTER_ERROR_EXECUTE_FUNCTION 1<<3 //master request execute function error event
/* ----------------------- Variables ----------------------------------------*/ /* ----------------------- Variables ----------------------------------------*/
static eMBMasterEventType eMasterQueuedEvent; static eMBMasterEventType eMasterQueuedEvent;
static BOOL xMasterEventInQueue; static BOOL xMasterEventInQueue;
static struct rt_semaphore xMasterRunRes; static struct rt_semaphore xMasterRunRes;
static struct rt_event xMasterOsEvent;
/* ----------------------- Start implementation -----------------------------*/ /* ----------------------- Start implementation -----------------------------*/
BOOL BOOL
xMBMasterPortEventInit( void ) xMBMasterPortEventInit( void )
@ -61,18 +67,19 @@ xMBMasterPortEventGet( eMBMasterEventType * eEvent )
} }
/** /**
* This function is initialize the Mobus Master running resource . * This function is initialize the OS resource for modbus master.
* Note:The resource is define by Operating System.If you not use Opearting System this function can be empty. * Note:The resource is define by OS.If you not use OS this function can be empty.
* *
*/ */
void vMBMasterRunResInit( void ) void vMBMasterOsResInit( void )
{ {
rt_event_init(&xMasterOsEvent,"master event",RT_IPC_FLAG_PRIO);
rt_sem_init(&xMasterRunRes, "master res", 0x01 , RT_IPC_FLAG_PRIO); rt_sem_init(&xMasterRunRes, "master res", 0x01 , RT_IPC_FLAG_PRIO);
} }
/** /**
* This function is take Mobus Master running resource. * This function is take Mobus Master running resource.
* Note:The resource is define by Operating System.If you not use Opearting System this function can be just return TRUE. * Note:The resource is define by Operating System.If you not use OS this function can be just return TRUE.
* *
* @param lTimeOut the waiting time. * @param lTimeOut the waiting time.
* *
@ -86,7 +93,7 @@ BOOL xMBMasterRunResTake( LONG lTimeOut )
/** /**
* This function is release Mobus Master running resource. * This function is release Mobus Master running resource.
* Note:The resource is define by Operating System.If you not use Opearting System this function can be empty. * Note:The resource is define by Operating System.If you not use OS this function can be empty.
* *
*/ */
void vMBMasterRunResRelease( void ) void vMBMasterRunResRelease( void )
@ -95,4 +102,129 @@ void vMBMasterRunResRelease( void )
rt_sem_release(&xMasterRunRes); rt_sem_release(&xMasterRunRes);
} }
/**
* This is modbus master respond timeout error process callback function.
* @note There functions will block modbus master poll while execute OS waiting.
* So,for real-time of system.Do not execute too much waiting process.
*
* @param ucDestAddress destination salve address
* @param pucPDUData PDU buffer data
* @param ucPDULength PDU buffer length
*
*/
void vMBMasterErrorCBRespondTimeout(UCHAR ucDestAddress, const UCHAR* pucPDUData,
USHORT ucPDULength) {
/**
* @note This code is use OS's event mechanism for modbus master protocol stack.
* If you don't use OS, you can change it.
*/
rt_event_send(&xMasterOsEvent, EVENT_MASTER_ERROR_RESPOND_TIMEOUT);
/* You can add your code under here. */
}
/**
* This is modbus master receive data error process callback function.
* @note There functions will block modbus master poll while execute OS waiting.
* So,for real-time of system.Do not execute too much waiting process.
*
* @param ucDestAddress destination salve address
* @param pucPDUData PDU buffer data
* @param ucPDULength PDU buffer length
*
*/
void vMBMasterErrorCBReceiveData(UCHAR ucDestAddress, const UCHAR* pucPDUData,
USHORT ucPDULength) {
/**
* @note This code is use OS's event mechanism for modbus master protocol stack.
* If you don't use OS, you can change it.
*/
rt_event_send(&xMasterOsEvent, EVENT_MASTER_ERROR_RECEIVE_DATA);
/* You can add your code under here. */
}
/**
* This is modbus master execute function error process callback function.
* @note There functions will block modbus master poll while execute OS waiting.
* So,for real-time of system.Do not execute too much waiting process.
*
* @param ucDestAddress destination salve address
* @param pucPDUData PDU buffer data
* @param ucPDULength PDU buffer length
*
*/
void vMBMasterErrorCBExecuteFunction(UCHAR ucDestAddress, const UCHAR* pucPDUData,
USHORT ucPDULength) {
/**
* @note This code is use OS's event mechanism for modbus master protocol stack.
* If you don't use OS, you can change it.
*/
rt_event_send(&xMasterOsEvent, EVENT_MASTER_ERROR_EXECUTE_FUNCTION);
/* You can add your code under here. */
}
/**
* This is modbus master request process success callback function.
* @note There functions will block modbus master poll while execute OS waiting.
* So,for real-time of system.Do not execute too much waiting process.
*
*/
void vMBMasterCBRequestScuuess( void ) {
/**
* @note This code is use OS's event mechanism for modbus master protocol stack.
* If you don't use OS, you can change it.
*/
rt_event_send(&xMasterOsEvent, EVENT_MASTER_PROCESS_SUCESS);
/* You can add your code under here. */
}
/**
* This function is wait for modbus master request finish and return result.
* Waiting result include request process success, request respond timeout,
* receive data error and execute function error.You can use the above callback function.
* @note If you are use OS, you can use OS's event mechanism. Otherwise you have to run
* much user custom delay for waiting.
*
* @return request error code
*/
eMBMasterReqErrCode vMBMasterWaitRequestFinish( void ) {
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
rt_uint32_t recvedEvent;
/* waiting for OS event */
rt_event_recv(&xMasterOsEvent,
EVENT_MASTER_PROCESS_SUCESS | EVENT_MASTER_ERROR_RESPOND_TIMEOUT
| EVENT_MASTER_ERROR_RECEIVE_DATA
| EVENT_MASTER_ERROR_EXECUTE_FUNCTION,
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER,
&recvedEvent);
switch (recvedEvent)
{
case EVENT_MASTER_PROCESS_SUCESS:
break;
case EVENT_MASTER_ERROR_RESPOND_TIMEOUT:
{
eErrStatus = MB_MRE_TIMEDOUT;
break;
}
case EVENT_MASTER_ERROR_RECEIVE_DATA:
{
eErrStatus = MB_MRE_REV_DATA;
break;
}
case EVENT_MASTER_ERROR_EXECUTE_FUNCTION:
{
eErrStatus = MB_MRE_EXE_FUN;
break;
}
}
return eErrStatus;
}
#endif #endif