eMBException eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen ) { UCHAR *ucMBFrame; USHORT usRegAddress; USHORT usRegCount; eMBException eStatus = MB_EX_NONE; eMBErrorCode eRegStatus; /* If this request is broadcast, and it's read mode. This request don't need execute. */ if ( xMBMasterRequestIsBroadcast() ) { eStatus = MB_EX_NONE; } else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) { vMBMasterGetPDUSndBuf(&ucMBFrame); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); usRegAddress++; usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] << 8 ); usRegCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] ); /* Check if the number of registers to read is valid. If not * return Modbus illegal data value exception. */ if( ( usRegCount >= 1 ) && ( 2 * usRegCount == pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] ) ) { /* Make callback to fill the buffer. */ eRegStatus = eMBMasterRegHoldingCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usRegCount, MB_REG_READ ); if(eMBMasterGetErrorType() == EV_ERROR_RESPOND_TIMEOUT) nReqTimeOut++; vMBMasterSetErrorType(EV_NO_ERROR); populate(&list_RTC, (DeviceElement){(uint32_t)ucMBMasterGetDestAddress(), "RTC", CONNECTED, nReqTimeOut, nReqs, 3, usRegAddress, usRegCount, 0, 0}); /* If an error occured convert it into a Modbus exception. */ if( eRegStatus != MB_ENOERR ) { eStatus = prveMBError2Exception( eRegStatus ); populate(&list_RTC, (DeviceElement){(uint32_t)ucMBMasterGetDestAddress(), "RTC(E)", CONNECTIONLESS, nReqTimeOut, nReqs, 3, usRegAddress, usRegCount, 0, 0}); } } else { eStatus = MB_EX_ILLEGAL_DATA_VALUE; populate(&list_RTC, (DeviceElement){(uint32_t)ucMBMasterGetDestAddress(), "RTC(IDV)", CONNECTIONLESS, nReqTimeOut, nReqs, 3, usRegAddress, usRegCount, 0, 0}); } } else { /* Can't be a valid request because the length is incorrect. */ eStatus = MB_EX_ILLEGAL_DATA_VALUE; } return eStatus; }
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; } }