1 /*
2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
3 * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
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_m.h"
42 #include "mbframe.h"
43 #include "mbproto.h"
44 #include "mbconfig.h"
45
46 /* ----------------------- Defines ------------------------------------------*/
47 #define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
48 #define MB_PDU_REQ_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
49 #define MB_PDU_REQ_READ_SIZE ( 4 )
50 #define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 0 )
51 #define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
52 #define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
53
54 /* ----------------------- Static functions ---------------------------------*/
55 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
56
57 /* ----------------------- Start implementation -----------------------------*/
58 #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
59
60 #if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED
61
62 /**
63 * This function will request read discrete inputs.
64 *
65 * @param ucSndAddr salve address
66 * @param usDiscreteAddr discrete start address
67 * @param usNDiscreteIn discrete total number
68 * @param lTimeOut timeout (-1 will waiting forever)
69 *
70 * @return error code
71 */
72 eMBMasterReqErrCode
eMBMasterReqReadDiscreteInputs(UCHAR ucSndAddr,USHORT usDiscreteAddr,USHORT usNDiscreteIn,LONG lTimeOut)73 eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn, 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_DISCRETE_INPUTS;
85 ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8;
86 ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr;
87 ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8;
88 ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn;
89 vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
90 ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
91 eErrStatus = eMBMasterWaitRequestFinish( );
92 }
93 return eErrStatus;
94 }
95
96 eMBException
eMBMasterFuncReadDiscreteInputs(UCHAR * pucFrame,USHORT * usLen)97 eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
98 {
99 USHORT usRegAddress;
100 USHORT usDiscreteCnt;
101 UCHAR ucNBytes;
102 UCHAR *ucMBFrame;
103
104 eMBException eStatus = MB_EX_NONE;
105 eMBErrorCode eRegStatus;
106
107 /* If this request is broadcast, and it's read mode. This request don't need execute. */
108 if ( xMBMasterRequestIsBroadcast() )
109 {
110 eStatus = MB_EX_NONE;
111 }
112 else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
113 {
114 vMBMasterGetPDUSndBuf(&ucMBFrame);
115 usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
116 usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
117 usRegAddress++;
118
119 usDiscreteCnt = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF] << 8 );
120 usDiscreteCnt |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] );
121
122 /* Test if the quantity of coils is a multiple of 8. If not last
123 * byte is only partially field with unused coils set to zero. */
124 if( ( usDiscreteCnt & 0x0007 ) != 0 )
125 {
126 ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 );
127 }
128 else
129 {
130 ucNBytes = ( UCHAR )( usDiscreteCnt / 8 );
131 }
132
133 /* Check if the number of registers to read is valid. If not
134 * return Modbus illegal data value exception.
135 */
136 if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF])
137 {
138 /* Make callback to fill the buffer. */
139 eRegStatus = eMBMasterRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt );
140
141 /* If an error occured convert it into a Modbus exception. */
142 if( eRegStatus != MB_ENOERR )
143 {
144 eStatus = prveMBError2Exception( eRegStatus );
145 }
146 }
147 else
148 {
149 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
150 }
151 }
152 else
153 {
154 /* Can't be a valid read coil register request because the length
155 * is incorrect. */
156 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
157 }
158 return eStatus;
159 }
160
161 #endif
162 #endif // #if MB_SERIAL_MASTER_RTU_ENABLED || MB_SERIAL_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
163