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