Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mb.h
00001 /* 00002 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. 00003 * Copyright (c) 2006 Christian Walter <wolti@sil.at> 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 3. The name of the author may not be used to endorse or promote products 00015 * derived from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * 00028 * File: $Id: mb.h,v 1.17 2006/12/07 22:10:34 wolti Exp $ 00029 */ 00030 00031 #ifndef _MB_H 00032 #define _MB_H 00033 00034 #include "port.h" 00035 00036 #ifdef __cplusplus 00037 PR_BEGIN_EXTERN_C 00038 #endif 00039 00040 #include "mbport.h" 00041 #include "mbproto.h" 00042 00043 /*! \defgroup modbus Modbus 00044 * \code #include "mb.h" \endcode 00045 * 00046 * This module defines the interface for the application. It contains 00047 * the basic functions and types required to use the Modbus protocol stack. 00048 * A typical application will want to call eMBInit() first. If the device 00049 * is ready to answer network requests it must then call eMBEnable() to activate 00050 * the protocol stack. In the main loop the function eMBPoll() must be called 00051 * periodically. The time interval between pooling depends on the configured 00052 * Modbus timeout. If an RTOS is available a separate task should be created 00053 * and the task should always call the function eMBPoll(). 00054 * 00055 * \code 00056 * // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A 00057 * eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN ); 00058 * // Enable the Modbus Protocol Stack. 00059 * eMBEnable( ); 00060 * for( ;; ) 00061 * { 00062 * // Call the main polling loop of the Modbus protocol stack. 00063 * eMBPoll( ); 00064 * ... 00065 * } 00066 * \endcode 00067 */ 00068 00069 /* ----------------------- Defines ------------------------------------------*/ 00070 00071 /*! \ingroup modbus 00072 * \brief Use the default Modbus TCP port (502) 00073 */ 00074 #define MB_TCP_PORT_USE_DEFAULT 0 00075 00076 /* ----------------------- Type definitions ---------------------------------*/ 00077 00078 /*! \ingroup modbus 00079 * \brief Modbus serial transmission modes (RTU/ASCII). 00080 * 00081 * Modbus serial supports two transmission modes. Either ASCII or RTU. RTU 00082 * is faster but has more hardware requirements and requires a network with 00083 * a low jitter. ASCII is slower and more reliable on slower links (E.g. modems) 00084 */ 00085 typedef enum 00086 { 00087 MB_RTU , /*!< RTU transmission mode. */ 00088 MB_ASCII , /*!< ASCII transmission mode. */ 00089 MB_TCP /*!< TCP mode. */ 00090 } eMBMode; 00091 00092 /*! \ingroup modbus 00093 * \brief If register should be written or read. 00094 * 00095 * This value is passed to the callback functions which support either 00096 * reading or writing register values. Writing means that the application 00097 * registers should be updated and reading means that the modbus protocol 00098 * stack needs to know the current register values. 00099 * 00100 * \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and 00101 * eMBRegInputCB( ). 00102 */ 00103 typedef enum 00104 { 00105 MB_REG_READ , /*!< Read register values and pass to protocol stack. */ 00106 MB_REG_WRITE /*!< Update register values. */ 00107 } eMBRegisterMode; 00108 00109 /*! \ingroup modbus 00110 * \brief Errorcodes used by all function in the protocol stack. 00111 */ 00112 typedef enum 00113 { 00114 MB_ENOERR , /*!< no error. */ 00115 MB_ENOREG , /*!< illegal register address. */ 00116 MB_EINVAL , /*!< illegal argument. */ 00117 MB_EPORTERR , /*!< porting layer error. */ 00118 MB_ENORES , /*!< insufficient resources. */ 00119 MB_EIO , /*!< I/O error. */ 00120 MB_EILLSTATE , /*!< protocol stack in illegal state. */ 00121 MB_ETIMEDOUT /*!< timeout error occurred. */ 00122 } eMBErrorCode; 00123 00124 00125 /* ----------------------- Function prototypes ------------------------------*/ 00126 /*! \ingroup modbus 00127 * \brief Initialize the Modbus protocol stack. 00128 * 00129 * This functions initializes the ASCII or RTU module and calls the 00130 * init functions of the porting layer to prepare the hardware. Please 00131 * note that the receiver is still disabled and no Modbus frames are 00132 * processed until eMBEnable( ) has been called. 00133 * 00134 * \param eMode If ASCII or RTU mode should be used. 00135 * \param ucSlaveAddress The slave address. Only frames sent to this 00136 * address or to the broadcast address are processed. 00137 * \param ucPort The port to use. E.g. 1 for COM1 on windows. This value 00138 * is platform dependent and some ports simply choose to ignore it. 00139 * \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend 00140 * on the porting layer. 00141 * \param eParity Parity used for serial transmission. 00142 * 00143 * \return If no error occurs the function returns eMBErrorCode::MB_ENOERR. 00144 * The protocol is then in the disabled state and ready for activation 00145 * by calling eMBEnable( ). Otherwise one of the following error codes 00146 * is returned: 00147 * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid 00148 * slave addresses are in the range 1 - 247. 00149 * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error. 00150 */ 00151 eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, 00152 UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity ); 00153 00154 /*! \ingroup modbus 00155 * \brief Initialize the Modbus protocol stack for Modbus TCP. 00156 * 00157 * This function initializes the Modbus TCP Module. Please note that 00158 * frame processing is still disabled until eMBEnable( ) is called. 00159 * 00160 * \param usTCPPort The TCP port to listen on. 00161 * \return If the protocol stack has been initialized correctly the function 00162 * returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error 00163 * codes is returned: 00164 * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid 00165 * slave addresses are in the range 1 - 247. 00166 * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error. 00167 */ 00168 eMBErrorCode eMBTCPInit( USHORT usTCPPort ); 00169 00170 /*! \ingroup modbus 00171 * \brief Release resources used by the protocol stack. 00172 * 00173 * This function disables the Modbus protocol stack and release all 00174 * hardware resources. It must only be called when the protocol stack 00175 * is disabled. 00176 * 00177 * \note Note all ports implement this function. A port which wants to 00178 * get an callback must define the macro MB_PORT_HAS_CLOSE to 1. 00179 * 00180 * \return If the resources where released it return eMBErrorCode::MB_ENOERR. 00181 * If the protocol stack is not in the disabled state it returns 00182 * eMBErrorCode::MB_EILLSTATE. 00183 */ 00184 eMBErrorCode eMBClose( void ); 00185 00186 /*! \ingroup modbus 00187 * \brief Enable the Modbus protocol stack. 00188 * 00189 * This function enables processing of Modbus frames. Enabling the protocol 00190 * stack is only possible if it is in the disabled state. 00191 * 00192 * \return If the protocol stack is now in the state enabled it returns 00193 * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it 00194 * return eMBErrorCode::MB_EILLSTATE. 00195 */ 00196 eMBErrorCode eMBEnable( void ); 00197 00198 /*! \ingroup modbus 00199 * \brief Disable the Modbus protocol stack. 00200 * 00201 * This function disables processing of Modbus frames. 00202 * 00203 * \return If the protocol stack has been disabled it returns 00204 * eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns 00205 * eMBErrorCode::MB_EILLSTATE. 00206 */ 00207 eMBErrorCode eMBDisable( void ); 00208 00209 /*! \ingroup modbus 00210 * \brief The main pooling loop of the Modbus protocol stack. 00211 * 00212 * This function must be called periodically. The timer interval required 00213 * is given by the application dependent Modbus slave timeout. Internally the 00214 * function calls xMBPortEventGet() and waits for an event from the receiver or 00215 * transmitter state machines. 00216 * 00217 * \return If the protocol stack is not in the enabled state the function 00218 * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns 00219 * eMBErrorCode::MB_ENOERR. 00220 */ 00221 eMBErrorCode eMBPoll( void ); 00222 00223 /*! \ingroup modbus 00224 * \brief Configure the slave id of the device. 00225 * 00226 * This function should be called when the Modbus function <em>Report Slave ID</em> 00227 * is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ). 00228 * 00229 * \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the 00230 * <em>Report Slave ID</em> response. 00231 * \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF. 00232 * otherwise the <em>Run Indicator Status</em> is 0x00. 00233 * \param pucAdditional Values which should be returned in the <em>Additional</em> 00234 * bytes of the <em> Report Slave ID</em> response. 00235 * \param usAdditionalLen Length of the buffer <code>pucAdditonal</code>. 00236 * 00237 * \return If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in 00238 * mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise 00239 * it returns eMBErrorCode::MB_ENOERR. 00240 */ 00241 eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning, 00242 UCHAR const *pucAdditional, 00243 USHORT usAdditionalLen ); 00244 00245 /*! \ingroup modbus 00246 * \brief Registers a callback handler for a given function code. 00247 * 00248 * This function registers a new callback handler for a given function code. 00249 * The callback handler supplied is responsible for interpreting the Modbus PDU and 00250 * the creation of an appropriate response. In case of an error it should return 00251 * one of the possible Modbus exceptions which results in a Modbus exception frame 00252 * sent by the protocol stack. 00253 * 00254 * \param ucFunctionCode The Modbus function code for which this handler should 00255 * be registers. Valid function codes are in the range 1 to 127. 00256 * \param pxHandler The function handler which should be called in case 00257 * such a frame is received. If \c NULL a previously registered function handler 00258 * for this function code is removed. 00259 * 00260 * \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no 00261 * more resources are available it returns eMBErrorCode::MB_ENORES. In this 00262 * case the values in mbconfig.h should be adjusted. If the argument was not 00263 * valid it returns eMBErrorCode::MB_EINVAL. 00264 */ 00265 eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode, 00266 pxMBFunctionHandler pxHandler ); 00267 00268 /* ----------------------- Callback -----------------------------------------*/ 00269 00270 /*! \defgroup modbus_registers Modbus Registers 00271 * \code #include "mb.h" \endcode 00272 * The protocol stack does not internally allocate any memory for the 00273 * registers. This makes the protocol stack very small and also usable on 00274 * low end targets. In addition the values don't have to be in the memory 00275 * and could for example be stored in a flash.<br> 00276 * Whenever the protocol stack requires a value it calls one of the callback 00277 * function with the register address and the number of registers to read 00278 * as an argument. The application should then read the actual register values 00279 * (for example the ADC voltage) and should store the result in the supplied 00280 * buffer.<br> 00281 * If the protocol stack wants to update a register value because a write 00282 * register function was received a buffer with the new register values is 00283 * passed to the callback function. The function should then use these values 00284 * to update the application register values. 00285 */ 00286 00287 /*! \ingroup modbus_registers 00288 * \brief Callback function used if the value of a <em>Input Register</em> 00289 * is required by the protocol stack. The starting register address is given 00290 * by \c usAddress and the last register is given by <tt>usAddress + 00291 * usNRegs - 1</tt>. 00292 * 00293 * \param pucRegBuffer A buffer where the callback function should write 00294 * the current value of the modbus registers to. 00295 * \param usAddress The starting address of the register. Input registers 00296 * are in the range 1 - 65535. 00297 * \param usNRegs Number of registers the callback function must supply. 00298 * 00299 * \return The function must return one of the following error codes: 00300 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal 00301 * Modbus response is sent. 00302 * - eMBErrorCode::MB_ENOREG If the application can not supply values 00303 * for registers within this range. In this case a 00304 * <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response. 00305 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is 00306 * currently not available and the application dependent response 00307 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b> 00308 * exception is sent as a response. 00309 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case 00310 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response. 00311 */ 00312 eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, 00313 USHORT usNRegs ); 00314 00315 /*! \ingroup modbus_registers 00316 * \brief Callback function used if a <em>Holding Register</em> value is 00317 * read or written by the protocol stack. The starting register address 00318 * is given by \c usAddress and the last register is given by 00319 * <tt>usAddress + usNRegs - 1</tt>. 00320 * 00321 * \param pucRegBuffer If the application registers values should be updated the 00322 * buffer points to the new registers values. If the protocol stack needs 00323 * to now the current values the callback function should write them into 00324 * this buffer. 00325 * \param usAddress The starting address of the register. 00326 * \param usNRegs Number of registers to read or write. 00327 * \param eMode If eMBRegisterMode::MB_REG_WRITE the application register 00328 * values should be updated from the values in the buffer. For example 00329 * this would be the case when the Modbus master has issued an 00330 * <b>WRITE SINGLE REGISTER</b> command. 00331 * If the value eMBRegisterMode::MB_REG_READ the application should copy 00332 * the current values into the buffer \c pucRegBuffer. 00333 * 00334 * \return The function must return one of the following error codes: 00335 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal 00336 * Modbus response is sent. 00337 * - eMBErrorCode::MB_ENOREG If the application can not supply values 00338 * for registers within this range. In this case a 00339 * <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response. 00340 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is 00341 * currently not available and the application dependent response 00342 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b> 00343 * exception is sent as a response. 00344 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case 00345 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response. 00346 */ 00347 eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, 00348 USHORT usNRegs, eMBRegisterMode eMode ); 00349 00350 /*! \ingroup modbus_registers 00351 * \brief Callback function used if a <em>Coil Register</em> value is 00352 * read or written by the protocol stack. If you are going to use 00353 * this function you might use the functions xMBUtilSetBits( ) and 00354 * xMBUtilGetBits( ) for working with bitfields. 00355 * 00356 * \param pucRegBuffer The bits are packed in bytes where the first coil 00357 * starting at address \c usAddress is stored in the LSB of the 00358 * first byte in the buffer <code>pucRegBuffer</code>. 00359 * If the buffer should be written by the callback function unused 00360 * coil values (I.e. if not a multiple of eight coils is used) should be set 00361 * to zero. 00362 * \param usAddress The first coil number. 00363 * \param usNCoils Number of coil values requested. 00364 * \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should 00365 * be updated from the values supplied in the buffer \c pucRegBuffer. 00366 * If eMBRegisterMode::MB_REG_READ the application should store the current 00367 * values in the buffer \c pucRegBuffer. 00368 * 00369 * \return The function must return one of the following error codes: 00370 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal 00371 * Modbus response is sent. 00372 * - eMBErrorCode::MB_ENOREG If the application does not map an coils 00373 * within the requested address range. In this case a 00374 * <b>ILLEGAL DATA ADDRESS</b> is sent as a response. 00375 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is 00376 * currently not available and the application dependent response 00377 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b> 00378 * exception is sent as a response. 00379 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case 00380 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response. 00381 */ 00382 eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, 00383 USHORT usNCoils, eMBRegisterMode eMode ); 00384 00385 /*! \ingroup modbus_registers 00386 * \brief Callback function used if a <em>Input Discrete Register</em> value is 00387 * read by the protocol stack. 00388 * 00389 * If you are going to use his function you might use the functions 00390 * xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields. 00391 * 00392 * \param pucRegBuffer The buffer should be updated with the current 00393 * coil values. The first discrete input starting at \c usAddress must be 00394 * stored at the LSB of the first byte in the buffer. If the requested number 00395 * is not a multiple of eight the remaining bits should be set to zero. 00396 * \param usAddress The starting address of the first discrete input. 00397 * \param usNDiscrete Number of discrete input values. 00398 * \return The function must return one of the following error codes: 00399 * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal 00400 * Modbus response is sent. 00401 * - eMBErrorCode::MB_ENOREG If no such discrete inputs exists. 00402 * In this case a <b>ILLEGAL DATA ADDRESS</b> exception frame is sent 00403 * as a response. 00404 * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is 00405 * currently not available and the application dependent response 00406 * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b> 00407 * exception is sent as a response. 00408 * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case 00409 * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response. 00410 */ 00411 eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, 00412 USHORT usNDiscrete ); 00413 00414 #ifdef __cplusplus 00415 PR_END_EXTERN_C 00416 #endif 00417 #endif
Generated on Sun Jul 17 2022 02:04:12 by
1.7.2