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 * FreeRTOS Modbus Libary: A Modbus serial implementation for FreeRTOS
giryan 0:274eb57e1df3 3 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
giryan 0:274eb57e1df3 4 *
giryan 0:274eb57e1df3 5 * This library is free software; you can redistribute it and/or
giryan 0:274eb57e1df3 6 * modify it under the terms of the GNU Lesser General Public
giryan 0:274eb57e1df3 7 * License as published by the Free Software Foundation; either
giryan 0:274eb57e1df3 8 * version 2.1 of the License, or (at your option) any later version.
giryan 0:274eb57e1df3 9 *
giryan 0:274eb57e1df3 10 * This library is distributed in the hope that it will be useful,
giryan 0:274eb57e1df3 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
giryan 0:274eb57e1df3 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
giryan 0:274eb57e1df3 13 * Lesser General Public License for more details.
giryan 0:274eb57e1df3 14 *
giryan 0:274eb57e1df3 15 * You should have received a copy of the GNU Lesser General Public
giryan 0:274eb57e1df3 16 * License along with this library; if not, write to the Free Software
giryan 0:274eb57e1df3 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
giryan 0:274eb57e1df3 18 */
giryan 0:274eb57e1df3 19
giryan 0:274eb57e1df3 20
giryan 0:274eb57e1df3 21
giryan 0:274eb57e1df3 22 /* ----------------------- System includes ----------------------------------*/
giryan 0:274eb57e1df3 23 #include "stdlib.h"
giryan 0:274eb57e1df3 24 #include "string.h"
giryan 0:274eb57e1df3 25
giryan 0:274eb57e1df3 26 /* ----------------------- Platform includes --------------------------------*/
giryan 0:274eb57e1df3 27 #include "port.h"
giryan 0:274eb57e1df3 28
giryan 0:274eb57e1df3 29 /* ----------------------- Modbus includes ----------------------------------*/
giryan 0:274eb57e1df3 30 #include "mb.h"
giryan 0:274eb57e1df3 31 #include "mbframe.h"
giryan 0:274eb57e1df3 32 #include "mbproto.h"
giryan 0:274eb57e1df3 33 #include "mbconfig.h"
giryan 0:274eb57e1df3 34
giryan 0:274eb57e1df3 35 /* ----------------------- Defines ------------------------------------------*/
giryan 0:274eb57e1df3 36 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
giryan 0:274eb57e1df3 37 #define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
giryan 0:274eb57e1df3 38 #define MB_PDU_FUNC_READ_SIZE ( 4 )
giryan 0:274eb57e1df3 39 #define MB_PDU_FUNC_READ_DISCCNT_MAX ( 0x07D0 )
giryan 0:274eb57e1df3 40
giryan 0:274eb57e1df3 41 /* ----------------------- Static functions ---------------------------------*/
giryan 0:274eb57e1df3 42 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
giryan 0:274eb57e1df3 43
giryan 0:274eb57e1df3 44 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 45
giryan 0:274eb57e1df3 46 #if MB_FUNC_READ_COILS_ENABLED > 0
giryan 0:274eb57e1df3 47
giryan 0:274eb57e1df3 48 eMBException
giryan 0:274eb57e1df3 49 eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
giryan 0:274eb57e1df3 50 {
giryan 0:274eb57e1df3 51 USHORT usRegAddress;
giryan 0:274eb57e1df3 52 USHORT usDiscreteCnt;
giryan 0:274eb57e1df3 53 UCHAR ucNBytes;
giryan 0:274eb57e1df3 54 UCHAR *pucFrameCur;
giryan 0:274eb57e1df3 55
giryan 0:274eb57e1df3 56 eMBException eStatus = MB_EX_NONE;
giryan 0:274eb57e1df3 57 eMBErrorCode eRegStatus;
giryan 0:274eb57e1df3 58
giryan 0:274eb57e1df3 59 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
giryan 0:274eb57e1df3 60 {
giryan 0:274eb57e1df3 61 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
giryan 0:274eb57e1df3 62 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
giryan 0:274eb57e1df3 63 usRegAddress++;
giryan 0:274eb57e1df3 64
giryan 0:274eb57e1df3 65 usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
giryan 0:274eb57e1df3 66 usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
giryan 0:274eb57e1df3 67
giryan 0:274eb57e1df3 68 /* Check if the number of registers to read is valid. If not
giryan 0:274eb57e1df3 69 * return Modbus illegal data value exception.
giryan 0:274eb57e1df3 70 */
giryan 0:274eb57e1df3 71 if( ( usDiscreteCnt >= 1 ) &&
giryan 0:274eb57e1df3 72 ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
giryan 0:274eb57e1df3 73 {
giryan 0:274eb57e1df3 74 /* Set the current PDU data pointer to the beginning. */
giryan 0:274eb57e1df3 75 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
giryan 0:274eb57e1df3 76 *usLen = MB_PDU_FUNC_OFF;
giryan 0:274eb57e1df3 77
giryan 0:274eb57e1df3 78 /* First byte contains the function code. */
giryan 0:274eb57e1df3 79 *pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
giryan 0:274eb57e1df3 80 *usLen += 1;
giryan 0:274eb57e1df3 81
giryan 0:274eb57e1df3 82 /* Test if the quantity of coils is a multiple of 8. If not last
giryan 0:274eb57e1df3 83 * byte is only partially field with unused coils set to zero. */
giryan 0:274eb57e1df3 84 if( ( usDiscreteCnt & 0x0007 ) != 0 )
giryan 0:274eb57e1df3 85 {
giryan 0:274eb57e1df3 86 ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
giryan 0:274eb57e1df3 87 }
giryan 0:274eb57e1df3 88 else
giryan 0:274eb57e1df3 89 {
giryan 0:274eb57e1df3 90 ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
giryan 0:274eb57e1df3 91 }
giryan 0:274eb57e1df3 92 *pucFrameCur++ = ucNBytes;
giryan 0:274eb57e1df3 93 *usLen += 1;
giryan 0:274eb57e1df3 94
giryan 0:274eb57e1df3 95 eRegStatus =
giryan 0:274eb57e1df3 96 eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
giryan 0:274eb57e1df3 97
giryan 0:274eb57e1df3 98 /* If an error occured convert it into a Modbus exception. */
giryan 0:274eb57e1df3 99 if( eRegStatus != MB_ENOERR )
giryan 0:274eb57e1df3 100 {
giryan 0:274eb57e1df3 101 eStatus = prveMBError2Exception( eRegStatus );
giryan 0:274eb57e1df3 102 }
giryan 0:274eb57e1df3 103 else
giryan 0:274eb57e1df3 104 {
giryan 0:274eb57e1df3 105 /* The response contains the function code, the starting address
giryan 0:274eb57e1df3 106 * and the quantity of registers. We reuse the old values in the
giryan 0:274eb57e1df3 107 * buffer because they are still valid. */
giryan 0:274eb57e1df3 108 *usLen += ucNBytes;;
giryan 0:274eb57e1df3 109 }
giryan 0:274eb57e1df3 110 }
giryan 0:274eb57e1df3 111 else
giryan 0:274eb57e1df3 112 {
giryan 0:274eb57e1df3 113 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
giryan 0:274eb57e1df3 114 }
giryan 0:274eb57e1df3 115 }
giryan 0:274eb57e1df3 116 else
giryan 0:274eb57e1df3 117 {
giryan 0:274eb57e1df3 118 /* Can't be a valid read coil register request because the length
giryan 0:274eb57e1df3 119 * is incorrect. */
giryan 0:274eb57e1df3 120 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
giryan 0:274eb57e1df3 121 }
giryan 0:274eb57e1df3 122 return eStatus;
giryan 0:274eb57e1df3 123 }
giryan 0:274eb57e1df3 124
giryan 0:274eb57e1df3 125 #endif