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