资源简介
用它可以很轻松完成一般串口编程工作,几分钟就可搭好串口通信框架,使编程者可以将大部分精力放在数据处理上,而且我们可以对这个类进行改进,使其更加完善,相信您使用后一定会喜欢上它的

代码片段和文件信息
/*
Module : SERIALPORT.CPP
Purpose: Implementation for an MFC wrapper class for serial ports
Created: PJN / 31-05-1999
History: PJN / 03-06-1999 1. Fixed problem with code using CancelIo which does not exist on 95.
2. Fixed leaks which can occur in sample app when an exception is thrown
PJN / 16-06-1999 1. Fixed a bug whereby CString::ReleaseBuffer was not being called in
CSerialException::GetErrorMessage
PJN / 29-09-1999 1. Fixed a simple copy and paste bug in CSerialPort::SetDTR
Copyright (c) 1999 by PJ Naughter.
All rights reserved.
*/
///////////////////////////////// Includes //////////////////////////////////
#include “stdafx.h“
#include “serialport.h“
#include “winerror.h“
///////////////////////////////// defines /////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////// Implementation ///////////////////////////////
//Class which handles CancelIo function which must be constructed at run time
//since it is not imeplemented on NT 3.51 or Windows 95. To avoid the loader
//bringing up a message such as “Failed to load due to missing export...“ the
//function is constructed using GetProcAddress. The CSerialPort::CancelIo
//function then checks to see if the function pointer is NULL and if it is it
//throws an exception using the error code ERROR_CALL_NOT_IMPLEMENTED which
//is what 95 would have done if it had implemented a stub for it in the first
//place !!
class _SERIAL_PORT_DATA
{
public:
//Constructors /Destructors
_SERIAL_PORT_DATA();
~_SERIAL_PORT_DATA();
HINSTANCE m_hKernel32;
typedef BOOL (CANCELIO)(HANDLE);
typedef CANCELIO* LPCANCELIO;
LPCANCELIO m_lpfnCancelIo;
};
_SERIAL_PORT_DATA::_SERIAL_PORT_DATA()
{
m_hKernel32 = LoadLibrary(_T(“KERNEL32.DLL“));
VERIFY(m_hKernel32 != NULL);
m_lpfnCancelIo = (LPCANCELIO) GetProcAddress(m_hKernel32 “CancelIo“);
}
_SERIAL_PORT_DATA::~_SERIAL_PORT_DATA()
{
FreeLibrary(m_hKernel32);
m_hKernel32 = NULL;
}
//The local variable which handle the function pointers
_SERIAL_PORT_DATA _SerialPortData;
////////// Exception handling code
void AfxThrowSerialException(DWORD dwError /* = 0 */)
{
if (dwError == 0)
dwError = ::GetLastError();
CSerialException* pException = new CSerialException(dwError);
TRACE(_T(“Warning: throwing CSerialException for error %d\n“) dwError);
THROW(pException);
}
BOOL CSerialException::GetErrorMessage(LPTSTR pstrError UINT nMaxError PUINT pnHelpContext)
{
ASSERT(pstrError != NULL && AfxIsValidString(pstrError nMaxError));
if (pnHelpContext != NULL)
*pnHelpContext = 0;
LPTSTR lpBuffer;
BOOL bRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
NULL m_dwError MAKELANGID(
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4238 1999-05-31 23:48 serialport\serialport.h
文件 19021 1999-09-29 21:09 serialport\serialport.cpp
目录 0 2008-07-22 15:54 serialport
----------- --------- ---------- ----- ----
23259 3
相关资源
- Modbus协议官方文档中、英文全
- 易语言OTG串口通信
- delphi 串口通讯
- STM32 2.4G通信例程
- Visio图标-最新最全的网络通信图标库
- 基于蓝牙4.0的设备通信方案设计与实
- STM32蓝牙和串口程序
- LCD显示温度+串口接收温度.rar
- WPF USB 网络 串口 通信软件
- 使用选择性重传协议实现UDP可靠通信
- DELPHI与西门子200PLC的串口通信实例
- Labview与CH372CH375的通信测试
- Verilog FPGA UART串口控制器
- 通信软件的具体实例──基于Socket的
- Windows异步套接字网络编程
- USB转串口驱动,FT232R驱动程序,最新
- VC 获得文件属性 获取文件的创建时
- 基于MVC模式的会员管理系统
- silicon lab公司的收音IC SI47XX全套开发工
- 读者写者问题(读者优先,写者优先
- MFC程序-碰撞的小球
- vc 柱形图 CBarChart
- 用vc 写的导线测量,针对刚学测绘的
- 用VC 编写的仿QQ聊天室程序源代码
- 栅栏填充算法源码(VC)
- PC -- 单片机的串口数据传输系统设计
- STM32F103 串口程序(完整版)
- stm32 ds18b20 温度传感器 测试通过
- 简单的房屋租赁系统
- PC 串口调试软件
评论
共有 条评论