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,
MFC串口编程实战指南

本文介绍了如何在MFC中进行串口编程,重点是一个串口封装类的使用。首先,提供了CommUtils.cpp和CommUtils.h的代码,然后详细说明了如何将这些文件添加到工程中,并在对话框类中包含头文件,创建并使用串口对象来执行打开、读取、写入和关闭串口的操作。文章最后给出了串口封装类的下载链接。
最低0.47元/天 解锁文章
3205

被折叠的 条评论
为什么被折叠?



