Beispiel #1
0
eMBErrorCode
eMBMasterPoll( void )
{
    static UCHAR   *ucMBFrame;
    static UCHAR    ucRcvAddress;
    static UCHAR    ucFunctionCode;
    static USHORT   usLength;
    static eMBException eException;
//    int             i;
    eMBErrorCode    eStatus = MB_ENOERR;
    eMBMasterEventType    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( xMBMasterPortEventGet( &eEvent ) == TRUE )
    {
        switch ( eEvent )
        {
        case EV_MASTER_READY:
            break;

        case EV_MASTER_FRAME_RECEIVED:

			eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );

			if ( eStatus == MB_ENOERR  )
			{
				( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
			}
			else
			{
				( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
			}
			#if 0
			/* Check if the frame is for us. If not ,send an error process event. */
			if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
			{
				( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
			}
			else
			{
				( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
			}
			#endif

			break;

        case EV_MASTER_EXECUTE:
				//接受到了一帧RTU 数据

		    //Mb2TcpBuff[256];
			   if(usLength<256)
			   {

             Mb2TcpLenth=usLength;//更新接受到的数据长度,准备启动TCP发送
			 Mb2TcpBuff[0]=0xFF;
			 Mb2TcpBuff[1]=0x55;
			 Mb2TcpBuff[2]=Statues_MB;
			 Mb2TcpBuff[3]=UID_STM32[0];
			 Mb2TcpBuff[4]=UID_STM32[1];
			 Mb2TcpBuff[5]=UID_STM32[2];
			 memcpy(&Mb2TcpBuff[6],ucMBFrame,Mb2TcpLenth);//数据复制进TCP发送buf中
			 Mb2TcpBuff[Mb2TcpLenth+6]=0x24;
			 Mb2TcpBuff[Mb2TcpLenth+6+1]=0x0d;
			 Mb2TcpBuff[Mb2TcpLenth+6+2]=0x0a;	 
			 Mb2TcpLenth+=9;

			   }
			 eException = MB_EX_ILLEGAL_FUNCTION;
			
		#if 0
            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( xMasterFuncHandlers[i].ucFunctionCode == 0 )
                {
                    break;
                }
                else if( xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode )
                {
                	vMBMasterSetCBRunInMasterMode(TRUE);
                    eException = xMasterFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
                    vMBMasterSetCBRunInMasterMode(FALSE);
                    break;
                }
            }  

            /* If receive frame has exception .The receive function code highest bit is 1.*/
            if(ucFunctionCode >> 7) eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
            /* 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 vMBMasterSetIsBusy( FALSE );
			#endif
            break;

        case EV_MASTER_FRAME_SENT:
        	/* Master is busy now. */
        	vMBMasterSetIsBusy( TRUE );
        	vMBMasterGetPDUSndBuf( &ucMBFrame );
			eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, ucMBMasterGetPDUSndLength() );
            break;
        case EV_MASTER_ERROR_PROCESS:
        	vMBMasterSetIsBusy( FALSE );
        	break;
        }
    }
    return MB_ENOERR;
}
Beispiel #2
0
eMBErrorCode
eMBMasterPoll( void )
{
    static UCHAR   *ucMBFrame;
    static UCHAR    ucRcvAddress;
    static UCHAR    ucFunctionCode;
    static USHORT   usLength;
    static eMBException eException;

    int             i , j;
    eMBErrorCode    eStatus = MB_ENOERR;
    eMBMasterEventType    eEvent;
    eMBMasterErrorEventType errorType;

    /* 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( xMBMasterPortEventGet( &eEvent ) == TRUE )
    {
        switch ( eEvent )
        {
        case EV_MASTER_READY:
            break;

        case EV_MASTER_FRAME_RECEIVED:
			eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
			/* Check if the frame is for us. If not ,send an error process event. */
			if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
			{
				( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
			}
			else
			{
				vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
				( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
			}
			break;

        case EV_MASTER_EXECUTE:
            ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
            eException = MB_EX_ILLEGAL_FUNCTION;
            /* If receive frame has exception .The receive function code highest bit is 1.*/
            if(ucFunctionCode >> 7) {
            	eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
            }
						else
						{
							for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++)
							{
								/* No more function handlers registered. Abort. */
								if (xMasterFuncHandlers[i].ucFunctionCode == 0)	{
									break;
								}
								else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
									vMBMasterSetCBRunInMasterMode(TRUE);
									/* If master request is broadcast,
									 * the master need execute function for all slave.
									 */
									if ( xMBMasterRequestIsBroadcast() ) {
										usLength = usMBMasterGetPDUSndLength();
										for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){
											vMBMasterSetDestAddress(j);
											eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
										}
									}
									else {
										eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
									}
									vMBMasterSetCBRunInMasterMode(FALSE);
									break;
								}
							}
						}
            /* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
            if (eException != MB_EX_NONE) {
            	vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
            	( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
            }
            else {
            	vMBMasterCBRequestScuuess( ucMBMasterGetDestAddress());
            	vMBMasterRunResRelease( );
            }
            break;

        case EV_MASTER_FRAME_SENT:
        	/* Master is busy now. */
        	vMBMasterGetPDUSndBuf( &ucMBFrame );
			eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
            break;

        case EV_MASTER_ERROR_PROCESS:
        	/* 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;
        }
    }