亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? serialport.cpp

?? 非常好用的VC++源代碼
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
/*
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
         PJN / 08-05-2000 1. Fixed an unreferrenced variable in CSerialPort::GetOverlappedResult in VC 6
         PJN / 10-12-2000 1. Made class destructor virtual
         PJN / 15-01-2001 1. Attach method now also allows you to specify whether the serial port
                          is being attached to in overlapped mode
                          2. Removed some ASSERT's which were unnecessary in some of the functions
                          3. Updated the Read method which uses OVERLAPPED IO to also return the bytes
                          read. This allows calls to WriteFile with a 0'ized overlapped structure (This
                          is required when dealing with TAPI and serial communications)
                          4. Now includes copyright message in the source code and documentation.
         PJN / 24-03-2001 1. Added a BytesWaiting method
         PJN / 04-04-2001 1. Provided an overriden version of BytesWaiting which specifies a timeout
         PJN / 23-04-2001 1. Fixed a memory leak in DataWaiting method
         PJN / 01-05-2002 1. Fixed a problem in Open method which was failing to initialize the DCB 
                          structure incorrectly, when calling GetState. Thanks to Ben Newson for this fix.
         PJN / 29-05-2002 1. Fixed an problem where the GetProcAddress for CancelIO was using the
                          wrong calling convention
         PJN / 07-08-2002 1. Changed the declaration of CSerialPort::WaitEvent to be consistent with the
                          rest of the methods in CSerialPort which can operate in "OVERLAPPED" mode. A note
                          about the usage of this: If the method succeeds then the overlapped operation
                          has completed synchronously and there is no need to do a WaitForSingle/Multiple]Object.
                          If any other unexpected error occurs then a CSerialException will be thrown. See
                          the implementation of the CSerialPort::DataWaiting which has been rewritten to use
                          this new design pattern. Thanks to Serhiy Pavlov for spotting this inconsistency.
         PJN / 20-09-2002 1. Addition of an additional ASSERT in the internal _OnCompletion function.
                          2. Addition of an optional out parameter to the Write method which operates in 
                          overlapped mode. Thanks to Kevin Pinkerton for this addition.



Copyright (c) 1996 - 2002 by PJ Naughter.  (Web: www.naughter.com, Email: pjna@naughter.com)

All rights reserved.

Copyright / Usage Details:

You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) 
when your product is released in binary form. You are allowed to modify the source code in any way you want 
except you cannot modify the copyright details at the top of each module. If you want to distribute source 
code with your application, then you are only allowed to distribute versions released by the author. This is 
to maintain a single distribution point for the source code. 

*/

/////////////////////////////////  Includes  //////////////////////////////////

#include "stdafx.h"
#include "serialport.h"
#ifndef _WINERROR_
#include "winerror.h"
#endif



///////////////////////////////// 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 (WINAPI 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(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
			                      (LPTSTR) &lpBuffer, 0, NULL);

	if (bRet == FALSE)
		*pstrError = '\0';
	else
	{
		lstrcpyn(pstrError, lpBuffer, nMaxError);
		bRet = TRUE;

		LocalFree(lpBuffer);
	}

	return bRet;
}

CString CSerialException::GetErrorMessage()
{
  CString rVal;
  LPTSTR pstrError = rVal.GetBuffer(4096);
  GetErrorMessage(pstrError, 4096, NULL);
  rVal.ReleaseBuffer();
  return rVal;
}

CSerialException::CSerialException(DWORD dwError)
{
	m_dwError = dwError;
}

CSerialException::~CSerialException()
{
}

IMPLEMENT_DYNAMIC(CSerialException, CException)

#ifdef _DEBUG
void CSerialException::Dump(CDumpContext& dc) const
{
	CObject::Dump(dc);

	dc << "m_dwError = " << m_dwError;
}
#endif





////////// The actual serial port code

