from Cam Marshall original modbus

Dependencies:   mbed

Fork of Modbus by Cam Marshall

Committer:
stanley1228
Date:
Thu Mar 02 12:08:11 2017 +0000
Revision:
1:7dc3a566a85a
Parent:
mbfuncdisc.cpp@0:0453a0a7e500
from Cam Marshall original modbus

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cam 0:0453a0a7e500 1 /*
cam 0:0453a0a7e500 2 * FreeRTOS Modbus Libary: A Modbus serial implementation for FreeRTOS
cam 0:0453a0a7e500 3 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
cam 0:0453a0a7e500 4 *
cam 0:0453a0a7e500 5 * This library is free software; you can redistribute it and/or
cam 0:0453a0a7e500 6 * modify it under the terms of the GNU Lesser General Public
cam 0:0453a0a7e500 7 * License as published by the Free Software Foundation; either
cam 0:0453a0a7e500 8 * version 2.1 of the License, or (at your option) any later version.
cam 0:0453a0a7e500 9 *
cam 0:0453a0a7e500 10 * This library is distributed in the hope that it will be useful,
cam 0:0453a0a7e500 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cam 0:0453a0a7e500 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cam 0:0453a0a7e500 13 * Lesser General Public License for more details.
cam 0:0453a0a7e500 14 *
cam 0:0453a0a7e500 15 * You should have received a copy of the GNU Lesser General Public
cam 0:0453a0a7e500 16 * License along with this library; if not, write to the Free Software
cam 0:0453a0a7e500 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
cam 0:0453a0a7e500 18 */
cam 0:0453a0a7e500 19
cam 0:0453a0a7e500 20
cam 0:0453a0a7e500 21
cam 0:0453a0a7e500 22 /* ----------------------- System includes ----------------------------------*/
cam 0:0453a0a7e500 23 #include "stdlib.h"
cam 0:0453a0a7e500 24 #include "string.h"
cam 0:0453a0a7e500 25
cam 0:0453a0a7e500 26 /* ----------------------- Platform includes --------------------------------*/
cam 0:0453a0a7e500 27 #include "port.h"
cam 0:0453a0a7e500 28
cam 0:0453a0a7e500 29 /* ----------------------- Modbus includes ----------------------------------*/
cam 0:0453a0a7e500 30 #include "mb.h"
cam 0:0453a0a7e500 31 #include "mbframe.h"
cam 0:0453a0a7e500 32 #include "mbproto.h"
cam 0:0453a0a7e500 33 #include "mbconfig.h"
cam 0:0453a0a7e500 34
cam 0:0453a0a7e500 35 /* ----------------------- Defines ------------------------------------------*/
cam 0:0453a0a7e500 36 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
cam 0:0453a0a7e500 37 #define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
cam 0:0453a0a7e500 38 #define MB_PDU_FUNC_READ_SIZE ( 4 )
cam 0:0453a0a7e500 39 #define MB_PDU_FUNC_READ_DISCCNT_MAX ( 0x07D0 )
cam 0:0453a0a7e500 40
cam 0:0453a0a7e500 41 /* ----------------------- Static functions ---------------------------------*/
cam 0:0453a0a7e500 42 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
cam 0:0453a0a7e500 43
cam 0:0453a0a7e500 44 /* ----------------------- Start implementation -----------------------------*/
cam 0:0453a0a7e500 45
cam 0:0453a0a7e500 46 #if MB_FUNC_READ_COILS_ENABLED > 0
cam 0:0453a0a7e500 47
cam 0:0453a0a7e500 48 eMBException
cam 0:0453a0a7e500 49 eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
cam 0:0453a0a7e500 50 {
cam 0:0453a0a7e500 51 USHORT usRegAddress;
cam 0:0453a0a7e500 52 USHORT usDiscreteCnt;
cam 0:0453a0a7e500 53 UCHAR ucNBytes;
cam 0:0453a0a7e500 54 UCHAR *pucFrameCur;
cam 0:0453a0a7e500 55
cam 0:0453a0a7e500 56 eMBException eStatus = MB_EX_NONE;
cam 0:0453a0a7e500 57 eMBErrorCode eRegStatus;
cam 0:0453a0a7e500 58
cam 0:0453a0a7e500 59 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
cam 0:0453a0a7e500 60 {
cam 0:0453a0a7e500 61 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
cam 0:0453a0a7e500 62 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
cam 0:0453a0a7e500 63 usRegAddress++;
cam 0:0453a0a7e500 64
cam 0:0453a0a7e500 65 usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
cam 0:0453a0a7e500 66 usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
cam 0:0453a0a7e500 67
cam 0:0453a0a7e500 68 /* Check if the number of registers to read is valid. If not
cam 0:0453a0a7e500 69 * return Modbus illegal data value exception.
cam 0:0453a0a7e500 70 */
cam 0:0453a0a7e500 71 if( ( usDiscreteCnt >= 1 ) &&
cam 0:0453a0a7e500 72 ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
cam 0:0453a0a7e500 73 {
cam 0:0453a0a7e500 74 /* Set the current PDU data pointer to the beginning. */
cam 0:0453a0a7e500 75 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
cam 0:0453a0a7e500 76 *usLen = MB_PDU_FUNC_OFF;
cam 0:0453a0a7e500 77
cam 0:0453a0a7e500 78 /* First byte contains the function code. */
cam 0:0453a0a7e500 79 *pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
cam 0:0453a0a7e500 80 *usLen += 1;
cam 0:0453a0a7e500 81
cam 0:0453a0a7e500 82 /* Test if the quantity of coils is a multiple of 8. If not last
cam 0:0453a0a7e500 83 * byte is only partially field with unused coils set to zero. */
cam 0:0453a0a7e500 84 if( ( usDiscreteCnt & 0x0007 ) != 0 )
cam 0:0453a0a7e500 85 {
cam 0:0453a0a7e500 86 ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
cam 0:0453a0a7e500 87 }
cam 0:0453a0a7e500 88 else
cam 0:0453a0a7e500 89 {
cam 0:0453a0a7e500 90 ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
cam 0:0453a0a7e500 91 }
cam 0:0453a0a7e500 92 *pucFrameCur++ = ucNBytes;
cam 0:0453a0a7e500 93 *usLen += 1;
cam 0:0453a0a7e500 94
cam 0:0453a0a7e500 95 eRegStatus =
cam 0:0453a0a7e500 96 eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
cam 0:0453a0a7e500 97
cam 0:0453a0a7e500 98 /* If an error occured convert it into a Modbus exception. */
cam 0:0453a0a7e500 99 if( eRegStatus != MB_ENOERR )
cam 0:0453a0a7e500 100 {
cam 0:0453a0a7e500 101 eStatus = prveMBError2Exception( eRegStatus );
cam 0:0453a0a7e500 102 }
cam 0:0453a0a7e500 103 else
cam 0:0453a0a7e500 104 {
cam 0:0453a0a7e500 105 /* The response contains the function code, the starting address
cam 0:0453a0a7e500 106 * and the quantity of registers. We reuse the old values in the
cam 0:0453a0a7e500 107 * buffer because they are still valid. */
cam 0:0453a0a7e500 108 *usLen += ucNBytes;;
cam 0:0453a0a7e500 109 }
cam 0:0453a0a7e500 110 }
cam 0:0453a0a7e500 111 else
cam 0:0453a0a7e500 112 {
cam 0:0453a0a7e500 113 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
cam 0:0453a0a7e500 114 }
cam 0:0453a0a7e500 115 }
cam 0:0453a0a7e500 116 else
cam 0:0453a0a7e500 117 {
cam 0:0453a0a7e500 118 /* Can't be a valid read coil register request because the length
cam 0:0453a0a7e500 119 * is incorrect. */
cam 0:0453a0a7e500 120 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
cam 0:0453a0a7e500 121 }
cam 0:0453a0a7e500 122 return eStatus;
cam 0:0453a0a7e500 123 }
cam 0:0453a0a7e500 124
cam 0:0453a0a7e500 125 #endif