Jason Engelman / Mbed 2 deprecated Modbus

Dependencies:   mbed

Committer:
tecnosys
Date:
Tue Oct 05 11:51:06 2010 +0000
Revision:
0:7b5d37a81b6b

        

Who changed what in which revision?

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