Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbfunccoils.cpp@0:7b5d37a81b6b, 2010-10-05 (annotated)
- Committer:
- tecnosys
- Date:
- Tue Oct 05 11:51:06 2010 +0000
- Revision:
- 0:7b5d37a81b6b
Who changed what in which revision?
User | Revision | Line number | New 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: mbfunccoils.c,v 1.8 2007/02/18 23:47:16 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 ) |
tecnosys | 0:7b5d37a81b6b | 46 | #define MB_PDU_FUNC_READ_COILCNT_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_COILCNT_MAX ( 0x07D0 ) |
tecnosys | 0:7b5d37a81b6b | 49 | |
tecnosys | 0:7b5d37a81b6b | 50 | #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF ) |
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 ) |
tecnosys | 0:7b5d37a81b6b | 55 | #define MB_PDU_FUNC_WRITE_MUL_COILCNT_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_COILCNT_MAX ( 0x07B0 ) |
tecnosys | 0:7b5d37a81b6b | 60 | |
tecnosys | 0:7b5d37a81b6b | 61 | /* ----------------------- Static functions ---------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 62 | eMBException prveMBError2Exception( eMBErrorCode eErrorCode ); |
tecnosys | 0:7b5d37a81b6b | 63 | |
tecnosys | 0:7b5d37a81b6b | 64 | /* ----------------------- Start implementation -----------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 65 | |
tecnosys | 0:7b5d37a81b6b | 66 | #if MB_FUNC_READ_COILS_ENABLED > 0 |
tecnosys | 0:7b5d37a81b6b | 67 | |
tecnosys | 0:7b5d37a81b6b | 68 | eMBException |
tecnosys | 0:7b5d37a81b6b | 69 | eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen ) |
tecnosys | 0:7b5d37a81b6b | 70 | { |
tecnosys | 0:7b5d37a81b6b | 71 | USHORT usRegAddress; |
tecnosys | 0:7b5d37a81b6b | 72 | USHORT usCoilCount; |
tecnosys | 0:7b5d37a81b6b | 73 | UCHAR ucNBytes; |
tecnosys | 0:7b5d37a81b6b | 74 | UCHAR *pucFrameCur; |
tecnosys | 0:7b5d37a81b6b | 75 | |
tecnosys | 0:7b5d37a81b6b | 76 | eMBException eStatus = MB_EX_NONE; |
tecnosys | 0:7b5d37a81b6b | 77 | eMBErrorCode eRegStatus; |
tecnosys | 0:7b5d37a81b6b | 78 | |
tecnosys | 0:7b5d37a81b6b | 79 | if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) ) |
tecnosys | 0:7b5d37a81b6b | 80 | { |
tecnosys | 0:7b5d37a81b6b | 81 | usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 ); |
tecnosys | 0:7b5d37a81b6b | 82 | usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] ); |
tecnosys | 0:7b5d37a81b6b | 83 | usRegAddress++; |
tecnosys | 0:7b5d37a81b6b | 84 | |
tecnosys | 0:7b5d37a81b6b | 85 | usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 ); |
tecnosys | 0:7b5d37a81b6b | 86 | usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] ); |
tecnosys | 0:7b5d37a81b6b | 87 | |
tecnosys | 0:7b5d37a81b6b | 88 | /* Check if the number of registers to read is valid. If not |
tecnosys | 0:7b5d37a81b6b | 89 | * return Modbus illegal data value exception. |
tecnosys | 0:7b5d37a81b6b | 90 | */ |
tecnosys | 0:7b5d37a81b6b | 91 | if( ( usCoilCount >= 1 ) && |
tecnosys | 0:7b5d37a81b6b | 92 | ( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) ) |
tecnosys | 0:7b5d37a81b6b | 93 | { |
tecnosys | 0:7b5d37a81b6b | 94 | /* Set the current PDU data pointer to the beginning. */ |
tecnosys | 0:7b5d37a81b6b | 95 | pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF]; |
tecnosys | 0:7b5d37a81b6b | 96 | *usLen = MB_PDU_FUNC_OFF; |
tecnosys | 0:7b5d37a81b6b | 97 | |
tecnosys | 0:7b5d37a81b6b | 98 | /* First byte contains the function code. */ |
tecnosys | 0:7b5d37a81b6b | 99 | *pucFrameCur++ = MB_FUNC_READ_COILS; |
tecnosys | 0:7b5d37a81b6b | 100 | *usLen += 1; |
tecnosys | 0:7b5d37a81b6b | 101 | |
tecnosys | 0:7b5d37a81b6b | 102 | /* Test if the quantity of coils is a multiple of 8. If not last |
tecnosys | 0:7b5d37a81b6b | 103 | * byte is only partially field with unused coils set to zero. */ |
tecnosys | 0:7b5d37a81b6b | 104 | if( ( usCoilCount & 0x0007 ) != 0 ) |
tecnosys | 0:7b5d37a81b6b | 105 | { |
tecnosys | 0:7b5d37a81b6b | 106 | ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 ); |
tecnosys | 0:7b5d37a81b6b | 107 | } |
tecnosys | 0:7b5d37a81b6b | 108 | else |
tecnosys | 0:7b5d37a81b6b | 109 | { |
tecnosys | 0:7b5d37a81b6b | 110 | ucNBytes = ( UCHAR )( usCoilCount / 8 ); |
tecnosys | 0:7b5d37a81b6b | 111 | } |
tecnosys | 0:7b5d37a81b6b | 112 | *pucFrameCur++ = ucNBytes; |
tecnosys | 0:7b5d37a81b6b | 113 | *usLen += 1; |
tecnosys | 0:7b5d37a81b6b | 114 | |
tecnosys | 0:7b5d37a81b6b | 115 | eRegStatus = |
tecnosys | 0:7b5d37a81b6b | 116 | eMBRegCoilsCB( pucFrameCur, usRegAddress, usCoilCount, |
tecnosys | 0:7b5d37a81b6b | 117 | MB_REG_READ ); |
tecnosys | 0:7b5d37a81b6b | 118 | |
tecnosys | 0:7b5d37a81b6b | 119 | /* If an error occured convert it into a Modbus exception. */ |
tecnosys | 0:7b5d37a81b6b | 120 | if( eRegStatus != MB_ENOERR ) |
tecnosys | 0:7b5d37a81b6b | 121 | { |
tecnosys | 0:7b5d37a81b6b | 122 | eStatus = prveMBError2Exception( eRegStatus ); |
tecnosys | 0:7b5d37a81b6b | 123 | } |
tecnosys | 0:7b5d37a81b6b | 124 | else |
tecnosys | 0:7b5d37a81b6b | 125 | { |
tecnosys | 0:7b5d37a81b6b | 126 | /* The response contains the function code, the starting address |
tecnosys | 0:7b5d37a81b6b | 127 | * and the quantity of registers. We reuse the old values in the |
tecnosys | 0:7b5d37a81b6b | 128 | * buffer because they are still valid. */ |
tecnosys | 0:7b5d37a81b6b | 129 | *usLen += ucNBytes;; |
tecnosys | 0:7b5d37a81b6b | 130 | } |
tecnosys | 0:7b5d37a81b6b | 131 | } |
tecnosys | 0:7b5d37a81b6b | 132 | else |
tecnosys | 0:7b5d37a81b6b | 133 | { |
tecnosys | 0:7b5d37a81b6b | 134 | eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
tecnosys | 0:7b5d37a81b6b | 135 | } |
tecnosys | 0:7b5d37a81b6b | 136 | } |
tecnosys | 0:7b5d37a81b6b | 137 | else |
tecnosys | 0:7b5d37a81b6b | 138 | { |
tecnosys | 0:7b5d37a81b6b | 139 | /* Can't be a valid read coil register request because the length |
tecnosys | 0:7b5d37a81b6b | 140 | * is incorrect. */ |
tecnosys | 0:7b5d37a81b6b | 141 | eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
tecnosys | 0:7b5d37a81b6b | 142 | } |
tecnosys | 0:7b5d37a81b6b | 143 | return eStatus; |
tecnosys | 0:7b5d37a81b6b | 144 | } |
tecnosys | 0:7b5d37a81b6b | 145 | |
tecnosys | 0:7b5d37a81b6b | 146 | #if MB_FUNC_WRITE_COIL_ENABLED > 0 |
tecnosys | 0:7b5d37a81b6b | 147 | eMBException |
tecnosys | 0:7b5d37a81b6b | 148 | eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen ) |
tecnosys | 0:7b5d37a81b6b | 149 | { |
tecnosys | 0:7b5d37a81b6b | 150 | USHORT usRegAddress; |
tecnosys | 0:7b5d37a81b6b | 151 | UCHAR ucBuf[2]; |
tecnosys | 0:7b5d37a81b6b | 152 | |
tecnosys | 0:7b5d37a81b6b | 153 | eMBException eStatus = MB_EX_NONE; |
tecnosys | 0:7b5d37a81b6b | 154 | eMBErrorCode eRegStatus; |
tecnosys | 0:7b5d37a81b6b | 155 | |
tecnosys | 0:7b5d37a81b6b | 156 | if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) ) |
tecnosys | 0:7b5d37a81b6b | 157 | { |
tecnosys | 0:7b5d37a81b6b | 158 | usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 ); |
tecnosys | 0:7b5d37a81b6b | 159 | usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] ); |
tecnosys | 0:7b5d37a81b6b | 160 | usRegAddress++; |
tecnosys | 0:7b5d37a81b6b | 161 | |
tecnosys | 0:7b5d37a81b6b | 162 | if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) && |
tecnosys | 0:7b5d37a81b6b | 163 | ( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) || |
tecnosys | 0:7b5d37a81b6b | 164 | ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) ) |
tecnosys | 0:7b5d37a81b6b | 165 | { |
tecnosys | 0:7b5d37a81b6b | 166 | ucBuf[1] = 0; |
tecnosys | 0:7b5d37a81b6b | 167 | if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) |
tecnosys | 0:7b5d37a81b6b | 168 | { |
tecnosys | 0:7b5d37a81b6b | 169 | ucBuf[0] = 1; |
tecnosys | 0:7b5d37a81b6b | 170 | } |
tecnosys | 0:7b5d37a81b6b | 171 | else |
tecnosys | 0:7b5d37a81b6b | 172 | { |
tecnosys | 0:7b5d37a81b6b | 173 | ucBuf[0] = 0; |
tecnosys | 0:7b5d37a81b6b | 174 | } |
tecnosys | 0:7b5d37a81b6b | 175 | eRegStatus = |
tecnosys | 0:7b5d37a81b6b | 176 | eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE ); |
tecnosys | 0:7b5d37a81b6b | 177 | |
tecnosys | 0:7b5d37a81b6b | 178 | /* If an error occured convert it into a Modbus exception. */ |
tecnosys | 0:7b5d37a81b6b | 179 | if( eRegStatus != MB_ENOERR ) |
tecnosys | 0:7b5d37a81b6b | 180 | { |
tecnosys | 0:7b5d37a81b6b | 181 | eStatus = prveMBError2Exception( eRegStatus ); |
tecnosys | 0:7b5d37a81b6b | 182 | } |
tecnosys | 0:7b5d37a81b6b | 183 | } |
tecnosys | 0:7b5d37a81b6b | 184 | else |
tecnosys | 0:7b5d37a81b6b | 185 | { |
tecnosys | 0:7b5d37a81b6b | 186 | eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
tecnosys | 0:7b5d37a81b6b | 187 | } |
tecnosys | 0:7b5d37a81b6b | 188 | } |
tecnosys | 0:7b5d37a81b6b | 189 | else |
tecnosys | 0:7b5d37a81b6b | 190 | { |
tecnosys | 0:7b5d37a81b6b | 191 | /* Can't be a valid write coil register request because the length |
tecnosys | 0:7b5d37a81b6b | 192 | * is incorrect. */ |
tecnosys | 0:7b5d37a81b6b | 193 | eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
tecnosys | 0:7b5d37a81b6b | 194 | } |
tecnosys | 0:7b5d37a81b6b | 195 | return eStatus; |
tecnosys | 0:7b5d37a81b6b | 196 | } |
tecnosys | 0:7b5d37a81b6b | 197 | |
tecnosys | 0:7b5d37a81b6b | 198 | #endif |
tecnosys | 0:7b5d37a81b6b | 199 | |
tecnosys | 0:7b5d37a81b6b | 200 | #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0 |
tecnosys | 0:7b5d37a81b6b | 201 | eMBException |
tecnosys | 0:7b5d37a81b6b | 202 | eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen ) |
tecnosys | 0:7b5d37a81b6b | 203 | { |
tecnosys | 0:7b5d37a81b6b | 204 | USHORT usRegAddress; |
tecnosys | 0:7b5d37a81b6b | 205 | USHORT usCoilCnt; |
tecnosys | 0:7b5d37a81b6b | 206 | UCHAR ucByteCount; |
tecnosys | 0:7b5d37a81b6b | 207 | UCHAR ucByteCountVerify; |
tecnosys | 0:7b5d37a81b6b | 208 | |
tecnosys | 0:7b5d37a81b6b | 209 | eMBException eStatus = MB_EX_NONE; |
tecnosys | 0:7b5d37a81b6b | 210 | eMBErrorCode eRegStatus; |
tecnosys | 0:7b5d37a81b6b | 211 | |
tecnosys | 0:7b5d37a81b6b | 212 | if( *usLen > ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) ) |
tecnosys | 0:7b5d37a81b6b | 213 | { |
tecnosys | 0:7b5d37a81b6b | 214 | usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 ); |
tecnosys | 0:7b5d37a81b6b | 215 | usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] ); |
tecnosys | 0:7b5d37a81b6b | 216 | usRegAddress++; |
tecnosys | 0:7b5d37a81b6b | 217 | |
tecnosys | 0:7b5d37a81b6b | 218 | usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 ); |
tecnosys | 0:7b5d37a81b6b | 219 | usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] ); |
tecnosys | 0:7b5d37a81b6b | 220 | |
tecnosys | 0:7b5d37a81b6b | 221 | ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF]; |
tecnosys | 0:7b5d37a81b6b | 222 | |
tecnosys | 0:7b5d37a81b6b | 223 | /* Compute the number of expected bytes in the request. */ |
tecnosys | 0:7b5d37a81b6b | 224 | if( ( usCoilCnt & 0x0007 ) != 0 ) |
tecnosys | 0:7b5d37a81b6b | 225 | { |
tecnosys | 0:7b5d37a81b6b | 226 | ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 ); |
tecnosys | 0:7b5d37a81b6b | 227 | } |
tecnosys | 0:7b5d37a81b6b | 228 | else |
tecnosys | 0:7b5d37a81b6b | 229 | { |
tecnosys | 0:7b5d37a81b6b | 230 | ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 ); |
tecnosys | 0:7b5d37a81b6b | 231 | } |
tecnosys | 0:7b5d37a81b6b | 232 | |
tecnosys | 0:7b5d37a81b6b | 233 | if( ( usCoilCnt >= 1 ) && |
tecnosys | 0:7b5d37a81b6b | 234 | ( usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ) && |
tecnosys | 0:7b5d37a81b6b | 235 | ( ucByteCountVerify == ucByteCount ) ) |
tecnosys | 0:7b5d37a81b6b | 236 | { |
tecnosys | 0:7b5d37a81b6b | 237 | eRegStatus = |
tecnosys | 0:7b5d37a81b6b | 238 | eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF], |
tecnosys | 0:7b5d37a81b6b | 239 | usRegAddress, usCoilCnt, MB_REG_WRITE ); |
tecnosys | 0:7b5d37a81b6b | 240 | |
tecnosys | 0:7b5d37a81b6b | 241 | /* If an error occured convert it into a Modbus exception. */ |
tecnosys | 0:7b5d37a81b6b | 242 | if( eRegStatus != MB_ENOERR ) |
tecnosys | 0:7b5d37a81b6b | 243 | { |
tecnosys | 0:7b5d37a81b6b | 244 | eStatus = prveMBError2Exception( eRegStatus ); |
tecnosys | 0:7b5d37a81b6b | 245 | } |
tecnosys | 0:7b5d37a81b6b | 246 | else |
tecnosys | 0:7b5d37a81b6b | 247 | { |
tecnosys | 0:7b5d37a81b6b | 248 | /* The response contains the function code, the starting address |
tecnosys | 0:7b5d37a81b6b | 249 | * and the quantity of registers. We reuse the old values in the |
tecnosys | 0:7b5d37a81b6b | 250 | * buffer because they are still valid. */ |
tecnosys | 0:7b5d37a81b6b | 251 | *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF; |
tecnosys | 0:7b5d37a81b6b | 252 | } |
tecnosys | 0:7b5d37a81b6b | 253 | } |
tecnosys | 0:7b5d37a81b6b | 254 | else |
tecnosys | 0:7b5d37a81b6b | 255 | { |
tecnosys | 0:7b5d37a81b6b | 256 | eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
tecnosys | 0:7b5d37a81b6b | 257 | } |
tecnosys | 0:7b5d37a81b6b | 258 | } |
tecnosys | 0:7b5d37a81b6b | 259 | else |
tecnosys | 0:7b5d37a81b6b | 260 | { |
tecnosys | 0:7b5d37a81b6b | 261 | /* Can't be a valid write coil register request because the length |
tecnosys | 0:7b5d37a81b6b | 262 | * is incorrect. */ |
tecnosys | 0:7b5d37a81b6b | 263 | eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
tecnosys | 0:7b5d37a81b6b | 264 | } |
tecnosys | 0:7b5d37a81b6b | 265 | return eStatus; |
tecnosys | 0:7b5d37a81b6b | 266 | } |
tecnosys | 0:7b5d37a81b6b | 267 | |
tecnosys | 0:7b5d37a81b6b | 268 | #endif |
tecnosys | 0:7b5d37a81b6b | 269 | |
tecnosys | 0:7b5d37a81b6b | 270 | #endif |