/* ----------------------- Start implementation -----------------------------*/ eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity ) { eMBErrorCode eStatus = MB_ENOERR; /* check preconditions */ if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) || ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) ) { eStatus = MB_EINVAL; printf("Alamat MODBUS Client diluar range [1-247]"); } else { ucMBAddress = ucSlaveAddress; switch ( eMode ) { #if MB_RTU_ENABLED > 0 case MB_RTU: pvMBFrameStartCur = eMBRTUStart; pvMBFrameStopCur = eMBRTUStop; peMBFrameSendCur = eMBRTUSend; peMBFrameReceiveCur = eMBRTUReceive; pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL; pxMBFrameCBByteReceived = xMBRTUReceiveFSM; pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM; pxMBPortCBTimerExpired = xMBRTUTimerT35Expired; eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity ); break; #endif #if MB_ASCII_ENABLED > 0 case MB_ASCII: pvMBFrameStartCur = eMBASCIIStart; pvMBFrameStopCur = eMBASCIIStop; peMBFrameSendCur = eMBASCIISend; peMBFrameReceiveCur = eMBASCIIReceive; pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL; pxMBFrameCBByteReceived = xMBASCIIReceiveFSM; pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM; pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired; eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity ); break; #endif default: eStatus = MB_EINVAL; } if( eStatus == MB_ENOERR ) { if( !xMBPortEventInit( ) ) { /* port dependent event module initalization failed. */ eStatus = MB_EPORTERR; } else { eMBCurrentMode = eMode; eMBState = STATE_DISABLED; } } } return eStatus; }
eMBErrorCode eMBMasterInit( eMBMode eMode, char *ucPort, ULONG ulBaudRate, eMBParity eParity ) { eMBErrorCode eStatus = MB_ENOERR; switch ( eMode ) { case MB_RTU: pvMBFrameStartCur = eMBRTUStart; pvMBFrameStopCur = eMBRTUStop; peMBFrameSendCur = eMBRTUSend; peMBFrameReceiveCur = eMBRTUReceive; pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL; pxMBFrameCBByteReceived = xMBRTUReceiveFSM; pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM; pxMBPortCBTimerExpired = xMBRTUTimerT35Expired; eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity ); break; default: eStatus = MB_EINVAL; } if( eStatus == MB_ENOERR ) { if( !xMBPortEventInit( ) ) { /* port dependent event module initalization failed. */ eStatus = MB_EPORTERR; } else { eMBCurrentMode = eMode; eMBState = STATE_DISABLED; } } return eStatus; }
/* ----------------------- Start implementation -----------------------------*/ eMBErrorCode eMBInit( stMBContext *stContext, eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity ) { eMBErrorCode eStatus = MB_ENOERR; stContext->eState=STATE_NOT_INITIALIZED; /* check preconditions */ if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) || ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) ) { eStatus = MB_EINVAL; } else { stContext->ucMBAddress = ucSlaveAddress; switch ( eMode ) { #if MB_RTU_ENABLED > 0 case MB_RTU: stContext->pvMBFrameStartCur = eMBRTUStart; stContext->pvMBFrameStopCur = eMBRTUStop; stContext->peMBFrameSendCur = eMBRTUSend; stContext->peMBFrameReceiveCur = eMBRTUReceive; stContext->pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? stContext->stCommunication.vMBPortClose : NULL; stContext->pxMBFrameCBByteReceived = xMBRTUReceiveFSM; stContext->pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM; stContext->pxMBPortCBTimerExpired = xMBRTUTimerT35Expired; eStatus = eMBRTUInit(&stContext->stCommunication, &stContext->stTimer, stContext->ucMBAddress, ucPort, ulBaudRate, eParity ); break; #endif #if MB_ASCII_ENABLED > 0 case MB_ASCII: stContext->pvMBFrameStartCur = eMBASCIIStart; stContext->pvMBFrameStopCur = eMBASCIIStop; stContext->peMBFrameSendCur = eMBASCIISend; stContext->peMBFrameReceiveCur = eMBASCIIReceive; stContext->pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL; stContext->pxMBFrameCBByteReceived = xMBASCIIReceiveFSM; stContext->pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM; stContext->pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired; eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity ); break; #endif default: eStatus = MB_EINVAL; } if( eStatus == MB_ENOERR ) { if( !xMBPortEventInit(&stContext->stEvent ) ) { /* port dependent event module initalization failed. */ eStatus = MB_EPORTERR; } else { stContext->eMBCurrentMode = eMode; stContext->eState = STATE_DISABLED; } } } return eStatus; }
eMBErrorCode eMBInit(eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParity eParity) { eMBErrorCode eStatus = MB_ENOERR; /* check preconditions */ if ((ucSlaveAddress == MB_ADDRESS_BROADCAST) || (ucSlaveAddress < MB_ADDRESS_MIN) || (ucSlaveAddress > MB_ADDRESS_MAX)) { eStatus = MB_EINVAL; } else { ucMBAddress = ucSlaveAddress; switch (eMode) { #ifdef CONFIG_MB_RTU_ENABLED case MB_RTU: pvMBFrameStartCur = eMBRTUStart; pvMBFrameStopCur = eMBRTUStop; peMBFrameSendCur = eMBRTUSend; peMBFrameReceiveCur = eMBRTUReceive; pvMBFrameCloseCur = vMBPortClose; pxMBFrameCBByteReceived = xMBRTUReceiveFSM; pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM; pxMBPortCBTimerExpired = xMBRTUTimerT35Expired; eStatus = eMBRTUInit(ucMBAddress, ucPort, ulBaudRate, eParity); break; #endif #ifdef CONFIG_MB_ASCII_ENABLED case MB_ASCII: pvMBFrameStartCur = eMBASCIIStart; pvMBFrameStopCur = eMBASCIIStop; peMBFrameSendCur = eMBASCIISend; peMBFrameReceiveCur = eMBASCIIReceive; pvMBFrameCloseCur = vMBPortClose; pxMBFrameCBByteReceived = xMBASCIIReceiveFSM; pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM; pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired; eStatus = eMBASCIIInit(ucMBAddress, ucPort, ulBaudRate, eParity); break; #endif default: eStatus = MB_EINVAL; } if (eStatus == MB_ENOERR) { if (!xMBPortEventInit()) { /* port dependent event module initialization failed. */ eStatus = MB_EPORTERR; } else { eMBCurrentMode = eMode; eMBState = STATE_DISABLED; } } } return eStatus; }