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

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

?? serial.cpp

?? 串口通信,一個簡單的例程,對學習WINCE很有幫助,尤其是串口編程.希望大家喜歡
?? CPP
?? 第 1 頁 / 共 3 頁
字號:
//	Serial.cpp - Implementation of the CSerial class
//
//	Copyright (C) 1999-2003 Ramon de Klein (Ramon.de.Klein@ict.nl)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


//////////////////////////////////////////////////////////////////////
// Include the standard header files

#define STRICT
#include <crtdbg.h>
#include <tchar.h>
#include <windows.h>


//////////////////////////////////////////////////////////////////////
// Include module headerfile

#include "Serial.h"


//////////////////////////////////////////////////////////////////////
// Disable warning C4127: conditional expression is constant, which
// is generated when using the _RPTF and _ASSERTE macros.

#pragma warning(disable: 4127)


//////////////////////////////////////////////////////////////////////
// Enable debug memory manager

#ifdef _DEBUG

#ifdef THIS_FILE
#undef THIS_FILE
#endif

static const char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW

#endif


//////////////////////////////////////////////////////////////////////
// Helper methods

inline void CSerial::CheckRequirements (LPOVERLAPPED lpOverlapped, DWORD dwTimeout) const
{
#ifdef SERIAL_NO_OVERLAPPED

	// Check if an overlapped structure has been specified
	if (lpOverlapped || (dwTimeout != INFINITE))
	{
		// Quit application
		::MessageBox(0,_T("Overlapped I/O and time-outs are not supported, when overlapped I/O is disabled."),_T("Serial library"), MB_ICONERROR | MB_TASKMODAL);
		::DebugBreak();
		::ExitProcess(0xFFFFFFF);
	}

#endif

#ifdef SERIAL_NO_CANCELIO

	// Check if 0 or INFINITE time-out has been specified, because
	// the communication I/O cannot be cancelled.
	if ((dwTimeout != 0) && (dwTimeout != INFINITE))
	{
		// Quit application
		::MessageBox(0,_T("Timeouts are not supported, when SERIAL_NO_CANCELIO is defined"),_T("Serial library"), MB_ICONERROR | MB_TASKMODAL);
		::DebugBreak();
		::ExitProcess(0xFFFFFFF);
	}

#endif	// SERIAL_NO_CANCELIO

	// Avoid warnings
	(void) dwTimeout;
	(void) lpOverlapped;
}

inline BOOL CSerial::CancelCommIo (void)
{
#ifdef SERIAL_NO_CANCELIO
	// CancelIo shouldn't have been called at this point
	::DebugBreak();
	return FALSE;
#else

	// Cancel the I/O request
	return ::CancelIo(m_hFile);

#endif	// SERIAL_NO_CANCELIO
}


//////////////////////////////////////////////////////////////////////
// Code

CSerial::CSerial ()
	: m_lLastError(ERROR_SUCCESS)
	, m_hFile(0)
	, m_eEvent(EEventNone)
	, m_dwEventMask(0)
#ifndef SERIAL_NO_OVERLAPPED
	, m_hevtOverlapped(0)
#endif
{
}

CSerial::~CSerial ()
{
	// If the device is already closed,
	// then we don't need to do anything.
	if (m_hFile)
	{
		// Display a warning
		_RPTF0(_CRT_WARN,"CSerial::~CSerial - Serial port not closed\n");

		// Close implicitly
		Close();
	}
}

CSerial::EPort CSerial::CheckPort (LPCTSTR lpszDevice)
{
	// Try to open the device
	HANDLE hFile = ::CreateFile(lpszDevice, 
						   GENERIC_READ|GENERIC_WRITE, 
						   0, 
						   0, 
						   OPEN_EXISTING, 
						   0,
						   0);

	// Check if we could open the device
	if (hFile == INVALID_HANDLE_VALUE)
	{
		// Display error
		switch (::GetLastError())
		{
		case ERROR_FILE_NOT_FOUND:
			// The specified COM-port does not exist
			return EPortNotAvailable;

		case ERROR_ACCESS_DENIED:
			// The specified COM-port is in use
			return EPortInUse;

		default:
			// Something else is wrong
			return EPortUnknownError;
		}
	}

	// Close handle
	::CloseHandle(hFile);

	// Port is available
	return EPortAvailable;
}

