串口类

头文件

// Serial.h



//////////////////////////////////////////////////////////////////////
//类名:  Serial
//功能:  串口类操作
//编写:  王帅宇
//时间:  09.10.20
//修改:
//====================================================================//
#undef UNICODE

#ifndef __SERIAL_H__
#define __SERIAL_H__

#define FC_DTRDSR       0x01
#define FC_RTSCTS       0x02
#define FC_XONXOFF      0x04
#define ASCII_BEL       0x07
#define ASCII_BS        0x08
#define ASCII_LF        0x0A
#define ASCII_CR        0x0D
#define ASCII_XON       0x11
#define ASCII_XOFF      0x13

class CSerial
{

public:
    CSerial();
    ~CSerial();

    BOOL Open( int nPort = 2, int nBaud = 9600 );
    BOOL Close( void );

    int ReadData( void *, int );
    int SendData( const char *, int );
    int ReadDataWaiting( void );

    VOID Flush( void ) {     // flush the port
    PurgeComm(m_hIDComDev, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
    }
    BOOL IsOpened( void ){ return( m_bOpened ); }

protected:
    BOOL WriteCommByte( unsigned char );

    HANDLE m_hIDComDev;
    OVERLAPPED m_OverlappedRead, m_OverlappedWrite;
    BOOL m_bOpened;

};

#endif

cpp文件

// Serial.cpp


#include "stdafx.h"
#include "Serial.h"
#include "stdio.h"



CSerial::CSerial()
{

    memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
     memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
    m_hIDComDev = NULL;
    m_bOpened = FALSE;

}


CSerial::~CSerial()
{

    Close();

}
BOOL CSerial::Open( int nPort, int nBaud )
{

    if( m_bOpened ) return( TRUE );

    char szPort[15];
    char szComParams[50];
    DCB dcb;

    sprintf( szPort,  "COM%d", nPort );
    m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
    if( m_hIDComDev == NULL ) return( FALSE );

    memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
    memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

    COMMTIMEOUTS CommTimeOuts;
    CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
    CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
    CommTimeOuts.ReadTotalTimeoutConstant = 0;
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
    CommTimeOuts.WriteTotalTimeoutConstant = 5000;
    SetCommTimeouts( m_hIDComDev, &CommTimeOuts );

    wsprintf( szComParams, "COM%d:%d,n,8,1", nPort, nBaud );

    m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
    m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

    dcb.DCBlength = sizeof( DCB );
    GetCommState( m_hIDComDev, &dcb );
    dcb.BaudRate = nBaud;
    dcb.ByteSize = 8;
    unsigned char ucSet;
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
    if( !SetCommState( m_hIDComDev, &dcb ) ||
        !SetupComm( m_hIDComDev, 10000, 10000 ) ||
        m_OverlappedRead.hEvent == NULL ||
        m_OverlappedWrite.hEvent == NULL ){
            DWORD dwError = GetLastError();
            if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
            if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
            CloseHandle( m_hIDComDev );
            return( FALSE );
    }

    m_bOpened = TRUE;

    return( m_bOpened );

}
/*
BOOL CSerial::Open( int nPort, int nBaud )
{

    if( m_bOpened ) return( TRUE );

    //char szPort[15];
    //char szComParams[50];
    TCHAR szPort[10];
    TCHAR szComParams[50];

    //sprintf_s(szPort, "COM%d", nPort );
    
    DCB dcb;

    wsprintf( szPort, _T("COM%d"), nPort );
    m_hIDComDev = CreateFile(szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
    if( m_hIDComDev == NULL ) return( FALSE );

    memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
     memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

    COMMTIMEOUTS CommTimeOuts;
    CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
    CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
    CommTimeOuts.ReadTotalTimeoutConstant = 0;
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
    CommTimeOuts.WriteTotalTimeoutConstant = 5000;
    SetCommTimeouts( m_hIDComDev, &CommTimeOuts );

    wsprintf( szComParams, _T("COM%d:%d,n,8,1"), nPort, nBaud );

    m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
    m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

    dcb.DCBlength = sizeof( DCB );
    if(GetCommState( m_hIDComDev, &dcb ) == FALSE)
    {
        DWORD dwError = GetLastError();
        if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
        if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
        CloseHandle( m_hIDComDev );
        return( FALSE );
    }
    dcb.BaudRate = nBaud;
    dcb.ByteSize = 8;
    unsigned char ucSet;
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
    if( !SetCommState( m_hIDComDev, &dcb ) ||
        !SetupComm( m_hIDComDev, 10000, 10000 ) ||
        m_OverlappedRead.hEvent == NULL ||
        m_OverlappedWrite.hEvent == NULL )
    {
        DWORD dwError = GetLastError();
        if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
        if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
        CloseHandle( m_hIDComDev );
        return( FALSE );
    }

    m_bOpened = TRUE;

    return( m_bOpened );

}*/

BOOL CSerial::Close( void )
{

    if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE );

