Programme Nucleo RS485 - Modbus TCP/IP (ACP 40)

Fork of Modbus by Cam Marshall

Committer:
TomTom83
Date:
Fri Jul 06 11:45:57 2018 +0000
Revision:
2:b6ae32d99b4a
Parent:
1:390d9808cdde
Programme RS485 - Modbus TCP/IP (ACP40)

Who changed what in which revision?

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