modified for nuc472

Dependents:   modbus-over-rs485-sample NTOUEE-mbed-modbus-RTU NuMaker_NuWicam_Lite

Fork of Modbus by Matthew Waddilove

Committer:
wclin
Date:
Fri Oct 06 02:54:27 2017 +0000
Revision:
7:be466464a18c
Parent:
0:274eb57e1df3
Support M487 board.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giryan 0:274eb57e1df3 1 /*
giryan 0:274eb57e1df3 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
giryan 0:274eb57e1df3 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
giryan 0:274eb57e1df3 4 * All rights reserved.
giryan 0:274eb57e1df3 5 *
giryan 0:274eb57e1df3 6 * Redistribution and use in source and binary forms, with or without
giryan 0:274eb57e1df3 7 * modification, are permitted provided that the following conditions
giryan 0:274eb57e1df3 8 * are met:
giryan 0:274eb57e1df3 9 * 1. Redistributions of source code must retain the above copyright
giryan 0:274eb57e1df3 10 * notice, this list of conditions and the following disclaimer.
giryan 0:274eb57e1df3 11 * 2. Redistributions in binary form must reproduce the above copyright
giryan 0:274eb57e1df3 12 * notice, this list of conditions and the following disclaimer in the
giryan 0:274eb57e1df3 13 * documentation and/or other materials provided with the distribution.
giryan 0:274eb57e1df3 14 * 3. The name of the author may not be used to endorse or promote products
giryan 0:274eb57e1df3 15 * derived from this software without specific prior written permission.
giryan 0:274eb57e1df3 16 *
giryan 0:274eb57e1df3 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
giryan 0:274eb57e1df3 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
giryan 0:274eb57e1df3 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
giryan 0:274eb57e1df3 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
giryan 0:274eb57e1df3 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
giryan 0:274eb57e1df3 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
giryan 0:274eb57e1df3 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
giryan 0:274eb57e1df3 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
giryan 0:274eb57e1df3 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
giryan 0:274eb57e1df3 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giryan 0:274eb57e1df3 27 *
giryan 0:274eb57e1df3 28 * File: $Id: mbfunccoils.c,v 1.8 2007/02/18 23:47:16 wolti Exp $
giryan 0:274eb57e1df3 29 */
giryan 0:274eb57e1df3 30
giryan 0:274eb57e1df3 31 /* ----------------------- System includes ----------------------------------*/
giryan 0:274eb57e1df3 32 #include "stdlib.h"
giryan 0:274eb57e1df3 33 #include "string.h"
giryan 0:274eb57e1df3 34
giryan 0:274eb57e1df3 35 /* ----------------------- Platform includes --------------------------------*/
giryan 0:274eb57e1df3 36 #include "port.h"
giryan 0:274eb57e1df3 37
giryan 0:274eb57e1df3 38 /* ----------------------- Modbus includes ----------------------------------*/
giryan 0:274eb57e1df3 39 #include "mb.h"
giryan 0:274eb57e1df3 40 #include "mbframe.h"
giryan 0:274eb57e1df3 41 #include "mbproto.h"
giryan 0:274eb57e1df3 42 #include "mbconfig.h"
giryan 0:274eb57e1df3 43
giryan 0:274eb57e1df3 44 /* ----------------------- Defines ------------------------------------------*/
giryan 0:274eb57e1df3 45 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
giryan 0:274eb57e1df3 46 #define MB_PDU_FUNC_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
giryan 0:274eb57e1df3 47 #define MB_PDU_FUNC_READ_SIZE ( 4 )
giryan 0:274eb57e1df3 48 #define MB_PDU_FUNC_READ_COILCNT_MAX ( 0x07D0 )
giryan 0:274eb57e1df3 49
giryan 0:274eb57e1df3 50 #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
giryan 0:274eb57e1df3 51 #define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
giryan 0:274eb57e1df3 52 #define MB_PDU_FUNC_WRITE_SIZE ( 4 )
giryan 0:274eb57e1df3 53
giryan 0:274eb57e1df3 54 #define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
giryan 0:274eb57e1df3 55 #define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
giryan 0:274eb57e1df3 56 #define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
giryan 0:274eb57e1df3 57 #define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
giryan 0:274eb57e1df3 58 #define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
giryan 0:274eb57e1df3 59 #define MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ( 0x07B0 )
giryan 0:274eb57e1df3 60
giryan 0:274eb57e1df3 61 /* ----------------------- Static functions ---------------------------------*/
giryan 0:274eb57e1df3 62 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
giryan 0:274eb57e1df3 63
giryan 0:274eb57e1df3 64 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 65
giryan 0:274eb57e1df3 66 #if MB_FUNC_READ_COILS_ENABLED > 0
giryan 0:274eb57e1df3 67
giryan 0:274eb57e1df3 68 eMBException
giryan 0:274eb57e1df3 69 eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
giryan 0:274eb57e1df3 70 {
giryan 0:274eb57e1df3 71 USHORT usRegAddress;
giryan 0:274eb57e1df3 72 USHORT usCoilCount;
giryan 0:274eb57e1df3 73 UCHAR ucNBytes;
giryan 0:274eb57e1df3 74 UCHAR *pucFrameCur;
giryan 0:274eb57e1df3 75
giryan 0:274eb57e1df3 76 eMBException eStatus = MB_EX_NONE;
giryan 0:274eb57e1df3 77 eMBErrorCode eRegStatus;
giryan 0:274eb57e1df3 78
giryan 0:274eb57e1df3 79 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
giryan 0:274eb57e1df3 80 {
giryan 0:274eb57e1df3 81 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
giryan 0:274eb57e1df3 82 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
giryan 0:274eb57e1df3 83 usRegAddress++;
giryan 0:274eb57e1df3 84
giryan 0:274eb57e1df3 85 usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
giryan 0:274eb57e1df3 86 usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
giryan 0:274eb57e1df3 87
giryan 0:274eb57e1df3 88 /* Check if the number of registers to read is valid. If not
giryan 0:274eb57e1df3 89 * return Modbus illegal data value exception.
giryan 0:274eb57e1df3 90 */
giryan 0:274eb57e1df3 91 if( ( usCoilCount >= 1 ) &&
giryan 0:274eb57e1df3 92 ( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) )
giryan 0:274eb57e1df3 93 {
giryan 0:274eb57e1df3 94 /* Set the current PDU data pointer to the beginning. */
giryan 0:274eb57e1df3 95 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
giryan 0:274eb57e1df3 96 *usLen = MB_PDU_FUNC_OFF;
giryan 0:274eb57e1df3 97
giryan 0:274eb57e1df3 98 /* First byte contains the function code. */
giryan 0:274eb57e1df3 99 *pucFrameCur++ = MB_FUNC_READ_COILS;
giryan 0:274eb57e1df3 100 *usLen += 1;
giryan 0:274eb57e1df3 101
giryan 0:274eb57e1df3 102 /* Test if the quantity of coils is a multiple of 8. If not last
giryan 0:274eb57e1df3 103 * byte is only partially field with unused coils set to zero. */
giryan 0:274eb57e1df3 104 if( ( usCoilCount & 0x0007 ) != 0 )
giryan 0:274eb57e1df3 105 {
giryan 0:274eb57e1df3 106 ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
giryan 0:274eb57e1df3 107 }
giryan 0:274eb57e1df3 108 else
giryan 0:274eb57e1df3 109 {
giryan 0:274eb57e1df3 110 ucNBytes = ( UCHAR )( usCoilCount / 8 );
giryan 0:274eb57e1df3 111 }
giryan 0:274eb57e1df3 112 *pucFrameCur++ = ucNBytes;
giryan 0:274eb57e1df3 113 *usLen += 1;
giryan 0:274eb57e1df3 114
giryan 0:274eb57e1df3 115 eRegStatus =
giryan 0:274eb57e1df3 116 eMBRegCoilsCB( pucFrameCur, usRegAddress, usCoilCount,
giryan 0:274eb57e1df3 117 MB_REG_READ );
giryan 0:274eb57e1df3 118
giryan 0:274eb57e1df3 119 /* If an error occured convert it into a Modbus exception. */
giryan 0:274eb57e1df3 120 if( eRegStatus != MB_ENOERR )
giryan 0:274eb57e1df3 121 {
giryan 0:274eb57e1df3 122 eStatus = prveMBError2Exception( eRegStatus );
giryan 0:274eb57e1df3 123 }
giryan 0:274eb57e1df3 124 else
giryan 0:274eb57e1df3 125 {
giryan 0:274eb57e1df3 126 /* The response contains the function code, the starting address
giryan 0:274eb57e1df3 127 * and the quantity of registers. We reuse the old values in the
giryan 0:274eb57e1df3 128 * buffer because they are still valid. */
giryan 0:274eb57e1df3 129 *usLen += ucNBytes;;
giryan 0:274eb57e1df3 130 }
giryan 0:274eb57e1df3 131 }
giryan 0:274eb57e1df3 132 else
giryan 0:274eb57e1df3 133 {
giryan 0:274eb57e1df3 134 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
giryan 0:274eb57e1df3 135 }
giryan 0:274eb57e1df3 136 }
giryan 0:274eb57e1df3 137 else
giryan 0:274eb57e1df3 138 {
giryan 0:274eb57e1df3 139 /* Can't be a valid read coil register request because the length
giryan 0:274eb57e1df3 140 * is incorrect. */
giryan 0:274eb57e1df3 141 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
giryan 0:274eb57e1df3 142 }
giryan 0:274eb57e1df3 143 return eStatus;
giryan 0:274eb57e1df3 144 }
giryan 0:274eb57e1df3 145
giryan 0:274eb57e1df3 146 #if MB_FUNC_WRITE_COIL_ENABLED > 0
giryan 0:274eb57e1df3 147 eMBException
giryan 0:274eb57e1df3 148 eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
giryan 0:274eb57e1df3 149 {
giryan 0:274eb57e1df3 150 USHORT usRegAddress;
giryan 0:274eb57e1df3 151 UCHAR ucBuf[2];
giryan 0:274eb57e1df3 152
giryan 0:274eb57e1df3 153 eMBException eStatus = MB_EX_NONE;
giryan 0:274eb57e1df3 154 eMBErrorCode eRegStatus;
giryan 0:274eb57e1df3 155
giryan 0:274eb57e1df3 156 if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
giryan 0:274eb57e1df3 157 {
giryan 0:274eb57e1df3 158 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
giryan 0:274eb57e1df3 159 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
giryan 0:274eb57e1df3 160 usRegAddress++;
giryan 0:274eb57e1df3 161
giryan 0:274eb57e1df3 162 if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
giryan 0:274eb57e1df3 163 ( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
giryan 0:274eb57e1df3 164 ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
giryan 0:274eb57e1df3 165 {
giryan 0:274eb57e1df3 166 ucBuf[1] = 0;
giryan 0:274eb57e1df3 167 if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
giryan 0:274eb57e1df3 168 {
giryan 0:274eb57e1df3 169 ucBuf[0] = 1;
giryan 0:274eb57e1df3 170 }
giryan 0:274eb57e1df3 171 else
giryan 0:274eb57e1df3 172 {
giryan 0:274eb57e1df3 173 ucBuf[0] = 0;
giryan 0:274eb57e1df3 174 }
giryan 0:274eb57e1df3 175 eRegStatus =
giryan 0:274eb57e1df3 176 eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
giryan 0:274eb57e1df3 177
giryan 0:274eb57e1df3 178 /* If an error occured convert it into a Modbus exception. */
giryan 0:274eb57e1df3 179 if( eRegStatus != MB_ENOERR )
giryan 0:274eb57e1df3 180 {
giryan 0:274eb57e1df3 181 eStatus = prveMBError2Exception( eRegStatus );
giryan 0:274eb57e1df3 182 }
giryan 0:274eb57e1df3 183 }
giryan 0:274eb57e1df3 184 else
giryan 0:274eb57e1df3 185 {
giryan 0:274eb57e1df3 186 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
giryan 0:274eb57e1df3 187 }
giryan 0:274eb57e1df3 188 }
giryan 0:274eb57e1df3 189 else
giryan 0:274eb57e1df3 190 {
giryan 0:274eb57e1df3 191 /* Can't be a valid write coil register request because the length
giryan 0:274eb57e1df3 192 * is incorrect. */
giryan 0:274eb57e1df3 193 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
giryan 0:274eb57e1df3 194 }
giryan 0:274eb57e1df3 195 return eStatus;
giryan 0:274eb57e1df3 196 }
giryan 0:274eb57e1df3 197
giryan 0:274eb57e1df3 198 #endif
giryan 0:274eb57e1df3 199
giryan 0:274eb57e1df3 200 #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
giryan 0:274eb57e1df3 201 eMBException
giryan 0:274eb57e1df3 202 eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
giryan 0:274eb57e1df3 203 {
giryan 0:274eb57e1df3 204 USHORT usRegAddress;
giryan 0:274eb57e1df3 205 USHORT usCoilCnt;
giryan 0:274eb57e1df3 206 UCHAR ucByteCount;
giryan 0:274eb57e1df3 207 UCHAR ucByteCountVerify;
giryan 0:274eb57e1df3 208
giryan 0:274eb57e1df3 209 eMBException eStatus = MB_EX_NONE;
giryan 0:274eb57e1df3 210 eMBErrorCode eRegStatus;
giryan 0:274eb57e1df3 211
giryan 0:274eb57e1df3 212 if( *usLen > ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
giryan 0:274eb57e1df3 213 {
giryan 0:274eb57e1df3 214 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
giryan 0:274eb57e1df3 215 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
giryan 0:274eb57e1df3 216 usRegAddress++;
giryan 0:274eb57e1df3 217
giryan 0:274eb57e1df3 218 usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
giryan 0:274eb57e1df3 219 usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
giryan 0:274eb57e1df3 220
giryan 0:274eb57e1df3 221 ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
giryan 0:274eb57e1df3 222
giryan 0:274eb57e1df3 223 /* Compute the number of expected bytes in the request. */
giryan 0:274eb57e1df3 224 if( ( usCoilCnt & 0x0007 ) != 0 )
giryan 0:274eb57e1df3 225 {
giryan 0:274eb57e1df3 226 ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
giryan 0:274eb57e1df3 227 }
giryan 0:274eb57e1df3 228 else
giryan 0:274eb57e1df3 229 {
giryan 0:274eb57e1df3 230 ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
giryan 0:274eb57e1df3 231 }
giryan 0:274eb57e1df3 232
giryan 0:274eb57e1df3 233 if( ( usCoilCnt >= 1 ) &&
giryan 0:274eb57e1df3 234 ( usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ) &&
giryan 0:274eb57e1df3 235 ( ucByteCountVerify == ucByteCount ) )
giryan 0:274eb57e1df3 236 {
giryan 0:274eb57e1df3 237 eRegStatus =
giryan 0:274eb57e1df3 238 eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
giryan 0:274eb57e1df3 239 usRegAddress, usCoilCnt, MB_REG_WRITE );
giryan 0:274eb57e1df3 240
giryan 0:274eb57e1df3 241 /* If an error occured convert it into a Modbus exception. */
giryan 0:274eb57e1df3 242 if( eRegStatus != MB_ENOERR )
giryan 0:274eb57e1df3 243 {
giryan 0:274eb57e1df3 244 eStatus = prveMBError2Exception( eRegStatus );
giryan 0:274eb57e1df3 245 }
giryan 0:274eb57e1df3 246 else
giryan 0:274eb57e1df3 247 {
giryan 0:274eb57e1df3 248 /* The response contains the function code, the starting address
giryan 0:274eb57e1df3 249 * and the quantity of registers. We reuse the old values in the
giryan 0:274eb57e1df3 250 * buffer because they are still valid. */
giryan 0:274eb57e1df3 251 *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
giryan 0:274eb57e1df3 252 }
giryan 0:274eb57e1df3 253 }
giryan 0:274eb57e1df3 254 else
giryan 0:274eb57e1df3 255 {
giryan 0:274eb57e1df3 256 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
giryan 0:274eb57e1df3 257 }
giryan 0:274eb57e1df3 258 }
giryan 0:274eb57e1df3 259 else
giryan 0:274eb57e1df3 260 {
giryan 0:274eb57e1df3 261 /* Can't be a valid write coil register request because the length
giryan 0:274eb57e1df3 262 * is incorrect. */
giryan 0:274eb57e1df3 263 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
giryan 0:274eb57e1df3 264 }
giryan 0:274eb57e1df3 265 return eStatus;
giryan 0:274eb57e1df3 266 }
giryan 0:274eb57e1df3 267
giryan 0:274eb57e1df3 268 #endif
giryan 0:274eb57e1df3 269
giryan 0:274eb57e1df3 270 #endif