    if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
    if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
    CloseHandle( m_hIDComDev );
    m_bOpened = FALSE;
    m_hIDComDev = NULL;

    return( TRUE );

}

BOOL CSerial::WriteCommByte( unsigned char ucByte )
{
    BOOL bWriteStat;
    DWORD dwBytesWritten;

    bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten, &m_OverlappedWrite );
    if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
        if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) ) dwBytesWritten = 0;
        else{
            GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite, &dwBytesWritten, FALSE );
            m_OverlappedWrite.Offset += dwBytesWritten;
            }
        }

    return( TRUE );

}

int CSerial::SendData( const char *buffer, int size )
{

    if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

    DWORD dwBytesWritten = 0;
    int i;
    for( i=0; i<size; i++ ){
        WriteCommByte( buffer[i] );
        dwBytesWritten++;
        }

    return( (int) dwBytesWritten );

}

int CSerial::ReadDataWaiting( void )
{

    if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

    DWORD dwErrorFlags;
    COMSTAT ComStat;

    ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );

    return( (int) ComStat.cbInQue );

}

int CSerial::ReadData( void *buffer, int limit )
{

    if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

    BOOL bReadStatus;
    DWORD dwBytesRead, dwErrorFlags;
    COMSTAT ComStat;

    ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
    if( !ComStat.cbInQue ) return( 0 );

    dwBytesRead = (DWORD) ComStat.cbInQue;
    if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;

    ResetEvent(m_OverlappedRead.hEvent);
    bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );
            
    if( !bReadStatus ){
        if( GetLastError() == ERROR_IO_PENDING ){

            WaitForSingleObject( m_OverlappedRead.hEvent, 2000000 );
            return( (int) dwBytesRead );
            }
        return( 0 );
        }

    return( (int) dwBytesRead );

}

操作:

//quzhixun 增加开机读取MATREADY机制 
    pItemName = _T("Power ON...");
    MTRACE(m_hDebugTrace, "<%d>CNbIoTThread::ProductionLineWorkflow():Power ON ...",m_nThreadID);
    pItemValue = RUNNING;
    UpdateTaskListItem(pItemName, pItemValue);
    iFindCount = 0;
    MTRACE(m_hDebugTrace, "<%d>CNbIoTThread::ProductionLineWorkflow:Open(): m_comPort = %s",m_nThreadID, m_comPort);
    
    strComTmp1 = m_comPort;
    strComTmp2 = strComTmp1.substr(3);
    iComTmp = atoi(strComTmp2.c_str());
    if(iComTmp <= 0)
    {
        pItemValue = FAIL;
        UpdateTaskListItem(pItemName, pItemValue);
        result = 1;
        MTRACE(m_hDebugTrace, "<%d>CNbIoTThread::ProductionLineWorkflow():Com%d.is not allow", m_nThreadID,iComTmp);
        sprintf_s(errMsg, "Com%d.is not allow",iComTmp);
        goto End;
    }

    if(mSerial.Open(iComTmp,115200) != TRUE)
    {
        pItemValue = FAIL;
        UpdateTaskListItem(pItemName, pItemValue);
        result = 1;
        MTRACE(m_hDebugTrace, "<%d>CNbIoTThread::ProductionLineWorkflow():Open COM Fail.", m_nThreadID);
        sprintf_s(errMsg, "Power ON Fail.");
        goto End;
    }
    while(true && iFindCount < 6000)
    {
        memset(buffer,0x00,512);
        mSerial.ReadData(buffer,512);
        strTmp = buffer;
        MTRACE(m_hDebugTrace, "<%d>CNbIoTThread::ProductionLineWorkflow:Time:%d,ReadData Buffer:%s:",m_nThreadID, iFindCount,strTmp);
        found = strTmp.find("MATREADY");
        if(found!=std::string::npos)
        {
            break;
        }
        iFindCount++;
        ::Sleep(10);
    }
    mSerial.Close();
    if(iFindCount == 6000)
    {
        pItemValue = FAIL;
        UpdateTaskListItem(pItemName, pItemValue);
        result = 1;
        MTRACE(m_hDebugTrace, "<%d>CNbIoTThread::ProductionLineWorkflow():Power ON Fail.", m_nThreadID);
        sprintf_s(errMsg, "Power ON Fail.");
        goto End;
    }
    pItemValue = PASS;
    UpdateTaskListItem(pItemName, pItemValue);
    //~增加开机读取MATREADY机制 

 

posted @ 2018-09-30 07:16  quzhixun  阅读(372)  评论(0编辑  收藏  举报