这是一个自己编写的串口访问类,使用的是API函数,不需要MSCOMM控件。
在使用的时候只需要调用OpenComm函数创建和打开串口,然后通过SendMsg和RecvMsg收发串口信息就可以了。
非常好用,源码已经贴上,用记事本新建两个文件,Comm.h和Comm.cpp,把代码复制到相应的文件就可以了。
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//以下是Comm.cpp文件
#include <stdafx.h>
#include"Comm.h"
#include<stdio.h>
//Constructor
CComm::CComm()
{
open = false;
dwError = 0;
}
CComm::CComm(char* com, unsigned int baudrate)
{
dwError = 0;
OpenComm(com, baudrate);
}
CComm::~CComm()
{
if(!open)return;
else CloseComm();
}
bool CComm::OpenComm(char* com, unsigned int baudrate)
{
if(!CreateFile(com))
{
open = false;
return false;
}
else open = true;
//调用SetupComm函数可以设置串行口的输入和输出缓冲区的大小
SetupComm( hComm, 1024, 1024 );//缓冲区的大小为1024
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
TimeOuts.WriteTotalTimeoutMultiplier=100;
TimeOuts.WriteTotalTimeoutConstant=500;
SetCommTimeouts(hComm, &TimeOuts); //设置超时
//初始化串口
GetCommState(hComm, &dcb);
dcb.BaudRate = baudrate; // 波特率
dcb.ByteSize = 8; // 每个字符有8位
dcb.Parity = NOPARITY; //无校验
dcb.StopBits = ONESTOPBIT; //一个停止位
SetCommState(hComm, &dcb);
//SetCommMask(hComm, EV_RXCHAR);
PurgeComm(hComm, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
return true;
}
bool CComm::CloseComm()
{
waitforsingleobject函数 if(!open)return false;
CloseHandle(hComm);
return true;
}
void CComm::RecvMsg(unsigned char* buff, int size, unsigned long* recv_num)
{
memset(buff, 0, size);
if(!open)return;
BOOL bReadStatus;
COMSTAT ComStat;
memset(&overlapped,0,sizeof(OVERLAPPED));
overlapped.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
ClearCommError(hComm,&dwError,&ComStat);
bReadStatus=ReadFile(hComm, buff, size, recv_num, &overlapped);
if(!bReadStatus) //如果ReadFile函数返回FALSE
{
if(GetLastError()==ERROR_IO_PENDING)
//GetLastError()函数返回ERROR_IO_PENDING,表明串口正在进行读操作
{
WaitForSingleObject(overlapped.hEvent,2000);
//使用WaitForSingleObject函数等待,直到读操作完成或延时已达到2秒钟
//当串口读操作进行完毕后,overlapped的hEvent事件会变为有信号
}
return;
}
PurgeComm(hComm, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
dwError = GetLastError();
}
void CComm::SendMsg(unsigned char* buff, int size, unsigned long* send_num)
{
if(!open)return;
BOOL bWriteStat;
COMSTAT ComStat;
memset(&overlapped,0,sizeof(OVERLAPPED));
overlapped.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
ClearCommError(hComm,&dwError,&ComStat);
bWriteStat=WriteFile(hComm, buff, size, send_num, &overlapped);
if(!bWriteStat)
{
if(GetLastError()==ERROR_IO_PENDING)
{
WaitForSingleObject(overlapped.hEvent,1000);
}
return;
}
dwError = GetLastError();
}
bool CComm::CreateFile(char* com)
{
hComm = ::CreateFile(com,
GENERIC_READ|GENERIC_WRITE,
0,//独占方式
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);
if(hComm == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
CloseHandle(hComm);
return false;
}
return true;
}
void CComm::GetError(char* errmsg)
{
sprintf(errmsg, "ErrorID = %d", dwError);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//以下是Comm.h文件
#include<windows.h>
#ifndef COMM_H
#define COMM_H
class CComm
{
public:
CComm();
CComm(char* com, unsigned int baudrate);
~CComm();
bool OpenComm(char* com, unsigned int baudrate=4800);
bool CloseComm();
void RecvMsg(unsigned char* buff, int size, unsigned long* recv_num);
void SendMsg(unsigned char* buff, int size, unsigned long* send_num);
void GetError(char* errmsg);
private:
HANDLE hComm;
DWORD dwError;
bool open;
DCB dcb;
COMMTIMEOUTS TimeOuts;//超时结构体
OVERLAPPED overlapped;
private:
bool CreateFile(char* com);
};
#endif
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论