1、【修改】FreeModbus主机部分源码,采用RT-Thread信号量方式来实现主机请求的同步机制。

Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
armink 2014-01-08 14:08:00 +08:00
parent f88aa70856
commit 58f8bdbbc8
8 changed files with 35 additions and 33 deletions

View File

@ -80,7 +80,7 @@ eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils ,LONG
UCHAR *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
else
{
@ -167,7 +167,7 @@ eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LO
UCHAR *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else if ( usCoilAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
else if ( ( usCoilData != 0xFF00 ) && ( usCoilData != 0x0000 ) ) eErrStatus = MB_MRE_ILL_ARG;
else
@ -249,7 +249,7 @@ eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
UCHAR ucByteCount;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
else if ( usNCoils > MB_PDU_REQ_WRITE_MUL_COILCNT_MAX ) eErrStatus = MB_MRE_ILL_ARG;
else

View File

@ -65,7 +65,7 @@ eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT u
UCHAR *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
else
{

View File

@ -92,7 +92,7 @@ eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRe
UCHAR *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
else
{
@ -151,7 +151,7 @@ eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
USHORT usRegIndex = 0;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
else
{
@ -233,7 +233,7 @@ eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRe
UCHAR *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
else
{
@ -309,7 +309,7 @@ eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
USHORT usRegIndex = 0;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
else
{

View File

@ -65,7 +65,7 @@ eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs
UCHAR *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if ( xMBasterRunMutexLock( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
if ( xMBasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
else
{

View File

@ -92,11 +92,11 @@ BOOL xMBMasterPortEventPost( eMBMasterEventType eEvent );
BOOL xMBMasterPortEventGet( /*@out@ */ eMBMasterEventType * eEvent );
void vMBasterRunMutexInit( void );
void vMBasterRunResInit( void );
BOOL xMBasterRunMutexLock( int32_t time );
BOOL xMBasterRunResTake( int32_t time );
void vMBasterRunMutexUnlock( void );
void vMBasterRunResRelease( void );
/* ----------------------- Serial port functions ----------------------------*/

View File

@ -184,8 +184,8 @@ eMBMasterInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity
{
eMBState = STATE_DISABLED;
}
/* initialize the Mobus Master mutex */
vMBasterRunMutexInit();
/* initialize the Mobus Master running resource. */
vMBasterRunResInit();
}
return eStatus;
}
@ -319,7 +319,7 @@ 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 );
else vMBasterRunMutexUnlock();
else vMBasterRunResRelease();
break;
case EV_MASTER_FRAME_SENT:
@ -329,7 +329,7 @@ eMBMasterPoll( void )
break;
case EV_MASTER_ERROR_PROCESS:
vMBasterRunMutexUnlock();
vMBasterRunResRelease();
break;
}
}

View File

@ -393,7 +393,7 @@ xMBMasterRTUTimerExpired(void)
vMBMasterPortTimersDisable( );
/* If timer mode is convert delay ,then Master is idel now. */
if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) vMBasterRunMutexUnlock();
if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) vMBasterRunResRelease();
return xNeedPoll;
}

View File

@ -26,9 +26,9 @@
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
/* ----------------------- Variables ----------------------------------------*/
static eMBMasterEventType eMasterQueuedEvent;
static BOOL xMasterEventInQueue;
static struct rt_mutex xMasterRunMutex;
static eMBMasterEventType eMasterQueuedEvent;
static BOOL xMasterEventInQueue;
static struct rt_semaphore xMasterRunRes;
/* ----------------------- Start implementation -----------------------------*/
BOOL
@ -61,37 +61,39 @@ xMBMasterPortEventGet( eMBMasterEventType * eEvent )
}
/**
* This function is initialize the Mobus Master mutex .
* Note:The mutex is define by Operating System.If you not use Opearting System this function can be empty.
* This function is initialize the Mobus Master running resource .
* Note:The resource is define by Operating System.If you not use Opearting System this function can be empty.
*
*/
void vMBasterRunMutexInit( void )
void vMBasterRunResInit( void )
{
rt_mutex_init(&xMasterRunMutex, "master run", RT_IPC_FLAG_PRIO);
rt_sem_init(&xMasterRunRes, "master res", 0x01 , RT_IPC_FLAG_PRIO);
}
/**
* This function is lock Mobus Master mutex.
* Note:The mutex is define by Operating System.If you not use Opearting System this function can be just return TRUE.
* 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.
*
* @param lTimeOut the waiting time.
*
* @return mutex lock result
* @return resource taked result
*/
BOOL xMBasterRunMutexLock( LONG lTimeOut )
BOOL xMBasterRunResTake( LONG lTimeOut )
{
/*If waiting time is -1 .It will wait forever */
return rt_mutex_take(&xMasterRunMutex, lTimeOut) ? FALSE : TRUE ;
return rt_sem_take(&xMasterRunRes, lTimeOut) ? FALSE : TRUE ;
}
/**
* This function is unlock Mobus Master mutex.
* Note:The mutex is define by Operating System.If you not use Opearting System this function can be empty.
* 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.
*
*/
void vMBasterRunMutexUnlock( void )
void vMBasterRunResRelease( void )
{
rt_mutex_release(&xMasterRunMutex);
/* Clear up resource when need release resource. */
rt_sem_control(&xMasterRunRes, RT_IPC_CMD_RESET, 0);
rt_sem_release(&xMasterRunRes);
}
#endif