sw k
/
Modbus
a
Fork of Modbus by
Embed:
(wiki syntax)
Show/hide line numbers
mbfuncinput.cpp
00001 /* 00002 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. 00003 * Copyright (c) 2006 Christian Walter <wolti@sil.at> 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 3. The name of the author may not be used to endorse or promote products 00015 * derived from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * 00028 * File: $Id: mbfuncinput.c,v 1.10 2007/09/12 10:15:56 wolti Exp $ 00029 */ 00030 00031 /* ----------------------- System includes ----------------------------------*/ 00032 #include "stdlib.h" 00033 #include "string.h" 00034 00035 /* ----------------------- Platform includes --------------------------------*/ 00036 #include "port.h" 00037 00038 /* ----------------------- Modbus includes ----------------------------------*/ 00039 #include "mb.h" 00040 #include "mbframe.h" 00041 #include "mbproto.h" 00042 #include "mbconfig.h" 00043 00044 /* ----------------------- Defines ------------------------------------------*/ 00045 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF ) 00046 #define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 ) 00047 #define MB_PDU_FUNC_READ_SIZE ( 4 ) 00048 #define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D ) 00049 00050 #define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF ) 00051 00052 /* ----------------------- Static functions ---------------------------------*/ 00053 eMBException prveMBError2Exception( eMBErrorCode eErrorCode ); 00054 00055 /* ----------------------- Start implementation -----------------------------*/ 00056 #if MB_FUNC_READ_INPUT_ENABLED > 0 00057 00058 eMBException 00059 eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen ) 00060 { 00061 USHORT usRegAddress; 00062 USHORT usRegCount; 00063 UCHAR *pucFrameCur; 00064 00065 eMBException eStatus = MB_EX_NONE; 00066 eMBErrorCode eRegStatus; 00067 00068 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) ) 00069 { 00070 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 ); 00071 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] ); 00072 usRegAddress++; 00073 00074 usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 ); 00075 usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] ); 00076 00077 /* Check if the number of registers to read is valid. If not 00078 * return Modbus illegal data value exception. 00079 */ 00080 if( ( usRegCount >= 1 ) 00081 && ( usRegCount < MB_PDU_FUNC_READ_REGCNT_MAX ) ) 00082 { 00083 /* Set the current PDU data pointer to the beginning. */ 00084 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF]; 00085 *usLen = MB_PDU_FUNC_OFF; 00086 00087 /* First byte contains the function code. */ 00088 *pucFrameCur++ = MB_FUNC_READ_INPUT_REGISTER; 00089 *usLen += 1; 00090 00091 /* Second byte in the response contain the number of bytes. */ 00092 *pucFrameCur++ = ( UCHAR )( usRegCount * 2 ); 00093 *usLen += 1; 00094 00095 eRegStatus = eMBRegInputCB( pucFrameCur, usRegAddress, usRegCount ); 00096 00097 /* If an error occured convert it into a Modbus exception. */ 00098 if( eRegStatus != MB_ENOERR ) 00099 { 00100 eStatus = prveMBError2Exception( eRegStatus ); 00101 } 00102 else 00103 { 00104 *usLen += usRegCount * 2; 00105 } 00106 } 00107 else 00108 { 00109 eStatus = MB_EX_ILLEGAL_DATA_VALUE; 00110 } 00111 } 00112 else 00113 { 00114 /* Can't be a valid read input register request because the length 00115 * is incorrect. */ 00116 eStatus = MB_EX_ILLEGAL_DATA_VALUE; 00117 } 00118 return eStatus; 00119 } 00120 00121 #endif
Generated on Tue Jul 12 2022 23:11:12 by 1.7.2