1 /*
2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
3 * Copyright (c) 2006 Christian Walter <[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: mbfunccoils.c,v 1.8 2007/02/18 23:47:16 wolti 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 "mbframe.h"
41 #include "mbproto.h"
42 #include "mbconfig.h"
43
44 /* ----------------------- Defines ------------------------------------------*/
45 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
46 #define MB_PDU_FUNC_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
47 #define MB_PDU_FUNC_READ_SIZE ( 4 )
48 #define MB_PDU_FUNC_READ_COILCNT_MAX ( 0x07D0 )
49
50 #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
51 #define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
52 #define MB_PDU_FUNC_WRITE_SIZE ( 4 )
53
54 #define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
55 #define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
56 #define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
57 #define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
58 #define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
59 #define MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ( 0x07B0 )
60
61 /* ----------------------- Static functions ---------------------------------*/
62 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
63
64 /* ----------------------- Start implementation -----------------------------*/
65
66 #if MB_FUNC_READ_COILS_ENABLED > 0
67
68 eMBException
eMBFuncReadCoils(UCHAR * pucFrame,USHORT * usLen)69 eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
70 {
71 USHORT usRegAddress;
72 USHORT usCoilCount;
73 UCHAR ucNBytes;
74 UCHAR *pucFrameCur;
75
76 eMBException eStatus = MB_EX_NONE;
77 eMBErrorCode eRegStatus;
78
79 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
80 {
81 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
82 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
83 usRegAddress++;
84
85 usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
86 usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
87
88 /* Check if the number of registers to read is valid. If not
89 * return Modbus illegal data value exception.
90 */
91 if( ( usCoilCount >= 1 ) &&
92 ( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) )
93 {
94 /* Set the current PDU data pointer to the beginning. */
95 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
96 *usLen = MB_PDU_FUNC_OFF;
97
98 /* First byte contains the function code. */
99 *pucFrameCur++ = MB_FUNC_READ_COILS;
100 *usLen += 1;
101
102 /* Test if the quantity of coils is a multiple of 8. If not last
103 * byte is only partially field with unused coils set to zero. */
104 if( ( usCoilCount & 0x0007 ) != 0 )
105 {
106 ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
107 }
108 else
109 {
110 ucNBytes = ( UCHAR )( usCoilCount / 8 );
111 }
112 *pucFrameCur++ = ucNBytes;
113 *usLen += 1;
114
115 eRegStatus =
116 eMBRegCoilsCB( pucFrameCur, usRegAddress, usCoilCount,
117 MB_REG_READ );
118
119 /* If an error occured convert it into a Modbus exception. */
120 if( eRegStatus != MB_ENOERR )
121 {
122 eStatus = prveMBError2Exception( eRegStatus );
123 }
124 else
125 {
126 /* The response contains the function code, the starting address
127 * and the quantity of registers. We reuse the old values in the
128 * buffer because they are still valid. */
129 *usLen += ucNBytes;;
130 }
131 }
132 else
133 {
134 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
135 }
136 }
137 else
138 {
139 /* Can't be a valid read coil register request because the length
140 * is incorrect. */
141 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
142 }
143 return eStatus;
144 }
145 #endif
146
147 #if MB_FUNC_WRITE_COIL_ENABLED > 0
148 eMBException
eMBFuncWriteCoil(UCHAR * pucFrame,USHORT * usLen)149 eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
150 {
151 USHORT usRegAddress;
152 UCHAR ucBuf[2];
153
154 eMBException eStatus = MB_EX_NONE;
155 eMBErrorCode eRegStatus;
156
157 if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
158 {
159 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
160 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
161 usRegAddress++;
162
163 if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
164 ( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
165 ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
166 {
167 ucBuf[1] = 0;
168 if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
169 {
170 ucBuf[0] = 1;
171 }
172 else
173 {
174 ucBuf[0] = 0;
175 }
176 eRegStatus =
177 eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
178
179 /* If an error occured convert it into a Modbus exception. */
180 if( eRegStatus != MB_ENOERR )
181 {
182 eStatus = prveMBError2Exception( eRegStatus );
183 }
184 }
185 else
186 {
187 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
188 }
189 }
190 else
191 {
192 /* Can't be a valid write coil register request because the length
193 * is incorrect. */
194 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
195 }
196 return eStatus;
197 }
198
199 #endif
200
201 #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
202 eMBException
eMBFuncWriteMultipleCoils(UCHAR * pucFrame,USHORT * usLen)203 eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
204 {
205 USHORT usRegAddress;
206 USHORT usCoilCnt;
207 UCHAR ucByteCount;
208 UCHAR ucByteCountVerify;
209
210 eMBException eStatus = MB_EX_NONE;
211 eMBErrorCode eRegStatus;
212
213 if( *usLen > ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
214 {
215 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
216 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
217 usRegAddress++;
218
219 usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
220 usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
221
222 ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
223
224 /* Compute the number of expected bytes in the request. */
225 if( ( usCoilCnt & 0x0007 ) != 0 )
226 {
227 ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
228 }
229 else
230 {
231 ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
232 }
233
234 if( ( usCoilCnt >= 1 ) &&
235 ( usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ) &&
236 ( ucByteCountVerify == ucByteCount ) )
237 {
238 eRegStatus =
239 eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
240 usRegAddress, usCoilCnt, MB_REG_WRITE );
241
242 /* If an error occured convert it into a Modbus exception. */
243 if( eRegStatus != MB_ENOERR )
244 {
245 eStatus = prveMBError2Exception( eRegStatus );
246 }
247 else
248 {
249 /* The response contains the function code, the starting address
250 * and the quantity of registers. We reuse the old values in the
251 * buffer because they are still valid. */
252 *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
253 }
254 }
255 else
256 {
257 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
258 }
259 }
260 else
261 {
262 /* Can't be a valid write coil register request because the length
263 * is incorrect. */
264 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
265 }
266 return eStatus;
267 }
268
269 #endif
270