CSerialPort::CSerialPort()
{
  m_hComm = INVALID_HANDLE_VALUE;
  m_bOverlapped = FALSE;
  m_hEvent = NULL;
}

CSerialPort::~CSerialPort()
{
  Close();
}

IMPLEMENT_DYNAMIC(CSerialPort, CObject)

#ifdef _DEBUG
void CSerialPort::Dump(CDumpContext& dc) const
{
	CObject::Dump(dc);

	dc << _T("m_hComm = ") << m_hComm << _T("\n");
  dc << _T("m_bOverlapped = ") << m_bOverlapped;
}
#endif

void CSerialPort::Open(int nPort, DWORD dwBaud, Parity parity, BYTE DataBits, StopBits stopbits, FlowControl fc, BOOL bOverlapped)
{
  //Validate our parameters
  ASSERT(nPort>0 && nPort<=255);

  Close(); //In case we are already open

  //Call CreateFile to open up the comms port
  CString sPort;
  sPort.Format(_T("\\\\.\\COM%d"), nPort);
  DWORD dwCreateProperty;
  if(bOverlapped) 
  {
     dwCreateProperty=FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED;
  }
  else
  {
	 dwCreateProperty=FILE_ATTRIBUTE_NORMAL;
  }
  // bOverlapped ? FILE_FLAG_OVERLAPPED : 0
  m_hComm = CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,dwCreateProperty, NULL);
  if (m_hComm == INVALID_HANDLE_VALUE)
  {
    TRACE(_T("Failed to open up the comms port\n"));
    AfxThrowSerialException();
  }

  this->m_CurPortNum = nPort;
  //Create the event we need for later synchronisation use
  /*
  if(bOverlapped)
  {
	m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	if (m_hEvent == NULL)
	{
		Close();
		TRACE(_T("Failed in call to CreateEvent in Open\n"));
		AfxThrowSerialException();
	}
  }
  */
 
  m_bOverlapped = bOverlapped;
  
  //Get the current state prior to changing it
  DCB dcb;
  dcb.DCBlength = sizeof(DCB);
  GetState(dcb);

  //Setup the baud rate
  dcb.BaudRate = dwBaud; 

  //Setup the Parity
  switch (parity)
  {
    case EvenParity:  dcb.Parity = EVENPARITY;  break;
    case MarkParity:  dcb.Parity = MARKPARITY;  break;
    case NoParity:    dcb.Parity = NOPARITY;    break;
    case OddParity:   dcb.Parity = ODDPARITY;   break;
    case SpaceParity: dcb.Parity = SPACEPARITY; break;
    default:          ASSERT(FALSE);            break;
  }

  //Setup the data bits
  dcb.ByteSize = DataBits;

  //Setup the stop bits
  switch (stopbits)
  {
    case OneStopBit:           dcb.StopBits = ONESTOPBIT;   break;
    case OnePointFiveStopBits: dcb.StopBits = ONE5STOPBITS; break;
    case TwoStopBits:          dcb.StopBits = TWOSTOPBITS;  break;
    default:                   ASSERT(FALSE);               break;
  }

  //Setup the flow control 
  dcb.fDsrSensitivity = FALSE;
  switch (fc)
  {
    case NoFlowControl:
    {
      dcb.fOutxCtsFlow = FALSE;
      dcb.fOutxDsrFlow = FALSE;
      dcb.fOutX = FALSE;
      dcb.fInX = FALSE;
      break;
    }
    case CtsRtsFlowControl:
    {
      dcb.fOutxCtsFlow = TRUE;
      dcb.fOutxDsrFlow = FALSE;
      dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
      dcb.fOutX = FALSE;
      dcb.fInX = FALSE;
      break;
    }
    case CtsDtrFlowControl:
    {
      dcb.fOutxCtsFlow = TRUE;
      dcb.fOutxDsrFlow = FALSE;
      dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
      dcb.fOutX = FALSE;
      dcb.fInX = FALSE;
      break;
    }
    case DsrRtsFlowControl:
    {
      dcb.fOutxCtsFlow = FALSE;
      dcb.fOutxDsrFlow = TRUE;
      dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
      dcb.fOutX = FALSE;
      dcb.fInX = FALSE;
      break;
    }
    case DsrDtrFlowControl:
    {
      dcb.fOutxCtsFlow = FALSE;
      dcb.fOutxDsrFlow = TRUE;
      dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
      dcb.fOutX = FALSE;
      dcb.fInX = FALSE;
      break;
    }
    case XonXoffFlowControl:
    {
      dcb.fOutxCtsFlow = FALSE;
      dcb.fOutxDsrFlow = FALSE;
      dcb.fOutX = TRUE;
      dcb.fInX = TRUE;
      dcb.XonChar = 0x11;
      dcb.XoffChar = 0x13;
      dcb.XoffLim = 100;
      dcb.XonLim = 100;
      break;
    }
    default:
    {
      ASSERT(FALSE);
      break;
    }
  }
  
  //Now that we have all the settings in place, make the changes
  SetState(dcb);
}

