move freemodbus tcp server from control to connetion

This commit is contained in:
Wien.b
2023-10-16 02:34:04 -07:00
parent a247de691c
commit b82bdaea6b
37 changed files with 153 additions and 71 deletions

View File

@@ -14,11 +14,3 @@ if CONTROL_PROTOCOL_MODBUS_UART
source "$APP_DIR/Framework/control/ipc_protocol/modbus_uart/Kconfig"
endif
config CONTROL_PROTOCOL_FREEMODBUS_TCP_SERVER
bool "Using modbus_tcp_server control protocol"
default n
select CONTROL_USING_SOCKET
select LIB_USING_FREEMODBUS
if CONTROL_PROTOCOL_FREEMODBUS_TCP_SERVER
source "$APP_DIR/Framework/control/ipc_protocol/freemodbustcpserver/Kconfig"
endif

View File

@@ -6,9 +6,5 @@ ifeq ($(CONFIG_CONTROL_PROTOCOL_MODBUS_UART), y)
SRC_DIR := modbus_uart
endif
ifeq ($(CONFIG_CONTROL_PROTOCOL_FREEMODBUS_TCP_SERVER), y)
SRC_DIR := freemodbustcpserver
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -1,3 +0,0 @@
SRC_FILES := $(wildcard ./*.c)
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -1,281 +0,0 @@
/*
* FreeModbus Libary: Win32 Demo Application
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* File: $Id$
*/
/**********************************************************
* Linux TCP support.
* Based on Walter's project.
* Modified by Steven Guo <gotop167@163.com>
***********************************************************/
/* ----------------------- Standard C Libs includes --------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
#include "freemodbustcpserver.h"
/* ----------------------- Defines ------------------------------------------*/
#define PROG "freemodbus"
#define REG_INPUT_START 1000
#define REG_INPUT_NREGS 4
#define REG_HOLDING_START 2000
#define REG_HOLDING_NREGS 10
/* ----------------------- Static variables ---------------------------------*/
static USHORT usRegInputStart = REG_INPUT_START;
static USHORT usRegInputBuf[REG_INPUT_NREGS];
static USHORT usRegHoldingStart = REG_HOLDING_START;
static USHORT usRegHoldingBuf[REG_HOLDING_NREGS];
static pthread_mutex_t xLock;
static enum ThreadState
{
STOPPED,
RUNNING,
SHUTDOWN
} ePollThreadState;
/* ----------------------- Static functions ---------------------------------*/
static BOOL bCreatePollingThread( void );
static enum ThreadState eGetPollingThreadState( void );
static void eSetPollingThreadState( enum ThreadState eNewState );
static void* pvPollingThread( void *pvParameter );
/* ----------------------- Start implementation -----------------------------*/
int MBServer()
{
int iExitCode;
CHAR cCh;
BOOL bDoExit;
usRegHoldingBuf[5] = 123;
usRegHoldingBuf[7] = 234;
if( eMBTCPInit( MB_TCP_PORT_USE_DEFAULT ) != MB_ENOERR )
{
fprintf( stderr, "%s: can't initialize modbus stack!\r\n", PROG );
iExitCode = EXIT_FAILURE;
}
else
{
eSetPollingThreadState( STOPPED );
/* CLI interface. */
if( bCreatePollingThread( ) != TRUE )
{
printf( "Can't start protocol stack! Already running?\r\n" );
}
}
printf("%d %d\n",sizeof(usRegHoldingBuf),__LINE__);
while(1)
{
for(int i =0; i<sizeof(usRegHoldingBuf)/2;i++)
{
printf("poll recv is %3d\n", usRegHoldingBuf[i]);
MdelayKTask(100);
}
}
return iExitCode;
}
BOOL bCreatePollingThread( void )
{
BOOL bResult;
pthread_t xThread;
if( eGetPollingThreadState( ) == STOPPED )
{
if( pthread_create( &xThread, NULL, pvPollingThread, NULL ) != 0 )
{
/* Can't create the polling thread. */
bResult = FALSE;
}
else
{
bResult = TRUE;
}
}
else
{
bResult = FALSE;
}
return bResult;
}
void* pvPollingThread( void *pvParameter )
{
eSetPollingThreadState( RUNNING );
if( eMBEnable( ) == MB_ENOERR )
{
do
{
if( eMBPoll( ) != MB_ENOERR )
break;
}
while( eGetPollingThreadState( ) != SHUTDOWN );
}
( void )eMBDisable( );
eSetPollingThreadState( STOPPED );
return 0;
}
enum ThreadState eGetPollingThreadState( )
{
enum ThreadState eCurState;
( void )pthread_mutex_lock( &xLock );
eCurState = ePollThreadState;
( void )pthread_mutex_unlock( &xLock );
return eCurState;
}
void eSetPollingThreadState( enum ThreadState eNewState )
{
( void )pthread_mutex_lock( &xLock );
ePollThreadState = eNewState;
( void )pthread_mutex_unlock( &xLock );
}
eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
{
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex;
if( ( usAddress >= REG_INPUT_START )
&& ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
{
iRegIndex = ( int )( usAddress - usRegInputStart );
while( usNRegs > 0 )
{
*pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
*pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
iRegIndex++;
usNRegs--;
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
{
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex;
if( ( usAddress >= REG_HOLDING_START ) &&
( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
{
iRegIndex = ( int )( usAddress - usRegHoldingStart );
switch ( eMode )
{
/* Pass current register values to the protocol stack. */
case MB_REG_READ:
while( usNRegs > 0 )
{
*pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
*pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] & 0xFF );
iRegIndex++;
usNRegs--;
}
break;
/* Update current register values with new values from the
* protocol stack. */
case MB_REG_WRITE:
while( usNRegs > 0 )
{
usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
iRegIndex++;
usNRegs--;
}
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
{
return MB_ENOREG;
}
eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
return MB_ENOREG;
}
/**
* @description: Modbus Tcp Server Open
* @param control_protocol - control protocol pointer
* @return success : 0 error
*/
int FreeModbusTcpOpen(struct ControlProtocol *control_protocol)
{
ControlProtocolOpenDef(control_protocol);
return 0;
}
static struct ControlDone FreeModbusTcp_protocol_done =
{
._open = FreeModbusTcpOpen,
._close = NULL,
._read = NULL,
._write = NULL,
._ioctl = NULL,
};
void *ReceivePlcDataTask(void *parameter)
{
MBServer();
}
/**
* @description: Modbus Tcp Server Init
* @param p_recipe - recipe pointer
* @return success : 0 error : -1
*/
int FreeModbusTcpServerInit(struct ControlRecipe *p_recipe)
{
p_recipe->done = &FreeModbusTcp_protocol_done;
return 0;
}

View File

@@ -1,6 +0,0 @@
#ifndef FREEMODBUSTCP_H
#define FREEMODBUSTCP_H
#include <control_def.h>
#endif

View File

@@ -1,83 +0,0 @@
/*
* FreeModbus Libary: BSD Socket Library Port
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* File: $Id$
*/
/**********************************************************
* Linux TCP support.
* Based on Walter's project.
* Modified by Steven Guo <gotop167@163.com>
***********************************************************/
#ifndef _PORT_H
#define _PORT_H
#include <assert.h>
#define INLINE
#define PR_BEGIN_EXTERN_C extern "C" {
#define PR_END_EXTERN_C }
#ifdef __cplusplus
PR_BEGIN_EXTERN_C
#endif
/* ----------------------- Defines ------------------------------------------*/
#define ENTER_CRITICAL_SECTION( )
#define EXIT_CRITICAL_SECTION( )
#define MB_PORT_HAS_CLOSE 1
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* ----------------------- Type definitions ---------------------------------*/
typedef int SOCKET;
#define SOCKET_ERROR (-1)
#define INVALID_SOCKET (~0)
typedef char BOOL;
typedef unsigned char UCHAR;
typedef unsigned char BYTE;
typedef char CHAR;
typedef unsigned short USHORT;
typedef short SHORT;
typedef unsigned long ULONG;
typedef long LONG;
typedef enum
{
MB_LOG_DEBUG,
MB_LOG_INFO,
MB_LOG_WARN,
MB_LOG_ERROR
} eMBPortLogLevel;
/* ----------------------- Function prototypes ------------------------------*/
void TcpvMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, const CHAR * szFmt,
... );
#ifdef __cplusplus
PR_END_EXTERN_C
#endif
#endif

View File

@@ -1,72 +0,0 @@
/*
* FreeModbus Libary: Win32 Port
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* File: $Id$
*/
/**********************************************************
* Linux TCP support.
* Based on Walter's project.
* Modified by Steven Guo <gotop167@163.com>
***********************************************************/
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
/* ----------------------- Variables ----------------------------------------*/
static eMBEventType eQueuedEvent;
static BOOL xEventInQueue;
/* ----------------------- Function prototypes ------------------------------*/
BOOL xMBPortTCPPool( void );
/* ----------------------- Start implementation -----------------------------*/
BOOL
xMBPortEventInit( void )
{
xEventInQueue = FALSE;
return TRUE;
}
BOOL
xMBPortEventPost( eMBEventType eEvent )
{
xEventInQueue = TRUE;
eQueuedEvent = eEvent;
return TRUE;
}
BOOL
xMBPortEventGet( eMBEventType * eEvent )
{
BOOL xEventHappened = FALSE;
if( xEventInQueue )
{
*eEvent = eQueuedEvent;
xEventInQueue = FALSE;
xEventHappened = TRUE;
}
else
{
/* We can't do anything with errors from the pooling module. */
( void )xMBPortTCPPool( );
}
return xEventHappened;
}

View File

@@ -1,69 +0,0 @@
/*
* FreeModbus Libary: Win32 Port
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* File: $Id$
*/
/**********************************************************
* Linux TCP support.
* Based on Walter's project.
* Modified by Steven Guo <gotop167@163.com>
***********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "port.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
#include "mbconfig.h"
BOOL
prvMBTCPPortAddressToString( SOCKET xSocket, CHAR * szAddr, USHORT usBufSize )
{
return TRUE;
}
CHAR *
prvMBTCPPortFrameToString( UCHAR * pucFrame, USHORT usFrameLen )
{
return NULL;
}
CHAR *
WsaError2String( int iError )
{
return NULL;
}
void
vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, const CHAR * szFmt, ... )
{
va_list args;
static const CHAR *arszLevel2Str[] = { "DEBUG", "INFO", "WARN", "ERROR" };
fprintf( stderr, "%s: %s: ", arszLevel2Str[eLevel], szModule );
va_start( args, szFmt );
fprintf( stderr, szFmt, args );
va_end( args );
}

View File

@@ -1,367 +0,0 @@
/*
* FreeModbus Libary: Win32 Port
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* File: $Id$
*/
/*
* Design Notes:
*
* The xMBPortTCPInit function allocates a socket and binds the socket to
* all available interfaces ( bind with INADDR_ANY ). In addition it
* creates an array of event objects which is used to check the state of
* the clients. On event object is used to handle new connections or
* closed ones. The other objects are used on a per client basis for
* processing.
*/
/**********************************************************
* Linux TCP support.
* Based on Walter's project.
* Modified by Steven Guo <gotop167@163.com>
***********************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sockets.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include "port.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
/* ----------------------- MBAP Header --------------------------------------*/
#define MB_TCP_UID 6
#define MB_TCP_LEN 4
#define MB_TCP_FUNC 7
/* ----------------------- Defines -----------------------------------------*/
#define MB_TCP_DEFAULT_PORT 502 /* TCP listening port. */
#define MB_TCP_POOL_TIMEOUT 50 /* pool timeout for event waiting. */
#define MB_TCP_READ_TIMEOUT 1000 /* Maximum timeout to wait for packets. */
#define MB_TCP_READ_CYCLE 100 /* Time between checking for new data. */
#define MB_TCP_DEBUG 1 /* Set to 1 for additional debug output. */
#define MB_TCP_BUF_SIZE ( 256 + 7 ) /* Must hold a complete Modbus TCP frame. */
#define EV_CONNECTION 0
#define EV_CLIENT 1
#define EV_NEVENTS EV_CLIENT + 1
/* ----------------------- Static variables ---------------------------------*/
SOCKET xListenSocket;
SOCKET xClientSocket = INVALID_SOCKET;
static fd_set allset;
static UCHAR aucTCPBuf[MB_TCP_BUF_SIZE];
static USHORT usTCPBufPos;
static USHORT usTCPFrameBytesLeft;
/* ----------------------- External functions -------------------------------*/
CHAR *WsaError2String( int dwError );
/* ----------------------- Static functions ---------------------------------*/
BOOL prvMBTCPPortAddressToString( SOCKET xSocket, CHAR * szAddr, USHORT usBufSize );
CHAR *prvMBTCPPortFrameToString( UCHAR * pucFrame, USHORT usFrameLen );
static BOOL prvbMBPortAcceptClient( void );
static void prvvMBPortReleaseClient( void );
/* ----------------------- Begin implementation -----------------------------*/
BOOL
xMBTCPPortInit( USHORT usTCPPort )
{
USHORT usPort;
struct sockaddr_in serveraddr;
if( usTCPPort == 0 )
{
usPort = MB_TCP_DEFAULT_PORT;
}
else
{
usPort = ( USHORT ) usTCPPort;
}
memset( &serveraddr, 0, sizeof( serveraddr ) );
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl( INADDR_ANY );
serveraddr.sin_port = htons( usPort );
if( ( xListenSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == -1 )
{
fprintf( stderr, "Create socket failed.\r\n" );
return FALSE;
}
else if( bind( xListenSocket, ( struct sockaddr * )&serveraddr, sizeof( serveraddr ) ) == -1 )
{
perror("bind");
fprintf( stderr, "Bind socket failed.\r\n" );
return FALSE;
}
else if( listen( xListenSocket, 5 ) == -1 )
{
fprintf( stderr, "Listen socket failed.\r\n" );
return FALSE;
}
FD_ZERO( &allset );
FD_SET( xListenSocket, &allset );
return TRUE;
}
void
vMBTCPPortClose( )
{
// Close all client sockets.
if( xClientSocket != SOCKET_ERROR )
{
prvvMBPortReleaseClient( );
}
// Close the listener socket.
if( xListenSocket != SOCKET_ERROR )
{
close( xListenSocket );
}
}
void
vMBTCPPortDisable( void )
{
/* Close all client sockets. */
if( xClientSocket != SOCKET_ERROR )
{
prvvMBPortReleaseClient( );
}
}
/*! \ingroup port_win32tcp
*
* \brief Pool the listening socket and currently connected Modbus TCP clients
* for new events.
* \internal
*
* This function checks if new clients want to connect or if already connected
* clients are sending requests. If a new client is connected and there are
* still client slots left (The current implementation supports only one)
* then the connection is accepted and an event object for the new client
* socket is activated (See prvbMBPortAcceptClient() ).
* Events for already existing clients in \c FD_READ and \c FD_CLOSE. In case of
* an \c FD_CLOSE the client connection is released (See prvvMBPortReleaseClient() ).
* In case of an \c FD_READ command the existing data is read from the client
* and if a complete frame has been received the Modbus Stack is notified.
*
* \return FALSE in case of an internal I/O error. For example if the internal
* event objects are in an invalid state. Note that this does not include any
* client errors. In all other cases returns TRUE.
*/
BOOL
xMBPortTCPPool( void )
{
int n;
fd_set fread;
struct timeval tval;
tval.tv_sec = 0;
tval.tv_usec = 5000;
int ret;
USHORT usLength;
if( xClientSocket == INVALID_SOCKET )
{
/* Accept to client */
if( ( n = select( xListenSocket + 1, &allset, NULL, NULL, NULL ) ) < 0 )
{
if( errno == EINTR )
{
;
}
else
{
;
}
}
if( FD_ISSET( xListenSocket, &allset ) )
{
( void )prvbMBPortAcceptClient( );
}
}
while( TRUE )
{
FD_ZERO( &fread );
FD_SET( xClientSocket, &fread );
if( ( ( ret = select( xClientSocket + 1, &fread, NULL, NULL, &tval ) ) == SOCKET_ERROR )
|| !ret )
{
continue;
}
if( ret > 0 )
{
if( FD_ISSET( xClientSocket, &fread ) )
{
if( ( ( ret =
recv( xClientSocket, &aucTCPBuf[usTCPBufPos], usTCPFrameBytesLeft,
0 ) ) == SOCKET_ERROR ) || ( !ret ) )
{
close( xClientSocket );
xClientSocket = INVALID_SOCKET;
return TRUE;
}
usTCPBufPos += ret;
usTCPFrameBytesLeft -= ret;
if( usTCPBufPos >= MB_TCP_FUNC )
{
/* Length is a byte count of Modbus PDU (function code + data) and the
* unit identifier. */
usLength = aucTCPBuf[MB_TCP_LEN] << 8U;
usLength |= aucTCPBuf[MB_TCP_LEN + 1];
/* Is the frame already complete. */
if( usTCPBufPos < ( MB_TCP_UID + usLength ) )
{
usTCPFrameBytesLeft = usLength + MB_TCP_UID - usTCPBufPos;
}
/* The frame is complete. */
else if( usTCPBufPos == ( MB_TCP_UID + usLength ) )
{
( void )xMBPortEventPost( EV_FRAME_RECEIVED );
return TRUE;
}
/* This can not happend because we always calculate the number of bytes
* to receive. */
else
{
assert( usTCPBufPos <= ( MB_TCP_UID + usLength ) );
}
}
}
}
}
return TRUE;
}
/*!
* \ingroup port_win32tcp
* \brief Receives parts of a Modbus TCP frame and if complete notifies
* the protocol stack.
* \internal
*
* This function reads a complete Modbus TCP frame from the protocol stack.
* It starts by reading the header with an initial request size for
* usTCPFrameBytesLeft = MB_TCP_FUNC. If the header is complete the
* number of bytes left can be calculated from it (See Length in MBAP header).
* Further read calls are issued until the frame is complete.
*
* \return \c TRUE if part of a Modbus TCP frame could be processed. In case
* of a communication error the function returns \c FALSE.
*/
BOOL
xMBTCPPortGetRequest( UCHAR ** ppucMBTCPFrame, USHORT * usTCPLength )
{
*ppucMBTCPFrame = &aucTCPBuf[0];
*usTCPLength = usTCPBufPos;
/* Reset the buffer. */
usTCPBufPos = 0;
usTCPFrameBytesLeft = MB_TCP_FUNC;
return TRUE;
}
BOOL
xMBTCPPortSendResponse( const UCHAR * pucMBTCPFrame, USHORT usTCPLength )
{
BOOL bFrameSent = FALSE;
BOOL bAbort = FALSE;
int res;
int iBytesSent = 0;
int iTimeOut = MB_TCP_READ_TIMEOUT;
do
{
res = send( xClientSocket, &pucMBTCPFrame[iBytesSent], usTCPLength - iBytesSent, 0 );
switch ( res )
{
case -1:
if( iTimeOut > 0 )
{
iTimeOut -= MB_TCP_READ_CYCLE;
// usleep( MB_TCP_READ_CYCLE );
// PrivTaskDelay( MB_TCP_READ_CYCLE );
}
else
{
bAbort = TRUE;
}
break;
case 0:
prvvMBPortReleaseClient( );
bAbort = TRUE;
break;
default:
iBytesSent += res;
break;
}
}
while( ( iBytesSent != usTCPLength ) && !bAbort );
bFrameSent = iBytesSent == usTCPLength ? TRUE : FALSE;
return bFrameSent;
}
void
prvvMBPortReleaseClient( )
{
( void )recv( xClientSocket, &aucTCPBuf[0], MB_TCP_BUF_SIZE, 0 );
( void )close( xClientSocket );
xClientSocket = INVALID_SOCKET;
}
BOOL
prvbMBPortAcceptClient( )
{
SOCKET xNewSocket;
BOOL bOkay;
/* Check if we can handle a new connection. */
if( xClientSocket != INVALID_SOCKET )
{
fprintf( stderr, "can't accept new client. all connections in use.\n" );
bOkay = FALSE;
}
else if( ( xNewSocket = accept( xListenSocket, NULL, NULL ) ) == INVALID_SOCKET )
{
bOkay = FALSE;
}
else
{
xClientSocket = xNewSocket;
usTCPBufPos = 0;
usTCPFrameBytesLeft = MB_TCP_FUNC;
bOkay = TRUE;
}
return bOkay;
}

View File

@@ -1,13 +0,0 @@
{
"device_id": 1,
"device_name": "freemodbustcp",
"communication_type": 0,
"socket_config": {
"local_ip": "192.168.250.233",
"gateway": "192.168.250.1",
"netmask": "255.255.255.0",
"port": 502
},
"protocol_type": 2,
"read_period": 100
}