API串口操作封裝類

字號(hào):

Comm.h頭文件:
    #ifndef __CCOMM_H__
    #define __CCOMM_H__
    class CComm //串口操作封裝類
    {
    private:
    HANDLE m_hComm;
    public:
    CComm(); //構(gòu)造函數(shù)
    ~CComm(); //析構(gòu)函數(shù)
    BOOL OpenComm(int nComm); //打開串口函數(shù)
    void CloseComm(); //關(guān)閉串口函數(shù)
    BOOL SetComm(int nBaudRate/*波特率*/,int nParity/*奇偶校驗(yàn)位*/,int nByteSize/*字節(jié)位數(shù)*/,int nStopBits/*停止位*/); //設(shè)置串口函數(shù)
    BOOL SetTimeOuts(); //設(shè)置超時(shí)函數(shù)
    BOOL ReadComm(char * lpBuf,int nLen); //讀串口函數(shù)
    BOOL WriteComm(char * lpBuf,int nLen); //寫串口函數(shù)
    };
    #endif
    Comm.cpp實(shí)現(xiàn)文件:
    #include "StdAfx.h"
    #include "Comm.h"
    //構(gòu)造函數(shù)
    CComm::CComm()
    {
    }
    //析構(gòu)函數(shù)
    CComm::~CComm()
    {
    CloseComm(); //關(guān)閉串口
    }
    //打開串口函數(shù)
    BOOL CComm::OpenComm(int nComm)
    {
    CString strCommName;
    CString strErrInfo;
    strCommName.Format("COM%d",nComm);
    m_hComm = ::CreateFile(strCommName, /*要打開串口名稱*/
    GENERIC_READ | GENERIC_WRITE, /*允許讀和寫*/
    0, /*獨(dú)占方式*/
    NULL, /*安全屬性*/
    OPEN_EXISTING, /*打開而不是創(chuàng)建*/
    0, /*同步方式*/
    NULL); /*模板句柄*/
    if(m_hComm == INVALID_HANDLE_VALUE)
    {
    strErrInfo.Format("打開%s失敗!",strCommName);
    AfxMessageBox(strErrInfo);
    return FALSE;
    }
    else
    {
    return TRUE;
    }
    }
    //關(guān)閉串口函數(shù)
    void CComm::CloseComm()
    {
    if(m_hComm != INVALID_HANDLE_VALUE)
    {
    ::CloseHandle(m_hComm);
    m_hComm = INVALID_HANDLE_VALUE;
    }
    }
    //設(shè)置串口函數(shù)
    BOOL CComm::SetComm(int nBaudRate/*波特率*/,int nParity/*奇偶校驗(yàn)位*/,int nByteSize/*字節(jié)位數(shù)*/,int nStopBits/*停止位*/)
    {
    DCB stDCB;
    memset(&stDCB,0,sizeof(stDCB));
    if(!::GetCommState(m_hComm,&stDCB)) //獲取串口當(dāng)前狀態(tài)屬性
    return FALSE;
    stDCB.BaudRate = nBaudRate; //波特率
    stDCB.fParity = 0;
    stDCB.Parity = nParity; //奇偶校驗(yàn)位(NOPARITY等)
    stDCB.ByteSize = nByteSize; //每個(gè)字節(jié)有8位
    stDCB.StopBits = nStopBits; //停止位(ONESBIT等)
    if(!::SetCommState(m_hComm,&stDCB)) //設(shè)置串口狀態(tài)屬性
    return FALSE;
    if(!::SetupComm(m_hComm,1024,1024)) //設(shè)置輸入緩沖區(qū)和輸出緩沖區(qū)的大小
    return FALSE;
    ::PurgeComm(m_hComm,PURGE_TXCLEAR | PURGE_RXCLEAR); //清空輸入輸出緩沖區(qū)
    return TRUE;
    }
    //Examda提示: 設(shè)置超時(shí)函數(shù)
    BOOL CComm::SetTimeOuts()
    {
    COMMTIMEOUTS stTimeOuts;
    stTimeOuts.ReadIntervalTimeout = 0; //設(shè)定讀超時(shí)
    stTimeOuts.ReadTotalTimeoutMultiplier = 100;
    stTimeOuts.ReadTotalTimeoutConstant = 500;
    stTimeOuts.WriteTotalTimeoutMultiplier = 100; //設(shè)定寫超時(shí)
    stTimeOuts.WriteTotalTimeoutConstant = 500;
    ::SetCommTimeouts(m_hComm,&stTimeOuts); //設(shè)置超時(shí)
    ::PurgeComm(m_hComm,PURGE_TXCLEAR | PURGE_RXCLEAR); //清空輸入輸出緩沖區(qū)
    return TRUE;
    }
    //讀串口函數(shù)
    BOOL CComm::ReadComm(char * lpBuf,int nLen)
    {
    if(::ReadFile(m_hComm,lpBuf,nLen,(DWORD *)&nLen,NULL) == FALSE)
    {
    return FALSE;
    }
    else
    {
    /*
    CString str;
    str.Format("%d",nLen);
    AfxMessageBox(str);
    */
    //::PurgeComm(m_hComm,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
    return TRUE;
    }
    }
    //寫串口函數(shù)
    BOOL CComm::WriteComm(char * lpBuf,int nLen)
    {
    if(::WriteFile(m_hComm,lpBuf,nLen,(DWORD *)&nLen,NULL) == FALSE)
    {
    return FALSE;
    }
    else
    {
    /*
    CString str;
    str.Format("%d",nLen);
    AfxMessageBox(str);
    */
    return TRUE;
    }
    }