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: mbfuncinput_m.c,v 1.60 2013/10/12 14:23:40 Armink Add Master Functions Exp $
29 */
30
31 /* ----------------------- System includes ----------------------------------*/
32 #include "stdlib.h"
33 #include "string.h"
34
35 /* ----------------------- Platform includes --------------------------------*/
36 #include "port.h"
37
38 /* ----------------------- Modbus includes ----------------------------------*/
39 #include "mb.h"
40 #include "mb_m.h"
41 #include "mbframe.h"
42 #include "mbproto.h"
43 #include "mbconfig.h"
44
45 /* ----------------------- Defines ------------------------------------------*/
46 #define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
47 #define MB_PDU_REQ_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
48 #define MB_PDU_REQ_READ_SIZE ( 4 )
49 #define MB_PDU_FUNC_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
50 #define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
51 #define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
52
53 #define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF )
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_INPUT_ENABLED > 0
61
62 /**
63 * This function will request read input register.
64 *
65 * @param ucSndAddr salve address
66 * @param usRegAddr register start address
67 * @param usNRegs register total number
68 * @param lTimeOut timeout (-1 will waiting forever)
69 *
70 * @return error code
71 */
72 eMBMasterReqErrCode
eMBMasterReqReadInputRegister(UCHAR ucSndAddr,USHORT usRegAddr,USHORT usNRegs,LONG lTimeOut)73 eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut )
74 {
75 UCHAR *ucMBFrame;
76 eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
77
78 if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
79 else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
80 else
81 {
82 vMBMasterGetPDUSndBuf(&ucMBFrame);
83 vMBMasterSetDestAddress(ucSndAddr);
84 ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER;
85 ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
86 ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
87 ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
88 ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
89 vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
90 ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
91 eErrStatus = eMBMasterWaitRequestFinish( );
92 }
93 return eErrStatus;
94 }
95
96 eMBException
eMBMasterFuncReadInputRegister(UCHAR * pucFrame,USHORT * usLen)97 eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
98 {
99 UCHAR *ucMBFrame;
100 USHORT usRegAddress;
101 USHORT usRegCount;
102
103 eMBException eStatus = MB_EX_NONE;
104 eMBErrorCode eRegStatus;
105
106 /* If this request is broadcast, and it's read mode. This request don't need execute. */
107 if ( xMBMasterRequestIsBroadcast() )
108 {
109 eStatus = MB_EX_NONE;
110 }
111 else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
112 {
113 vMBMasterGetPDUSndBuf(&ucMBFrame);
114 usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
115 usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
116 usRegAddress++;
117
118 usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] << 8 );
119 usRegCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] );
120
121 /* Check if the number of registers to read is valid. If not
122 * return Modbus illegal data value exception.
123 */
124 if( ( usRegCount >= 1 ) && ( 2 * usRegCount == pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] ) )
125 {
126 /* Make callback to fill the buffer. */
127 eRegStatus = eMBMasterRegInputCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usRegCount );
128 /* If an error occured convert it into a Modbus exception. */
129 if( eRegStatus != MB_ENOERR )
130 {
131 eStatus = prveMBError2Exception( eRegStatus );
132 }
133 }
134 else
135 {
136 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
137 }
138 }
139 else
140 {
141 /* Can't be a valid request because the length is incorrect. */
142 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
143 }
144 return eStatus;
145 }
146
147 #endif
148 #endif
149