Modified version of ModbusTCP

Dependencies:   EthernetNetIf mbed

Committer:
paleskyjp
Date:
Thu Mar 15 15:28:14 2012 +0000
Revision:
3:d7d7c67f21fa
Parent:
0:62be54b8975d
eMBRegCoilsCB was corrected.
(Implementation of writing single coil was wrong.)

Who changed what in which revision?

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