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