modified for NuMaker_PFM series

Dependents:   NuMaker-mbed-modbus-sample NuMaker_NuWicam_Lite NuMaker-mbed-modbus-sample

Fork of Modbus by Wayne Lin

Committer:
morgandu
Date:
Mon May 13 08:55:07 2024 +0000
Revision:
14:f55bf6e86868
Parent:
0:274eb57e1df3
Support NuMaker-IoT-M487

Who changed what in which revision?

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