亚洲欧美第一页_禁久久精品乱码_粉嫩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);
  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();

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
136国产福利精品导航| 中日韩免费视频中文字幕| 一区二区欧美国产| 欧洲一区在线电影| 性欧美疯狂xxxxbbbb| 欧美肥妇毛茸茸| 美腿丝袜亚洲一区| 久久久亚洲国产美女国产盗摄| 韩国欧美国产一区| 中文字幕av资源一区| 色综合夜色一区| 日韩高清不卡一区| 久久色.com| 91碰在线视频| 亚洲成精国产精品女| 精品国产a毛片| 成人app网站| 视频一区欧美精品| 国产午夜精品一区二区三区视频 | 午夜精品视频一区| 欧美电视剧免费观看| 成人视屏免费看| 亚洲一区二区偷拍精品| 日韩一级高清毛片| av亚洲精华国产精华精华| 午夜电影久久久| 久久久国产精品午夜一区ai换脸| 一本色道久久综合亚洲aⅴ蜜桃 | 五月婷婷综合网| 国产午夜精品久久| 欧美日韩成人一区二区| 国产一区二区美女诱惑| 亚洲精品欧美激情| 久久品道一品道久久精品| 欧美在线视频全部完| 国产一区激情在线| 亚洲国产一区二区在线播放| 国产三级精品三级| 欧美一区二区精品在线| 99免费精品视频| 精品中文字幕一区二区| 亚洲午夜在线观看视频在线| 久久久久久久久久看片| 欧美色综合久久| 国产91综合一区在线观看| 亚洲成人av电影| 亚洲视频一二区| 久久这里只精品最新地址| 欧美日韩一本到| 99免费精品在线| 国产成人精品综合在线观看 | 久久综合九色综合97婷婷女人 | 极品瑜伽女神91| 亚洲一区二区视频在线| 国产精品国产精品国产专区不片| 精品久久久久久综合日本欧美| 欧美日韩亚洲国产综合| 91猫先生在线| 99免费精品在线观看| 国产精品91一区二区| 蜜桃一区二区三区在线观看| 午夜精品久久久久久久久久| 一区二区中文字幕在线| 国产欧美一区二区精品性| 精品久久99ma| 日韩视频免费观看高清完整版| 欧美日韩在线精品一区二区三区激情| 93久久精品日日躁夜夜躁欧美| 国产成人av电影在线| 国产精品一区二区在线看| 麻豆成人久久精品二区三区小说| 午夜婷婷国产麻豆精品| 亚洲国产综合视频在线观看| 亚洲丰满少妇videoshd| 亚洲一区二区三区四区五区黄 | 国产精品欧美久久久久一区二区| 26uuu久久天堂性欧美| 日韩欧美精品在线视频| 精品久久久久久久一区二区蜜臀| 精品国产制服丝袜高跟| 久久―日本道色综合久久| 欧美精品一区二区不卡| 久久久久久久久久看片| 中文字幕av免费专区久久| 国产精品久久久久久妇女6080| 中文字幕亚洲不卡| 亚洲免费视频中文字幕| 亚洲成人av中文| 久久激五月天综合精品| 国产精品资源在线| 99久久精品国产精品久久| 91久久奴性调教| 91精品国产欧美一区二区18| 日韩午夜在线观看视频| 久久亚洲私人国产精品va媚药| 久久久久久久av麻豆果冻| 国产精品久久久久影视| 亚洲国产美国国产综合一区二区| 天天综合天天综合色| 久久不见久久见中文字幕免费| 国产精品一区在线观看乱码| 成人app网站| 欧美人动与zoxxxx乱| 26uuu欧美| 一区二区三区色| 久久国产精品免费| www.欧美精品一二区| 欧美在线视频全部完| 精品sm捆绑视频| 中文字幕不卡在线观看| 亚洲成人在线网站| 国产精品1区2区3区在线观看| 97久久久精品综合88久久| 4438x亚洲最大成人网| 欧美激情在线一区二区| 亚洲成a人v欧美综合天堂| 国产在线视视频有精品| 在线欧美小视频| 久久精品视频网| 午夜婷婷国产麻豆精品| 成人性色生活片| 91精品中文字幕一区二区三区| 欧美激情在线一区二区三区| 天堂va蜜桃一区二区三区漫画版| 国产成人综合网| 欧美一区在线视频| 亚洲人成网站色在线观看| 精品在线免费观看| 91久久免费观看| 国产精品天美传媒| 免费欧美日韩国产三级电影| www.亚洲国产| 久久久久一区二区三区四区| 性感美女极品91精品| av在线不卡电影| 久久婷婷综合激情| 日本不卡在线视频| 色噜噜狠狠色综合中国| 国产精品人成在线观看免费| 美女视频第一区二区三区免费观看网站 | 色综合久久久久久久久| 久久嫩草精品久久久精品| 奇米精品一区二区三区在线观看| 色天使久久综合网天天| 国产精品久久久久久一区二区三区| 久久av资源站| 日韩一区二区三区精品视频| 亚洲一卡二卡三卡四卡无卡久久| 岛国精品一区二区| 国产日韩欧美高清| 国产一区二区日韩精品| 精品入口麻豆88视频| 日本成人中文字幕在线视频| 欧美片网站yy| 午夜视频一区二区三区| 欧美亚洲另类激情小说| 日韩久久一区二区| 99视频一区二区三区| 国产精品麻豆欧美日韩ww| 成人免费视频一区| 欧美韩国日本不卡| 成人av电影在线观看| 国产精品久久久一本精品 | 91视频免费观看| 成人欧美一区二区三区视频网页| 成人午夜电影小说| 国产精品理论在线观看| 成人黄色软件下载| 日韩一区欧美小说| 色av一区二区| 亚洲国产综合视频在线观看| 欧美性生活久久| 日韩va亚洲va欧美va久久| 欧美一级二级三级蜜桃| 久久成人综合网| 国产欧美一区二区精品性色超碰| 懂色av一区二区三区蜜臀| 国产精品久久一卡二卡| 91啦中文在线观看| 亚洲一区二区中文在线| 91.麻豆视频| 激情综合五月天| 欧美国产精品一区二区| www.成人网.com| 亚洲一区二区三区精品在线| 欧美高清激情brazzers| 另类小说图片综合网| 久久九九久久九九| 色婷婷综合中文久久一本| 亚洲国产日产av| 26uuu国产在线精品一区二区| 国产成人一区二区精品非洲| 国产精品护士白丝一区av| 欧美午夜精品久久久| 麻豆成人免费电影| 最新不卡av在线| 日韩欧美亚洲国产精品字幕久久久| 国产成人在线视频网站| 亚洲国产精品久久久男人的天堂| 欧美一区二区日韩|