搜索
bottom↓
回复: 9

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

[复制链接]

出0入0汤圆

发表于 2014-12-2 08:00:12 | 显示全部楼层 |阅读模式
本帖最后由 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 length  is not equal with the want data length
//        if(dwBytesRead != dwBufferSize)
//                return FALSE;

        return TRUE;
}

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

发表于 2014-12-2 08:04:29 | 显示全部楼层
楼主标题不合格。是分享代码?还是同步读写串口代码有问题?

出0入0汤圆

发表于 2014-12-2 08:20:11 | 显示全部楼层
楼主如果再不改标题,我要去举报,哈哈;
建议楼主改改标题,你这个确实如上楼所述,让人产生歧义;

出0入0汤圆

发表于 2014-12-2 08:41:00 | 显示全部楼层
不知道楼主想干什么

出0入0汤圆

发表于 2014-12-2 12:44:04 | 显示全部楼层
不知所云一堆代码,希望楼主尽快编辑,描述清楚

出0入0汤圆

发表于 2014-12-2 12:51:59 | 显示全部楼层
This is the source code.
头像被屏蔽

出0入0汤圆

发表于 2014-12-2 13:57:10 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

出0入0汤圆

 楼主| 发表于 2014-12-2 18:09:03 | 显示全部楼层
就是VC的串口类, .h,.cpp  文件。

出0入0汤圆

发表于 2014-12-7 15:56:52 | 显示全部楼层
没有用过

出0入0汤圆

发表于 2014-12-7 17:08:04 | 显示全部楼层
看不懂~~
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-25 21:19

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表