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