LONG CSerial::Open (LPCTSTR lpszDevice, DWORD dwInQueue, DWORD dwOutQueue, bool fOverlapped)
{
	// Reset error state
	m_lLastError = ERROR_SUCCESS;

	// Check if the port isn't already opened
	if (m_hFile)
	{
		m_lLastError = ERROR_ALREADY_INITIALIZED;
		_RPTF0(_CRT_WARN,"CSerial::Open - Port already opened\n");
		return m_lLastError;
	}

	// Open the device
	m_hFile = ::CreateFile(lpszDevice,
						   GENERIC_READ|GENERIC_WRITE,
						   0,
						   0,
						   OPEN_EXISTING,
						   fOverlapped?FILE_FLAG_OVERLAPPED:0,
						   0);
	if (m_hFile == INVALID_HANDLE_VALUE)
	{
		// Reset file handle
		m_hFile = 0;

		// Display error
		m_lLastError = ::GetLastError();
		_RPTF0(_CRT_WARN, "CSerial::Open - Unable to open port\n");
		return m_lLastError;
	}

#ifndef SERIAL_NO_OVERLAPPED
	// We cannot have an event handle yet
	_ASSERTE(m_hevtOverlapped == 0);

	// Create the event handle for internal overlapped operations (manual reset)
	if (fOverlapped)
	{
		m_hevtOverlapped = ::CreateEvent(0,true,false,0);
		if (m_hevtOverlapped == 0)
		{
			// Obtain the error information
			m_lLastError = ::GetLastError();
			_RPTF0(_CRT_WARN,"CSerial::Open - Unable to create event\n");

			// Close the port
			::CloseHandle(m_hFile);
			m_hFile = 0;

			// Return the error
			return m_lLastError;
		}
	}
#else
	
	// Overlapped flag shouldn't be specified
	_ASSERTE(!fOverlapped);

#endif

	// Setup the COM-port
	if (dwInQueue || dwOutQueue)
	{
		// Make sure the queue-sizes are reasonable sized. Win9X systems crash
		// if the input queue-size is zero. Both queues need to be at least
		// 16 bytes large.
		_ASSERTE(dwInQueue >= 16);
		_ASSERTE(dwOutQueue >= 16);

		if (!::SetupComm(m_hFile,dwInQueue,dwOutQueue))
		{
			// Display a warning
			long lLastError = ::GetLastError();
			_RPTF0(_CRT_WARN,"CSerial::Open - Unable to setup the COM-port\n");

			// Close the port
			Close();

			// Save last error from SetupComm
			m_lLastError = lLastError;
			return m_lLastError;	
		}
	}

	// Setup the default communication mask
	SetMask();

	// Non-blocking reads is default
	SetupReadTimeouts(EReadTimeoutNonblocking);

	// Setup the device for default settings
 	COMMCONFIG commConfig = {0};
	DWORD dwSize = sizeof(commConfig);
	commConfig.dwSize = dwSize;
	if (::GetDefaultCommConfig(lpszDevice,&commConfig,&dwSize))
	{
		// Set the default communication configuration
		if (!::SetCommConfig(m_hFile,&commConfig,dwSize))
		{
			// Display a warning
			_RPTF0(_CRT_WARN,"CSerial::Open - Unable to set default communication configuration.\n");
		}
	}
	else
	{
		// Display a warning
		_RPTF0(_CRT_WARN,"CSerial::Open - Unable to obtain default communication configuration.\n");
	}

	// Return successful
	return m_lLastError;
}

