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

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? serialport.cpp

?? 該程序實現了串口之間的相互通信
?? 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);
  m_hComm = CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, bOverlapped ? FILE_FLAG_OVERLAPPED : 0, NULL);
  if (m_hComm == INVALID_HANDLE_VALUE)
  {
    TRACE(_T("Failed to open up the comms port\n"));
    AfxThrowSerialException();
  }

  //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 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
    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 dwBytesRead = 0;
  if (!ReadFile(m_hComm, lpBuf, dwCount, &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;
  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();
    }
  }
  else
  {
    if (pBytesWritten)
      *pBytesWritten = dwBytesWritten;
  }

  return bSuccess;
}

void CSerialPort::GetOverlappedResult(OVERLAPPED& overlapped, DWORD& dwBytesTransferred, BOOL bWait)
{
  ASSERT(IsOpen());
  ASSERT(m_bOverlapped);

  if (!::GetOverlappedResult(m_hComm, &overlapped, &dwBytesTransferred, bWait))
  {
    if (GetLastError() != ERROR_IO_PENDING)
    {
      TRACE(_T("Failed in call to GetOverlappedResult\n"));

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
一区二区三区四区蜜桃| 五月婷婷久久综合| 欧美精品色综合| 粉嫩av一区二区三区| 亚洲第一久久影院| 亚洲欧洲日韩女同| 精品国产91乱码一区二区三区| 色婷婷激情综合| 国产经典欧美精品| 裸体健美xxxx欧美裸体表演| 亚洲视频一二区| 中文在线一区二区| 91啪九色porn原创视频在线观看| 国内精品自线一区二区三区视频| 香港成人在线视频| 一区二区三区蜜桃网| 国产精品免费久久久久| 久久网站热最新地址| 5566中文字幕一区二区电影| 色妞www精品视频| 不卡一卡二卡三乱码免费网站| 国产自产高清不卡| 韩国一区二区三区| 看国产成人h片视频| 视频在线在亚洲| 亚洲成年人网站在线观看| 亚洲精品国产第一综合99久久 | 日韩av在线发布| 亚洲成人三级小说| 亚洲一区二区成人在线观看| 亚洲视频一二三区| 国产精品嫩草99a| 国产精品美女久久久久久久网站| 久久亚洲一区二区三区四区| www国产精品av| 久久色在线观看| 久久久久久久国产精品影院| 欧美不卡一区二区三区| 日韩美女视频在线| 久久久综合精品| 欧美激情一区在线观看| 国产精品视频观看| 亚洲视频免费看| 一区二区三区精品视频| 亚洲一区二区三区不卡国产欧美| 亚洲高清免费观看高清完整版在线观看| 亚洲精品国产品国语在线app| 亚洲自拍偷拍av| 午夜电影久久久| 老司机免费视频一区二区| 国内成人精品2018免费看| 国产.精品.日韩.另类.中文.在线.播放| 国产伦理精品不卡| 99精品黄色片免费大全| 在线观看亚洲精品| 91精品黄色片免费大全| 精品国产乱码久久久久久免费 | 午夜精品一区二区三区三上悠亚| 午夜精品福利一区二区蜜股av| 五月综合激情婷婷六月色窝| 麻豆国产91在线播放| 国产精品88av| 91福利资源站| 欧美一区二区福利在线| 久久精品人人爽人人爽| 亚洲精品乱码久久久久| 蜜臀久久久久久久| 福利一区二区在线| 欧美亚洲综合久久| 精品国产三级a在线观看| 国产精品视频你懂的| 一区av在线播放| 精品中文字幕一区二区| 不卡的av中国片| 欧美高清精品3d| 国产欧美精品在线观看| 洋洋av久久久久久久一区| 另类综合日韩欧美亚洲| 99久久精品免费看国产免费软件| 欧美福利一区二区| 欧美激情一区二区三区全黄| 亚洲大片精品永久免费| 在线一区二区三区做爰视频网站| 91精品欧美综合在线观看最新 | 婷婷夜色潮精品综合在线| 国产麻豆精品视频| 精品视频全国免费看| 欧美极品少妇xxxxⅹ高跟鞋| 亚洲国产sm捆绑调教视频 | 成人福利视频在线| 欧美福利视频一区| 亚洲三级久久久| 国产麻豆欧美日韩一区| 欧美日韩成人综合在线一区二区 | 亚洲成av人影院在线观看网| 国产精品2024| 欧美一区二区三区精品| 亚洲欧美一区二区不卡| 国产自产高清不卡| 欧美老女人在线| 亚洲人午夜精品天堂一二香蕉| 精品一区二区国语对白| 欧美撒尿777hd撒尿| 国产精品福利一区二区| 国产在线播放一区| 5566中文字幕一区二区电影| 亚洲精品高清视频在线观看| 岛国精品在线播放| 精品91自产拍在线观看一区| 亚洲国产精品视频| 色综合久久综合网欧美综合网 | 五月综合激情日本mⅴ| 91美女福利视频| 日本一区二区在线不卡| 九色综合狠狠综合久久| 91精品国产日韩91久久久久久| 亚洲摸摸操操av| 99视频国产精品| 中文一区二区在线观看| 国产福利一区在线| 久久久亚洲高清| 国内不卡的二区三区中文字幕 | 国产亚洲人成网站| 麻豆精品国产传媒mv男同| 6080国产精品一区二区| 亚洲福中文字幕伊人影院| 91久久精品一区二区二区| 成人欧美一区二区三区在线播放| 成人开心网精品视频| 国产日韩影视精品| 顶级嫩模精品视频在线看| 久久综合久久综合亚洲| 韩国成人在线视频| 久久精品亚洲一区二区三区浴池 | 国产成人日日夜夜| 国产喷白浆一区二区三区| 国产毛片精品视频| 国产欧美久久久精品影院| 成人中文字幕电影| 国产精品久久影院| 99国产麻豆精品| 亚洲一区二区三区四区的| 欧美性猛片xxxx免费看久爱| 亚洲一二三区在线观看| 欧美福利视频一区| 久久精工是国产品牌吗| 久久久亚洲精品一区二区三区| 国产成人高清在线| 中文字幕一区二区不卡| 在线视频亚洲一区| 丝袜亚洲精品中文字幕一区| 欧美电影影音先锋| 国内一区二区在线| 国产精品无人区| 色狠狠桃花综合| 日韩中文字幕av电影| 精品国产露脸精彩对白 | 国模套图日韩精品一区二区| 国产亚洲精品bt天堂精选| 9色porny自拍视频一区二区| 亚洲精品国产视频| 日韩欧美一卡二卡| 国产成人高清视频| 亚洲午夜免费视频| 精品国产三级a在线观看| 成人h精品动漫一区二区三区| 一区二区三区久久| 精品国产亚洲一区二区三区在线观看| 高清成人在线观看| 亚洲一区二区三区四区五区中文 | 国产三级一区二区| 一本大道久久a久久综合| 日本va欧美va瓶| 欧美国产日韩一二三区| 91天堂素人约啪| 久久国产剧场电影| |精品福利一区二区三区| 91精品国产福利在线观看| 国产精品69毛片高清亚洲| 一区二区三区美女视频| 26uuu亚洲综合色欧美| 91激情五月电影| 韩国成人在线视频| 亚洲成年人影院| 国产精品国产三级国产aⅴ无密码| 欧美日韩午夜在线| 成人美女视频在线观看18| 日韩av中文字幕一区二区| ㊣最新国产の精品bt伙计久久| 69堂成人精品免费视频| 97精品久久久午夜一区二区三区 | 视频一区欧美精品| 国产精品国产三级国产aⅴ中文| 欧美日韩国产首页| 99久久久久免费精品国产| 久久精品久久精品| 亚洲一区二区三区美女| 中文欧美字幕免费| 日韩欧美国产成人一区二区| 在线视频综合导航|