wcl_0308 发表于 2014-12-2 08:00:12

VC++ 对串口操作之串口类

本帖最后由 wcl_0308 于 2014-12-2 08:50 编辑

命名为
//////////////////////////////////////////////////////////////////////
OpComm.h
#if !defined(AFX_OPCOMM_H__82138A00_2098_44B0_8F5F_C96B6BDB8880__INCLUDED_)
#define AFX_OPCOMM_H__82138A00_2098_44B0_8F5F_C96B6BDB8880__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class COpComm
{
public:
        // Read the data from the receive buffer of the COM port
        BOOL ReadBuffer(unsigned char* pInBuffer, DWORD dwBufferSize);
       
        // write the data to the transmit buffer of the COM port
        BOOL WriteBuffer(unsigned char* pOutBuffer, DWORD dwBufferSize);

        // Close the COM port
        VOID ClosePort();

        // Configure the COM Port upon the input parameter,
        // Open the COM Port.
        BOOL OpenPort(LPTSTR strPortName,         // COM port index
                        DWORD dwBaudRate = 115200,    // Baud Rate:115200
                                  BYTE bByteSize = 8,         // 8 data bits
                                  BYTE bParity =NOPARITY,   // no parity
                                  BYTE bStopBits = ONESTOPBIT); // one stop bit
        COpComm();

        virtual ~COpComm();

protected:
        HANDLE        m_hCommPort; // COM port handler

};

#endif // !defined(AFX_OPCOMM_H__82138A00_2098_44B0_8F5F_C96B6BDB8880__INCLUDED_)


OpComm.cpp
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "OpComm.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

COpComm::COpComm()
{
        m_hCommPort = INVALID_HANDLE_VALUE;
}

COpComm::~COpComm()
{
        ClosePort();
}

BOOL COpComm::OpenPort(LPTSTR strPortName, DWORD dwBaudRate,
                                           BYTE bByteSize, BYTE bParity, BYTE bStopBit)
{
        BOOL                        bPortReady = FALSE;
        DCB                                PortDCB;
        COMMTIMEOUTS        PortTimeOuts;

        //        If the port was previously open, close it first
        ClosePort();

        //        Open the port and do new settings
        m_hCommPort = CreateFile(strPortName,
                                                       GENERIC_READ | GENERIC_WRITE,
                                                       0,
                                                       NULL,
                                                       OPEN_EXISTING,
                                                       0,
                                                       NULL);
        if(m_hCommPort == INVALID_HANDLE_VALUE)
                return bPortReady;

        bPortReady = GetCommState(m_hCommPort, &PortDCB);
        if(!bPortReady)
                return bPortReady;

        PortDCB.BaudRate = dwBaudRate;
        PortDCB.ByteSize = bByteSize;
        PortDCB.fParity= FALSE;   //Disable parity checking
        PortDCB.StopBits = bStopBit;
        PortDCB.fAbortOnError = TRUE;

        bPortReady = SetCommState(m_hCommPort, &PortDCB);
        if(!bPortReady)
                return bPortReady;

        bPortReady = GetCommTimeouts(m_hCommPort, &PortTimeOuts);
        if(!bPortReady)
                return bPortReady;

        PortTimeOuts.ReadIntervalTimeout = 50;
        PortTimeOuts.ReadTotalTimeoutConstant = 50;
        PortTimeOuts.ReadTotalTimeoutMultiplier = 10;
        PortTimeOuts.WriteTotalTimeoutConstant = 50;
        PortTimeOuts.WriteTotalTimeoutMultiplier = 10;

        bPortReady = SetCommTimeouts(m_hCommPort, &PortTimeOuts);
        if(!bPortReady)
                return bPortReady;

        bPortReady = SetupComm(m_hCommPort, 1024, 1024);
        if(!bPortReady)
                return bPortReady;

        bPortReady = PurgeComm(m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR);

        return bPortReady;
}

VOID COpComm::ClosePort()
{
        if(m_hCommPort != INVALID_HANDLE_VALUE)
        {
                CloseHandle(m_hCommPort);
                m_hCommPort = INVALID_HANDLE_VALUE;
        }
}

BOOL COpComm::WriteBuffer(unsigned char* pOutBuffer, DWORD dwBufferSize)
{
        DWORD dwBytesWritten;

        if(m_hCommPort == INVALID_HANDLE_VALUE)
                return FALSE;

        // Clear the TX and RX buffer Error information
        if (!PurgeComm(m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR))
        {
                return FALSE;
        }

        WriteFile(m_hCommPort, pOutBuffer, dwBufferSize, &dwBytesWritten, NULL);
        if(dwBytesWritten != dwBufferSize)
                return FALSE;

        return TRUE;
}

BOOL COpComm::ReadBuffer(unsigned char* pInBuffer, DWORD dwBufferSize)
{
        DWORD dwBytesRead;

        if(m_hCommPort == INVALID_HANDLE_VALUE)
                return FALSE;

        ReadFile(m_hCommPort, pInBuffer, dwBufferSize, &dwBytesRead, NULL);

        // if the received buffer is empty, return FALSE
        if(dwBytesRead == 0)
                return FALSE;

        // if received data lengthis not equal with the want data length
//        if(dwBytesRead != dwBufferSize)
//                return FALSE;

        return TRUE;
}

sunjianmax232 发表于 2014-12-2 08:04:29

楼主标题不合格。是分享代码?还是同步读写串口代码有问题?

朝闻夕道 发表于 2014-12-2 08:20:11

楼主如果再不改标题,我要去举报,哈哈;
建议楼主改改标题,你这个确实如上楼所述,让人产生歧义;

zwgmail 发表于 2014-12-2 08:41:00

不知道楼主想干什么

dxzky 发表于 2014-12-2 12:44:04

不知所云一堆代码,希望楼主尽快编辑,描述清楚

funnynypd 发表于 2014-12-2 12:51:59

This is the source code.

xinxinyu2013 发表于 2014-12-2 13:57:10

wcl_0308 发表于 2014-12-2 18:09:03

就是VC的串口类, .h,.cpp文件。

vc9181 发表于 2014-12-7 15:56:52

没有用过{:lol:}

eliterxzgxu 发表于 2014-12-7 17:08:04

看不懂~~
页: [1]
查看完整版本: VC++ 对串口操作之串口类