LONG CSerial::Close (void)
{
	// Reset error state
	m_lLastError = ERROR_SUCCESS;

	// If the device is already closed,
	// then we don't need to do anything.
	if (m_hFile == 0)
	{
		// Display a warning
		_RPTF0(_CRT_WARN,"CSerial::Close - Method called when device is not open\n");
		return m_lLastError;
	}

#ifndef SERIAL_NO_OVERLAPPED
	// Free event handle
	if (m_hevtOverlapped)
	{
		::CloseHandle(m_hevtOverlapped);
		m_hevtOverlapped = 0;
	}
#endif

	// Close COM port
	::CloseHandle(m_hFile);
	m_hFile = 0;

	// Return successful
	return m_lLastError;
}

LONG CSerial::Setup (EBaudrate eBaudrate, EDataBits eDataBits, EParity eParity, EStopBits eStopBits)
{
	// Reset error state
	m_lLastError = ERROR_SUCCESS;

	// Check if the device is open
	if (m_hFile == 0)
	{
		// Set the internal error code
		m_lLastError = ERROR_INVALID_HANDLE;

		// Issue an error and quit
		_RPTF0(_CRT_WARN,"CSerial::Setup - Device is not opened\n");
		return m_lLastError;
	}

	// Obtain the DCB structure for the device
	CDCB dcb;
	if (!::GetCommState(m_hFile,&dcb))
	{
		// Obtain the error code
		m_lLastError = ::	GetLastError();

		// Display a warning
		_RPTF0(_CRT_WARN,"CSerial::Setup - Unable to obtain DCB information\n");
		return m_lLastError;
	}

	// Set the new data
	dcb.BaudRate = DWORD(eBaudrate);
	dcb.ByteSize = BYTE(eDataBits);
	dcb.Parity   = BYTE(eParity);
	dcb.StopBits = BYTE(eStopBits);

	// Determine if parity is used
	dcb.fParity  = (eParity != EParNone);

	// Set the new DCB structure
	if (!::SetCommState(m_hFile,&dcb))
	{
		// Obtain the error code
		m_lLastError = ::GetLastError();

		// Display a warning
		_RPTF0(_CRT_WARN,"CSerial::Setup - Unable to set DCB information\n");
		return m_lLastError;
	}

	// Return successful
	return m_lLastError;
}

LONG CSerial::SetEventChar (BYTE bEventChar, bool fAdjustMask)
{
	// Reset error state
	m_lLastError = ERROR_SUCCESS;

	// Check if the device is open
	if (m_hFile == 0)
	{
		// Set the internal error code
		m_lLastError = ERROR_INVALID_HANDLE;

		// Issue an error and quit
		_RPTF0(_CRT_WARN,"CSerial::SetEventChar - Device is not opened\n");
		return m_lLastError;
	}

	// Obtain the DCB structure for the device
	CDCB dcb;
	if (!::GetCommState(m_hFile,&dcb))
	{
		// Obtain the error code
		m_lLastError = ::GetLastError();

		// Display a warning
		_RPTF0(_CRT_WARN,"CSerial::SetEventChar - Unable to obtain DCB information\n");
		return m_lLastError;
	}

	// Set the new event character
	dcb.EvtChar = char(bEventChar);

	// Adjust the event mask, to make sure the event will be received
	if (fAdjustMask)
	{
		// Enable 'receive event character' event.  Note that this
		// will generate an EEventNone if there is an asynchronous
		// WaitCommEvent pending.
		SetMask(GetEventMask() | EEventRcvEv);
	}

	// Set the new DCB structure
	if (!::SetCommState(m_hFile,&dcb))
	{
		// Obtain the error code
		m_lLastError = ::GetLastError();

		// Display a warning
		_RPTF0(_CRT_WARN,"CSerial::SetEventChar - Unable to set DCB information\n");
		return m_lLastError;
	}

	// Return successful
	return m_lLastError;
}

