|
| 1 | +/* |
| 2 | + * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. |
| 3 | + * Copyright (C) 2013 Armink <[email protected]> |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * 1. Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * 3. The name of the author may not be used to endorse or promote products |
| 15 | + * derived from this software without specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * |
| 28 | + * File: $Id: mbfuncdisc_m.c,v 1.60 2013/10/15 8:48:20 Armink Add Master Functions Exp $ |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +/* ----------------------- System includes ----------------------------------*/ |
| 34 | +#include "stdlib.h" |
| 35 | +#include "string.h" |
| 36 | + |
| 37 | +/* ----------------------- Platform includes --------------------------------*/ |
| 38 | +#include "port.h" |
| 39 | + |
| 40 | +/* ----------------------- Modbus includes ----------------------------------*/ |
| 41 | +#include "mb.h" |
| 42 | +#include "mb_m.h" |
| 43 | +#include "mbframe.h" |
| 44 | +#include "mbproto.h" |
| 45 | +#include "mbconfig.h" |
| 46 | + |
| 47 | +/* ----------------------- Defines ------------------------------------------*/ |
| 48 | +#define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 ) |
| 49 | +#define MB_PDU_REQ_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 ) |
| 50 | +#define MB_PDU_REQ_READ_SIZE ( 4 ) |
| 51 | +#define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 0 ) |
| 52 | +#define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 ) |
| 53 | +#define MB_PDU_FUNC_READ_SIZE_MIN ( 1 ) |
| 54 | + |
| 55 | +/* ----------------------- Static functions ---------------------------------*/ |
| 56 | +eMBException prveMBError2Exception( eMBErrorCode eErrorCode ); |
| 57 | + |
| 58 | +/* ----------------------- Start implementation -----------------------------*/ |
| 59 | +#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 |
| 60 | +#if MB_FUNC_READ_COILS_ENABLED > 0 |
| 61 | + |
| 62 | +eMBMasterReqErrCode |
| 63 | +eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn ) |
| 64 | +{ |
| 65 | + UCHAR *ucMBFrame; |
| 66 | + eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; |
| 67 | + |
| 68 | + if ( xMBMasterGetIsBusy() ) eErrStatus = MB_MRE_MASTER_BUSY; |
| 69 | + else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG; |
| 70 | + else |
| 71 | + { |
| 72 | + vMBMasterGetPDUSndBuf(&ucMBFrame); |
| 73 | + vMBMasterSetDestAddress(ucSndAddr); |
| 74 | + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS; |
| 75 | + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8; |
| 76 | + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr; |
| 77 | + ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8; |
| 78 | + ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn; |
| 79 | + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); |
| 80 | + ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); |
| 81 | + } |
| 82 | + return eErrStatus; |
| 83 | +} |
| 84 | + |
| 85 | +eMBException |
| 86 | +eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen ) |
| 87 | +{ |
| 88 | + USHORT usRegAddress; |
| 89 | + USHORT usDiscreteCnt; |
| 90 | + UCHAR ucNBytes; |
| 91 | + UCHAR *ucMBFrame; |
| 92 | + |
| 93 | + eMBException eStatus = MB_EX_NONE; |
| 94 | + eMBErrorCode eRegStatus; |
| 95 | + |
| 96 | + if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) |
| 97 | + { |
| 98 | + vMBMasterGetPDUSndBuf(&ucMBFrame); |
| 99 | + usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); |
| 100 | + usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); |
| 101 | + usRegAddress++; |
| 102 | + |
| 103 | + usDiscreteCnt = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF] << 8 ); |
| 104 | + usDiscreteCnt |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] ); |
| 105 | + |
| 106 | + /* Test if the quantity of coils is a multiple of 8. If not last |
| 107 | + * byte is only partially field with unused coils set to zero. */ |
| 108 | + if( ( usDiscreteCnt & 0x0007 ) != 0 ) |
| 109 | + { |
| 110 | + ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 ); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + ucNBytes = ( UCHAR )( usDiscreteCnt / 8 ); |
| 115 | + } |
| 116 | + |
| 117 | + /* Check if the number of registers to read is valid. If not |
| 118 | + * return Modbus illegal data value exception. |
| 119 | + */ |
| 120 | + if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF]) |
| 121 | + { |
| 122 | + /* Make callback to fill the buffer. */ |
| 123 | + eRegStatus = eMBRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt ); |
| 124 | + |
| 125 | + /* If an error occured convert it into a Modbus exception. */ |
| 126 | + if( eRegStatus != MB_ENOERR ) |
| 127 | + { |
| 128 | + eStatus = prveMBError2Exception( eRegStatus ); |
| 129 | + } |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
| 134 | + } |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + /* Can't be a valid read coil register request because the length |
| 139 | + * is incorrect. */ |
| 140 | + eStatus = MB_EX_ILLEGAL_DATA_VALUE; |
| 141 | + } |
| 142 | + return eStatus; |
| 143 | +} |
| 144 | + |
| 145 | +#endif |
| 146 | +#endif |
0 commit comments