2012-07-21 15:35:35 +02:00
|
|
|
/*
|
|
|
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
|
|
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* File: $Id: mbtcp.c,v 1.3 2006/12/07 22:10:34 wolti Exp $
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------------- System includes ----------------------------------*/
|
2012-07-21 16:56:21 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
/* ----------------------- Platform includes --------------------------------*/
|
|
|
|
#include "port.h"
|
|
|
|
|
|
|
|
/* ----------------------- Modbus includes ----------------------------------*/
|
2012-07-21 18:18:16 +02:00
|
|
|
#include <apps/modbusmb.h>
|
|
|
|
#include <apps/modbusmbframe.h>
|
|
|
|
#include <apps/modbusmbport.h>
|
|
|
|
|
2012-07-21 15:35:35 +02:00
|
|
|
#include "mbtcp.h"
|
|
|
|
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_TCP_ENABLED
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
/* ----------------------- Defines ------------------------------------------*/
|
|
|
|
|
|
|
|
/* ----------------------- MBAP Header --------------------------------------*/
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* <------------------------ MODBUS TCP/IP ADU(1) ------------------------->
|
|
|
|
* <----------- MODBUS PDU (1') ---------------->
|
|
|
|
* +-----------+---------------+------------------------------------------+
|
|
|
|
* | TID | PID | Length | UID |Code | Data |
|
|
|
|
* +-----------+---------------+------------------------------------------+
|
|
|
|
* | | | | |
|
|
|
|
* (2) (3) (4) (5) (6)
|
|
|
|
*
|
|
|
|
* (2) ... MB_TCP_TID = 0 (Transaction Identifier - 2 Byte)
|
|
|
|
* (3) ... MB_TCP_PID = 2 (Protocol Identifier - 2 Byte)
|
|
|
|
* (4) ... MB_TCP_LEN = 4 (Number of bytes - 2 Byte)
|
|
|
|
* (5) ... MB_TCP_UID = 6 (Unit Identifier - 1 Byte)
|
|
|
|
* (6) ... MB_TCP_FUNC = 7 (Modbus Function Code)
|
|
|
|
*
|
|
|
|
* (1) ... Modbus TCP/IP Application Data Unit
|
|
|
|
* (1') ... Modbus Protocol Data Unit
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MB_TCP_TID 0
|
|
|
|
#define MB_TCP_PID 2
|
|
|
|
#define MB_TCP_LEN 4
|
|
|
|
#define MB_TCP_UID 6
|
|
|
|
#define MB_TCP_FUNC 7
|
|
|
|
|
|
|
|
#define MB_TCP_PROTOCOL_ID 0 /* 0 = Modbus Protocol */
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------- Start implementation -----------------------------*/
|
|
|
|
eMBErrorCode
|
2012-07-21 23:23:18 +02:00
|
|
|
eMBTCPDoInit( uint16_t ucTCPPort )
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
if( xMBTCPPortInit( ucTCPPort ) == false )
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
|
|
|
eStatus = MB_EPORTERR;
|
|
|
|
}
|
|
|
|
return eStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
eMBTCPStart( void )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
eMBTCPStop( void )
|
|
|
|
{
|
|
|
|
/* Make sure that no more clients are connected. */
|
|
|
|
vMBTCPPortDisable( );
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode
|
2012-07-21 23:23:18 +02:00
|
|
|
eMBTCPReceive( uint8_t * pucRcvAddress, uint8_t ** ppucFrame, uint16_t * pusLength )
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
|
|
|
eMBErrorCode eStatus = MB_EIO;
|
2012-07-21 23:23:18 +02:00
|
|
|
uint8_t *pucMBTCPFrame;
|
|
|
|
uint16_t usLength;
|
|
|
|
uint16_t usPID;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
if( xMBTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != false )
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
|
|
|
usPID = pucMBTCPFrame[MB_TCP_PID] << 8U;
|
|
|
|
usPID |= pucMBTCPFrame[MB_TCP_PID + 1];
|
|
|
|
|
|
|
|
if( usPID == MB_TCP_PROTOCOL_ID )
|
|
|
|
{
|
|
|
|
*ppucFrame = &pucMBTCPFrame[MB_TCP_FUNC];
|
|
|
|
*pusLength = usLength - MB_TCP_FUNC;
|
|
|
|
eStatus = MB_ENOERR;
|
|
|
|
|
|
|
|
/* Modbus TCP does not use any addresses. Fake the source address such
|
|
|
|
* that the processing part deals with this frame.
|
|
|
|
*/
|
|
|
|
*pucRcvAddress = MB_TCP_PSEUDO_ADDRESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eStatus = MB_EIO;
|
|
|
|
}
|
|
|
|
return eStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode
|
2012-07-21 23:23:18 +02:00
|
|
|
eMBTCPSend( uint8_t _unused, const uint8_t * pucFrame, uint16_t usLength )
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
2012-07-21 23:23:18 +02:00
|
|
|
uint8_t *pucMBTCPFrame = ( uint8_t * ) pucFrame - MB_TCP_FUNC;
|
|
|
|
uint16_t usTCPLength = usLength + MB_TCP_FUNC;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
/* The MBAP header is already initialized because the caller calls this
|
|
|
|
* function with the buffer returned by the previous call. Therefore we
|
|
|
|
* only have to update the length in the header. Note that the length
|
|
|
|
* header includes the size of the Modbus PDU and the UID Byte. Therefore
|
|
|
|
* the length is usLength plus one.
|
|
|
|
*/
|
|
|
|
pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U;
|
|
|
|
pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF;
|
2012-07-21 23:23:18 +02:00
|
|
|
if( xMBTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == false )
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
|
|
|
eStatus = MB_EIO;
|
|
|
|
}
|
|
|
|
return eStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|