Windows串口通信同步与异步方式的比较

同步方式无法同时读写串口,甚至有读或写操作正在进行时关闭串口也会遇到阻塞。

#include <iostream>
#include <windows.h>
#include <assert.h>

using namespace std;

HANDLE g_hCom = INVALID_HANDLE_VALUE;
const unsigned MAX_BUF_LEN = 32;
const char* const g_pData = "hello";

DWORD WINAPI ThreadRead( LPVOID lpParameter )
{
    char szBuf[ MAX_BUF_LEN ] = { 0 };
    DWORD dwRead;
    if( !ReadFile( g_hCom, szBuf, strlen( g_pData ), &dwRead, NULL ) )
    {
        cout << "Read com failed." << endl;
        return 0;
    }
    if( dwRead != strlen( g_pData ) )
    {
        cout << "Failed to get all the data." << endl;
        return 0;
    }
    cout << "Read: " << szBuf << endl;
    return 1;
}

int main()
{
    g_hCom = CreateFile( "com1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
    assert( g_hCom != INVALID_HANDLE_VALUE );

    HANDLE hThread = CreateThread( NULL, 0, ThreadRead, NULL, 0, NULL );
    assert( hThread );

    Sleep( 100 );

    DWORD dwWritten;
    if( !WriteFile( g_hCom, g_pData, strlen( g_pData ), &dwWritten, NULL ) )
    {
        cout << "Write com failed." << endl;
        return 0;
    }
    if( dwWritten != strlen( g_pData ) )
    {
        cout << "Failed to write all the data." << endl;
        return 0;
    }
    cout << "Write: " << g_pData << endl;
    return 1;
}

 

使用异步方式则可以实现自发自收的功能。

#include <iostream>
#include <windows.h>
#include <assert.h>

using namespace std;

HANDLE g_hCom = INVALID_HANDLE_VALUE;
const unsigned MAX_BUF_LEN = 32;
const char* const g_pData = "hello";

DWORD WINAPI ThreadRead( LPVOID lpParameter )
{
    char szBuf[ MAX_BUF_LEN ] = { 0 };
    DWORD dwRead;
    OVERLAPPED ov;
    memset( &ov, 0, sizeof( ov ) );
    ov.hEvent = CreateEvent( NULL, TRUE, TRUE, NULL );
    assert( ov.hEvent );

    if( !ReadFile( g_hCom, szBuf, strlen( g_pData ), &dwRead, &ov ) )
    {
        if( GetLastError() != ERROR_IO_PENDING )
        {
            cout << "Read com failed." << endl;
            return 0;
        }
        WaitForSingleObject( ov.hEvent, INFINITE );
        GetOverlappedResult( g_hCom, &ov, &dwRead, TRUE );
    }
    if( dwRead != strlen( g_pData ) )
    {
        cout << "Failed to get all the data." << endl;
        return 0;
    }
    cout << "Read: " << szBuf << endl;
    return 1;
}

int main()
{
    g_hCom = CreateFile( "com1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL );
    assert( g_hCom != INVALID_HANDLE_VALUE );

    HANDLE hThread = CreateThread( NULL, 0, ThreadRead, NULL, 0, NULL );
    assert( hThread );

    Sleep( 100 );

    DWORD dwWritten;
    OVERLAPPED ov;
    memset( &ov, 0, sizeof( ov ) );
    ov.hEvent = CreateEvent( NULL, TRUE, TRUE, NULL );
    assert( ov.hEvent );

    if( !WriteFile( g_hCom, g_pData, strlen( g_pData ), &dwWritten, &ov ) )
    {
        if( GetLastError() != ERROR_IO_PENDING )
        {
            cout << "Write com failed." << endl;
            return 0;
        }
        WaitForSingleObject( ov.hEvent, INFINITE );
        GetOverlappedResult( g_hCom, &ov, &dwWritten, TRUE );
    }
    if( dwWritten != strlen( g_pData ) )
    {
        cout << "Failed to write all the data." << endl;
        return 0;
    }
    cout << "Write: " << g_pData << endl;

    Sleep( 100 );
    return 1;
}

你可能感兴趣的:(C++,同步,异步,串口通信,Windows编程)