modified for nuc472

Dependents:   modbus-over-rs485-sample NTOUEE-mbed-modbus-RTU NuMaker_NuWicam_Lite

Fork of Modbus by Matthew Waddilove

Committer:
giryan
Date:
Sun Aug 22 16:34:20 2010 +0000
Revision:
0:274eb57e1df3
Child:
3:419ee4c5e10f

        

Who changed what in which revision?

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