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