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