資源簡(jiǎn)介
用它可以很輕松完成一般串口編程工作,幾分鐘就可搭好串口通信框架,使編程者可以將大部分精力放在數(shù)據(jù)處理上,而且我們可以對(duì)這個(gè)類進(jìn)行改進(jìn),使其更加完善,相信您使用后一定會(huì)喜歡上它的

代碼片段和文件信息
/*
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(
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????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
評(píng)論
共有 條評(píng)論