Modbus RTU/ASCII/TCP with lwip TCP working partial, but with errors (retransmitions)
Dependencies: EthernetNetIf mbed
ModBus/mbtcp.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 | * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. |
tmav123 | 0:f54e9507171b | 3 | * Copyright (c) 2006 Christian Walter <wolti@sil.at> |
tmav123 | 0:f54e9507171b | 4 | * All rights reserved. |
tmav123 | 0:f54e9507171b | 5 | * |
tmav123 | 0:f54e9507171b | 6 | * Redistribution and use in source and binary forms, with or without |
tmav123 | 0:f54e9507171b | 7 | * modification, are permitted provided that the following conditions |
tmav123 | 0:f54e9507171b | 8 | * are met: |
tmav123 | 0:f54e9507171b | 9 | * 1. Redistributions of source code must retain the above copyright |
tmav123 | 0:f54e9507171b | 10 | * notice, this list of conditions and the following disclaimer. |
tmav123 | 0:f54e9507171b | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
tmav123 | 0:f54e9507171b | 12 | * notice, this list of conditions and the following disclaimer in the |
tmav123 | 0:f54e9507171b | 13 | * documentation and/or other materials provided with the distribution. |
tmav123 | 0:f54e9507171b | 14 | * 3. The name of the author may not be used to endorse or promote products |
tmav123 | 0:f54e9507171b | 15 | * derived from this software without specific prior written permission. |
tmav123 | 0:f54e9507171b | 16 | * |
tmav123 | 0:f54e9507171b | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
tmav123 | 0:f54e9507171b | 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
tmav123 | 0:f54e9507171b | 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
tmav123 | 0:f54e9507171b | 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
tmav123 | 0:f54e9507171b | 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
tmav123 | 0:f54e9507171b | 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
tmav123 | 0:f54e9507171b | 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
tmav123 | 0:f54e9507171b | 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
tmav123 | 0:f54e9507171b | 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
tmav123 | 0:f54e9507171b | 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
tmav123 | 0:f54e9507171b | 27 | * |
tmav123 | 0:f54e9507171b | 28 | * File: $Id: mbtcp.c,v 1.3 2006/12/07 22:10:34 wolti Exp $ |
tmav123 | 0:f54e9507171b | 29 | */ |
tmav123 | 0:f54e9507171b | 30 | |
tmav123 | 0:f54e9507171b | 31 | /* ----------------------- System includes ----------------------------------*/ |
tmav123 | 0:f54e9507171b | 32 | #include "stdlib.h" |
tmav123 | 0:f54e9507171b | 33 | #include "string.h" |
tmav123 | 0:f54e9507171b | 34 | |
tmav123 | 0:f54e9507171b | 35 | /* ----------------------- Platform includes --------------------------------*/ |
tmav123 | 0:f54e9507171b | 36 | #include "port.h" |
tmav123 | 0:f54e9507171b | 37 | |
tmav123 | 0:f54e9507171b | 38 | /* ----------------------- Modbus includes ----------------------------------*/ |
tmav123 | 0:f54e9507171b | 39 | #include "mb.h" |
tmav123 | 0:f54e9507171b | 40 | #include "mbconfig.h" |
tmav123 | 0:f54e9507171b | 41 | #include "mbtcp.h" |
tmav123 | 0:f54e9507171b | 42 | #include "mbframe.h" |
tmav123 | 0:f54e9507171b | 43 | #include "mbport.h" |
tmav123 | 0:f54e9507171b | 44 | |
tmav123 | 0:f54e9507171b | 45 | #if MB_TCP_ENABLED > 0 |
tmav123 | 0:f54e9507171b | 46 | |
tmav123 | 0:f54e9507171b | 47 | /* ----------------------- Defines ------------------------------------------*/ |
tmav123 | 0:f54e9507171b | 48 | |
tmav123 | 0:f54e9507171b | 49 | /* ----------------------- MBAP Header --------------------------------------*/ |
tmav123 | 0:f54e9507171b | 50 | /* |
tmav123 | 0:f54e9507171b | 51 | * |
tmav123 | 0:f54e9507171b | 52 | * <------------------------ MODBUS TCP/IP ADU(1) -------------------------> |
tmav123 | 0:f54e9507171b | 53 | * <----------- MODBUS PDU (1') ----------------> |
tmav123 | 0:f54e9507171b | 54 | * +-----------+---------------+------------------------------------------+ |
tmav123 | 0:f54e9507171b | 55 | * | TID | PID | Length | UID |Code | Data | |
tmav123 | 0:f54e9507171b | 56 | * +-----------+---------------+------------------------------------------+ |
tmav123 | 0:f54e9507171b | 57 | * | | | | | |
tmav123 | 0:f54e9507171b | 58 | * (2) (3) (4) (5) (6) |
tmav123 | 0:f54e9507171b | 59 | * |
tmav123 | 0:f54e9507171b | 60 | * (2) ... MB_TCP_TID = 0 (Transaction Identifier - 2 Byte) |
tmav123 | 0:f54e9507171b | 61 | * (3) ... MB_TCP_PID = 2 (Protocol Identifier - 2 Byte) |
tmav123 | 0:f54e9507171b | 62 | * (4) ... MB_TCP_LEN = 4 (Number of bytes - 2 Byte) |
tmav123 | 0:f54e9507171b | 63 | * (5) ... MB_TCP_UID = 6 (Unit Identifier - 1 Byte) |
tmav123 | 0:f54e9507171b | 64 | * (6) ... MB_TCP_FUNC = 7 (Modbus Function Code) |
tmav123 | 0:f54e9507171b | 65 | * |
tmav123 | 0:f54e9507171b | 66 | * (1) ... Modbus TCP/IP Application Data Unit |
tmav123 | 0:f54e9507171b | 67 | * (1') ... Modbus Protocol Data Unit |
tmav123 | 0:f54e9507171b | 68 | */ |
tmav123 | 0:f54e9507171b | 69 | |
tmav123 | 0:f54e9507171b | 70 | #define MB_TCP_TID 0 |
tmav123 | 0:f54e9507171b | 71 | #define MB_TCP_PID 2 |
tmav123 | 0:f54e9507171b | 72 | #define MB_TCP_LEN 4 |
tmav123 | 0:f54e9507171b | 73 | #define MB_TCP_UID 6 |
tmav123 | 0:f54e9507171b | 74 | #define MB_TCP_FUNC 7 |
tmav123 | 0:f54e9507171b | 75 | |
tmav123 | 0:f54e9507171b | 76 | #define MB_TCP_PROTOCOL_ID 0 /* 0 = Modbus Protocol */ |
tmav123 | 0:f54e9507171b | 77 | |
tmav123 | 0:f54e9507171b | 78 | |
tmav123 | 0:f54e9507171b | 79 | /* ----------------------- Start implementation -----------------------------*/ |
tmav123 | 0:f54e9507171b | 80 | eMBErrorCode |
tmav123 | 0:f54e9507171b | 81 | eMBTCPDoInit( USHORT ucTCPPort ) |
tmav123 | 0:f54e9507171b | 82 | { |
tmav123 | 0:f54e9507171b | 83 | eMBErrorCode eStatus = MB_ENOERR; |
tmav123 | 0:f54e9507171b | 84 | |
tmav123 | 0:f54e9507171b | 85 | if( xMBTCPPortInit( ucTCPPort ) == FALSE ) |
tmav123 | 0:f54e9507171b | 86 | { |
tmav123 | 0:f54e9507171b | 87 | eStatus = MB_EPORTERR; |
tmav123 | 0:f54e9507171b | 88 | } |
tmav123 | 0:f54e9507171b | 89 | return eStatus; |
tmav123 | 0:f54e9507171b | 90 | } |
tmav123 | 0:f54e9507171b | 91 | |
tmav123 | 0:f54e9507171b | 92 | void |
tmav123 | 0:f54e9507171b | 93 | eMBTCPStart( void ) |
tmav123 | 0:f54e9507171b | 94 | { |
tmav123 | 0:f54e9507171b | 95 | } |
tmav123 | 0:f54e9507171b | 96 | |
tmav123 | 0:f54e9507171b | 97 | void |
tmav123 | 0:f54e9507171b | 98 | eMBTCPStop( void ) |
tmav123 | 0:f54e9507171b | 99 | { |
tmav123 | 0:f54e9507171b | 100 | /* Make sure that no more clients are connected. */ |
tmav123 | 0:f54e9507171b | 101 | vMBTCPPortDisable( ); |
tmav123 | 0:f54e9507171b | 102 | } |
tmav123 | 0:f54e9507171b | 103 | |
tmav123 | 0:f54e9507171b | 104 | eMBErrorCode |
tmav123 | 0:f54e9507171b | 105 | eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength ) |
tmav123 | 0:f54e9507171b | 106 | { |
tmav123 | 0:f54e9507171b | 107 | eMBErrorCode eStatus = MB_EIO; |
tmav123 | 0:f54e9507171b | 108 | UCHAR *pucMBTCPFrame; |
tmav123 | 0:f54e9507171b | 109 | USHORT usLength; |
tmav123 | 0:f54e9507171b | 110 | USHORT usPID; |
tmav123 | 0:f54e9507171b | 111 | |
tmav123 | 0:f54e9507171b | 112 | if( xMBTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != FALSE ) |
tmav123 | 0:f54e9507171b | 113 | { |
tmav123 | 0:f54e9507171b | 114 | usPID = pucMBTCPFrame[MB_TCP_PID] << 8U; |
tmav123 | 0:f54e9507171b | 115 | usPID |= pucMBTCPFrame[MB_TCP_PID + 1]; |
tmav123 | 0:f54e9507171b | 116 | |
tmav123 | 0:f54e9507171b | 117 | if( usPID == MB_TCP_PROTOCOL_ID ) |
tmav123 | 0:f54e9507171b | 118 | { |
tmav123 | 0:f54e9507171b | 119 | *ppucFrame = &pucMBTCPFrame[MB_TCP_FUNC]; |
tmav123 | 0:f54e9507171b | 120 | *pusLength = usLength - MB_TCP_FUNC; |
tmav123 | 0:f54e9507171b | 121 | eStatus = MB_ENOERR; |
tmav123 | 0:f54e9507171b | 122 | |
tmav123 | 0:f54e9507171b | 123 | /* Modbus TCP does not use any addresses. Fake the source address such |
tmav123 | 0:f54e9507171b | 124 | * that the processing part deals with this frame. |
tmav123 | 0:f54e9507171b | 125 | */ |
tmav123 | 0:f54e9507171b | 126 | *pucRcvAddress = MB_TCP_PSEUDO_ADDRESS; |
tmav123 | 0:f54e9507171b | 127 | } |
tmav123 | 0:f54e9507171b | 128 | } |
tmav123 | 0:f54e9507171b | 129 | else |
tmav123 | 0:f54e9507171b | 130 | { |
tmav123 | 0:f54e9507171b | 131 | eStatus = MB_EIO; |
tmav123 | 0:f54e9507171b | 132 | } |
tmav123 | 0:f54e9507171b | 133 | return eStatus; |
tmav123 | 0:f54e9507171b | 134 | } |
tmav123 | 0:f54e9507171b | 135 | |
tmav123 | 0:f54e9507171b | 136 | eMBErrorCode |
tmav123 | 0:f54e9507171b | 137 | eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength ) |
tmav123 | 0:f54e9507171b | 138 | { |
tmav123 | 0:f54e9507171b | 139 | eMBErrorCode eStatus = MB_ENOERR; |
tmav123 | 0:f54e9507171b | 140 | UCHAR *pucMBTCPFrame = ( UCHAR * ) pucFrame - MB_TCP_FUNC; |
tmav123 | 0:f54e9507171b | 141 | USHORT usTCPLength = usLength + MB_TCP_FUNC; |
tmav123 | 0:f54e9507171b | 142 | |
tmav123 | 0:f54e9507171b | 143 | /* The MBAP header is already initialized because the caller calls this |
tmav123 | 0:f54e9507171b | 144 | * function with the buffer returned by the previous call. Therefore we |
tmav123 | 0:f54e9507171b | 145 | * only have to update the length in the header. Note that the length |
tmav123 | 0:f54e9507171b | 146 | * header includes the size of the Modbus PDU and the UID Byte. Therefore |
tmav123 | 0:f54e9507171b | 147 | * the length is usLength plus one. |
tmav123 | 0:f54e9507171b | 148 | */ |
tmav123 | 0:f54e9507171b | 149 | pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U; |
tmav123 | 0:f54e9507171b | 150 | pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF; |
tmav123 | 0:f54e9507171b | 151 | if( xMBTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == FALSE ) |
tmav123 | 0:f54e9507171b | 152 | { |
tmav123 | 0:f54e9507171b | 153 | eStatus = MB_EIO; |
tmav123 | 0:f54e9507171b | 154 | } |
tmav123 | 0:f54e9507171b | 155 | return eStatus; |
tmav123 | 0:f54e9507171b | 156 | } |
tmav123 | 0:f54e9507171b | 157 | |
tmav123 | 0:f54e9507171b | 158 | #endif |