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.
mbfuncinput.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: mbfuncinput.c,v 1.10 2007/09/12 10:15:56 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_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_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF ) |
tecnosys | 0:7b5d37a81b6b | 51 | |
tecnosys | 0:7b5d37a81b6b | 52 | /* ----------------------- Static functions ---------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 53 | eMBException prveMBError2Exception( eMBErrorCode eErrorCode ); |
tecnosys | 0:7b5d37a81b6b | 54 | |
tecnosys | 0:7b5d37a81b6b | 55 | /* ----------------------- Start implementation -----------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 56 | #if MB_FUNC_READ_INPUT_ENABLED > 0 |
tecnosys | 0:7b5d37a81b6b | 57 | |
tecnosys | 0:7b5d37a81b6b | 58 | eMBException |
tecnosys | 0:7b5d37a81b6b | 59 | eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen ) |
tecnosys | 0:7b5d37a81b6b | 60 | { |
tecnosys | 0:7b5d37a81b6b | 61 | USHORT usRegAddress; |
tecnosys | 0:7b5d37a81b6b | 62 | USHORT usRegCount; |
tecnosys | 0:7b5d37a81b6b | 63 | UCHAR *pucFrameCur; |
tecnosys | 0:7b5d37a81b6b | 64 | |
tecnosys | 0:7b5d37a81b6b | 65 | eMBException eStatus = MB_EX_NONE; |
tecnosys | 0:7b5d37a81b6b | 66 | eMBErrorCode eRegStatus; |
tecnosys | 0:7b5d37a81b6b | 67 | |
tecnosys | 0:7b5d37a81b6b | 68 | if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) ) |
tecnosys | 0:7b5d37a81b6b | 69 | { |
tecnosys | 0:7b5d37a81b6b | 70 | usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 ); |
tecnosys | 0:7b5d37a81b6b | 71 | usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] ); |
tecnosys | 0:7b5d37a81b6b | 72 | usRegAddress++; |
tecnosys | 0:7b5d37a81b6b | 73 | |
tecnosys | 0:7b5d37a81b6b | 74 | usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 ); |
tecnosys | 0:7b5d37a81b6b | 75 | usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] ); |
tecnosys | 0:7b5d37a81b6b | 76 | |
tecnosys | 0:7b5d37a81b6b | 77 | /* Check if the number of registers to read is valid. If not |
tecnosys | 0:7b5d37a81b6b | 78 | * return Modbus illegal data value exception. |
tecnosys | 0:7b5d37a81b6b | 79 | */ |
tecnosys | 0:7b5d37a81b6b | 80 | if( ( usRegCount >= 1 ) |
tecnosys | 0:7b5d37a81b6b | 81 | && ( usRegCount < MB_PDU_FUNC_READ_REGCNT_MAX ) ) |
tecnosys | 0:7b5d37a81b6b | 82 | { |
tecnosys | 0:7b5d37a81b6b | 83 | /* Set the current PDU data pointer to the beginning. */ |
tecnosys | 0:7b5d37a81b6b | 84 | pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF]; |
tecnosys | 0:7b5d37a81b6b | 85 | *usLen = MB_PDU_FUNC_OFF; |
tecnosys | 0:7b5d37a81b6b | 86 | |
tecnosys | 0:7b5d37a81b6b | 87 | /* First byte contains the function code. */ |
tecnosys | 0:7b5d37a81b6b | 88 | *pucFrameCur++ = MB_FUNC_READ_INPUT_REGISTER; |
tecnosys | 0:7b5d37a81b6b | 89 | *usLen += 1; |
tecnosys | 0:7b5d37a81b6b | 90 | |
tecnosys | 0:7b5d37a81b6b | 91 | /* Second byte in the response contain the number of bytes. */ |
tecnosys | 0:7b5d37a81b6b | 92 | *pucFrameCur++ = ( UCHAR )( usRegCount * 2 ); |
tecnosys | 0:7b5d37a81b6b | 93 | *usLen += 1; |
tecnosys | 0:7b5d37a81b6b | 94 | |
tecnosys | 0:7b5d37a81b6b | 95 | eRegStatus = eMBRegInputCB( pucFrameCur, usRegAddress, usRegCount ); |
tecnosys | 0:7b5d37a81b6b | 96 | |
tecnosys | 0:7b5d37a81b6b | 97 | /* If an error occured convert it into a Modbus exception. */ |
tecnosys | 0:7b5d37a81b6b | 98 | if( eRegStatus != MB_ENOERR ) |
tecnosys | 0:7b5d37a81b6b | 99 | { |
tecnosys | 0:7b5d37a81b6b | 100 | eStatus = prveMBError2Exception( eRegStatus ); |
tecnosys | 0:7b5d37a81b6b | 101 | } |
tecnosys | 0:7b5d37a81b6b | 102 | else |
tecnosys | 0:7b5d37a81b6b | 103 | { |
tecnosys | 0:7b5d37a81b6b | 104 | *usLen += usRegCount * 2; |
tecnosys | 0:7b5d37a81b6b | 105 | } |
tecnosys | 0:7b5d37a81b6b | 106 | } |
tecnosys | 0:7b5d37a81b6b | 107 | else |
tecnosys | 0:7b5d37a81b6b | 108 | { |
tecnosys | 0:7b5d37a81b6b | 109 | eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
tecnosys | 0:7b5d37a81b6b | 110 | } |
tecnosys | 0:7b5d37a81b6b | 111 | } |
tecnosys | 0:7b5d37a81b6b | 112 | else |
tecnosys | 0:7b5d37a81b6b | 113 | { |
tecnosys | 0:7b5d37a81b6b | 114 | /* Can't be a valid read input register request because the length |
tecnosys | 0:7b5d37a81b6b | 115 | * is incorrect. */ |
tecnosys | 0:7b5d37a81b6b | 116 | eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
tecnosys | 0:7b5d37a81b6b | 117 | } |
tecnosys | 0:7b5d37a81b6b | 118 | return eStatus; |
tecnosys | 0:7b5d37a81b6b | 119 | } |
tecnosys | 0:7b5d37a81b6b | 120 | |
tecnosys | 0:7b5d37a81b6b | 121 | #endif |