edog

冰冻热狗
数据加载中……

一个串口通讯的类

/* CommUtils.h */
#ifndef _CommUtils_H__
#define _CommUtils_H__

class CommUtils  
{
public:
    
bool ReadCom(unsigned char * ReceiveData, DWORD& ReceiveLength);
    
void CloseCom();
    
bool WriteCom(unsigned char * sendchar,int sendsize);
    
bool OpenCom(int Port);

    CommUtils();
    
virtual ~CommUtils();
    
int m_Port;
    
char szCurPath[256];

private:
    OVERLAPPED ReadovReady, WriteovReady;
    HANDLE hComm;
    
bool bOpenCom;
}
;

#endif

/* 
    CommUtils.cpp    串口通讯类
    Author: edog    2007-11-21
    EMail: comwell@126.com
 
*/

#include 
"stdafx.h"
#include 
"CommUtils.h"
#include 
"stdio.h"
const int READ_TIMEOUT = 500;

CommUtils::CommUtils()
{
    bOpenCom 
= false;
}


CommUtils::
~CommUtils()
{
    
this->CloseCom();
}


bool CommUtils::OpenCom(int Port)
{
    
if (bOpenCom)
    
{
        
this->CloseCom();
        bOpenCom 
= false;
    }

    
char szport[10];
    sprintf(szport,
"COM%d",Port);
    hComm 
= CreateFile(    szport,
                        GENERIC_READ
|GENERIC_WRITE,
                        
0,
                        NULL,
                        OPEN_EXISTING,
                        FILE_FLAG_OVERLAPPED, 
//FILE_ATTRIBUTE_NORMAL|
                        NULL);
    
    
    
if (hComm == INVALID_HANDLE_VALUE)        return false;
    
if (!SetupComm(hComm, 1024512))        return false;
    
    COMMTIMEOUTS commtimeouts;
    commtimeouts.ReadIntervalTimeout 
= MAXDWORD;
    commtimeouts.ReadTotalTimeoutConstant 
=0;
    commtimeouts.ReadTotalTimeoutMultiplier 
=0;
    commtimeouts.WriteTotalTimeoutConstant 
=0;
    commtimeouts.WriteTotalTimeoutMultiplier
=0;
    
    
if (!SetCommTimeouts(hComm, &commtimeouts))        return false;

    memset(
&ReadovReady,0,sizeof(OVERLAPPED));
    memset(
&WriteovReady,0,sizeof(OVERLAPPED));
    ReadovReady.hEvent 
= CreateEvent(NULL,TRUE,FALSE,NULL);
    WriteovReady.hEvent 
=CreateEvent(NULL,TRUE,FALSE,NULL);
    
    SECURITY_ATTRIBUTES sa;
    sa.nLength
=sizeof(SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor
=NULL;
    sa.bInheritHandle
=TRUE;
    
    DCB dcb;
    GetCommState(hComm, 
&dcb);
    dcb.fBinary 
= TRUE;
    dcb.fParity 
= TRUE;
    dcb.BaudRate 
= CBR_9600;        // 波特率 9600
    dcb.ByteSize = 8;                // 8 位数据位
    dcb.Parity = NOPARITY;            // 无奇偶校验
    dcb.StopBits = ONESTOPBIT;        // 1 个停止位
    
    
if (!SetCommState(hComm, &dcb ))        return false;

    bOpenCom 
= true;
    
return bOpenCom;
}


bool CommUtils::WriteCom(unsigned char *sendchar, int sendsize)
{
    
if (!bOpenCom)    return false;
    
    DWORD    BytesSent;
    DWORD    resD;        
    
    PurgeComm(hComm, PURGE_RXCLEAR 
| PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
    
    BytesSent
=0;
    BOOL hr 
= WriteFile(hComm,                            // Handle to COMM Port
                        sendchar,                        // Pointer to message buffer in calling finction
                        sendsize,                        // Length of message to send
                        &BytesSent,                        // Where to store the number of bytes sent
                        &WriteovReady);                    // Overlapped structure
    if(!hr)
    
{
        
if(GetLastError() != ERROR_IO_PENDING)
        
{
            
return false;
        }

        
else
        
{
            resD
=WaitForSingleObject(WriteovReady.hEvent,INFINITE);
        }

        
switch(resD)
        
{
            
case WAIT_OBJECT_0:
            
{
                
if(!GetOverlappedResult(hComm,&WriteovReady,&BytesSent,false))
                    
return false;
                
else
                    
return true;
                
            }

            
default:
                
return false;
                
break;
        }

    }

    
return true;
}


void CommUtils::CloseCom()
{
    
if (!bOpenCom)    return;

    CloseHandle(hComm);
    hComm
=NULL;
    
    CloseHandle(ReadovReady.hEvent);
    CloseHandle(WriteovReady.hEvent );
    ReadovReady.hEvent 
=NULL;
    WriteovReady.hEvent 
=NULL;
}


bool CommUtils::ReadCom(unsigned char * ReceiveData, DWORD& ReceiveLength)
{
    
if (!bOpenCom)    return false;
    
if (ReadovReady.hEvent == NULL)    return false;
    
    ReceiveLength 
= 0;
    
if (ReadFile(hComm, ReceiveData, 128&ReceiveLength, &ReadovReady) == FALSE) 
    
{
        
if (GetLastError() != ERROR_IO_PENDING)    return false;
    }


    
if(ReceiveLength == 0)    return false;
    ReceiveData[ReceiveLength] 
= 0;

    DWORD dwRead;
    DWORD dwRes 
= WaitForSingleObject(ReadovReady.hEvent, READ_TIMEOUT);
    
switch(dwRes)
    
{
        
case WAIT_OBJECT_0:
            
if (!GetOverlappedResult(hComm, &ReadovReady, &dwRead, FALSE))    return false;
            
break;

        
case WAIT_TIMEOUT:
            
break;                
            
        
default:
            
break;
    }

    
return true;
}




以下为使用方法:
// 1. 包含头文件,定义变量
#include "CommUtils.h"
CommUtils theComm;
unsigned 
char data[1024];
unsigned 
long len = 0;

// 2. 打开串口,设置接收定时器
theComm.OpenCom(1);        // 打开COM1口
SetTimer(1500);

// 3. 发送数据
theComm.WriteCom(data, len);

// 4. 接收数据处理
void CUARTDlg::OnTimer(UINT nIDEvent) 
{
    
if (nIDEvent == 1)
    
{
        
if (theComm.ReadCom(data, len))
        
{
            
if (len > 0)    
            
{
                
// 接收数据处理。。。
            }

        }

    }

    CDialog::OnTimer(nIDEvent);
}

posted on 2007-11-21 10:28 冰冻热狗 阅读(3660) 评论(4)  编辑 收藏 引用 所属分类: 应用程序

评论

# re: 一个串口通讯的类  回复  更多评论   

谢谢。 正要找这么一个东西,搜索到这里,顺利用上了~~~
没有条件测试,应该没有问题。
2008-10-19 21:40 | summer

# re: 一个串口通讯的类  回复  更多评论   

thinks a lot!
2008-12-20 00:17 | 王建壮

# re: 一个串口通讯的类  回复  更多评论   

@Sharman
广告都作到这了,安哥拉的沙曼。。。
2013-04-13 11:59 | ileson

# re: 一个串口通讯的类  回复  更多评论   

多谢楼主!正好需要!
2014-03-16 21:55 | 水煮鱼丸

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理