Jason Engelman / Mbed 2 deprecated Modbus

Dependencies:   mbed

Committer:
tecnosys
Date:
Tue Oct 05 11:51:06 2010 +0000
Revision:
0:7b5d37a81b6b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tecnosys 0:7b5d37a81b6b 1 /*
tecnosys 0:7b5d37a81b6b 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
tecnosys 0:7b5d37a81b6b 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
tecnosys 0:7b5d37a81b6b 4 * All rights reserved.
tecnosys 0:7b5d37a81b6b 5 *
tecnosys 0:7b5d37a81b6b 6 * Redistribution and use in source and binary forms, with or without
tecnosys 0:7b5d37a81b6b 7 * modification, are permitted provided that the following conditions
tecnosys 0:7b5d37a81b6b 8 * are met:
tecnosys 0:7b5d37a81b6b 9 * 1. Redistributions of source code must retain the above copyright
tecnosys 0:7b5d37a81b6b 10 * notice, this list of conditions and the following disclaimer.
tecnosys 0:7b5d37a81b6b 11 * 2. Redistributions in binary form must reproduce the above copyright
tecnosys 0:7b5d37a81b6b 12 * notice, this list of conditions and the following disclaimer in the
tecnosys 0:7b5d37a81b6b 13 * documentation and/or other materials provided with the distribution.
tecnosys 0:7b5d37a81b6b 14 * 3. The name of the author may not be used to endorse or promote products
tecnosys 0:7b5d37a81b6b 15 * derived from this software without specific prior written permission.
tecnosys 0:7b5d37a81b6b 16 *
tecnosys 0:7b5d37a81b6b 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
tecnosys 0:7b5d37a81b6b 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
tecnosys 0:7b5d37a81b6b 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
tecnosys 0:7b5d37a81b6b 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
tecnosys 0:7b5d37a81b6b 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
tecnosys 0:7b5d37a81b6b 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
tecnosys 0:7b5d37a81b6b 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
tecnosys 0:7b5d37a81b6b 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
tecnosys 0:7b5d37a81b6b 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
tecnosys 0:7b5d37a81b6b 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
tecnosys 0:7b5d37a81b6b 27 *
tecnosys 0:7b5d37a81b6b 28 * File: $Id: mb.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
tecnosys 0:7b5d37a81b6b 29 */
tecnosys 0:7b5d37a81b6b 30
tecnosys 0:7b5d37a81b6b 31 #ifndef _MB_H
tecnosys 0:7b5d37a81b6b 32 #define _MB_H
tecnosys 0:7b5d37a81b6b 33
tecnosys 0:7b5d37a81b6b 34 #include "port.h"
tecnosys 0:7b5d37a81b6b 35
tecnosys 0:7b5d37a81b6b 36 #ifdef __cplusplus
tecnosys 0:7b5d37a81b6b 37 PR_BEGIN_EXTERN_C
tecnosys 0:7b5d37a81b6b 38 #endif
tecnosys 0:7b5d37a81b6b 39
tecnosys 0:7b5d37a81b6b 40 #include "mbport.h"
tecnosys 0:7b5d37a81b6b 41 #include "mbproto.h"
tecnosys 0:7b5d37a81b6b 42
tecnosys 0:7b5d37a81b6b 43 /*! \defgroup modbus Modbus
tecnosys 0:7b5d37a81b6b 44 * \code #include "mb.h" \endcode
tecnosys 0:7b5d37a81b6b 45 *
tecnosys 0:7b5d37a81b6b 46 * This module defines the interface for the application. It contains
tecnosys 0:7b5d37a81b6b 47 * the basic functions and types required to use the Modbus protocol stack.
tecnosys 0:7b5d37a81b6b 48 * A typical application will want to call eMBInit() first. If the device
tecnosys 0:7b5d37a81b6b 49 * is ready to answer network requests it must then call eMBEnable() to activate
tecnosys 0:7b5d37a81b6b 50 * the protocol stack. In the main loop the function eMBPoll() must be called
tecnosys 0:7b5d37a81b6b 51 * periodically. The time interval between pooling depends on the configured
tecnosys 0:7b5d37a81b6b 52 * Modbus timeout. If an RTOS is available a separate task should be created
tecnosys 0:7b5d37a81b6b 53 * and the task should always call the function eMBPoll().
tecnosys 0:7b5d37a81b6b 54 *
tecnosys 0:7b5d37a81b6b 55 * \code
tecnosys 0:7b5d37a81b6b 56 * // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A
tecnosys 0:7b5d37a81b6b 57 * eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN );
tecnosys 0:7b5d37a81b6b 58 * // Enable the Modbus Protocol Stack.
tecnosys 0:7b5d37a81b6b 59 * eMBEnable( );
tecnosys 0:7b5d37a81b6b 60 * for( ;; )
tecnosys 0:7b5d37a81b6b 61 * {
tecnosys 0:7b5d37a81b6b 62 * // Call the main polling loop of the Modbus protocol stack.
tecnosys 0:7b5d37a81b6b 63 * eMBPoll( );
tecnosys 0:7b5d37a81b6b 64 * ...
tecnosys 0:7b5d37a81b6b 65 * }
tecnosys 0:7b5d37a81b6b 66 * \endcode
tecnosys 0:7b5d37a81b6b 67 */
tecnosys 0:7b5d37a81b6b 68
tecnosys 0:7b5d37a81b6b 69 /* ----------------------- Defines ------------------------------------------*/
tecnosys 0:7b5d37a81b6b 70
tecnosys 0:7b5d37a81b6b 71 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 72 * \brief Use the default Modbus TCP port (502)
tecnosys 0:7b5d37a81b6b 73 */
tecnosys 0:7b5d37a81b6b 74 #define MB_TCP_PORT_USE_DEFAULT 0
tecnosys 0:7b5d37a81b6b 75
tecnosys 0:7b5d37a81b6b 76 /* ----------------------- Type definitions ---------------------------------*/
tecnosys 0:7b5d37a81b6b 77
tecnosys 0:7b5d37a81b6b 78 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 79 * \brief Modbus serial transmission modes (RTU/ASCII).
tecnosys 0:7b5d37a81b6b 80 *
tecnosys 0:7b5d37a81b6b 81 * Modbus serial supports two transmission modes. Either ASCII or RTU. RTU
tecnosys 0:7b5d37a81b6b 82 * is faster but has more hardware requirements and requires a network with
tecnosys 0:7b5d37a81b6b 83 * a low jitter. ASCII is slower and more reliable on slower links (E.g. modems)
tecnosys 0:7b5d37a81b6b 84 */
tecnosys 0:7b5d37a81b6b 85 typedef enum
tecnosys 0:7b5d37a81b6b 86 {
tecnosys 0:7b5d37a81b6b 87 MB_RTU, /*!< RTU transmission mode. */
tecnosys 0:7b5d37a81b6b 88 MB_ASCII, /*!< ASCII transmission mode. */
tecnosys 0:7b5d37a81b6b 89 MB_TCP /*!< TCP mode. */
tecnosys 0:7b5d37a81b6b 90 } eMBMode;
tecnosys 0:7b5d37a81b6b 91
tecnosys 0:7b5d37a81b6b 92 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 93 * \brief If register should be written or read.
tecnosys 0:7b5d37a81b6b 94 *
tecnosys 0:7b5d37a81b6b 95 * This value is passed to the callback functions which support either
tecnosys 0:7b5d37a81b6b 96 * reading or writing register values. Writing means that the application
tecnosys 0:7b5d37a81b6b 97 * registers should be updated and reading means that the modbus protocol
tecnosys 0:7b5d37a81b6b 98 * stack needs to know the current register values.
tecnosys 0:7b5d37a81b6b 99 *
tecnosys 0:7b5d37a81b6b 100 * \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and
tecnosys 0:7b5d37a81b6b 101 * eMBRegInputCB( ).
tecnosys 0:7b5d37a81b6b 102 */
tecnosys 0:7b5d37a81b6b 103 typedef enum
tecnosys 0:7b5d37a81b6b 104 {
tecnosys 0:7b5d37a81b6b 105 MB_REG_READ, /*!< Read register values and pass to protocol stack. */
tecnosys 0:7b5d37a81b6b 106 MB_REG_WRITE /*!< Update register values. */
tecnosys 0:7b5d37a81b6b 107 } eMBRegisterMode;
tecnosys 0:7b5d37a81b6b 108
tecnosys 0:7b5d37a81b6b 109 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 110 * \brief Errorcodes used by all function in the protocol stack.
tecnosys 0:7b5d37a81b6b 111 */
tecnosys 0:7b5d37a81b6b 112 typedef enum
tecnosys 0:7b5d37a81b6b 113 {
tecnosys 0:7b5d37a81b6b 114 MB_ENOERR, /*!< no error. */
tecnosys 0:7b5d37a81b6b 115 MB_ENOREG, /*!< illegal register address. */
tecnosys 0:7b5d37a81b6b 116 MB_EINVAL, /*!< illegal argument. */
tecnosys 0:7b5d37a81b6b 117 MB_EPORTERR, /*!< porting layer error. */
tecnosys 0:7b5d37a81b6b 118 MB_ENORES, /*!< insufficient resources. */
tecnosys 0:7b5d37a81b6b 119 MB_EIO, /*!< I/O error. */
tecnosys 0:7b5d37a81b6b 120 MB_EILLSTATE, /*!< protocol stack in illegal state. */
tecnosys 0:7b5d37a81b6b 121 MB_ETIMEDOUT /*!< timeout error occurred. */
tecnosys 0:7b5d37a81b6b 122 } eMBErrorCode;
tecnosys 0:7b5d37a81b6b 123
tecnosys 0:7b5d37a81b6b 124
tecnosys 0:7b5d37a81b6b 125 /* ----------------------- Function prototypes ------------------------------*/
tecnosys 0:7b5d37a81b6b 126 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 127 * \brief Initialize the Modbus protocol stack.
tecnosys 0:7b5d37a81b6b 128 *
tecnosys 0:7b5d37a81b6b 129 * This functions initializes the ASCII or RTU module and calls the
tecnosys 0:7b5d37a81b6b 130 * init functions of the porting layer to prepare the hardware. Please
tecnosys 0:7b5d37a81b6b 131 * note that the receiver is still disabled and no Modbus frames are
tecnosys 0:7b5d37a81b6b 132 * processed until eMBEnable( ) has been called.
tecnosys 0:7b5d37a81b6b 133 *
tecnosys 0:7b5d37a81b6b 134 * \param eMode If ASCII or RTU mode should be used.
tecnosys 0:7b5d37a81b6b 135 * \param ucSlaveAddress The slave address. Only frames sent to this
tecnosys 0:7b5d37a81b6b 136 * address or to the broadcast address are processed.
tecnosys 0:7b5d37a81b6b 137 * \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
tecnosys 0:7b5d37a81b6b 138 * is platform dependent and some ports simply choose to ignore it.
tecnosys 0:7b5d37a81b6b 139 * \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
tecnosys 0:7b5d37a81b6b 140 * on the porting layer.
tecnosys 0:7b5d37a81b6b 141 * \param eParity Parity used for serial transmission.
tecnosys 0:7b5d37a81b6b 142 *
tecnosys 0:7b5d37a81b6b 143 * \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
tecnosys 0:7b5d37a81b6b 144 * The protocol is then in the disabled state and ready for activation
tecnosys 0:7b5d37a81b6b 145 * by calling eMBEnable( ). Otherwise one of the following error codes
tecnosys 0:7b5d37a81b6b 146 * is returned:
tecnosys 0:7b5d37a81b6b 147 * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
tecnosys 0:7b5d37a81b6b 148 * slave addresses are in the range 1 - 247.
tecnosys 0:7b5d37a81b6b 149 * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
tecnosys 0:7b5d37a81b6b 150 */
tecnosys 0:7b5d37a81b6b 151 eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
tecnosys 0:7b5d37a81b6b 152 UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
tecnosys 0:7b5d37a81b6b 153
tecnosys 0:7b5d37a81b6b 154 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 155 * \brief Initialize the Modbus protocol stack for Modbus TCP.
tecnosys 0:7b5d37a81b6b 156 *
tecnosys 0:7b5d37a81b6b 157 * This function initializes the Modbus TCP Module. Please note that
tecnosys 0:7b5d37a81b6b 158 * frame processing is still disabled until eMBEnable( ) is called.
tecnosys 0:7b5d37a81b6b 159 *
tecnosys 0:7b5d37a81b6b 160 * \param usTCPPort The TCP port to listen on.
tecnosys 0:7b5d37a81b6b 161 * \return If the protocol stack has been initialized correctly the function
tecnosys 0:7b5d37a81b6b 162 * returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
tecnosys 0:7b5d37a81b6b 163 * codes is returned:
tecnosys 0:7b5d37a81b6b 164 * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
tecnosys 0:7b5d37a81b6b 165 * slave addresses are in the range 1 - 247.
tecnosys 0:7b5d37a81b6b 166 * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
tecnosys 0:7b5d37a81b6b 167 */
tecnosys 0:7b5d37a81b6b 168 eMBErrorCode eMBTCPInit( USHORT usTCPPort );
tecnosys 0:7b5d37a81b6b 169
tecnosys 0:7b5d37a81b6b 170 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 171 * \brief Release resources used by the protocol stack.
tecnosys 0:7b5d37a81b6b 172 *
tecnosys 0:7b5d37a81b6b 173 * This function disables the Modbus protocol stack and release all
tecnosys 0:7b5d37a81b6b 174 * hardware resources. It must only be called when the protocol stack
tecnosys 0:7b5d37a81b6b 175 * is disabled.
tecnosys 0:7b5d37a81b6b 176 *
tecnosys 0:7b5d37a81b6b 177 * \note Note all ports implement this function. A port which wants to
tecnosys 0:7b5d37a81b6b 178 * get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
tecnosys 0:7b5d37a81b6b 179 *
tecnosys 0:7b5d37a81b6b 180 * \return If the resources where released it return eMBErrorCode::MB_ENOERR.
tecnosys 0:7b5d37a81b6b 181 * If the protocol stack is not in the disabled state it returns
tecnosys 0:7b5d37a81b6b 182 * eMBErrorCode::MB_EILLSTATE.
tecnosys 0:7b5d37a81b6b 183 */
tecnosys 0:7b5d37a81b6b 184 eMBErrorCode eMBClose( void );
tecnosys 0:7b5d37a81b6b 185
tecnosys 0:7b5d37a81b6b 186 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 187 * \brief Enable the Modbus protocol stack.
tecnosys 0:7b5d37a81b6b 188 *
tecnosys 0:7b5d37a81b6b 189 * This function enables processing of Modbus frames. Enabling the protocol
tecnosys 0:7b5d37a81b6b 190 * stack is only possible if it is in the disabled state.
tecnosys 0:7b5d37a81b6b 191 *
tecnosys 0:7b5d37a81b6b 192 * \return If the protocol stack is now in the state enabled it returns
tecnosys 0:7b5d37a81b6b 193 * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
tecnosys 0:7b5d37a81b6b 194 * return eMBErrorCode::MB_EILLSTATE.
tecnosys 0:7b5d37a81b6b 195 */
tecnosys 0:7b5d37a81b6b 196 eMBErrorCode eMBEnable( void );
tecnosys 0:7b5d37a81b6b 197
tecnosys 0:7b5d37a81b6b 198 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 199 * \brief Disable the Modbus protocol stack.
tecnosys 0:7b5d37a81b6b 200 *
tecnosys 0:7b5d37a81b6b 201 * This function disables processing of Modbus frames.
tecnosys 0:7b5d37a81b6b 202 *
tecnosys 0:7b5d37a81b6b 203 * \return If the protocol stack has been disabled it returns
tecnosys 0:7b5d37a81b6b 204 * eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
tecnosys 0:7b5d37a81b6b 205 * eMBErrorCode::MB_EILLSTATE.
tecnosys 0:7b5d37a81b6b 206 */
tecnosys 0:7b5d37a81b6b 207 eMBErrorCode eMBDisable( void );
tecnosys 0:7b5d37a81b6b 208
tecnosys 0:7b5d37a81b6b 209 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 210 * \brief The main pooling loop of the Modbus protocol stack.
tecnosys 0:7b5d37a81b6b 211 *
tecnosys 0:7b5d37a81b6b 212 * This function must be called periodically. The timer interval required
tecnosys 0:7b5d37a81b6b 213 * is given by the application dependent Modbus slave timeout. Internally the
tecnosys 0:7b5d37a81b6b 214 * function calls xMBPortEventGet() and waits for an event from the receiver or
tecnosys 0:7b5d37a81b6b 215 * transmitter state machines.
tecnosys 0:7b5d37a81b6b 216 *
tecnosys 0:7b5d37a81b6b 217 * \return If the protocol stack is not in the enabled state the function
tecnosys 0:7b5d37a81b6b 218 * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
tecnosys 0:7b5d37a81b6b 219 * eMBErrorCode::MB_ENOERR.
tecnosys 0:7b5d37a81b6b 220 */
tecnosys 0:7b5d37a81b6b 221 eMBErrorCode eMBPoll( void );
tecnosys 0:7b5d37a81b6b 222
tecnosys 0:7b5d37a81b6b 223 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 224 * \brief Configure the slave id of the device.
tecnosys 0:7b5d37a81b6b 225 *
tecnosys 0:7b5d37a81b6b 226 * This function should be called when the Modbus function <em>Report Slave ID</em>
tecnosys 0:7b5d37a81b6b 227 * is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ).
tecnosys 0:7b5d37a81b6b 228 *
tecnosys 0:7b5d37a81b6b 229 * \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
tecnosys 0:7b5d37a81b6b 230 * <em>Report Slave ID</em> response.
tecnosys 0:7b5d37a81b6b 231 * \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF.
tecnosys 0:7b5d37a81b6b 232 * otherwise the <em>Run Indicator Status</em> is 0x00.
tecnosys 0:7b5d37a81b6b 233 * \param pucAdditional Values which should be returned in the <em>Additional</em>
tecnosys 0:7b5d37a81b6b 234 * bytes of the <em> Report Slave ID</em> response.
tecnosys 0:7b5d37a81b6b 235 * \param usAdditionalLen Length of the buffer <code>pucAdditonal</code>.
tecnosys 0:7b5d37a81b6b 236 *
tecnosys 0:7b5d37a81b6b 237 * \return If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in
tecnosys 0:7b5d37a81b6b 238 * mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise
tecnosys 0:7b5d37a81b6b 239 * it returns eMBErrorCode::MB_ENOERR.
tecnosys 0:7b5d37a81b6b 240 */
tecnosys 0:7b5d37a81b6b 241 eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
tecnosys 0:7b5d37a81b6b 242 UCHAR const *pucAdditional,
tecnosys 0:7b5d37a81b6b 243 USHORT usAdditionalLen );
tecnosys 0:7b5d37a81b6b 244
tecnosys 0:7b5d37a81b6b 245 /*! \ingroup modbus
tecnosys 0:7b5d37a81b6b 246 * \brief Registers a callback handler for a given function code.
tecnosys 0:7b5d37a81b6b 247 *
tecnosys 0:7b5d37a81b6b 248 * This function registers a new callback handler for a given function code.
tecnosys 0:7b5d37a81b6b 249 * The callback handler supplied is responsible for interpreting the Modbus PDU and
tecnosys 0:7b5d37a81b6b 250 * the creation of an appropriate response. In case of an error it should return
tecnosys 0:7b5d37a81b6b 251 * one of the possible Modbus exceptions which results in a Modbus exception frame
tecnosys 0:7b5d37a81b6b 252 * sent by the protocol stack.
tecnosys 0:7b5d37a81b6b 253 *
tecnosys 0:7b5d37a81b6b 254 * \param ucFunctionCode The Modbus function code for which this handler should
tecnosys 0:7b5d37a81b6b 255 * be registers. Valid function codes are in the range 1 to 127.
tecnosys 0:7b5d37a81b6b 256 * \param pxHandler The function handler which should be called in case
tecnosys 0:7b5d37a81b6b 257 * such a frame is received. If \c NULL a previously registered function handler
tecnosys 0:7b5d37a81b6b 258 * for this function code is removed.
tecnosys 0:7b5d37a81b6b 259 *
tecnosys 0:7b5d37a81b6b 260 * \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
tecnosys 0:7b5d37a81b6b 261 * more resources are available it returns eMBErrorCode::MB_ENORES. In this
tecnosys 0:7b5d37a81b6b 262 * case the values in mbconfig.h should be adjusted. If the argument was not
tecnosys 0:7b5d37a81b6b 263 * valid it returns eMBErrorCode::MB_EINVAL.
tecnosys 0:7b5d37a81b6b 264 */
tecnosys 0:7b5d37a81b6b 265 eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode,
tecnosys 0:7b5d37a81b6b 266 pxMBFunctionHandler pxHandler );
tecnosys 0:7b5d37a81b6b 267
tecnosys 0:7b5d37a81b6b 268 /* ----------------------- Callback -----------------------------------------*/
tecnosys 0:7b5d37a81b6b 269
tecnosys 0:7b5d37a81b6b 270 /*! \defgroup modbus_registers Modbus Registers
tecnosys 0:7b5d37a81b6b 271 * \code #include "mb.h" \endcode
tecnosys 0:7b5d37a81b6b 272 * The protocol stack does not internally allocate any memory for the
tecnosys 0:7b5d37a81b6b 273 * registers. This makes the protocol stack very small and also usable on
tecnosys 0:7b5d37a81b6b 274 * low end targets. In addition the values don't have to be in the memory
tecnosys 0:7b5d37a81b6b 275 * and could for example be stored in a flash.<br>
tecnosys 0:7b5d37a81b6b 276 * Whenever the protocol stack requires a value it calls one of the callback
tecnosys 0:7b5d37a81b6b 277 * function with the register address and the number of registers to read
tecnosys 0:7b5d37a81b6b 278 * as an argument. The application should then read the actual register values
tecnosys 0:7b5d37a81b6b 279 * (for example the ADC voltage) and should store the result in the supplied
tecnosys 0:7b5d37a81b6b 280 * buffer.<br>
tecnosys 0:7b5d37a81b6b 281 * If the protocol stack wants to update a register value because a write
tecnosys 0:7b5d37a81b6b 282 * register function was received a buffer with the new register values is
tecnosys 0:7b5d37a81b6b 283 * passed to the callback function. The function should then use these values
tecnosys 0:7b5d37a81b6b 284 * to update the application register values.
tecnosys 0:7b5d37a81b6b 285 */
tecnosys 0:7b5d37a81b6b 286
tecnosys 0:7b5d37a81b6b 287 /*! \ingroup modbus_registers
tecnosys 0:7b5d37a81b6b 288 * \brief Callback function used if the value of a <em>Input Register</em>
tecnosys 0:7b5d37a81b6b 289 * is required by the protocol stack. The starting register address is given
tecnosys 0:7b5d37a81b6b 290 * by \c usAddress and the last register is given by <tt>usAddress +
tecnosys 0:7b5d37a81b6b 291 * usNRegs - 1</tt>.
tecnosys 0:7b5d37a81b6b 292 *
tecnosys 0:7b5d37a81b6b 293 * \param pucRegBuffer A buffer where the callback function should write
tecnosys 0:7b5d37a81b6b 294 * the current value of the modbus registers to.
tecnosys 0:7b5d37a81b6b 295 * \param usAddress The starting address of the register. Input registers
tecnosys 0:7b5d37a81b6b 296 * are in the range 1 - 65535.
tecnosys 0:7b5d37a81b6b 297 * \param usNRegs Number of registers the callback function must supply.
tecnosys 0:7b5d37a81b6b 298 *
tecnosys 0:7b5d37a81b6b 299 * \return The function must return one of the following error codes:
tecnosys 0:7b5d37a81b6b 300 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
tecnosys 0:7b5d37a81b6b 301 * Modbus response is sent.
tecnosys 0:7b5d37a81b6b 302 * - eMBErrorCode::MB_ENOREG If the application can not supply values
tecnosys 0:7b5d37a81b6b 303 * for registers within this range. In this case a
tecnosys 0:7b5d37a81b6b 304 * <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
tecnosys 0:7b5d37a81b6b 305 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
tecnosys 0:7b5d37a81b6b 306 * currently not available and the application dependent response
tecnosys 0:7b5d37a81b6b 307 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
tecnosys 0:7b5d37a81b6b 308 * exception is sent as a response.
tecnosys 0:7b5d37a81b6b 309 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
tecnosys 0:7b5d37a81b6b 310 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
tecnosys 0:7b5d37a81b6b 311 */
tecnosys 0:7b5d37a81b6b 312 eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
tecnosys 0:7b5d37a81b6b 313 USHORT usNRegs );
tecnosys 0:7b5d37a81b6b 314
tecnosys 0:7b5d37a81b6b 315 /*! \ingroup modbus_registers
tecnosys 0:7b5d37a81b6b 316 * \brief Callback function used if a <em>Holding Register</em> value is
tecnosys 0:7b5d37a81b6b 317 * read or written by the protocol stack. The starting register address
tecnosys 0:7b5d37a81b6b 318 * is given by \c usAddress and the last register is given by
tecnosys 0:7b5d37a81b6b 319 * <tt>usAddress + usNRegs - 1</tt>.
tecnosys 0:7b5d37a81b6b 320 *
tecnosys 0:7b5d37a81b6b 321 * \param pucRegBuffer If the application registers values should be updated the
tecnosys 0:7b5d37a81b6b 322 * buffer points to the new registers values. If the protocol stack needs
tecnosys 0:7b5d37a81b6b 323 * to now the current values the callback function should write them into
tecnosys 0:7b5d37a81b6b 324 * this buffer.
tecnosys 0:7b5d37a81b6b 325 * \param usAddress The starting address of the register.
tecnosys 0:7b5d37a81b6b 326 * \param usNRegs Number of registers to read or write.
tecnosys 0:7b5d37a81b6b 327 * \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
tecnosys 0:7b5d37a81b6b 328 * values should be updated from the values in the buffer. For example
tecnosys 0:7b5d37a81b6b 329 * this would be the case when the Modbus master has issued an
tecnosys 0:7b5d37a81b6b 330 * <b>WRITE SINGLE REGISTER</b> command.
tecnosys 0:7b5d37a81b6b 331 * If the value eMBRegisterMode::MB_REG_READ the application should copy
tecnosys 0:7b5d37a81b6b 332 * the current values into the buffer \c pucRegBuffer.
tecnosys 0:7b5d37a81b6b 333 *
tecnosys 0:7b5d37a81b6b 334 * \return The function must return one of the following error codes:
tecnosys 0:7b5d37a81b6b 335 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
tecnosys 0:7b5d37a81b6b 336 * Modbus response is sent.
tecnosys 0:7b5d37a81b6b 337 * - eMBErrorCode::MB_ENOREG If the application can not supply values
tecnosys 0:7b5d37a81b6b 338 * for registers within this range. In this case a
tecnosys 0:7b5d37a81b6b 339 * <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
tecnosys 0:7b5d37a81b6b 340 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
tecnosys 0:7b5d37a81b6b 341 * currently not available and the application dependent response
tecnosys 0:7b5d37a81b6b 342 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
tecnosys 0:7b5d37a81b6b 343 * exception is sent as a response.
tecnosys 0:7b5d37a81b6b 344 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
tecnosys 0:7b5d37a81b6b 345 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
tecnosys 0:7b5d37a81b6b 346 */
tecnosys 0:7b5d37a81b6b 347 eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
tecnosys 0:7b5d37a81b6b 348 USHORT usNRegs, eMBRegisterMode eMode );
tecnosys 0:7b5d37a81b6b 349
tecnosys 0:7b5d37a81b6b 350 /*! \ingroup modbus_registers
tecnosys 0:7b5d37a81b6b 351 * \brief Callback function used if a <em>Coil Register</em> value is
tecnosys 0:7b5d37a81b6b 352 * read or written by the protocol stack. If you are going to use
tecnosys 0:7b5d37a81b6b 353 * this function you might use the functions xMBUtilSetBits( ) and
tecnosys 0:7b5d37a81b6b 354 * xMBUtilGetBits( ) for working with bitfields.
tecnosys 0:7b5d37a81b6b 355 *
tecnosys 0:7b5d37a81b6b 356 * \param pucRegBuffer The bits are packed in bytes where the first coil
tecnosys 0:7b5d37a81b6b 357 * starting at address \c usAddress is stored in the LSB of the
tecnosys 0:7b5d37a81b6b 358 * first byte in the buffer <code>pucRegBuffer</code>.
tecnosys 0:7b5d37a81b6b 359 * If the buffer should be written by the callback function unused
tecnosys 0:7b5d37a81b6b 360 * coil values (I.e. if not a multiple of eight coils is used) should be set
tecnosys 0:7b5d37a81b6b 361 * to zero.
tecnosys 0:7b5d37a81b6b 362 * \param usAddress The first coil number.
tecnosys 0:7b5d37a81b6b 363 * \param usNCoils Number of coil values requested.
tecnosys 0:7b5d37a81b6b 364 * \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
tecnosys 0:7b5d37a81b6b 365 * be updated from the values supplied in the buffer \c pucRegBuffer.
tecnosys 0:7b5d37a81b6b 366 * If eMBRegisterMode::MB_REG_READ the application should store the current
tecnosys 0:7b5d37a81b6b 367 * values in the buffer \c pucRegBuffer.
tecnosys 0:7b5d37a81b6b 368 *
tecnosys 0:7b5d37a81b6b 369 * \return The function must return one of the following error codes:
tecnosys 0:7b5d37a81b6b 370 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
tecnosys 0:7b5d37a81b6b 371 * Modbus response is sent.
tecnosys 0:7b5d37a81b6b 372 * - eMBErrorCode::MB_ENOREG If the application does not map an coils
tecnosys 0:7b5d37a81b6b 373 * within the requested address range. In this case a
tecnosys 0:7b5d37a81b6b 374 * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
tecnosys 0:7b5d37a81b6b 375 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
tecnosys 0:7b5d37a81b6b 376 * currently not available and the application dependent response
tecnosys 0:7b5d37a81b6b 377 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
tecnosys 0:7b5d37a81b6b 378 * exception is sent as a response.
tecnosys 0:7b5d37a81b6b 379 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
tecnosys 0:7b5d37a81b6b 380 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
tecnosys 0:7b5d37a81b6b 381 */
tecnosys 0:7b5d37a81b6b 382 eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
tecnosys 0:7b5d37a81b6b 383 USHORT usNCoils, eMBRegisterMode eMode );
tecnosys 0:7b5d37a81b6b 384
tecnosys 0:7b5d37a81b6b 385 /*! \ingroup modbus_registers
tecnosys 0:7b5d37a81b6b 386 * \brief Callback function used if a <em>Input Discrete Register</em> value is
tecnosys 0:7b5d37a81b6b 387 * read by the protocol stack.
tecnosys 0:7b5d37a81b6b 388 *
tecnosys 0:7b5d37a81b6b 389 * If you are going to use his function you might use the functions
tecnosys 0:7b5d37a81b6b 390 * xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.
tecnosys 0:7b5d37a81b6b 391 *
tecnosys 0:7b5d37a81b6b 392 * \param pucRegBuffer The buffer should be updated with the current
tecnosys 0:7b5d37a81b6b 393 * coil values. The first discrete input starting at \c usAddress must be
tecnosys 0:7b5d37a81b6b 394 * stored at the LSB of the first byte in the buffer. If the requested number
tecnosys 0:7b5d37a81b6b 395 * is not a multiple of eight the remaining bits should be set to zero.
tecnosys 0:7b5d37a81b6b 396 * \param usAddress The starting address of the first discrete input.
tecnosys 0:7b5d37a81b6b 397 * \param usNDiscrete Number of discrete input values.
tecnosys 0:7b5d37a81b6b 398 * \return The function must return one of the following error codes:
tecnosys 0:7b5d37a81b6b 399 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
tecnosys 0:7b5d37a81b6b 400 * Modbus response is sent.
tecnosys 0:7b5d37a81b6b 401 * - eMBErrorCode::MB_ENOREG If no such discrete inputs exists.
tecnosys 0:7b5d37a81b6b 402 * In this case a <b>ILLEGAL DATA ADDRESS</b> exception frame is sent
tecnosys 0:7b5d37a81b6b 403 * as a response.
tecnosys 0:7b5d37a81b6b 404 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
tecnosys 0:7b5d37a81b6b 405 * currently not available and the application dependent response
tecnosys 0:7b5d37a81b6b 406 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
tecnosys 0:7b5d37a81b6b 407 * exception is sent as a response.
tecnosys 0:7b5d37a81b6b 408 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
tecnosys 0:7b5d37a81b6b 409 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
tecnosys 0:7b5d37a81b6b 410 */
tecnosys 0:7b5d37a81b6b 411 eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
tecnosys 0:7b5d37a81b6b 412 USHORT usNDiscrete );
tecnosys 0:7b5d37a81b6b 413
tecnosys 0:7b5d37a81b6b 414 #ifdef __cplusplus
tecnosys 0:7b5d37a81b6b 415 PR_END_EXTERN_C
tecnosys 0:7b5d37a81b6b 416 #endif
tecnosys 0:7b5d37a81b6b 417 #endif