Tom Martins
/
Probleme_implementation_lwip2
test public
Fork of Probleme_implementation_lwip by
mb.cpp@2:9e76d51d9fb6, 2018-07-02 (annotated)
- Committer:
- TomTom83
- Date:
- Mon Jul 02 15:18:18 2018 +0000
- Revision:
- 2:9e76d51d9fb6
- Parent:
- 0:0453a0a7e500
probl?mes d'adaptation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cam | 0:0453a0a7e500 | 1 | /* |
cam | 0:0453a0a7e500 | 2 | * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. |
cam | 0:0453a0a7e500 | 3 | * Copyright (c) 2006 Christian Walter <wolti@sil.at> |
cam | 0:0453a0a7e500 | 4 | * All rights reserved. |
cam | 0:0453a0a7e500 | 5 | * |
cam | 0:0453a0a7e500 | 6 | * Redistribution and use in source and binary forms, with or without |
cam | 0:0453a0a7e500 | 7 | * modification, are permitted provided that the following conditions |
cam | 0:0453a0a7e500 | 8 | * are met: |
cam | 0:0453a0a7e500 | 9 | * 1. Redistributions of source code must retain the above copyright |
cam | 0:0453a0a7e500 | 10 | * notice, this list of conditions and the following disclaimer. |
cam | 0:0453a0a7e500 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
cam | 0:0453a0a7e500 | 12 | * notice, this list of conditions and the following disclaimer in the |
cam | 0:0453a0a7e500 | 13 | * documentation and/or other materials provided with the distribution. |
cam | 0:0453a0a7e500 | 14 | * 3. The name of the author may not be used to endorse or promote products |
cam | 0:0453a0a7e500 | 15 | * derived from this software without specific prior written permission. |
cam | 0:0453a0a7e500 | 16 | * |
cam | 0:0453a0a7e500 | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
cam | 0:0453a0a7e500 | 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
cam | 0:0453a0a7e500 | 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
cam | 0:0453a0a7e500 | 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
cam | 0:0453a0a7e500 | 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
cam | 0:0453a0a7e500 | 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
cam | 0:0453a0a7e500 | 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
cam | 0:0453a0a7e500 | 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
cam | 0:0453a0a7e500 | 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
cam | 0:0453a0a7e500 | 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cam | 0:0453a0a7e500 | 27 | * |
cam | 0:0453a0a7e500 | 28 | * File: $Id: mb.c,v 1.27 2007/02/18 23:45:41 wolti Exp $ |
cam | 0:0453a0a7e500 | 29 | */ |
cam | 0:0453a0a7e500 | 30 | |
cam | 0:0453a0a7e500 | 31 | /* ----------------------- System includes ----------------------------------*/ |
cam | 0:0453a0a7e500 | 32 | #include "stdlib.h" |
cam | 0:0453a0a7e500 | 33 | #include "string.h" |
cam | 0:0453a0a7e500 | 34 | |
cam | 0:0453a0a7e500 | 35 | /* ----------------------- Platform includes --------------------------------*/ |
cam | 0:0453a0a7e500 | 36 | #include "port.h" |
cam | 0:0453a0a7e500 | 37 | |
cam | 0:0453a0a7e500 | 38 | /* ----------------------- Modbus includes ----------------------------------*/ |
cam | 0:0453a0a7e500 | 39 | #include "mb.h" |
cam | 0:0453a0a7e500 | 40 | #include "mbconfig.h" |
cam | 0:0453a0a7e500 | 41 | #include "mbframe.h" |
cam | 0:0453a0a7e500 | 42 | #include "mbproto.h" |
cam | 0:0453a0a7e500 | 43 | #include "mbfunc.h" |
cam | 0:0453a0a7e500 | 44 | #include "mbport.h" |
cam | 0:0453a0a7e500 | 45 | #if MB_RTU_ENABLED == 1 |
cam | 0:0453a0a7e500 | 46 | #include "mbrtu.h" |
cam | 0:0453a0a7e500 | 47 | #endif |
cam | 0:0453a0a7e500 | 48 | #if MB_ASCII_ENABLED == 1 |
cam | 0:0453a0a7e500 | 49 | #include "mbascii.h" |
cam | 0:0453a0a7e500 | 50 | #endif |
cam | 0:0453a0a7e500 | 51 | #if MB_TCP_ENABLED == 1 |
cam | 0:0453a0a7e500 | 52 | #include "mbtcp.h" |
cam | 0:0453a0a7e500 | 53 | #endif |
cam | 0:0453a0a7e500 | 54 | |
cam | 0:0453a0a7e500 | 55 | #ifndef MB_PORT_HAS_CLOSE |
cam | 0:0453a0a7e500 | 56 | #define MB_PORT_HAS_CLOSE 0 |
cam | 0:0453a0a7e500 | 57 | #endif |
cam | 0:0453a0a7e500 | 58 | |
cam | 0:0453a0a7e500 | 59 | /* ----------------------- Static variables ---------------------------------*/ |
cam | 0:0453a0a7e500 | 60 | |
cam | 0:0453a0a7e500 | 61 | |
cam | 0:0453a0a7e500 | 62 | static UCHAR ucMBAddress; |
cam | 0:0453a0a7e500 | 63 | static eMBMode eMBCurrentMode; |
cam | 0:0453a0a7e500 | 64 | |
cam | 0:0453a0a7e500 | 65 | static enum { |
cam | 0:0453a0a7e500 | 66 | STATE_ENABLED, |
cam | 0:0453a0a7e500 | 67 | STATE_DISABLED, |
cam | 0:0453a0a7e500 | 68 | STATE_NOT_INITIALIZED |
cam | 0:0453a0a7e500 | 69 | } eMBState = STATE_NOT_INITIALIZED; |
cam | 0:0453a0a7e500 | 70 | |
cam | 0:0453a0a7e500 | 71 | /* Functions pointer which are initialized in eMBInit( ). Depending on the |
cam | 0:0453a0a7e500 | 72 | * mode (RTU or ASCII) the are set to the correct implementations. |
cam | 0:0453a0a7e500 | 73 | */ |
cam | 0:0453a0a7e500 | 74 | static peMBFrameSend peMBFrameSendCur; |
cam | 0:0453a0a7e500 | 75 | static pvMBFrameStart pvMBFrameStartCur; |
cam | 0:0453a0a7e500 | 76 | static pvMBFrameStop pvMBFrameStopCur; |
cam | 0:0453a0a7e500 | 77 | static peMBFrameReceive peMBFrameReceiveCur; |
cam | 0:0453a0a7e500 | 78 | static pvMBFrameClose pvMBFrameCloseCur; |
cam | 0:0453a0a7e500 | 79 | |
cam | 0:0453a0a7e500 | 80 | /* Callback functions required by the porting layer. They are called when |
cam | 0:0453a0a7e500 | 81 | * an external event has happend which includes a timeout or the reception |
cam | 0:0453a0a7e500 | 82 | * or transmission of a character. |
cam | 0:0453a0a7e500 | 83 | */ |
cam | 0:0453a0a7e500 | 84 | BOOL( *pxMBFrameCBByteReceived ) ( void ); |
cam | 0:0453a0a7e500 | 85 | BOOL( *pxMBFrameCBTransmitterEmpty ) ( void ); |
cam | 0:0453a0a7e500 | 86 | BOOL( *pxMBPortCBTimerExpired ) ( void ); |
cam | 0:0453a0a7e500 | 87 | |
cam | 0:0453a0a7e500 | 88 | BOOL( *pxMBFrameCBReceiveFSMCur ) ( void ); |
cam | 0:0453a0a7e500 | 89 | BOOL( *pxMBFrameCBTransmitFSMCur ) ( void ); |
cam | 0:0453a0a7e500 | 90 | |
cam | 0:0453a0a7e500 | 91 | /* An array of Modbus functions handlers which associates Modbus function |
cam | 0:0453a0a7e500 | 92 | * codes with implementing functions. |
cam | 0:0453a0a7e500 | 93 | */ |
cam | 0:0453a0a7e500 | 94 | static xMBFunctionHandler xFuncHandlers[MB_FUNC_HANDLERS_MAX] = { |
cam | 0:0453a0a7e500 | 95 | #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0 |
cam | 0:0453a0a7e500 | 96 | {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID}, |
cam | 0:0453a0a7e500 | 97 | #endif |
cam | 0:0453a0a7e500 | 98 | #if MB_FUNC_READ_INPUT_ENABLED > 0 |
cam | 0:0453a0a7e500 | 99 | {MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister}, |
cam | 0:0453a0a7e500 | 100 | #endif |
cam | 0:0453a0a7e500 | 101 | #if MB_FUNC_READ_HOLDING_ENABLED > 0 |
cam | 0:0453a0a7e500 | 102 | {MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister}, |
cam | 0:0453a0a7e500 | 103 | #endif |
cam | 0:0453a0a7e500 | 104 | #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0 |
cam | 0:0453a0a7e500 | 105 | {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister}, |
cam | 0:0453a0a7e500 | 106 | #endif |
cam | 0:0453a0a7e500 | 107 | #if MB_FUNC_WRITE_HOLDING_ENABLED > 0 |
cam | 0:0453a0a7e500 | 108 | {MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister}, |
cam | 0:0453a0a7e500 | 109 | #endif |
cam | 0:0453a0a7e500 | 110 | #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0 |
cam | 0:0453a0a7e500 | 111 | {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister}, |
cam | 0:0453a0a7e500 | 112 | #endif |
cam | 0:0453a0a7e500 | 113 | #if MB_FUNC_READ_COILS_ENABLED > 0 |
cam | 0:0453a0a7e500 | 114 | {MB_FUNC_READ_COILS, eMBFuncReadCoils}, |
cam | 0:0453a0a7e500 | 115 | #endif |
cam | 0:0453a0a7e500 | 116 | #if MB_FUNC_WRITE_COIL_ENABLED > 0 |
cam | 0:0453a0a7e500 | 117 | {MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil}, |
cam | 0:0453a0a7e500 | 118 | #endif |
cam | 0:0453a0a7e500 | 119 | #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0 |
cam | 0:0453a0a7e500 | 120 | {MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils}, |
cam | 0:0453a0a7e500 | 121 | #endif |
cam | 0:0453a0a7e500 | 122 | #if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0 |
cam | 0:0453a0a7e500 | 123 | {MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs}, |
cam | 0:0453a0a7e500 | 124 | #endif |
cam | 0:0453a0a7e500 | 125 | }; |
cam | 0:0453a0a7e500 | 126 | |
cam | 0:0453a0a7e500 | 127 | /* ----------------------- Start implementation -----------------------------*/ |
cam | 0:0453a0a7e500 | 128 | eMBErrorCode |
cam | 0:0453a0a7e500 | 129 | eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity ) { |
cam | 0:0453a0a7e500 | 130 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 131 | |
cam | 0:0453a0a7e500 | 132 | /* check preconditions */ |
cam | 0:0453a0a7e500 | 133 | if ( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) || |
cam | 0:0453a0a7e500 | 134 | ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) ) { |
cam | 0:0453a0a7e500 | 135 | eStatus = MB_EINVAL; |
cam | 0:0453a0a7e500 | 136 | } else { |
cam | 0:0453a0a7e500 | 137 | ucMBAddress = ucSlaveAddress; |
cam | 0:0453a0a7e500 | 138 | |
cam | 0:0453a0a7e500 | 139 | switch ( eMode ) { |
cam | 0:0453a0a7e500 | 140 | #if MB_RTU_ENABLED > 0 |
cam | 0:0453a0a7e500 | 141 | case MB_RTU: |
cam | 0:0453a0a7e500 | 142 | pvMBFrameStartCur = eMBRTUStart; |
cam | 0:0453a0a7e500 | 143 | pvMBFrameStopCur = eMBRTUStop; |
cam | 0:0453a0a7e500 | 144 | peMBFrameSendCur = eMBRTUSend; |
cam | 0:0453a0a7e500 | 145 | peMBFrameReceiveCur = eMBRTUReceive; |
cam | 0:0453a0a7e500 | 146 | pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL; |
cam | 0:0453a0a7e500 | 147 | pxMBFrameCBByteReceived = xMBRTUReceiveFSM; |
cam | 0:0453a0a7e500 | 148 | pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM; |
cam | 0:0453a0a7e500 | 149 | pxMBPortCBTimerExpired = xMBRTUTimerT35Expired; |
cam | 0:0453a0a7e500 | 150 | |
cam | 0:0453a0a7e500 | 151 | eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity ); |
cam | 0:0453a0a7e500 | 152 | break; |
cam | 0:0453a0a7e500 | 153 | #endif |
cam | 0:0453a0a7e500 | 154 | #if MB_ASCII_ENABLED > 0 |
cam | 0:0453a0a7e500 | 155 | case MB_ASCII: |
cam | 0:0453a0a7e500 | 156 | pvMBFrameStartCur = eMBASCIIStart; |
cam | 0:0453a0a7e500 | 157 | pvMBFrameStopCur = eMBASCIIStop; |
cam | 0:0453a0a7e500 | 158 | peMBFrameSendCur = eMBASCIISend; |
cam | 0:0453a0a7e500 | 159 | peMBFrameReceiveCur = eMBASCIIReceive; |
cam | 0:0453a0a7e500 | 160 | pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL; |
cam | 0:0453a0a7e500 | 161 | pxMBFrameCBByteReceived = xMBASCIIReceiveFSM; |
cam | 0:0453a0a7e500 | 162 | pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM; |
cam | 0:0453a0a7e500 | 163 | pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired; |
cam | 0:0453a0a7e500 | 164 | |
cam | 0:0453a0a7e500 | 165 | eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity ); |
cam | 0:0453a0a7e500 | 166 | break; |
cam | 0:0453a0a7e500 | 167 | #endif |
cam | 0:0453a0a7e500 | 168 | default: |
cam | 0:0453a0a7e500 | 169 | eStatus = MB_EINVAL; |
cam | 0:0453a0a7e500 | 170 | } |
cam | 0:0453a0a7e500 | 171 | |
cam | 0:0453a0a7e500 | 172 | if ( eStatus == MB_ENOERR ) { |
cam | 0:0453a0a7e500 | 173 | if ( !xMBPortEventInit( ) ) { |
cam | 0:0453a0a7e500 | 174 | /* port dependent event module initalization failed. */ |
cam | 0:0453a0a7e500 | 175 | eStatus = MB_EPORTERR; |
cam | 0:0453a0a7e500 | 176 | } else { |
cam | 0:0453a0a7e500 | 177 | eMBCurrentMode = eMode; |
cam | 0:0453a0a7e500 | 178 | eMBState = STATE_DISABLED; |
cam | 0:0453a0a7e500 | 179 | } |
cam | 0:0453a0a7e500 | 180 | } |
cam | 0:0453a0a7e500 | 181 | } |
cam | 0:0453a0a7e500 | 182 | return eStatus; |
cam | 0:0453a0a7e500 | 183 | } |
cam | 0:0453a0a7e500 | 184 | |
cam | 0:0453a0a7e500 | 185 | #if MB_TCP_ENABLED > 0 |
cam | 0:0453a0a7e500 | 186 | eMBErrorCode |
cam | 0:0453a0a7e500 | 187 | eMBTCPInit( USHORT ucTCPPort ) { |
cam | 0:0453a0a7e500 | 188 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 189 | |
cam | 0:0453a0a7e500 | 190 | if ( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR ) { |
cam | 0:0453a0a7e500 | 191 | eMBState = STATE_DISABLED; |
cam | 0:0453a0a7e500 | 192 | } else if ( !xMBPortEventInit( ) ) { |
cam | 0:0453a0a7e500 | 193 | /* Port dependent event module initalization failed. */ |
cam | 0:0453a0a7e500 | 194 | eStatus = MB_EPORTERR; |
cam | 0:0453a0a7e500 | 195 | } else { |
cam | 0:0453a0a7e500 | 196 | pvMBFrameStartCur = eMBTCPStart; |
cam | 0:0453a0a7e500 | 197 | pvMBFrameStopCur = eMBTCPStop; |
cam | 0:0453a0a7e500 | 198 | peMBFrameReceiveCur = eMBTCPReceive; |
cam | 0:0453a0a7e500 | 199 | peMBFrameSendCur = eMBTCPSend; |
cam | 0:0453a0a7e500 | 200 | pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBTCPPortClose : NULL; |
cam | 0:0453a0a7e500 | 201 | ucMBAddress = MB_TCP_PSEUDO_ADDRESS; |
cam | 0:0453a0a7e500 | 202 | eMBCurrentMode = MB_TCP; |
cam | 0:0453a0a7e500 | 203 | eMBState = STATE_DISABLED; |
cam | 0:0453a0a7e500 | 204 | } |
cam | 0:0453a0a7e500 | 205 | return eStatus; |
cam | 0:0453a0a7e500 | 206 | } |
cam | 0:0453a0a7e500 | 207 | #endif |
cam | 0:0453a0a7e500 | 208 | |
cam | 0:0453a0a7e500 | 209 | |
cam | 0:0453a0a7e500 | 210 | eMBErrorCode |
cam | 0:0453a0a7e500 | 211 | eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler ) { |
cam | 0:0453a0a7e500 | 212 | int i; |
cam | 0:0453a0a7e500 | 213 | eMBErrorCode eStatus; |
cam | 0:0453a0a7e500 | 214 | |
cam | 0:0453a0a7e500 | 215 | if ( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) ) { |
cam | 0:0453a0a7e500 | 216 | ENTER_CRITICAL_SECTION( ); |
cam | 0:0453a0a7e500 | 217 | if ( pxHandler != NULL ) { |
cam | 0:0453a0a7e500 | 218 | for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) { |
cam | 0:0453a0a7e500 | 219 | if ( ( xFuncHandlers[i].pxHandler == NULL ) || |
cam | 0:0453a0a7e500 | 220 | ( xFuncHandlers[i].pxHandler == pxHandler ) ) { |
cam | 0:0453a0a7e500 | 221 | xFuncHandlers[i].ucFunctionCode = ucFunctionCode; |
cam | 0:0453a0a7e500 | 222 | xFuncHandlers[i].pxHandler = pxHandler; |
cam | 0:0453a0a7e500 | 223 | break; |
cam | 0:0453a0a7e500 | 224 | } |
cam | 0:0453a0a7e500 | 225 | } |
cam | 0:0453a0a7e500 | 226 | eStatus = ( i != MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES; |
cam | 0:0453a0a7e500 | 227 | } else { |
cam | 0:0453a0a7e500 | 228 | for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) { |
cam | 0:0453a0a7e500 | 229 | if ( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) { |
cam | 0:0453a0a7e500 | 230 | xFuncHandlers[i].ucFunctionCode = 0; |
cam | 0:0453a0a7e500 | 231 | xFuncHandlers[i].pxHandler = NULL; |
cam | 0:0453a0a7e500 | 232 | break; |
cam | 0:0453a0a7e500 | 233 | } |
cam | 0:0453a0a7e500 | 234 | } |
cam | 0:0453a0a7e500 | 235 | /* Remove can't fail. */ |
cam | 0:0453a0a7e500 | 236 | eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 237 | } |
cam | 0:0453a0a7e500 | 238 | EXIT_CRITICAL_SECTION( ); |
cam | 0:0453a0a7e500 | 239 | } else { |
cam | 0:0453a0a7e500 | 240 | eStatus = MB_EINVAL; |
cam | 0:0453a0a7e500 | 241 | } |
cam | 0:0453a0a7e500 | 242 | return eStatus; |
cam | 0:0453a0a7e500 | 243 | } |
cam | 0:0453a0a7e500 | 244 | |
cam | 0:0453a0a7e500 | 245 | |
cam | 0:0453a0a7e500 | 246 | eMBErrorCode |
cam | 0:0453a0a7e500 | 247 | eMBClose( void ) { |
cam | 0:0453a0a7e500 | 248 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 249 | |
cam | 0:0453a0a7e500 | 250 | if ( eMBState == STATE_DISABLED ) { |
cam | 0:0453a0a7e500 | 251 | if ( pvMBFrameCloseCur != NULL ) { |
cam | 0:0453a0a7e500 | 252 | pvMBFrameCloseCur( ); |
cam | 0:0453a0a7e500 | 253 | } |
cam | 0:0453a0a7e500 | 254 | } else { |
cam | 0:0453a0a7e500 | 255 | eStatus = MB_EILLSTATE; |
cam | 0:0453a0a7e500 | 256 | } |
cam | 0:0453a0a7e500 | 257 | return eStatus; |
cam | 0:0453a0a7e500 | 258 | } |
cam | 0:0453a0a7e500 | 259 | |
cam | 0:0453a0a7e500 | 260 | eMBErrorCode |
cam | 0:0453a0a7e500 | 261 | eMBEnable( void ) { |
cam | 0:0453a0a7e500 | 262 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 263 | |
cam | 0:0453a0a7e500 | 264 | if ( eMBState == STATE_DISABLED ) { |
cam | 0:0453a0a7e500 | 265 | /* Activate the protocol stack. */ |
cam | 0:0453a0a7e500 | 266 | pvMBFrameStartCur( ); |
cam | 0:0453a0a7e500 | 267 | eMBState = STATE_ENABLED; |
cam | 0:0453a0a7e500 | 268 | } else { |
cam | 0:0453a0a7e500 | 269 | eStatus = MB_EILLSTATE; |
cam | 0:0453a0a7e500 | 270 | } |
cam | 0:0453a0a7e500 | 271 | return eStatus; |
cam | 0:0453a0a7e500 | 272 | } |
cam | 0:0453a0a7e500 | 273 | |
cam | 0:0453a0a7e500 | 274 | eMBErrorCode |
cam | 0:0453a0a7e500 | 275 | eMBDisable( void ) { |
cam | 0:0453a0a7e500 | 276 | eMBErrorCode eStatus; |
cam | 0:0453a0a7e500 | 277 | |
cam | 0:0453a0a7e500 | 278 | if ( eMBState == STATE_ENABLED ) { |
cam | 0:0453a0a7e500 | 279 | pvMBFrameStopCur( ); |
cam | 0:0453a0a7e500 | 280 | eMBState = STATE_DISABLED; |
cam | 0:0453a0a7e500 | 281 | eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 282 | } else if ( eMBState == STATE_DISABLED ) { |
cam | 0:0453a0a7e500 | 283 | eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 284 | } else { |
cam | 0:0453a0a7e500 | 285 | eStatus = MB_EILLSTATE; |
cam | 0:0453a0a7e500 | 286 | } |
cam | 0:0453a0a7e500 | 287 | return eStatus; |
cam | 0:0453a0a7e500 | 288 | } |
cam | 0:0453a0a7e500 | 289 | |
cam | 0:0453a0a7e500 | 290 | eMBErrorCode |
cam | 0:0453a0a7e500 | 291 | eMBPoll( void ) { |
cam | 0:0453a0a7e500 | 292 | static UCHAR *ucMBFrame; |
cam | 0:0453a0a7e500 | 293 | static UCHAR ucRcvAddress; |
cam | 0:0453a0a7e500 | 294 | static UCHAR ucFunctionCode; |
cam | 0:0453a0a7e500 | 295 | static USHORT usLength; |
cam | 0:0453a0a7e500 | 296 | static eMBException eException; |
cam | 0:0453a0a7e500 | 297 | |
cam | 0:0453a0a7e500 | 298 | int i; |
cam | 0:0453a0a7e500 | 299 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 300 | eMBEventType eEvent; |
cam | 0:0453a0a7e500 | 301 | |
cam | 0:0453a0a7e500 | 302 | /* Check if the protocol stack is ready. */ |
cam | 0:0453a0a7e500 | 303 | if ( eMBState != STATE_ENABLED ) { |
cam | 0:0453a0a7e500 | 304 | return MB_EILLSTATE; |
cam | 0:0453a0a7e500 | 305 | } |
cam | 0:0453a0a7e500 | 306 | |
cam | 0:0453a0a7e500 | 307 | /* Check if there is a event available. If not return control to caller. |
cam | 0:0453a0a7e500 | 308 | * Otherwise we will handle the event. */ |
cam | 0:0453a0a7e500 | 309 | if ( xMBPortEventGet( &eEvent ) == TRUE ) { |
cam | 0:0453a0a7e500 | 310 | switch ( eEvent ) { |
cam | 0:0453a0a7e500 | 311 | case EV_READY: |
cam | 0:0453a0a7e500 | 312 | break; |
cam | 0:0453a0a7e500 | 313 | |
cam | 0:0453a0a7e500 | 314 | case EV_FRAME_RECEIVED: |
cam | 0:0453a0a7e500 | 315 | eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); |
cam | 0:0453a0a7e500 | 316 | if ( eStatus == MB_ENOERR ) { |
cam | 0:0453a0a7e500 | 317 | /* Check if the frame is for us. If not ignore the frame. */ |
cam | 0:0453a0a7e500 | 318 | if ( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) ) { |
cam | 0:0453a0a7e500 | 319 | ( void )xMBPortEventPost( EV_EXECUTE ); |
cam | 0:0453a0a7e500 | 320 | } |
cam | 0:0453a0a7e500 | 321 | } |
cam | 0:0453a0a7e500 | 322 | break; |
cam | 0:0453a0a7e500 | 323 | |
cam | 0:0453a0a7e500 | 324 | case EV_EXECUTE: |
cam | 0:0453a0a7e500 | 325 | ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; |
cam | 0:0453a0a7e500 | 326 | eException = MB_EX_ILLEGAL_FUNCTION; |
cam | 0:0453a0a7e500 | 327 | |
cam | 0:0453a0a7e500 | 328 | |
cam | 0:0453a0a7e500 | 329 | for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) { |
cam | 0:0453a0a7e500 | 330 | /* No more function handlers registered. Abort. */ |
cam | 0:0453a0a7e500 | 331 | if ( xFuncHandlers[i].ucFunctionCode == 0 ) { |
cam | 0:0453a0a7e500 | 332 | break; |
cam | 0:0453a0a7e500 | 333 | } else if ( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) { |
cam | 0:0453a0a7e500 | 334 | eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength ); |
cam | 0:0453a0a7e500 | 335 | break; |
cam | 0:0453a0a7e500 | 336 | } |
cam | 0:0453a0a7e500 | 337 | } |
cam | 0:0453a0a7e500 | 338 | |
cam | 0:0453a0a7e500 | 339 | /* If the request was not sent to the broadcast address we |
cam | 0:0453a0a7e500 | 340 | * return a reply. */ |
cam | 0:0453a0a7e500 | 341 | if ( ucRcvAddress != MB_ADDRESS_BROADCAST ) { |
cam | 0:0453a0a7e500 | 342 | if ( eException != MB_EX_NONE ) { |
cam | 0:0453a0a7e500 | 343 | /* An exception occured. Build an error frame. */ |
cam | 0:0453a0a7e500 | 344 | usLength = 0; |
cam | 0:0453a0a7e500 | 345 | ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR ); |
cam | 0:0453a0a7e500 | 346 | ucMBFrame[usLength++] = eException; |
cam | 0:0453a0a7e500 | 347 | } |
cam | 0:0453a0a7e500 | 348 | eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength ); |
cam | 0:0453a0a7e500 | 349 | } |
cam | 0:0453a0a7e500 | 350 | break; |
cam | 0:0453a0a7e500 | 351 | |
cam | 0:0453a0a7e500 | 352 | case EV_FRAME_SENT: |
cam | 0:0453a0a7e500 | 353 | break; |
cam | 0:0453a0a7e500 | 354 | } |
cam | 0:0453a0a7e500 | 355 | } |
cam | 0:0453a0a7e500 | 356 | return MB_ENOERR; |
cam | 0:0453a0a7e500 | 357 | } |