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