eMBErrorCode eMBPoll( void ) { static UCHAR *ucMBFrame; static UCHAR ucRcvAddress; static UCHAR ucFunctionCode; static USHORT usLength; static eMBException eException; int i; eMBErrorCode eStatus = MB_ENOERR; eMBEventType eEvent; /* Check if the protocol stack is ready. */ if( eMBState != STATE_ENABLED ) { return MB_EILLSTATE; } /* Check if there is a event available. If not return control to caller. * Otherwise we will handle the event. */ if( xMBPortEventGet( &eEvent ) == TRUE ) { switch ( eEvent ) { case EV_READY: break; case EV_FRAME_RECEIVED: eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); if( eStatus == MB_ENOERR ) { /* Check if the frame is for us. If not ignore the frame. */ if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) ) { ( void )xMBPortEventPost( EV_EXECUTE ); } } #ifdef _USE_BITTLE_PROTOCOL /*use bt protocol */ if( eStatus == MB_BITTLE ) { /* Check if the frame is for us. If not ignore the frame. */ if( ( ucRcvAddress == ucMBAddress ) ) { ( void )xMBPortEventPost( EV_BT_EXECUTE );//bittel protocol event } } #endif /*use bt protocol */ break; #ifdef _USE_BITTLE_PROTOCOL /*use bt protocol */ case EV_BT_EXECUTE: ucFunctionCode = ucMBFrame[BT_FUNC_OFFSET];//Get the function code eException = MB_EX_ILLEGAL_FUNCTION; //the first shoud be set the exception is ILLEGAL for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) { /* No more function handlers registered. Abort. */ if( xFuncHandlers[i].ucFunctionCode == 0 ) { break; } else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) { eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength ); break; } } /* If the request was not sent to the broadcast address we * return a reply. */ if( ucRcvAddress != MB_ADDRESS_BROADCAST ) { if( eException != MB_EX_NONE ) { /* An exception occured. Build an error frame. */ usLength = 0; ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR ); ucMBFrame[usLength++] = eException; } //sent data via usart. //There we can ref peMBFrameSendCur and rewrite code . //because the function code is only use MODBUS style. eStatus = peBTFrameSendCur( ucMBAddress, ucMBFrame, usLength ); } break; #endif /*use bt protocol */ case EV_EXECUTE: ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; eException = MB_EX_ILLEGAL_FUNCTION; for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) { /* No more function handlers registered. Abort. */ if( xFuncHandlers[i].ucFunctionCode == 0 ) { break; } else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) { eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength ); break; } } /* If the request was not sent to the broadcast address we * return a reply. */ if( ucRcvAddress != MB_ADDRESS_BROADCAST ) { if( eException != MB_EX_NONE ) { /* An exception occured. Build an error frame. */ usLength = 0; ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR ); ucMBFrame[usLength++] = eException; } eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength ); } break; case EV_FRAME_SENT: break; } } return MB_ENOERR; }
eMBErrorCode eMBPoll( void ) { static UCHAR *ucMBFrame; static UCHAR ucRcvAddress; static UCHAR ucFunctionCode; static USHORT usLength; static eMBException eException; int i; eMBErrorCode eStatus = MB_ENOERR; eMBEventType eEvent; /* Check if the protocol stack is ready. */ if( eMBState != STATE_ENABLED ) { return MB_EILLSTATE; } /* Check if there is a event available. If not return control to caller. * Otherwise we will handle the event. */ if( xMBPortEventGet( &eEvent ) == TRUE ) { switch ( eEvent ) { case EV_READY: break; case EV_FRAME_RECEIVED: eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); if( eStatus == MB_ENOERR ) { /* Check if the frame is for us. If not ignore the frame. */ if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) ) { ( void )xMBPortEventPost( EV_EXECUTE ); } } break; case EV_EXECUTE: ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; eException = MB_EX_ILLEGAL_FUNCTION; for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) { /* No more function handlers registered. Abort. */ if( xFuncHandlers[i].ucFunctionCode == 0 ) { break; } else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) { eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength ); break; } } /* If the request was not sent to the broadcast address we * return a reply. */ if( ucRcvAddress != MB_ADDRESS_BROADCAST ) { if( eException != MB_EX_NONE ) { /* An exception occured. Build an error frame. */ usLength = 0; ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR ); ucMBFrame[usLength++] = eException; } if( ( eMBCurrentMode == MB_ASCII ) && MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ) { vMBPortTimersDelay( MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ); } eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength ); } break; case EV_FRAME_SENT: break; } } return MB_ENOERR; }
eMBErrorCode eMBSendFrame( UCHAR *pucFrame, USHORT usLength ) { static UCHAR *ucMBFrame; static UCHAR ucFunctionCode; static UCHAR ucRcvAddress; static eMBException eException; eMBErrorCode eStatus = MB_ENOERR; UCHAR ucDone = 0; int i; eMBEventType eEvent; eMBEventType lastEvent = 0; ucMBFrame = pucFrame; ( void )xMBPortEventPost( EV_EXECUTE ); while(eStatus == MB_ENOERR && ucDone != 1) { /* Check if there is a event available. If not return control to caller. * Otherwise we will handle the event. */ if( xMBPortEventGet( &eEvent ) == TRUE ) { switch ( eEvent ) { case EV_READY: #ifdef DBG printf("%d : %s() -- EV_READY\n", __LINE__, __FUNCTION__); #endif break; case EV_FRAME_RECEIVED: #ifdef DBG printf("%d : %s() -- EV_FRAME_RECEIVED\n", __LINE__, __FUNCTION__); #endif eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); ucDone += 1; break; case EV_EXECUTE: #ifdef DBG printf("%d : %s() -- EV_EXECUTE\n", __LINE__, __FUNCTION__); #endif /* If the request was not sent to the broadcast address we * return a reply. */ eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength ); break; case EV_FRAME_SENT: #ifdef DBG printf("%d : %s() -- EV_FRAME_SENT\n", __LINE__, __FUNCTION__); #endif break; case EV_FRAME_TIMEOUT: eStatus = MB_ETIMEDOUT; #ifdef DBG printf("%d : %s() -- EV_FRAME_TIMEOUT\n", __LINE__, __FUNCTION__); #endif break; } } #ifdef DBG //printf("%d : %s() -- eStatus = %d \n", __LINE__, __FUNCTION__, eStatus); #endif } return eStatus; }