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