LONG CSerial::SetMask (DWORD dwEventMask)
{
	// Reset error state
	m_lLastError = ERROR_SUCCESS;

	// Check if the device is open
	if (m_hFile == 0)
	{
		// Set the internal error code
		m_lLastError = ERROR_INVALID_HANDLE;

		// Issue an error and quit
		_RPTF0(_CRT_WARN,"CSerial::SetMask - Device is not opened\n");
		return m_lLastError;
	}

	// Set the new mask. Note that this will generate an EEventNone
	// if there is an asynchronous WaitCommEvent pending.
	if (!::SetCommMask(m_hFile,dwEventMask))
	{
		// Obtain the error code
		m_lLastError = ::GetLastError();

		// Display a warning
		_RPTF0(_CRT_WARN,"CSerial::SetMask - Unable to set event mask\n");
		return m_lLastError;
	}

	// Save event mask and return successful
	m_dwEventMask = dwEventMask;
	return m_lLastError;
}

LONG CSerial::WaitEvent (LPOVERLAPPED lpOverlapped, DWORD dwTimeout)
{
	// Check if time-outs are supported
	CheckRequirements(lpOverlapped,dwTimeout);

	// Reset error state
	m_lLastError = ERROR_SUCCESS;

	// Check if the device is open
	if (m_hFile == 0)
	{
		// Set the internal error code
		m_lLastError = ERROR_INVALID_HANDLE;

		// Issue an error and quit
		_RPTF0(_CRT_WARN,"CSerial::WaitEvent - Device is not opened\n");
		return m_lLastError;
	}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产一区二区三区久久久| 国产精品国产三级国产aⅴ入口| 亚洲国产你懂的| 欧洲国产伦久久久久久久| 亚洲一区二区精品久久av| 欧美人妇做爰xxxⅹ性高电影| 亚洲成人高清在线| 日韩精品一区二区三区视频播放| 麻豆精品一区二区av白丝在线| 亚洲精品一区二区三区精华液| 国产白丝精品91爽爽久久| 亚洲欧美福利一区二区| 欧美日韩亚洲综合一区二区三区| 美女网站色91| 麻豆视频观看网址久久| 久久精品欧美日韩| 日本乱人伦aⅴ精品| 日韩国产精品久久久久久亚洲| 精品精品国产高清一毛片一天堂| 懂色av一区二区三区免费看| 一区二区三区视频在线看| 日韩一区二区视频| 成a人片国产精品| 午夜电影久久久| 国产欧美精品在线观看| 欧亚洲嫩模精品一区三区| 九九**精品视频免费播放| 成人欧美一区二区三区在线播放| 欧美日韩在线综合| 国产成人av影院| 午夜在线电影亚洲一区| 国产夜色精品一区二区av| 精品视频999| 成人国产精品免费| 日本午夜一本久久久综合| 日韩一区在线看| 精品国产91九色蝌蚪| 色综合久久精品| 国产一区二区精品在线观看| 夜夜揉揉日日人人青青一国产精品| 日韩欧美高清dvd碟片| 色综合天天综合色综合av| 精品一区二区三区久久| 一区二区三国产精华液| 国产日韩三级在线| 日韩一区和二区| 欧美色倩网站大全免费| 99这里都是精品| 国产综合久久久久影院| 亚洲成人免费视频| 国产suv一区二区三区88区| 亚洲国产aⅴ天堂久久| 国产精品久久久久久久蜜臀 | 99免费精品在线| 老司机免费视频一区二区三区| 日韩一区中文字幕| 欧美国产精品一区| www国产成人| 日韩欧美激情一区| 91精品婷婷国产综合久久性色| 欧美在线|欧美| 色婷婷久久久综合中文字幕| 成人黄色一级视频| 国产91丝袜在线播放| 国产一区二区电影| 久久精品99久久久| 久久精品国产亚洲5555| 蜜臀av亚洲一区中文字幕| 亚洲777理论| 亚洲国产美女搞黄色| 亚洲一区在线观看网站| 一区二区三区成人| 亚洲欧美偷拍卡通变态| 国产精品美女一区二区| 国产精品丝袜黑色高跟| 国产精品久久久久久久浪潮网站| 欧美激情一区二区三区| 欧美激情一区二区| 1024精品合集| 亚洲欧美日韩小说| 亚洲一区二区三区在线看| 夜夜操天天操亚洲| 亚洲成人自拍网| 视频一区在线播放| 蜜桃视频第一区免费观看| 精品亚洲国内自在自线福利| 激情五月婷婷综合| 国产成人av福利| 99这里只有精品| 在线免费观看日本欧美| 自拍偷拍亚洲激情| 99热精品一区二区| 日韩欧美综合一区| 成人性视频免费网站| 岛国精品在线观看| 91在线无精精品入口| 亚洲激情图片qvod| 亚洲一区二区在线免费观看视频| 中文字幕免费一区| 亚洲另类色综合网站| 国产一区二区三区四区五区入口| 欧美探花视频资源| 国产精品美女久久久久aⅴ| 日本不卡不码高清免费观看| 91色在线porny| 久久久三级国产网站| 日韩精品一区第一页| 色综合久久久久综合体桃花网| 久久精品无码一区二区三区| 欧美aa在线视频| 欧美专区日韩专区| 亚洲精品中文在线观看| 粉嫩高潮美女一区二区三区| 精品动漫一区二区三区在线观看| 性做久久久久久久免费看| 99久久99久久精品免费看蜜桃| 久久精品夜夜夜夜久久| 黑人精品欧美一区二区蜜桃| 制服丝袜日韩国产| 亚洲18女电影在线观看| 色呦呦一区二区三区| 国产精品久久久久久久久久免费看 | 国产欧美日韩麻豆91| 毛片一区二区三区| 欧美精品在欧美一区二区少妇| 亚洲婷婷在线视频| av在线播放不卡| 国产精品免费视频网站| 国产91丝袜在线播放九色| 精品国产人成亚洲区| 美女免费视频一区二区| 欧美一级欧美三级| 美女在线视频一区| 日韩一二三区视频| 久久精品99久久久| 精品国一区二区三区| 国产在线视频一区二区三区| 日韩美女天天操| 极品少妇xxxx精品少妇偷拍| 精品理论电影在线| 国产精品一级片在线观看| 久久午夜免费电影| 国产69精品久久777的优势| 欧美经典一区二区| 99综合电影在线视频| 亚洲精品免费播放| 欧美日韩亚洲高清一区二区| 五月综合激情日本mⅴ| 欧美一级二级三级蜜桃| 麻豆中文一区二区| 久久久99精品免费观看不卡| 丁香婷婷综合色啪| 亚洲自拍偷拍麻豆| 一区二区三区不卡视频在线观看| 欧洲亚洲精品在线| 日日摸夜夜添夜夜添亚洲女人| 日韩亚洲欧美成人一区| 国产制服丝袜一区| 中文字幕中文字幕中文字幕亚洲无线| 91视频91自| 日本中文字幕一区二区视频| 精品国产免费视频| av中文字幕在线不卡| 亚洲国产日韩a在线播放 | 久久精品亚洲乱码伦伦中文| 成人免费的视频| 亚洲午夜精品17c| 精品国产青草久久久久福利| av男人天堂一区| 亚洲国产成人91porn| 久久午夜老司机| 色综合久久久久| 激情五月婷婷综合网| 最新国产成人在线观看| 欧美一区二区三区啪啪| 成人免费视频国产在线观看| 亚洲自拍都市欧美小说| 亚洲精品在线观看网站| 色综合久久久久综合体桃花网| 日本网站在线观看一区二区三区 | 亚洲精品菠萝久久久久久久| 在线不卡欧美精品一区二区三区| 国产成人av一区二区三区在线 | 亚洲欧洲另类国产综合| 欧美精选午夜久久久乱码6080| 国产成人自拍高清视频在线免费播放| 亚洲精品视频在线看| 精品少妇一区二区三区日产乱码| 99re热视频精品| 毛片基地黄久久久久久天堂| 亚洲码国产岛国毛片在线| 精品91自产拍在线观看一区| 色婷婷亚洲一区二区三区| 国产美女主播视频一区| 午夜婷婷国产麻豆精品| 国产精品伦一区| 精品精品国产高清a毛片牛牛| 在线观看精品一区| 国产 日韩 欧美大片| 欧美日韩久久一区|