void CSerialPort::Close()
{
  if (IsOpen())
  {
    //Close down the comms port
    BOOL bSuccess = CloseHandle(m_hComm);
    m_hComm = INVALID_HANDLE_VALUE;
    if (!bSuccess)
      TRACE(_T("Failed to close up the comms port, GetLastError:%d\n"), GetLastError());
    m_bOverlapped = FALSE;

    //Free up the event object we are using
    if(m_hEvent)
	{
	   CloseHandle(m_hEvent);
       m_hEvent = NULL;
	}

  }
}

void CSerialPort::Attach(HANDLE hComm, BOOL bOverlapped)
{
  Close();
  m_hComm = hComm;  
  m_bOverlapped = bOverlapped;

  //Create the event we need for later synchronisation use
  m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  if (m_hEvent == NULL)
  {
    Close();
    TRACE(_T("Failed in call to CreateEvent in Attach\n"));
    AfxThrowSerialException();
  }
}

HANDLE CSerialPort::Detach()
{
  HANDLE hrVal = m_hComm;
  m_hComm = INVALID_HANDLE_VALUE;
  CloseHandle(m_hEvent);
  m_hEvent = NULL;
  return hrVal;
}

DWORD CSerialPort::Read(void* lpBuf, DWORD dwCount)
{
  ASSERT(IsOpen());
  ASSERT(!m_bOverlapped);
  
  DWORD Errors;
  COMSTAT comstat;
  BOOL ok;
  ok = ::ClearCommError(this->m_hComm,&Errors,&comstat);
  if(!ok)
  {
     return 0;
  }
  if(comstat.cbInQue==0)
  {
     return 0;
  }

  DWORD dwBytesRead = 0;
  if (!ReadFile(m_hComm, lpBuf, comstat.cbInQue, &dwBytesRead, NULL))
  {
    TRACE(_T("Failed in call to ReadFile\n"));
    AfxThrowSerialException();
  }

  return dwBytesRead;
}

BOOL CSerialPort::Read(void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesRead)
{
  ASSERT(IsOpen());
  ASSERT(m_bOverlapped);

  DWORD dwBytesRead = 0;
  DWORD Errors;
  COMSTAT comstat;
  /*
  BOOL ok;
  ok = ::ClearCommError(this->m_hComm,&Errors,&comstat);
  if(!ok)
  {
     return false;
  }
  if(comstat.cbInQue==0)
  {
     return false;
  }
  */
  BOOL bSuccess = ReadFile(m_hComm, lpBuf, dwCount, &dwBytesRead, &overlapped);
  if (!bSuccess)
  {
    if (GetLastError() != ERROR_IO_PENDING)
    {
      TRACE(_T("Failed in call to ReadFile\n"));
      AfxThrowSerialException();
    }
  }
  else
  {
    if (pBytesRead)
      *pBytesRead = dwBytesRead;
  }
  return bSuccess;
}

