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