随笔 - 18  文章 - 0  评论 - 21  阅读 - 39779

API串口操作封装类

Comm.h头文件:

 

 

复制代码
#ifndef __CCOMM_H__
#define __CCOMM_H__

class CComm  //串口操作封装类
{
private:
    HANDLE m_hComm;
public:
    CComm();  
//构造函数
    ~CComm();  //析构函数

    BOOL OpenComm(
int nComm);  //打开串口函数
    void CloseComm();  //关闭串口函数

    BOOL SetComm(
int nBaudRate/*波特率*/,int nParity/*奇偶校验位*/,int nByteSize/*字节位数*/,int nStopBits/*停止位*/);  //设置串口函数
    BOOL SetTimeOuts();  //设置超时函数

    BOOL ReadComm(
char * lpBuf,int nLen);  //读串口函数
    BOOL WriteComm(char * lpBuf,int nLen);  //写串口函数
};

#endif
复制代码

 

Comm.cpp实现文件:

 

复制代码
#include "StdAfx.h"
#include 
"Comm.h"

//构造函数
CComm::CComm()
{
}

//析构函数
CComm::~CComm()
{
    CloseComm();  
//关闭串口
}

//打开串口函数
BOOL CComm::OpenComm(int nComm)
{
    CString strCommName;
    CString strErrInfo;

    strCommName.Format(
"COM%d",nComm);

    m_hComm 
= ::CreateFile(strCommName,  /*要打开串口名称*/
                        GENERIC_READ 
| GENERIC_WRITE,  /*允许读和写*/
                        
0,  /*独占方式*/
                        NULL,  
/*安全属性*/
                        OPEN_EXISTING,  
/*打开而不是创建*/
                        
0/*同步方式*/
                        NULL);  
/*模板句柄*/

    
if(m_hComm == INVALID_HANDLE_VALUE)
    {
        strErrInfo.Format(
"打开%s失败!",strCommName);
        AfxMessageBox(strErrInfo);
        
return FALSE;
    }
    
else
    {
        
return TRUE;
    }
}

//关闭串口函数
void CComm::CloseComm()
{
    
if(m_hComm != INVALID_HANDLE_VALUE)
    {
        ::CloseHandle(m_hComm);
        m_hComm 
= INVALID_HANDLE_VALUE;
    }
}

//设置串口函数
BOOL CComm::SetComm(int nBaudRate/*波特率*/,int nParity/*奇偶校验位*/,int nByteSize/*字节位数*/,int nStopBits/*停止位*/)
{
    DCB stDCB;
    memset(
&stDCB,0,sizeof(stDCB));
    
if(!::GetCommState(m_hComm,&stDCB))  //获取串口当前状态属性
        return FALSE;
    stDCB.BaudRate 
= nBaudRate;  //波特率
    stDCB.fParity = 0;
    stDCB.Parity 
= nParity;  //奇偶校验位(NOPARITY等)
    stDCB.ByteSize = nByteSize;  //每个字节有8位
    stDCB.StopBits = nStopBits;  //停止位(ONESTOPBIT等)
    if(!::SetCommState(m_hComm,&stDCB))  //设置串口状态属性
        return FALSE;

    
if(!::SetupComm(m_hComm,1024,1024))  //设置输入缓冲区和输出缓冲区的大小
        return FALSE;
    ::PurgeComm(m_hComm,PURGE_TXCLEAR 
| PURGE_RXCLEAR);  //清空输入输出缓冲区

    
return TRUE;
}

//设置超时函数
BOOL CComm::SetTimeOuts()
{
    COMMTIMEOUTS stTimeOuts;

    stTimeOuts.ReadIntervalTimeout 
= 0;  //设定读超时
    stTimeOuts.ReadTotalTimeoutMultiplier = 100;
    stTimeOuts.ReadTotalTimeoutConstant 
= 500;

    stTimeOuts.WriteTotalTimeoutMultiplier 
= 100;  //设定写超时
    stTimeOuts.WriteTotalTimeoutConstant = 500;

    ::SetCommTimeouts(m_hComm,
&stTimeOuts);  //设置超时
    ::PurgeComm(m_hComm,PURGE_TXCLEAR | PURGE_RXCLEAR);  //清空输入输出缓冲区
    return TRUE;
}

//读串口函数
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;
    }
}

//写串口函数
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;
    }
}
复制代码
posted on   ZYM  阅读(563)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
< 2008年10月 >
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示