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

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?

UserRevisionLine numberNew 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: mbfuncholding.c,v 1.12 2007/02/18 23:48:22 wolti Exp $
Rajit Singh 0:9db3bed8fffd 29 */
Rajit Singh 0:9db3bed8fffd 30
Rajit Singh 0:9db3bed8fffd 31 /* ----------------------- System includes ----------------------------------*/
Rajit Singh 0:9db3bed8fffd 32 #include "stdlib.h"
Rajit Singh 0:9db3bed8fffd 33 #include "string.h"
Rajit Singh 0:9db3bed8fffd 34
Rajit Singh 0:9db3bed8fffd 35 /* ----------------------- Platform includes --------------------------------*/
Rajit Singh 0:9db3bed8fffd 36 #include "port.h"
Rajit Singh 0:9db3bed8fffd 37
Rajit Singh 0:9db3bed8fffd 38 /* ----------------------- Modbus includes ----------------------------------*/
Rajit Singh 0:9db3bed8fffd 39 #include "mb.h"
Rajit Singh 0:9db3bed8fffd 40 #include "mbframe.h"
Rajit Singh 0:9db3bed8fffd 41 #include "mbproto.h"
Rajit Singh 0:9db3bed8fffd 42 #include "mbconfig.h"
Rajit Singh 0:9db3bed8fffd 43
Rajit Singh 0:9db3bed8fffd 44 /* ----------------------- Defines ------------------------------------------*/
Rajit Singh 0:9db3bed8fffd 45 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
Rajit Singh 0:9db3bed8fffd 46 #define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
Rajit Singh 0:9db3bed8fffd 47 #define MB_PDU_FUNC_READ_SIZE ( 4 )
Rajit Singh 0:9db3bed8fffd 48 #define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
Rajit Singh 0:9db3bed8fffd 49
Rajit Singh 0:9db3bed8fffd 50 #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
Rajit Singh 0:9db3bed8fffd 51 #define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
Rajit Singh 0:9db3bed8fffd 52 #define MB_PDU_FUNC_WRITE_SIZE ( 4 )
Rajit Singh 0:9db3bed8fffd 53
Rajit Singh 0:9db3bed8fffd 54 #define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
Rajit Singh 0:9db3bed8fffd 55 #define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
Rajit Singh 0:9db3bed8fffd 56 #define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
Rajit Singh 0:9db3bed8fffd 57 #define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
Rajit Singh 0:9db3bed8fffd 58 #define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
Rajit Singh 0:9db3bed8fffd 59 #define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ( 0x0078 )
Rajit Singh 0:9db3bed8fffd 60
Rajit Singh 0:9db3bed8fffd 61 #define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
Rajit Singh 0:9db3bed8fffd 62 #define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
Rajit Singh 0:9db3bed8fffd 63 #define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 )
Rajit Singh 0:9db3bed8fffd 64 #define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 )
Rajit Singh 0:9db3bed8fffd 65 #define MB_PDU_FUNC_READWRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 )
Rajit Singh 0:9db3bed8fffd 66 #define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 )
Rajit Singh 0:9db3bed8fffd 67 #define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 9 )
Rajit Singh 0:9db3bed8fffd 68
Rajit Singh 0:9db3bed8fffd 69 /* ----------------------- Static functions ---------------------------------*/
Rajit Singh 0:9db3bed8fffd 70 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
Rajit Singh 0:9db3bed8fffd 71
Rajit Singh 0:9db3bed8fffd 72 /* ----------------------- Start implementation -----------------------------*/
Rajit Singh 0:9db3bed8fffd 73
Rajit Singh 0:9db3bed8fffd 74 #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 75
Rajit Singh 0:9db3bed8fffd 76 eMBException
Rajit Singh 0:9db3bed8fffd 77 eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
Rajit Singh 0:9db3bed8fffd 78 {
Rajit Singh 0:9db3bed8fffd 79 USHORT usRegAddress;
Rajit Singh 0:9db3bed8fffd 80 eMBException eStatus = MB_EX_NONE;
Rajit Singh 0:9db3bed8fffd 81 eMBErrorCode eRegStatus;
Rajit Singh 0:9db3bed8fffd 82
Rajit Singh 0:9db3bed8fffd 83 if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
Rajit Singh 0:9db3bed8fffd 84 {
Rajit Singh 0:9db3bed8fffd 85 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
Rajit Singh 0:9db3bed8fffd 86 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
Rajit Singh 0:9db3bed8fffd 87 usRegAddress++;
Rajit Singh 0:9db3bed8fffd 88
Rajit Singh 0:9db3bed8fffd 89 /* Make callback to update the value. */
Rajit Singh 0:9db3bed8fffd 90 eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
Rajit Singh 0:9db3bed8fffd 91 usRegAddress, 1, MB_REG_WRITE );
Rajit Singh 0:9db3bed8fffd 92
Rajit Singh 0:9db3bed8fffd 93 /* If an error occured convert it into a Modbus exception. */
Rajit Singh 0:9db3bed8fffd 94 if( eRegStatus != MB_ENOERR )
Rajit Singh 0:9db3bed8fffd 95 {
Rajit Singh 0:9db3bed8fffd 96 eStatus = prveMBError2Exception( eRegStatus );
Rajit Singh 0:9db3bed8fffd 97 }
Rajit Singh 0:9db3bed8fffd 98 }
Rajit Singh 0:9db3bed8fffd 99 else
Rajit Singh 0:9db3bed8fffd 100 {
Rajit Singh 0:9db3bed8fffd 101 /* Can't be a valid request because the length is incorrect. */
Rajit Singh 0:9db3bed8fffd 102 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
Rajit Singh 0:9db3bed8fffd 103 }
Rajit Singh 0:9db3bed8fffd 104 return eStatus;
Rajit Singh 0:9db3bed8fffd 105 }
Rajit Singh 0:9db3bed8fffd 106 #endif
Rajit Singh 0:9db3bed8fffd 107
Rajit Singh 0:9db3bed8fffd 108 #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 109 eMBException
Rajit Singh 0:9db3bed8fffd 110 eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
Rajit Singh 0:9db3bed8fffd 111 {
Rajit Singh 0:9db3bed8fffd 112 USHORT usRegAddress;
Rajit Singh 0:9db3bed8fffd 113 USHORT usRegCount;
Rajit Singh 0:9db3bed8fffd 114 UCHAR ucRegByteCount;
Rajit Singh 0:9db3bed8fffd 115
Rajit Singh 0:9db3bed8fffd 116 eMBException eStatus = MB_EX_NONE;
Rajit Singh 0:9db3bed8fffd 117 eMBErrorCode eRegStatus;
Rajit Singh 0:9db3bed8fffd 118
Rajit Singh 0:9db3bed8fffd 119 if( *usLen >= ( MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN ) )
Rajit Singh 0:9db3bed8fffd 120 {
Rajit Singh 0:9db3bed8fffd 121 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
Rajit Singh 0:9db3bed8fffd 122 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
Rajit Singh 0:9db3bed8fffd 123 usRegAddress++;
Rajit Singh 0:9db3bed8fffd 124
Rajit Singh 0:9db3bed8fffd 125 usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 );
Rajit Singh 0:9db3bed8fffd 126 usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] );
Rajit Singh 0:9db3bed8fffd 127
Rajit Singh 0:9db3bed8fffd 128 ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
Rajit Singh 0:9db3bed8fffd 129
Rajit Singh 0:9db3bed8fffd 130 if( ( usRegCount >= 1 ) &&
Rajit Singh 0:9db3bed8fffd 131 ( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) &&
Rajit Singh 0:9db3bed8fffd 132 ( ucRegByteCount == ( UCHAR ) ( 2 * usRegCount ) ) )
Rajit Singh 0:9db3bed8fffd 133 {
Rajit Singh 0:9db3bed8fffd 134 /* Make callback to update the register values. */
Rajit Singh 0:9db3bed8fffd 135 eRegStatus =
Rajit Singh 0:9db3bed8fffd 136 eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
Rajit Singh 0:9db3bed8fffd 137 usRegAddress, usRegCount, MB_REG_WRITE );
Rajit Singh 0:9db3bed8fffd 138
Rajit Singh 0:9db3bed8fffd 139 /* If an error occured convert it into a Modbus exception. */
Rajit Singh 0:9db3bed8fffd 140 if( eRegStatus != MB_ENOERR )
Rajit Singh 0:9db3bed8fffd 141 {
Rajit Singh 0:9db3bed8fffd 142 eStatus = prveMBError2Exception( eRegStatus );
Rajit Singh 0:9db3bed8fffd 143 }
Rajit Singh 0:9db3bed8fffd 144 else
Rajit Singh 0:9db3bed8fffd 145 {
Rajit Singh 0:9db3bed8fffd 146 /* The response contains the function code, the starting
Rajit Singh 0:9db3bed8fffd 147 * address and the quantity of registers. We reuse the
Rajit Singh 0:9db3bed8fffd 148 * old values in the buffer because they are still valid.
Rajit Singh 0:9db3bed8fffd 149 */
Rajit Singh 0:9db3bed8fffd 150 *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
Rajit Singh 0:9db3bed8fffd 151 }
Rajit Singh 0:9db3bed8fffd 152 }
Rajit Singh 0:9db3bed8fffd 153 else
Rajit Singh 0:9db3bed8fffd 154 {
Rajit Singh 0:9db3bed8fffd 155 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
Rajit Singh 0:9db3bed8fffd 156 }
Rajit Singh 0:9db3bed8fffd 157 }
Rajit Singh 0:9db3bed8fffd 158 else
Rajit Singh 0:9db3bed8fffd 159 {
Rajit Singh 0:9db3bed8fffd 160 /* Can't be a valid request because the length is incorrect. */
Rajit Singh 0:9db3bed8fffd 161 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
Rajit Singh 0:9db3bed8fffd 162 }
Rajit Singh 0:9db3bed8fffd 163 return eStatus;
Rajit Singh 0:9db3bed8fffd 164 }
Rajit Singh 0:9db3bed8fffd 165 #endif
Rajit Singh 0:9db3bed8fffd 166
Rajit Singh 0:9db3bed8fffd 167 #if MB_FUNC_READ_HOLDING_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 168
Rajit Singh 0:9db3bed8fffd 169 eMBException
Rajit Singh 0:9db3bed8fffd 170 eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
Rajit Singh 0:9db3bed8fffd 171 {
Rajit Singh 0:9db3bed8fffd 172 USHORT usRegAddress;
Rajit Singh 0:9db3bed8fffd 173 USHORT usRegCount;
Rajit Singh 0:9db3bed8fffd 174 UCHAR *pucFrameCur;
Rajit Singh 0:9db3bed8fffd 175
Rajit Singh 0:9db3bed8fffd 176 eMBException eStatus = MB_EX_NONE;
Rajit Singh 0:9db3bed8fffd 177 eMBErrorCode eRegStatus;
Rajit Singh 0:9db3bed8fffd 178
Rajit Singh 0:9db3bed8fffd 179 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
Rajit Singh 0:9db3bed8fffd 180 {
Rajit Singh 0:9db3bed8fffd 181 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
Rajit Singh 0:9db3bed8fffd 182 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
Rajit Singh 0:9db3bed8fffd 183 usRegAddress++;
Rajit Singh 0:9db3bed8fffd 184
Rajit Singh 0:9db3bed8fffd 185 usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
Rajit Singh 0:9db3bed8fffd 186 usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
Rajit Singh 0:9db3bed8fffd 187
Rajit Singh 0:9db3bed8fffd 188 /* Check if the number of registers to read is valid. If not
Rajit Singh 0:9db3bed8fffd 189 * return Modbus illegal data value exception.
Rajit Singh 0:9db3bed8fffd 190 */
Rajit Singh 0:9db3bed8fffd 191 if( ( usRegCount >= 1 ) && ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) )
Rajit Singh 0:9db3bed8fffd 192 {
Rajit Singh 0:9db3bed8fffd 193 /* Set the current PDU data pointer to the beginning. */
Rajit Singh 0:9db3bed8fffd 194 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
Rajit Singh 0:9db3bed8fffd 195 *usLen = MB_PDU_FUNC_OFF;
Rajit Singh 0:9db3bed8fffd 196
Rajit Singh 0:9db3bed8fffd 197 /* First byte contains the function code. */
Rajit Singh 0:9db3bed8fffd 198 *pucFrameCur++ = MB_FUNC_READ_HOLDING_REGISTER;
Rajit Singh 0:9db3bed8fffd 199 *usLen += 1;
Rajit Singh 0:9db3bed8fffd 200
Rajit Singh 0:9db3bed8fffd 201 /* Second byte in the response contain the number of bytes. */
Rajit Singh 0:9db3bed8fffd 202 *pucFrameCur++ = ( UCHAR ) ( usRegCount * 2 );
Rajit Singh 0:9db3bed8fffd 203 *usLen += 1;
Rajit Singh 0:9db3bed8fffd 204
Rajit Singh 0:9db3bed8fffd 205 /* Make callback to fill the buffer. */
Rajit Singh 0:9db3bed8fffd 206 eRegStatus = eMBRegHoldingCB( pucFrameCur, usRegAddress, usRegCount, MB_REG_READ );
Rajit Singh 0:9db3bed8fffd 207 /* If an error occured convert it into a Modbus exception. */
Rajit Singh 0:9db3bed8fffd 208 if( eRegStatus != MB_ENOERR )
Rajit Singh 0:9db3bed8fffd 209 {
Rajit Singh 0:9db3bed8fffd 210 eStatus = prveMBError2Exception( eRegStatus );
Rajit Singh 0:9db3bed8fffd 211 }
Rajit Singh 0:9db3bed8fffd 212 else
Rajit Singh 0:9db3bed8fffd 213 {
Rajit Singh 0:9db3bed8fffd 214 *usLen += usRegCount * 2;
Rajit Singh 0:9db3bed8fffd 215 }
Rajit Singh 0:9db3bed8fffd 216 }
Rajit Singh 0:9db3bed8fffd 217 else
Rajit Singh 0:9db3bed8fffd 218 {
Rajit Singh 0:9db3bed8fffd 219 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
Rajit Singh 0:9db3bed8fffd 220 }
Rajit Singh 0:9db3bed8fffd 221 }
Rajit Singh 0:9db3bed8fffd 222 else
Rajit Singh 0:9db3bed8fffd 223 {
Rajit Singh 0:9db3bed8fffd 224 /* Can't be a valid request because the length is incorrect. */
Rajit Singh 0:9db3bed8fffd 225 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
Rajit Singh 0:9db3bed8fffd 226 }
Rajit Singh 0:9db3bed8fffd 227 return eStatus;
Rajit Singh 0:9db3bed8fffd 228 }
Rajit Singh 0:9db3bed8fffd 229
Rajit Singh 0:9db3bed8fffd 230 #endif
Rajit Singh 0:9db3bed8fffd 231
Rajit Singh 0:9db3bed8fffd 232 #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 233
Rajit Singh 0:9db3bed8fffd 234 eMBException
Rajit Singh 0:9db3bed8fffd 235 eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
Rajit Singh 0:9db3bed8fffd 236 {
Rajit Singh 0:9db3bed8fffd 237 USHORT usRegReadAddress;
Rajit Singh 0:9db3bed8fffd 238 USHORT usRegReadCount;
Rajit Singh 0:9db3bed8fffd 239 USHORT usRegWriteAddress;
Rajit Singh 0:9db3bed8fffd 240 USHORT usRegWriteCount;
Rajit Singh 0:9db3bed8fffd 241 UCHAR ucRegWriteByteCount;
Rajit Singh 0:9db3bed8fffd 242 UCHAR *pucFrameCur;
Rajit Singh 0:9db3bed8fffd 243
Rajit Singh 0:9db3bed8fffd 244 eMBException eStatus = MB_EX_NONE;
Rajit Singh 0:9db3bed8fffd 245 eMBErrorCode eRegStatus;
Rajit Singh 0:9db3bed8fffd 246
Rajit Singh 0:9db3bed8fffd 247 if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) )
Rajit Singh 0:9db3bed8fffd 248 {
Rajit Singh 0:9db3bed8fffd 249 usRegReadAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
Rajit Singh 0:9db3bed8fffd 250 usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
Rajit Singh 0:9db3bed8fffd 251 usRegReadAddress++;
Rajit Singh 0:9db3bed8fffd 252
Rajit Singh 0:9db3bed8fffd 253 usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
Rajit Singh 0:9db3bed8fffd 254 usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
Rajit Singh 0:9db3bed8fffd 255
Rajit Singh 0:9db3bed8fffd 256 usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
Rajit Singh 0:9db3bed8fffd 257 usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
Rajit Singh 0:9db3bed8fffd 258 usRegWriteAddress++;
Rajit Singh 0:9db3bed8fffd 259
Rajit Singh 0:9db3bed8fffd 260 usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
Rajit Singh 0:9db3bed8fffd 261 usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
Rajit Singh 0:9db3bed8fffd 262
Rajit Singh 0:9db3bed8fffd 263 ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
Rajit Singh 0:9db3bed8fffd 264
Rajit Singh 0:9db3bed8fffd 265 if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) &&
Rajit Singh 0:9db3bed8fffd 266 ( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) &&
Rajit Singh 0:9db3bed8fffd 267 ( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
Rajit Singh 0:9db3bed8fffd 268 {
Rajit Singh 0:9db3bed8fffd 269 /* Make callback to update the register values. */
Rajit Singh 0:9db3bed8fffd 270 eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
Rajit Singh 0:9db3bed8fffd 271 usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
Rajit Singh 0:9db3bed8fffd 272
Rajit Singh 0:9db3bed8fffd 273 if( eRegStatus == MB_ENOERR )
Rajit Singh 0:9db3bed8fffd 274 {
Rajit Singh 0:9db3bed8fffd 275 /* Set the current PDU data pointer to the beginning. */
Rajit Singh 0:9db3bed8fffd 276 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
Rajit Singh 0:9db3bed8fffd 277 *usLen = MB_PDU_FUNC_OFF;
Rajit Singh 0:9db3bed8fffd 278
Rajit Singh 0:9db3bed8fffd 279 /* First byte contains the function code. */
Rajit Singh 0:9db3bed8fffd 280 *pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
Rajit Singh 0:9db3bed8fffd 281 *usLen += 1;
Rajit Singh 0:9db3bed8fffd 282
Rajit Singh 0:9db3bed8fffd 283 /* Second byte in the response contain the number of bytes. */
Rajit Singh 0:9db3bed8fffd 284 *pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
Rajit Singh 0:9db3bed8fffd 285 *usLen += 1;
Rajit Singh 0:9db3bed8fffd 286
Rajit Singh 0:9db3bed8fffd 287 /* Make the read callback. */
Rajit Singh 0:9db3bed8fffd 288 eRegStatus =
Rajit Singh 0:9db3bed8fffd 289 eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
Rajit Singh 0:9db3bed8fffd 290 if( eRegStatus == MB_ENOERR )
Rajit Singh 0:9db3bed8fffd 291 {
Rajit Singh 0:9db3bed8fffd 292 *usLen += 2 * usRegReadCount;
Rajit Singh 0:9db3bed8fffd 293 }
Rajit Singh 0:9db3bed8fffd 294 }
Rajit Singh 0:9db3bed8fffd 295 if( eRegStatus != MB_ENOERR )
Rajit Singh 0:9db3bed8fffd 296 {
Rajit Singh 0:9db3bed8fffd 297 eStatus = prveMBError2Exception( eRegStatus );
Rajit Singh 0:9db3bed8fffd 298 }
Rajit Singh 0:9db3bed8fffd 299 }
Rajit Singh 0:9db3bed8fffd 300 else
Rajit Singh 0:9db3bed8fffd 301 {
Rajit Singh 0:9db3bed8fffd 302 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
Rajit Singh 0:9db3bed8fffd 303 }
Rajit Singh 0:9db3bed8fffd 304 }
Rajit Singh 0:9db3bed8fffd 305 return eStatus;
Rajit Singh 0:9db3bed8fffd 306 }
Rajit Singh 0:9db3bed8fffd 307
Rajit Singh 0:9db3bed8fffd 308 #endif