概述
MFC 串口编程实例
VC串口编程从实现方法上一般分为两种,一种使用MSCOMM控件,这种方法比较简单,软件的移植性较低,在这里介绍一种串口封装类的使用方法。
代码
先看代码 CommUtils.cpp
#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,NULL);
int error=GetLastError();
if (hComm == INVALID_HANDLE_VALUE) return false;
if (!SetupComm(hComm, 1024, 512)) 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; // baud rate 9600
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
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;
}
/*
bResult = ReadFile(port->m_hComm, // Handle to COMM port
&RXBuff, // RX Buffer Pointer
1, // Read one byte
&BytesRead, // Stores number of bytes read
&port->m_ov); // pointer to the m_ov structure
// deal with the error code */
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;
}
头文件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
使用方法
1.将.cpp .h 文件拷贝到工程目录下;
2.右击项目—>添加—>现有项,将CommUtils.cpp CommUtils.h 添加进来
3.在自己的对话框类中包含该类的头文件
#include "CommUtils.h"
定义一个全局变量
CommUtils mCommUtils ;
接下来通过CommUtils 的实例mCommUtils 来操作串口
打开串口:
int Port; //要操作的串口号
mCommUtils.OpenCom(Port); //打开串口
读串口数据:
unsigned char COMdata[100]; //接收的数据
unsigned long len = 0;
mCommUtils.WriteCom(COMdata, len);
写数据:
unsigned char SendArr[6];
mCommUtils.WriteCom(SendArr,6)
最后关闭串口:
mCommUtils.CloseCom();
该类的析构函数中也调用了该函数,在对象被销毁时也会关掉串口。
最后
串口封装类的下载地址:CommUtils
只是想和大家分享经验,但是下载最少需要1个积分,大家可在自己的工程里右击—>添加—>类 ; 将代码复制过去,注意类名的变化,新建类的类名直接填CommUtils。
最后
以上就是冷静睫毛膏为你收集整理的MFC 串口编程实例MFC 串口编程实例的全部内容,希望文章能够帮你解决MFC 串口编程实例MFC 串口编程实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复