123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- #include "stdlib.h"
- #include "string.h"
- #include "port.h"
- #include "mb.h"
- #include "mbconfig.h"
- #include "mbframe.h"
- #include "mbproto.h"
- #include "mbfunc.h"
- #include "mbport.h"
- #if MB_RTU_ENABLED == 1
- #include "mbrtu.h"
- #endif
- #if MB_ASCII_ENABLED == 1
- #include "mbascii.h"
- #endif
- #if MB_TCP_ENABLED == 1
- #include "mbtcp.h"
- #endif
- #ifndef MB_PORT_HAS_CLOSE
- #define MB_PORT_HAS_CLOSE 0
- #endif
- UCHAR rcvAddress;
- static UCHAR ucMBAddress;
- static eMBMode eMBCurrentMode;
- static enum
- {
- STATE_ENABLED,
- STATE_DISABLED,
- STATE_NOT_INITIALIZED
- } eMBState = STATE_NOT_INITIALIZED;
- static peMBFrameSend peMBFrameSendCur;
- static pvMBFrameStart pvMBFrameStartCur;
- static pvMBFrameStop pvMBFrameStopCur;
- static peMBFrameReceive peMBFrameReceiveCur;
- static pvMBFrameClose pvMBFrameCloseCur;
- BOOL( *pxMBFrameCBByteReceived ) ( void );
- BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
- BOOL( *pxMBPortCBTimerExpired ) ( void );
- BOOL( *pxMBFrameCBReceiveFSMCur ) ( void );
- BOOL( *pxMBFrameCBTransmitFSMCur ) ( void );
- static xMBFunctionHandler xFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
- #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
- {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
- #endif
- #if MB_FUNC_READ_INPUT_ENABLED > 0
- {MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
- #endif
- #if MB_FUNC_READ_HOLDING_ENABLED > 0
- {MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
- #endif
- #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
- {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
- #endif
- #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
- {MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
- #endif
- #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
- {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
- #endif
- #if MB_FUNC_READ_COILS_ENABLED > 0
- {MB_FUNC_READ_COILS, eMBFuncReadCoils},
- #endif
- #if MB_FUNC_WRITE_COIL_ENABLED > 0
- {MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
- #endif
- #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
- {MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
- #endif
- #if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
- {MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
- #endif
- #if MB_FUNC_UPDATE_ENABLED > 0
- {MB_FUNC_UPDATE, eMBFuncUpdate},
- #endif
- #if MB_FUNC_SET_ADDR_ID_ENABLED > 0
- {MB_FUNC_SET_ADDR_ID, eMBFuncSetAddrId},
- #endif
- #if MB_FUNC_SET_ADDR_SERIAL_ENABLED > 0
- {MB_FUNC_SET_ADDR_SERIAL, eMBFuncSetAddrSerial}
- #endif
- };
- eMBErrorCode
- eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate,
- eMBParity eParity, unsigned int stop_bit )
- {
- eMBErrorCode eStatus = MB_ENOERR;
-
- if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
- ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) )
- {
- eStatus = MB_EINVAL;
- }
- 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, stop_bit );
- 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( ) )
- {
-
- eStatus = MB_EPORTERR;
- }
- else
- {
- eMBCurrentMode = eMode;
- eMBState = STATE_DISABLED;
- }
- }
- }
- return eStatus;
- }
- #if MB_TCP_ENABLED > 0
- eMBErrorCode
- eMBTCPInit( USHORT ucTCPPort )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- if( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR )
- {
- eMBState = STATE_DISABLED;
- }
- else if( !xMBPortEventInit( ) )
- {
-
- eStatus = MB_EPORTERR;
- }
- else
- {
- pvMBFrameStartCur = eMBTCPStart;
- pvMBFrameStopCur = eMBTCPStop;
- peMBFrameReceiveCur = eMBTCPReceive;
- peMBFrameSendCur = eMBTCPSend;
- pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBTCPPortClose : NULL;
- ucMBAddress = MB_TCP_PSEUDO_ADDRESS;
- eMBCurrentMode = MB_TCP;
- eMBState = STATE_DISABLED;
- }
- return eStatus;
- }
- #endif
- eMBErrorCode
- eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
- {
- int i;
- eMBErrorCode eStatus;
- if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) )
- {
- ENTER_CRITICAL_SECTION( );
- if( pxHandler != NULL )
- {
- for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
- {
- if( ( xFuncHandlers[i].pxHandler == NULL ) ||
- ( xFuncHandlers[i].pxHandler == pxHandler ) )
- {
- xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
- xFuncHandlers[i].pxHandler = pxHandler;
- break;
- }
- }
- eStatus = ( i != MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES;
- }
- else
- {
- for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
- {
- if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
- {
- xFuncHandlers[i].ucFunctionCode = 0;
- xFuncHandlers[i].pxHandler = NULL;
- break;
- }
- }
-
- eStatus = MB_ENOERR;
- }
- EXIT_CRITICAL_SECTION( );
- }
- else
- {
- eStatus = MB_EINVAL;
- }
- return eStatus;
- }
- eMBErrorCode
- eMBClose( void )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- if( eMBState == STATE_DISABLED )
- {
- if( pvMBFrameCloseCur != NULL )
- {
- pvMBFrameCloseCur( );
- }
- }
- else
- {
- eStatus = MB_EILLSTATE;
- }
- return eStatus;
- }
- eMBErrorCode
- eMBEnable( void )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- if( eMBState == STATE_DISABLED )
- {
-
- pvMBFrameStartCur( );
- eMBState = STATE_ENABLED;
- }
- else
- {
- eStatus = MB_EILLSTATE;
- }
- return eStatus;
- }
- eMBErrorCode
- eMBDisable( void )
- {
- eMBErrorCode eStatus;
- if( eMBState == STATE_ENABLED )
- {
- pvMBFrameStopCur( );
- eMBState = STATE_DISABLED;
- eStatus = MB_ENOERR;
- }
- else if( eMBState == STATE_DISABLED )
- {
- eStatus = MB_ENOERR;
- }
- else
- {
- eStatus = MB_EILLSTATE;
- }
- return eStatus;
- }
- 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;
-
- if( eMBState != STATE_ENABLED )
- {
- return MB_EILLSTATE;
- }
-
- if( xMBPortEventGet( &eEvent ) == TRUE )
- {
- switch ( eEvent )
- {
- case EV_READY:
- break;
- case EV_FRAME_RECEIVED:
- eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
- if( eStatus == MB_ENOERR )
- {
-
- if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
- {
- rcvAddress = ucRcvAddress;
- ( 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++ )
- {
-
- if( xFuncHandlers[i].ucFunctionCode == 0 )
- {
- break;
- }
- else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
- {
- eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
- break;
- }
- }
-
- if( ucRcvAddress != MB_ADDRESS_BROADCAST )
- {
- if( eException != MB_EX_NONE )
- {
-
- 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
- eMBCheckSlaveAddr(UCHAR ucSlaveAddress)
- {
- if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
- ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) )
- {
- return MB_EINVAL;
- }
- else
- {
- return MB_ENOERR;
- }
- }
- eMBErrorCode
- eMBSetSlaveAddr(UCHAR ucSlaveAddress)
- {
- if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
- ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) )
- {
- return MB_EINVAL;
- }
- else
- {
- ucMBAddress = ucSlaveAddress;
- }
- return MB_ENOERR;
- }
-
|