串口类serialport的头文件和cpp文件详细代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

DECLARE_DYNAMIC(CSerialPort)
};
#endif //__SERIALPORT_H__
/*
Module : SERIALPORT.CPP
Purpose: Implementation for an MFC wrapper class for serial ports
void CancelIo();
//Configuration Methods
void GetConfig(COMMCONFIG& config);
static void GetDefaultConfig(int nPort, COMMCONFIG& config);
void SetConfig(COMMCONFIG& Config);
void GetStatus(COMSTAT& stat);
void GetState(DCB& dcb);
void SetState(DCB& dcb);
void Escape(DWORD dwFunc);
void ClearDTR();
void ClearRTS();
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 / 29-09-1999 1. Fixed a simple copy and paste bug in CSerialPort::SetDTR
Copyright (c) 1999 by PJ Naughter.
All rights reserved.
*/
PJN / 16-06-1999 1. Fixed a bug whereby CString::ReleaseBuffer was not being called in
CSerialException::GetErrorMessage
void ReadEx(void* lpBuf, DWORD dwCount);
DWORD Write(const void* lpBuf, DWORD dwCount);
BOOL Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped);
//General Methods
void Open(int nPort, DWORD dwBaud = 9600, Parity parity = NoParity, BYTE DataBits = 8,
StopBits stopbits = OneStopBit, FlowControl fc = NoFlowControl, BOOL bOverlapped = FALSE);
};
enum StopBits
{
OneStopBit,
OnePointFiveStopBits,
TwoStopBits
};
//Constructors / Destructors
CSerialPort();
~CSerialPort();
DsrDtrFlowControl,
XonXoffFlowControl
};
enum Parity
{
EvenParity,
MarkParity,
NoParity,
OddParity,
SpaceParity
~CSerialException();
//Methods
#ifdef _DEBUG
virtual void Dump(CDumpContext& dc) const;
#endif
virtual BOOL GetErrorMessage(LPTSTR lpstrError, UINT nMaxError, PUINT pnHelpContext = NULL);
//Queue Methods
void Flush();
void Purge(DWORD dwFlags);
void TerminateOutstandingWrites();
void TerminateOutstandingReads();
void ClearWriteBuffer();
void AfxThrowSerialException(DWORD dwError = 0);
class CSerialException : public CException
{
public:
//Constructors / Destructors
CSerialException(DWORD dwError);
///////////////////////////////// defines /////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
void WriteEx(const void* lpBuf, DWORD dwCount);
void TransmitChar(char cChar);
void GetOverlappedResult(OVERLAPPED& overlapped, DWORD& dwBytesTransferred, BOOL bWait);
protected:
HANDLE m_hComm; //Handle to the comms port
BOOL m_bOverlapped; //Is the port open in overlapped IO
static void WINAPI _OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
void ClearReadBuffer();
void Setup(DWORD dwInQueue, DWORD dwOutQueue);
//Overridables
virtual void OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
///////////////////////////////// Includes //////////////////////////////////
#include "stdafx.h"
#include "serialport.h"
#include "winerror
void SetMask(DWORD dwMask);
void GetMask(DWORD& dwMask);
void WaitEvent(DWORD& dwMask);
void WaitEvent(DWORD& dwMask, OVERLAPPED& overlapped);
CString GetErrorMessage();
//Data members
DWORD m_dwError;
protected:
DECLARE_DYNAMIC(CSerialException)
};
//// The actual serial port class /////////////////////////////////////////////
/*
Module : SERIALPORT.H
Purpose: Declaration for an MFC wrapper class for serial ports
Created: PJN / 31-05-1999
History: None
Copyright (c) 1999 by PJ Naughter.
void Close();
void Attach(HANDLE hComm);
HANDLE Detach();
operator HANDLE() const { return m_hComm; };
BOOL IsOpen() const { return m_hComm != INVALID_HANDLE_VALUE; };
void SetDTR();
void SetRTS();
void SetXOFF();
void SetXON();
void GetProperties(COMMPROP& properties);
void GetModemStatus(DWORD& dwModemStatus);
class CSerialPort : public CObject
{
public:
//Enums
enum FlowControl
{
NoFlowControl,
CtsRtsFlowControl,
CtsDtrFlowControl,
DsrRtsFlowControl,
#ifdef _DEBUG
void CSerialPort::Dump(CDumpContext& dc) const;
#endif
//Reading / Writing Methods
DWORD Read(void* lpBuf, DWORD dwCount);
BOOL Read(void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped);
/////////////////////////// Classes ///////////////////////////////////////////
////// Serial port exception class ////////////////////////////////////////////
All rights reserved.
*/
///////////////////// Macros / Structs etc //////////////////////////
#ifndef __SERIALPORT_H__
#define __SERIALPORT_H__
static void SetDefaultConfig(int nPort, COMMCONFIG& config);
//Misc RS232 Methods
void ClearBreak();
void SetBreak();
void ClearError(DWORD& dwErrors);
//Timeouts
void SetTimeouts(COMMTIMEOUTS& timeouts);
void GetTimeouts(COMMTIMEOUTS& timeouts);
void Set0Timeout();
void Set0WriteTimeout();
void Set0ReadTimeout();
相关文档
最新文档