Modbus RTU/ASCII/TCP with lwip TCP working partial, but with errors (retransmitions)

Dependencies:   EthernetNetIf mbed

Committer:
tmav123
Date:
Mon Dec 05 22:49:02 2011 +0000
Revision:
0:f54e9507171b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tmav123 0:f54e9507171b 1 /*
tmav123 0:f54e9507171b 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
tmav123 0:f54e9507171b 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
tmav123 0:f54e9507171b 4 * All rights reserved.
tmav123 0:f54e9507171b 5 *
tmav123 0:f54e9507171b 6 * Redistribution and use in source and binary forms, with or without
tmav123 0:f54e9507171b 7 * modification, are permitted provided that the following conditions
tmav123 0:f54e9507171b 8 * are met:
tmav123 0:f54e9507171b 9 * 1. Redistributions of source code must retain the above copyright
tmav123 0:f54e9507171b 10 * notice, this list of conditions and the following disclaimer.
tmav123 0:f54e9507171b 11 * 2. Redistributions in binary form must reproduce the above copyright
tmav123 0:f54e9507171b 12 * notice, this list of conditions and the following disclaimer in the
tmav123 0:f54e9507171b 13 * documentation and/or other materials provided with the distribution.
tmav123 0:f54e9507171b 14 * 3. The name of the author may not be used to endorse or promote products
tmav123 0:f54e9507171b 15 * derived from this software without specific prior written permission.
tmav123 0:f54e9507171b 16 *
tmav123 0:f54e9507171b 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
tmav123 0:f54e9507171b 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
tmav123 0:f54e9507171b 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
tmav123 0:f54e9507171b 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
tmav123 0:f54e9507171b 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
tmav123 0:f54e9507171b 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
tmav123 0:f54e9507171b 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
tmav123 0:f54e9507171b 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
tmav123 0:f54e9507171b 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
tmav123 0:f54e9507171b 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
tmav123 0:f54e9507171b 27 *
tmav123 0:f54e9507171b 28 * File: $Id: mbfuncholding.c,v 1.12 2007/02/18 23:48:22 wolti Exp $
tmav123 0:f54e9507171b 29 */
tmav123 0:f54e9507171b 30
tmav123 0:f54e9507171b 31 /* ----------------------- System includes ----------------------------------*/
tmav123 0:f54e9507171b 32 #include "stdlib.h"
tmav123 0:f54e9507171b 33 #include "string.h"
tmav123 0:f54e9507171b 34
tmav123 0:f54e9507171b 35 /* ----------------------- Platform includes --------------------------------*/
tmav123 0:f54e9507171b 36 #include "port.h"
tmav123 0:f54e9507171b 37
tmav123 0:f54e9507171b 38 /* ----------------------- Modbus includes ----------------------------------*/
tmav123 0:f54e9507171b 39 #include "mb.h"
tmav123 0:f54e9507171b 40 #include "mbframe.h"
tmav123 0:f54e9507171b 41 #include "mbproto.h"
tmav123 0:f54e9507171b 42 #include "mbconfig.h"
tmav123 0:f54e9507171b 43
tmav123 0:f54e9507171b 44 /* ----------------------- Defines ------------------------------------------*/
tmav123 0:f54e9507171b 45 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
tmav123 0:f54e9507171b 46 #define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
tmav123 0:f54e9507171b 47 #define MB_PDU_FUNC_READ_SIZE ( 4 )
tmav123 0:f54e9507171b 48 #define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
tmav123 0:f54e9507171b 49
tmav123 0:f54e9507171b 50 #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
tmav123 0:f54e9507171b 51 #define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
tmav123 0:f54e9507171b 52 #define MB_PDU_FUNC_WRITE_SIZE ( 4 )
tmav123 0:f54e9507171b 53
tmav123 0:f54e9507171b 54 #define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
tmav123 0:f54e9507171b 55 #define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
tmav123 0:f54e9507171b 56 #define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
tmav123 0:f54e9507171b 57 #define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
tmav123 0:f54e9507171b 58 #define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
tmav123 0:f54e9507171b 59 #define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ( 0x0078 )
tmav123 0:f54e9507171b 60
tmav123 0:f54e9507171b 61 #define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
tmav123 0:f54e9507171b 62 #define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
tmav123 0:f54e9507171b 63 #define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 )
tmav123 0:f54e9507171b 64 #define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 )
tmav123 0:f54e9507171b 65 #define MB_PDU_FUNC_READWRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 )
tmav123 0:f54e9507171b 66 #define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 )
tmav123 0:f54e9507171b 67 #define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 9 )
tmav123 0:f54e9507171b 68
tmav123 0:f54e9507171b 69 /* ----------------------- Static functions ---------------------------------*/
tmav123 0:f54e9507171b 70 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
tmav123 0:f54e9507171b 71
tmav123 0:f54e9507171b 72 /* ----------------------- Start implementation -----------------------------*/
tmav123 0:f54e9507171b 73
tmav123 0:f54e9507171b 74 #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
tmav123 0:f54e9507171b 75
tmav123 0:f54e9507171b 76 eMBException
tmav123 0:f54e9507171b 77 eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
tmav123 0:f54e9507171b 78 {
tmav123 0:f54e9507171b 79 USHORT usRegAddress;
tmav123 0:f54e9507171b 80 eMBException eStatus = MB_EX_NONE;
tmav123 0:f54e9507171b 81 eMBErrorCode eRegStatus;
tmav123 0:f54e9507171b 82
tmav123 0:f54e9507171b 83 if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
tmav123 0:f54e9507171b 84 {
tmav123 0:f54e9507171b 85 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
tmav123 0:f54e9507171b 86 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
tmav123 0:f54e9507171b 87 usRegAddress++;
tmav123 0:f54e9507171b 88
tmav123 0:f54e9507171b 89 /* Make callback to update the value. */
tmav123 0:f54e9507171b 90 eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
tmav123 0:f54e9507171b 91 usRegAddress, 1, MB_REG_WRITE );
tmav123 0:f54e9507171b 92
tmav123 0:f54e9507171b 93 /* If an error occured convert it into a Modbus exception. */
tmav123 0:f54e9507171b 94 if( eRegStatus != MB_ENOERR )
tmav123 0:f54e9507171b 95 {
tmav123 0:f54e9507171b 96 eStatus = prveMBError2Exception( eRegStatus );
tmav123 0:f54e9507171b 97 }
tmav123 0:f54e9507171b 98 }
tmav123 0:f54e9507171b 99 else
tmav123 0:f54e9507171b 100 {
tmav123 0:f54e9507171b 101 /* Can't be a valid request because the length is incorrect. */
tmav123 0:f54e9507171b 102 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
tmav123 0:f54e9507171b 103 }
tmav123 0:f54e9507171b 104 return eStatus;
tmav123 0:f54e9507171b 105 }
tmav123 0:f54e9507171b 106 #endif
tmav123 0:f54e9507171b 107
tmav123 0:f54e9507171b 108 #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
tmav123 0:f54e9507171b 109 eMBException
tmav123 0:f54e9507171b 110 eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
tmav123 0:f54e9507171b 111 {
tmav123 0:f54e9507171b 112 USHORT usRegAddress;
tmav123 0:f54e9507171b 113 USHORT usRegCount;
tmav123 0:f54e9507171b 114 UCHAR ucRegByteCount;
tmav123 0:f54e9507171b 115
tmav123 0:f54e9507171b 116 eMBException eStatus = MB_EX_NONE;
tmav123 0:f54e9507171b 117 eMBErrorCode eRegStatus;
tmav123 0:f54e9507171b 118
tmav123 0:f54e9507171b 119 if( *usLen >= ( MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN ) )
tmav123 0:f54e9507171b 120 {
tmav123 0:f54e9507171b 121 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
tmav123 0:f54e9507171b 122 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
tmav123 0:f54e9507171b 123 usRegAddress++;
tmav123 0:f54e9507171b 124
tmav123 0:f54e9507171b 125 usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 );
tmav123 0:f54e9507171b 126 usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] );
tmav123 0:f54e9507171b 127
tmav123 0:f54e9507171b 128 ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
tmav123 0:f54e9507171b 129
tmav123 0:f54e9507171b 130 if( ( usRegCount >= 1 ) &&
tmav123 0:f54e9507171b 131 ( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) &&
tmav123 0:f54e9507171b 132 ( ucRegByteCount == ( UCHAR ) ( 2 * usRegCount ) ) )
tmav123 0:f54e9507171b 133 {
tmav123 0:f54e9507171b 134 /* Make callback to update the register values. */
tmav123 0:f54e9507171b 135 eRegStatus =
tmav123 0:f54e9507171b 136 eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
tmav123 0:f54e9507171b 137 usRegAddress, usRegCount, MB_REG_WRITE );
tmav123 0:f54e9507171b 138
tmav123 0:f54e9507171b 139 /* If an error occured convert it into a Modbus exception. */
tmav123 0:f54e9507171b 140 if( eRegStatus != MB_ENOERR )
tmav123 0:f54e9507171b 141 {
tmav123 0:f54e9507171b 142 eStatus = prveMBError2Exception( eRegStatus );
tmav123 0:f54e9507171b 143 }
tmav123 0:f54e9507171b 144 else
tmav123 0:f54e9507171b 145 {
tmav123 0:f54e9507171b 146 /* The response contains the function code, the starting
tmav123 0:f54e9507171b 147 * address and the quantity of registers. We reuse the
tmav123 0:f54e9507171b 148 * old values in the buffer because they are still valid.
tmav123 0:f54e9507171b 149 */
tmav123 0:f54e9507171b 150 *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
tmav123 0:f54e9507171b 151 }
tmav123 0:f54e9507171b 152 }
tmav123 0:f54e9507171b 153 else
tmav123 0:f54e9507171b 154 {
tmav123 0:f54e9507171b 155 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
tmav123 0:f54e9507171b 156 }
tmav123 0:f54e9507171b 157 }
tmav123 0:f54e9507171b 158 else
tmav123 0:f54e9507171b 159 {
tmav123 0:f54e9507171b 160 /* Can't be a valid request because the length is incorrect. */
tmav123 0:f54e9507171b 161 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
tmav123 0:f54e9507171b 162 }
tmav123 0:f54e9507171b 163 return eStatus;
tmav123 0:f54e9507171b 164 }
tmav123 0:f54e9507171b 165 #endif
tmav123 0:f54e9507171b 166
tmav123 0:f54e9507171b 167 #if MB_FUNC_READ_HOLDING_ENABLED > 0
tmav123 0:f54e9507171b 168
tmav123 0:f54e9507171b 169 eMBException
tmav123 0:f54e9507171b 170 eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
tmav123 0:f54e9507171b 171 {
tmav123 0:f54e9507171b 172 USHORT usRegAddress;
tmav123 0:f54e9507171b 173 USHORT usRegCount;
tmav123 0:f54e9507171b 174 UCHAR *pucFrameCur;
tmav123 0:f54e9507171b 175
tmav123 0:f54e9507171b 176 eMBException eStatus = MB_EX_NONE;
tmav123 0:f54e9507171b 177 eMBErrorCode eRegStatus;
tmav123 0:f54e9507171b 178
tmav123 0:f54e9507171b 179 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
tmav123 0:f54e9507171b 180 {
tmav123 0:f54e9507171b 181 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
tmav123 0:f54e9507171b 182 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
tmav123 0:f54e9507171b 183 usRegAddress++;
tmav123 0:f54e9507171b 184
tmav123 0:f54e9507171b 185 usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
tmav123 0:f54e9507171b 186 usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
tmav123 0:f54e9507171b 187
tmav123 0:f54e9507171b 188 /* Check if the number of registers to read is valid. If not
tmav123 0:f54e9507171b 189 * return Modbus illegal data value exception.
tmav123 0:f54e9507171b 190 */
tmav123 0:f54e9507171b 191 if( ( usRegCount >= 1 ) && ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) )
tmav123 0:f54e9507171b 192 {
tmav123 0:f54e9507171b 193 /* Set the current PDU data pointer to the beginning. */
tmav123 0:f54e9507171b 194 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
tmav123 0:f54e9507171b 195 *usLen = MB_PDU_FUNC_OFF;
tmav123 0:f54e9507171b 196
tmav123 0:f54e9507171b 197 /* First byte contains the function code. */
tmav123 0:f54e9507171b 198 *pucFrameCur++ = MB_FUNC_READ_HOLDING_REGISTER;
tmav123 0:f54e9507171b 199 *usLen += 1;
tmav123 0:f54e9507171b 200
tmav123 0:f54e9507171b 201 /* Second byte in the response contain the number of bytes. */
tmav123 0:f54e9507171b 202 *pucFrameCur++ = ( UCHAR ) ( usRegCount * 2 );
tmav123 0:f54e9507171b 203 *usLen += 1;
tmav123 0:f54e9507171b 204
tmav123 0:f54e9507171b 205 /* Make callback to fill the buffer. */
tmav123 0:f54e9507171b 206 eRegStatus = eMBRegHoldingCB( pucFrameCur, usRegAddress, usRegCount, MB_REG_READ );
tmav123 0:f54e9507171b 207 /* If an error occured convert it into a Modbus exception. */
tmav123 0:f54e9507171b 208 if( eRegStatus != MB_ENOERR )
tmav123 0:f54e9507171b 209 {
tmav123 0:f54e9507171b 210 eStatus = prveMBError2Exception( eRegStatus );
tmav123 0:f54e9507171b 211 }
tmav123 0:f54e9507171b 212 else
tmav123 0:f54e9507171b 213 {
tmav123 0:f54e9507171b 214 *usLen += usRegCount * 2;
tmav123 0:f54e9507171b 215 }
tmav123 0:f54e9507171b 216 }
tmav123 0:f54e9507171b 217 else
tmav123 0:f54e9507171b 218 {
tmav123 0:f54e9507171b 219 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
tmav123 0:f54e9507171b 220 }
tmav123 0:f54e9507171b 221 }
tmav123 0:f54e9507171b 222 else
tmav123 0:f54e9507171b 223 {
tmav123 0:f54e9507171b 224 /* Can't be a valid request because the length is incorrect. */
tmav123 0:f54e9507171b 225 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
tmav123 0:f54e9507171b 226 }
tmav123 0:f54e9507171b 227 return eStatus;
tmav123 0:f54e9507171b 228 }
tmav123 0:f54e9507171b 229
tmav123 0:f54e9507171b 230 #endif
tmav123 0:f54e9507171b 231
tmav123 0:f54e9507171b 232 #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
tmav123 0:f54e9507171b 233
tmav123 0:f54e9507171b 234 eMBException
tmav123 0:f54e9507171b 235 eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
tmav123 0:f54e9507171b 236 {
tmav123 0:f54e9507171b 237 USHORT usRegReadAddress;
tmav123 0:f54e9507171b 238 USHORT usRegReadCount;
tmav123 0:f54e9507171b 239 USHORT usRegWriteAddress;
tmav123 0:f54e9507171b 240 USHORT usRegWriteCount;
tmav123 0:f54e9507171b 241 UCHAR ucRegWriteByteCount;
tmav123 0:f54e9507171b 242 UCHAR *pucFrameCur;
tmav123 0:f54e9507171b 243
tmav123 0:f54e9507171b 244 eMBException eStatus = MB_EX_NONE;
tmav123 0:f54e9507171b 245 eMBErrorCode eRegStatus;
tmav123 0:f54e9507171b 246
tmav123 0:f54e9507171b 247 if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) )
tmav123 0:f54e9507171b 248 {
tmav123 0:f54e9507171b 249 usRegReadAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
tmav123 0:f54e9507171b 250 usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
tmav123 0:f54e9507171b 251 usRegReadAddress++;
tmav123 0:f54e9507171b 252
tmav123 0:f54e9507171b 253 usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
tmav123 0:f54e9507171b 254 usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
tmav123 0:f54e9507171b 255
tmav123 0:f54e9507171b 256 usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
tmav123 0:f54e9507171b 257 usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
tmav123 0:f54e9507171b 258 usRegWriteAddress++;
tmav123 0:f54e9507171b 259
tmav123 0:f54e9507171b 260 usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
tmav123 0:f54e9507171b 261 usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
tmav123 0:f54e9507171b 262
tmav123 0:f54e9507171b 263 ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
tmav123 0:f54e9507171b 264
tmav123 0:f54e9507171b 265 if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) &&
tmav123 0:f54e9507171b 266 ( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) &&
tmav123 0:f54e9507171b 267 ( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
tmav123 0:f54e9507171b 268 {
tmav123 0:f54e9507171b 269 /* Make callback to update the register values. */
tmav123 0:f54e9507171b 270 eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
tmav123 0:f54e9507171b 271 usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
tmav123 0:f54e9507171b 272
tmav123 0:f54e9507171b 273 if( eRegStatus == MB_ENOERR )
tmav123 0:f54e9507171b 274 {
tmav123 0:f54e9507171b 275 /* Set the current PDU data pointer to the beginning. */
tmav123 0:f54e9507171b 276 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
tmav123 0:f54e9507171b 277 *usLen = MB_PDU_FUNC_OFF;
tmav123 0:f54e9507171b 278
tmav123 0:f54e9507171b 279 /* First byte contains the function code. */
tmav123 0:f54e9507171b 280 *pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
tmav123 0:f54e9507171b 281 *usLen += 1;
tmav123 0:f54e9507171b 282
tmav123 0:f54e9507171b 283 /* Second byte in the response contain the number of bytes. */
tmav123 0:f54e9507171b 284 *pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
tmav123 0:f54e9507171b 285 *usLen += 1;
tmav123 0:f54e9507171b 286
tmav123 0:f54e9507171b 287 /* Make the read callback. */
tmav123 0:f54e9507171b 288 eRegStatus =
tmav123 0:f54e9507171b 289 eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
tmav123 0:f54e9507171b 290 if( eRegStatus == MB_ENOERR )
tmav123 0:f54e9507171b 291 {
tmav123 0:f54e9507171b 292 *usLen += 2 * usRegReadCount;
tmav123 0:f54e9507171b 293 }
tmav123 0:f54e9507171b 294 }
tmav123 0:f54e9507171b 295 if( eRegStatus != MB_ENOERR )
tmav123 0:f54e9507171b 296 {
tmav123 0:f54e9507171b 297 eStatus = prveMBError2Exception( eRegStatus );
tmav123 0:f54e9507171b 298 }
tmav123 0:f54e9507171b 299 }
tmav123 0:f54e9507171b 300 else
tmav123 0:f54e9507171b 301 {
tmav123 0:f54e9507171b 302 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
tmav123 0:f54e9507171b 303 }
tmav123 0:f54e9507171b 304 }
tmav123 0:f54e9507171b 305 return eStatus;
tmav123 0:f54e9507171b 306 }
tmav123 0:f54e9507171b 307
tmav123 0:f54e9507171b 308 #endif