DWORD CSerialPort::Write(const void* lpBuf, DWORD dwCount)
{
  ASSERT(IsOpen());
  ASSERT(!m_bOverlapped);

  DWORD dwBytesWritten = 0;
  if (!WriteFile(m_hComm, lpBuf, dwCount, &dwBytesWritten, NULL))
  {
    TRACE(_T("Failed in call to WriteFile\n"));
    AfxThrowSerialException();
  }

  return dwBytesWritten;
}

BOOL CSerialPort::Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesWritten)
{
  ASSERT(IsOpen());
  ASSERT(m_bOverlapped);

  DWORD dwBytesWritten = 0;
  BOOL bSuccess = WriteFile(m_hComm, lpBuf, dwCount, &dwBytesWritten, &overlapped);
  if (!bSuccess)
  {
    if (GetLastError() != ERROR_IO_PENDING)
    {
      TRACE(_T("Failed in call to WriteFile\n"));
      AfxThrowSerialException();

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
97成人超碰视| 久久成人麻豆午夜电影| 色香蕉久久蜜桃| 亚洲人一二三区| 色999日韩国产欧美一区二区| 国产精品免费视频网站| 91福利视频网站| 三级久久三级久久| 欧美xxxxx牲另类人与| 国产一区二区三区在线观看精品 | 欧美bbbbb| 日韩免费高清视频| 成人免费毛片片v| 亚洲欧洲中文日韩久久av乱码| 欧美在线免费观看亚洲| 奇米综合一区二区三区精品视频| 26uuu精品一区二区三区四区在线| 粉嫩一区二区三区性色av| 亚洲日本一区二区| 欧美另类z0zxhd电影| 国产一区福利在线| 亚洲日本在线视频观看| 欧美一区二区精品久久911| 国产91精品露脸国语对白| 亚洲一区av在线| 欧美大胆人体bbbb| 91猫先生在线| 九九精品一区二区| 一区二区三区日韩欧美精品| 日韩欧美一区二区三区在线| 91丨九色丨国产丨porny| 日本va欧美va精品| 成人欧美一区二区三区黑人麻豆| 欧美乱妇23p| 成人免费毛片嘿嘿连载视频| 日韩av高清在线观看| 国产精品久久久久9999吃药| 日韩午夜在线观看视频| 成人看片黄a免费看在线| 日本系列欧美系列| 成人免费在线播放视频| 精品国产a毛片| 91成人国产精品| 国产高清不卡二三区| 日韩不卡一区二区三区| 伊人开心综合网| 国产精品久久久久久福利一牛影视| 91精品国产欧美一区二区18| 在线免费观看日韩欧美| 成人美女视频在线观看| 极品尤物av久久免费看| 日本在线不卡视频| 亚洲一区二区不卡免费| 亚洲蜜桃精久久久久久久| 国产精品天天摸av网| 精品国产乱码久久久久久闺蜜| 欧美日韩精品专区| 色综合久久66| av亚洲产国偷v产偷v自拍| 国产精品一区在线观看你懂的| 日本人妖一区二区| 亚洲大片在线观看| 亚洲欧美日韩中文字幕一区二区三区| 国产色综合一区| 久久久不卡影院| 久久免费国产精品| 欧美成人精精品一区二区频| 欧美日韩电影在线播放| 欧日韩精品视频| 欧美在线999| 日本高清无吗v一区| 91麻豆产精品久久久久久| 成人国产精品免费观看视频| 国产成人av电影在线| 高清久久久久久| 成人app在线| 色综合久久久久综合99| 色综合天天综合| 色噜噜偷拍精品综合在线| 91在线国内视频| 日韩欧美精品在线视频| 日韩一区二区免费高清| 日韩欧美国产电影| 久久精品免费在线观看| 亚洲国产精品黑人久久久| 欧美国产日本韩| 日韩美女啊v在线免费观看| 一区二区三区成人| 午夜精品久久一牛影视| 蜜臀av性久久久久蜜臀aⅴ流畅| 蜜臀av性久久久久蜜臀aⅴ| 国产一区啦啦啦在线观看| 成人的网站免费观看| 色婷婷久久久综合中文字幕| 欧美人牲a欧美精品| 日韩视频免费观看高清完整版在线观看 | 久久综合九色综合久久久精品综合| 日韩免费电影一区| 欧美国产日韩亚洲一区| 玉足女爽爽91| 久久精工是国产品牌吗| 高清av一区二区| 欧美影院午夜播放| 日韩三级av在线播放| 国产色爱av资源综合区| 亚洲欧美日韩国产一区二区三区| 午夜伦欧美伦电影理论片| 国产美女一区二区三区| 91国偷自产一区二区三区成为亚洲经典| 欧美日韩国产精品自在自线| 精品电影一区二区| 亚洲麻豆国产自偷在线| 秋霞午夜鲁丝一区二区老狼| 成人小视频在线观看| 欧美久久久影院| 国产欧美va欧美不卡在线| 亚洲一二三四区| 国产乱人伦偷精品视频不卡 | 丁香六月综合激情| 欧美色手机在线观看| 久久一区二区三区四区| 一区二区三区四区五区视频在线观看 | av网站一区二区三区| 欧美一级理论片| 国产精品人人做人人爽人人添| 天堂av在线一区| 不卡电影一区二区三区| 日韩免费成人网| 亚洲午夜一区二区| 成人精品国产福利| 日韩一二在线观看| 亚洲综合色丁香婷婷六月图片| 国产麻豆一精品一av一免费| 7799精品视频| 亚洲女人****多毛耸耸8| 国产成人99久久亚洲综合精品| 欧美久久一区二区| 一区2区3区在线看| 粉嫩嫩av羞羞动漫久久久 | 免费在线一区观看| 91福利在线播放| 亚洲日本丝袜连裤袜办公室| 国产一区二区三区免费在线观看| 欧美日韩国产不卡| 亚洲精品国产a久久久久久| 国产高清视频一区| 337p粉嫩大胆噜噜噜噜噜91av| 一区二区免费视频| 成人av在线影院| 日本一二三四高清不卡| 国产一区二区在线免费观看| 欧美电影免费观看高清完整版在线 | 成人av网站免费观看| www激情久久| 捆绑调教一区二区三区| 欧美日韩日日摸| 午夜精品成人在线视频| 欧美色倩网站大全免费| 亚洲一区二区三区在线| 欧美色图第一页| 亚洲gay无套男同| 欧美区在线观看| 亚洲福利一区二区| 欧美日韩国产首页| 婷婷开心激情综合| 欧美日韩一区二区电影| 午夜伊人狠狠久久| 欧美精品v国产精品v日韩精品| 亚洲高清在线视频| 欧美老女人在线| 秋霞影院一区二区| 精品1区2区在线观看| 国产乱一区二区| 中文字幕一区二区三区不卡 | 欧美色倩网站大全免费| 亚洲成人tv网| 日韩欧美在线综合网| 国产中文字幕精品| 国产欧美日韩久久| 91在线看国产| 日日摸夜夜添夜夜添精品视频| 欧美精品一卡两卡| 另类小说一区二区三区| 精品国产伦一区二区三区观看体验 | 欧美日韩不卡一区二区| 男人的天堂亚洲一区| 26uuu欧美| 色综合久久88色综合天天免费| 亚洲综合999| 欧美成人艳星乳罩| 成人黄色777网| 图片区日韩欧美亚洲| 欧美精品一区二区在线观看| 成人精品国产免费网站| 亚洲一区二区三区自拍| xnxx国产精品| 在线亚洲人成电影网站色www| 麻豆精品视频在线观看免费| 国产欧美日韩一区二区三区在线观看| 一本到三区不卡视频|