modified for nuc472

Dependents:   modbus-over-rs485-sample NTOUEE-mbed-modbus-RTU NuMaker_NuWicam_Lite

Fork of Modbus by Matthew Waddilove

Committer:
giryan
Date:
Sun Aug 22 16:34:20 2010 +0000
Revision:
0:274eb57e1df3

        

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