⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 porttcp.c

📁 modbus的例程,非常不错,大家可以
💻 C
📖 第 1 页 / 共 2 页
字号:
                {
                    vMBPortLog( MB_LOG_DEBUG, _T( "TCP-POLL" ), _T( "FD_READ event\r\n" ) );
                }

                /* Process part of the Modbus TCP frame. In case of an I/O error we have to drop
                 * the client connection.
                 */
                if( prvMBTCPGetFrame(  ) != TRUE )
                {
                    prvvMBPortReleaseClient(  );
                }
            }
            else if( xNetworkEvents.lNetworkEvents & FD_CLOSE )
            {
                if( MB_TCP_DEBUG )
                {
                    vMBPortLog( MB_LOG_DEBUG, _T( "TCP-POLL" ), _T( "FD_CLOSE event\r\n" ) );
                }

                prvvMBPortReleaseClient(  );
            }
            else
            {
                vMBPortLog( MB_LOG_WARN, _T( "TCP-POLL" ), _T( "unknown EV_CLIENT event\r\n" ) );
            }
        }
        else
        {
            /* Error - Log a warning. */
        }
    }
    return bOkay;
}

/*!
 * \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
prvMBTCPGetFrame(  )
{
    BOOL            bOkay = TRUE;
    USHORT          usLength;
    int             iRes;
    LPTSTR          szFrameAsStr;

    /* Make sure that we can safely process the next read request. If there
     * is an overflow drop the client.
     */
    if( ( usTCPBufPos + usTCPFrameBytesLeft ) >= MB_TCP_BUF_SIZE )
    {
        vMBPortLog( MB_LOG_WARN, _T( "MBTCP-RCV" ),
                    _T( "Detected buffer overrun. Dropping client.\r\n" ) );
        return FALSE;
    }

    iRes = recv( xClientSocket, &aucTCPBuf[usTCPBufPos], usTCPFrameBytesLeft, 0 );
    switch ( iRes )
    {
    case SOCKET_ERROR:
        vMBPortLog( MB_LOG_WARN, _T( "MBTCP-RCV" ), _T( "recv failed: %s\r\n" ),
                    WsaError2String( WSAGetLastError(  ) ) );
        if( WSAGetLastError(  ) != WSAEWOULDBLOCK )
        {
            bOkay = FALSE;
        }
        break;
    case 0:
        bOkay = FALSE;
        break;
    default:
        usTCPBufPos += iRes;
        usTCPFrameBytesLeft -= iRes;
    }

    /* If we have received the MBAP header we can analyze it and calculate
     * the number of bytes left to complete the current request. If complete
     * notify the protocol stack.
     */
    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 ) )
        {
            if( MB_TCP_DEBUG )
            {
                szFrameAsStr = prvMBTCPPortFrameToString( aucTCPBuf, usTCPBufPos );
                if( szFrameAsStr != NULL )
                {
                    vMBPortLog( MB_LOG_DEBUG, _T( "MBTCP-RCV" ), _T( "Received: %s\r\n" ),
                                szFrameAsStr );
                    free( szFrameAsStr );
                }
            }
            ( void )xMBPortEventPost( EV_FRAME_RECEIVED );
        }
        /* This can not happend because we always calculate the number of bytes
         * to receive. */
        else
        {
            assert( usTCPBufPos <= ( MB_TCP_UID + usLength ) );
        }
    }
    return bOkay;
}

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;
    LPTSTR          szFrameAsStr;

    if( MB_TCP_DEBUG )
    {
        szFrameAsStr = prvMBTCPPortFrameToString( aucTCPBuf, usTCPLength );
        if( szFrameAsStr != NULL )
        {
            vMBPortLog( MB_LOG_DEBUG, _T( "MBTCP-SND" ), _T( "Snd: %s\r\n" ), szFrameAsStr );
            free( szFrameAsStr );
        }
    }

    do
    {
        res = send( xClientSocket, &pucMBTCPFrame[iBytesSent], usTCPLength - iBytesSent, 0 );
        switch ( res )
        {
        case SOCKET_ERROR:
            if( ( WSAGetLastError(  ) == WSAEWOULDBLOCK ) && ( iTimeOut > 0 ) )
            {
                iTimeOut -= MB_TCP_READ_CYCLE;
                Sleep( 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(  )
{
    TCHAR           szIPAddr[32];


    if( prvMBTCPPortAddressToString( xClientSocket, szIPAddr, _countof( szIPAddr ) ) == TRUE )
    {
        vMBPortLog( MB_LOG_INFO, _T( "MBTCP-CMGT" ), _T( "client %s disconnected.\r\n" ),
                    szIPAddr );
    }
    else
    {
        vMBPortLog( MB_LOG_INFO, _T( "MBTCP-CMGT" ), _T( "unknown client disconnected.\r\n" ) );
    }

    /* Disable event notification for this client socket. */
    if( WSAEventSelect( xClientSocket, xEvents[EV_CLIENT], 0 ) == SOCKET_ERROR )
    {
        vMBPortLog( MB_LOG_ERROR, _T( "MBTCP-CMGT" ),
                    _T( "can't disable events for disconnecting client socket: %s\r\n" ),
                    WsaError2String( WSAGetLastError(  ) ) );
    }

    /* Reset event object in case an event was still pending. */
    if( WSAResetEvent( xEvents[EV_CLIENT] ) == SOCKET_ERROR )
    {
        vMBPortLog( MB_LOG_ERROR, _T( "MBTCP-CMGT" ),
                    _T( "can't disable events for disconnecting client socket: %s\r\n" ),
                    WsaError2String( WSAGetLastError(  ) ) );
    }

    /* Disallow the sender side. This tells the other side that we have finished. */
    if( shutdown( xClientSocket, SD_SEND ) == SOCKET_ERROR )
    {
        vMBPortLog( MB_LOG_ERROR, _T( "MBTCP-CMGT" ), _T( "shutdown failed: %s\r\n" ),
                    WsaError2String( WSAGetLastError(  ) ) );
    }

    /* Read any unread data from the socket. Note that this is not the strictly 
     * correct way to do it because our sockets are non blocking and therefore
     * some bytes could remain.
     */
    ( void )recv( xClientSocket, &aucTCPBuf[0], MB_TCP_BUF_SIZE, 0 );

    ( void )closesocket( xClientSocket );
    xClientSocket = INVALID_SOCKET;
}

BOOL
prvbMBPortAcceptClient(  )
{
    SOCKET          xNewSocket;
    BOOL            bOkay;

    TCHAR           szIPAddr[32];

    /* Check if we can handle a new connection. */

    if( xClientSocket != INVALID_SOCKET )
    {
        vMBPortLog( MB_LOG_ERROR, _T( "MBTCP-CMGT" ),
                    _T( "can't accept new client. all connections in use.\r\n" ) );
        bOkay = FALSE;
    }
    else if( ( xNewSocket = accept( xListenSocket, NULL, NULL ) ) == INVALID_SOCKET )
    {
        bOkay = FALSE;
    }
    /* Register READ events on the socket file descriptor. */
    else if( WSAEventSelect( xNewSocket, xEvents[EV_CLIENT], FD_READ | FD_CLOSE ) == SOCKET_ERROR )
    {
        bOkay = FALSE;
        ( void )closesocket( xNewSocket );
    }
    /* Everything okay - Register the client connection. */
    else
    {
        xClientSocket = xNewSocket;
        usTCPBufPos = 0;
        usTCPFrameBytesLeft = MB_TCP_FUNC;

        if( prvMBTCPPortAddressToString( xClientSocket, szIPAddr, _countof( szIPAddr ) ) == TRUE )
        {
            vMBPortLog( MB_LOG_INFO, _T( "MBTCP-CMGT" ), _T( "accepted new client %s.\r\n" ),
                        szIPAddr );
        }
        else
        {
            vMBPortLog( MB_LOG_INFO, _T( "MBTCP-CMGT" ), _T( "accepted unknown client.\r\n" ) );
        }
        bOkay = TRUE;

    }
    return bOkay;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -