Rajit Singh / Mbed OS Modbus

Dependents:   NUCLEO-F401-printf

Committer:
Rajit Singh
Date:
Wed Aug 16 17:31:26 2017 +0100
Revision:
1:3e360cf155b6
Parent:
0:9db3bed8fffd
Remove main.cpp

Who changed what in which revision?

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