亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
日韩精品福利网| 国产精品自在在线| 日韩电影一区二区三区四区| 蜜桃视频一区二区三区在线观看| 午夜视频在线观看一区| 日韩精品一二三四| 国产99久久久精品| 欧美日韩精品是欧美日韩精品| 欧美不卡一二三| 亚洲九九爱视频| 国内精品视频666| 91精品国产综合久久精品麻豆| 国产成人午夜电影网| 色天天综合色天天久久| 26uuu久久天堂性欧美| 亚洲一区二区三区四区的| 国产乱码精品一区二区三区五月婷 | 国产精品久久久久永久免费观看 | 国产成人精品一区二| 欧美三级日韩在线| 国产精品电影一区二区| 国产一区二区三区免费在线观看| 欧美日韩色一区| 亚洲摸摸操操av| 不卡一卡二卡三乱码免费网站| 精品免费视频一区二区| 免费成人小视频| 精品成人a区在线观看| 亚洲成人动漫一区| a4yy欧美一区二区三区| 国产精品蜜臀在线观看| 国产成人精品网址| 国产精品久线在线观看| 成人永久免费视频| 亚洲欧洲99久久| 欧美性生交片4| 亚洲一二三区视频在线观看| 91久久精品一区二区| 午夜不卡在线视频| 日韩欧美国产综合在线一区二区三区| 视频在线在亚洲| 精品国内片67194| 成人精品国产福利| 亚洲国产综合人成综合网站| 欧美丰满少妇xxxxx高潮对白| 奇米精品一区二区三区在线观看 | 午夜影院久久久| 久久亚洲一级片| 91精品福利在线| 久久精品国产精品亚洲精品| 国产精品久久久久久亚洲伦| 91精品国产综合久久婷婷香蕉| 成人国产精品视频| 奇米888四色在线精品| 欧美国产成人精品| 欧美一区二区在线观看| 不卡电影一区二区三区| 麻豆久久一区二区| 一区二区免费视频| 国产亚洲精品精华液| 日韩限制级电影在线观看| av成人免费在线观看| 国产精品一线二线三线| 丰满少妇在线播放bd日韩电影| 亚洲成av人片在线观看| 一区二区三区鲁丝不卡| 国产丝袜美腿一区二区三区| 日韩久久精品一区| 欧美女孩性生活视频| 色综合久久久久久久久| 99国产精品国产精品久久| 99精品视频一区二区| 欧美α欧美αv大片| 欧洲亚洲国产日韩| 成人午夜视频免费看| 国产一区二区三区免费播放| 琪琪久久久久日韩精品| 同产精品九九九| 午夜精彩视频在线观看不卡| 亚洲1区2区3区视频| 日韩成人精品在线| 久久精品国产久精国产爱| 久久99久久99精品免视看婷婷| 六月丁香综合在线视频| 精品一区二区免费在线观看| 国内国产精品久久| 97se狠狠狠综合亚洲狠狠| 欧美亚洲国产一区二区三区va| 91精品国产综合久久久蜜臀图片| 欧美老女人第四色| 日韩欧美成人午夜| 中文字幕一区二区三区乱码在线 | 国产亚洲人成网站| 亚洲色图一区二区三区| 亚洲激情综合网| 久久99久国产精品黄毛片色诱| 粉嫩久久99精品久久久久久夜| 在线视频欧美精品| 久久久久久久电影| 亚洲成av人片一区二区三区| 激情综合色综合久久| 91成人免费网站| 欧美xxxxxxxx| 日本aⅴ免费视频一区二区三区| 国内精品国产成人国产三级粉色| 亚洲成人自拍偷拍| 高清久久久久久| 日韩一区二区三免费高清| 国产精品视频一二| 久久99久久99| 欧美一二三区在线观看| 一区二区三区四区蜜桃| 国内精品视频一区二区三区八戒| 欧洲精品一区二区| 国产精品久久久久久久浪潮网站| 美女性感视频久久| 欧美一区二区三区日韩视频| 亚洲一区二区三区自拍| 色综合色综合色综合色综合色综合| 日韩精品一区二区三区中文不卡| 亚洲一级二级在线| 欧美调教femdomvk| 亚洲黄网站在线观看| 欧美少妇xxx| 日本va欧美va瓶| 日韩欧美中文字幕制服| 国产综合色在线视频区| 欧美国产成人在线| 99精品国产热久久91蜜凸| 亚洲视频综合在线| 在线观看亚洲精品视频| 亚洲一区视频在线| 日韩精品最新网址| 成人激情午夜影院| 日韩高清在线电影| 国产亚洲精品福利| 97久久超碰国产精品电影| 亚洲精品免费一二三区| 91麻豆精品久久久久蜜臀| 国产成人精品午夜视频免费| 一区二区三区日韩精品视频| 欧美一级在线免费| 欧美日韩一区三区| 久久国产精品区| 亚洲欧美福利一区二区| 91精品国产高清一区二区三区 | 欧美日韩视频在线观看一区二区三区| 丝袜美腿亚洲一区| 国产精品嫩草影院av蜜臀| 欧美精品亚洲二区| kk眼镜猥琐国模调教系列一区二区 | 麻豆精品国产传媒mv男同| 亚洲欧美在线观看| 久久众筹精品私拍模特| 欧美无人高清视频在线观看| 成人一级黄色片| 国产老女人精品毛片久久| 国产福利精品一区二区| 黑人巨大精品欧美黑白配亚洲| 亚洲成人免费看| 性做久久久久久| 亚洲国产日韩在线一区模特 | 91视频免费看| 99re这里只有精品首页| 成人免费高清视频在线观看| 国产成人免费视频网站高清观看视频 | 秋霞国产午夜精品免费视频| 91精品福利在线一区二区三区 | 精品免费99久久| 在线视频中文字幕一区二区| 色呦呦网站一区| 欧美精品v国产精品v日韩精品 | 精品一区二区三区在线播放视频| 亚洲妇女屁股眼交7| 视频在线在亚洲| 国产美女在线观看一区| 风间由美一区二区av101| 99v久久综合狠狠综合久久| 欧美亚一区二区| 欧美va天堂va视频va在线| 久久久久久久综合| 亚洲成人午夜影院| 国产v日产∨综合v精品视频| 91小视频免费看| 欧美丰满少妇xxxbbb| 国产精品国产a级| 九九在线精品视频| 欧美日韩一区二区在线视频| 欧美mv日韩mv国产网站| 亚洲在线成人精品| 日本乱码高清不卡字幕| 国产日韩欧美不卡| 五月天激情小说综合| 色琪琪一区二区三区亚洲区| 日韩精品自拍偷拍| 九九精品视频在线看| 欧美视频一区在线| 亚洲高清三级视频| 欧美午夜免费电影| 亚洲午夜私人影院|