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

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

?? serialport.cpp

?? 這是一個用微軟的mscomm的源碼
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
#include "stdafx.h"
#include "SerialPort.h"

#include <assert.h>

 
//
// Constructor
//
CSerialPort::CSerialPort()
{
	m_hComm = NULL;

	// initialize overlapped structure members to zero
	m_ov.Offset = 0;
	m_ov.OffsetHigh = 0;

	// create events
	m_ov.hEvent = NULL;
	m_hWriteEvent = NULL;
	m_hShutdownEvent = NULL;

	m_szWriteBuffer = NULL;
	m_nWriteSize=1;

	m_bThreadAlive = FALSE;
}

//
// Delete dynamic memory
//
CSerialPort::~CSerialPort()
{
	do
	{
		SetEvent(m_hShutdownEvent);
	} while (m_bThreadAlive);


	TRACE("Thread ended\n");
	if(m_szWriteBuffer!=NULL)
		delete [] m_szWriteBuffer;
}



//
//  The CommThread Function.
//
UINT CSerialPort::CommThread(LPVOID pParam)
{
	// Cast the void pointer passed to the thread back to
	// a pointer of CSerialPort class
	CSerialPort *port = (CSerialPort*)pParam;
	
	// Set the status variable in the dialog class to
	// TRUE to indicate the thread is running.
	port->m_bThreadAlive = TRUE;	
		
	// Misc. variables
	DWORD BytesTransfered = 0; 
	DWORD Event = 0;
	DWORD CommEvent = 0;
	DWORD dwError = 0;
	COMSTAT comstat;
	BOOL  bResult = TRUE;
		
	// Clear comm buffers at startup
	if (port->m_hComm)		// check if the port is opened
		PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

	// begin forever loop.  This loop will run as long as the thread is alive.
	for (;;) 
	{ 

		// Make a call to WaitCommEvent().  This call will return immediatly
		// because our port was created as an async port (FILE_FLAG_OVERLAPPED
		// and an m_OverlappedStructerlapped structure specified).  This call will cause the 
		// m_OverlappedStructerlapped element m_OverlappedStruct.hEvent, which is part of the m_hEventArray to 
		// be placed in a non-signeled state if there are no bytes available to be read,
		// or to a signeled state if there are bytes available.  If this event handle 
		// is set to the non-signeled state, it will be set to signeled when a 
		// character arrives at the port.

		// we do this for each port!

		bResult = WaitCommEvent(port->m_hComm, &Event, &port->m_ov);

		if (!bResult)  
		{ 
			// If WaitCommEvent() returns FALSE, process the last error to determin
			// the reason..
			switch (dwError = GetLastError()) 
			{ 
			case ERROR_IO_PENDING: 	
				{ 
					// This is a normal return value if there are no bytes
					// to read at the port.
					// Do nothing and continue
					break;
				}
			case 87:
				{
					// Under Windows NT, this value is returned for some reason.
					// I have not investigated why, but it is also a valid reply
					// Also do nothing and continue.
					break;
				}
			default:
				{
					// All other error codes indicate a serious error has
					// occured.  Process this error.
					port->ProcessErrorMessage("WaitCommEvent()");
					break;
				}
			}
		}
		else
		{
			// If WaitCommEvent() returns TRUE, check to be sure there are
			// actually bytes in the buffer to read.  
			//
			// If you are reading more than one byte at a time from the buffer 
			// (which this program does not do) you will have the situation occur 
			// where the first byte to arrive will cause the WaitForMultipleObjects() 
			// function to stop waiting.  The WaitForMultipleObjects() function 
			// resets the event handle in m_OverlappedStruct.hEvent to the non-signelead state
			// as it returns.  
			//
			// If in the time between the reset of this event and the call to 
			// ReadFile() more bytes arrive, the m_OverlappedStruct.hEvent handle will be set again
			// to the signeled state. When the call to ReadFile() occurs, it will 
			// read all of the bytes from the buffer, and the program will
			// loop back around to WaitCommEvent().
			// 
			// At this point you will be in the situation where m_OverlappedStruct.hEvent is set,
			// but there are no bytes available to read.  If you proceed and call
			// ReadFile(), it will return immediatly due to the async port setup, but
			// GetOverlappedResults() will not return until the next character arrives.
			//
			// It is not desirable for the GetOverlappedResults() function to be in 
			// this state.  The thread shutdown event (event 0) and the WriteFile()
			// event (Event2) will not work if the thread is blocked by GetOverlappedResults().
			//
			// The solution to this is to check the buffer with a call to ClearCommError().
			// This call will reset the event handle, and if there are no bytes to read
			// we can loop back through WaitCommEvent() again, then proceed.
			// If there are really bytes to read, do nothing and proceed.
		
			bResult = ClearCommError(port->m_hComm, &dwError, &comstat);

			if (comstat.cbInQue == 0)
				continue;
		}	// end if bResult

		// Main wait function.  This function will normally block the thread
		// until one of nine events occur that require action.
		Event = WaitForMultipleObjects(3, port->m_hEventArray, FALSE, INFINITE);

		switch (Event)
		{
		case 0:
			{
				// Shutdown event.  This is event zero so it will be
				// the higest priority and be serviced first.
				CloseHandle(port->m_hComm);
				port->m_hComm=NULL;
				port->m_bThreadAlive = FALSE;
				// Kill this thread.  break is not needed, but makes me feel better.
				AfxEndThread(100);

				break;
			}
		case 1:	// read event
			{
				GetCommMask(port->m_hComm, &CommEvent);
				if (CommEvent & EV_RXCHAR)
					// Receive character event from port.
					ReceiveChar(port, comstat);
				if (CommEvent & EV_CTS)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_BREAK)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_ERR)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_RING)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				
				if (CommEvent & EV_RXFLAG)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
					
				break;
			}  
		case 2: // write event
			{
				// Write character event from port
				WriteChar(port);
				break;
			}

		} // end switch

	} // close forever loop

	return 0;
}

//
// start comm watching
//
BOOL CSerialPort::StartMonitoring()
{
	if (!(m_Thread = AfxBeginThread(CommThread, this)))
		return FALSE;
	TRACE("Thread started\n");
	return TRUE;	
}

//
// Restart the comm thread
//
BOOL CSerialPort::RestartMonitoring()
{
	TRACE("Thread resumed\n");
	m_Thread->ResumeThread();
	return TRUE;	
}


//
// Suspend the comm thread
//
BOOL CSerialPort::StopMonitoring()
{
	TRACE("Thread suspended\n");
	m_Thread->SuspendThread(); 
	return TRUE;	
}


//
// If there is a error, give the right message
//
void CSerialPort::ProcessErrorMessage(char* ErrorText)
{
	char *Temp = new char[200];
	
	LPVOID lpMsgBuf;

	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
		NULL,
		GetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,
		0,
		NULL 
	);

	sprintf(Temp, "WARNING:  %s Failed with the following error: \n%s\nPort: %d\n", (char*)ErrorText, lpMsgBuf, m_nPortNr); 
	MessageBox(NULL, Temp, "Application Error", MB_ICONSTOP);

	LocalFree(lpMsgBuf);
	delete [] Temp;
}

//
// Write a character.
//
void CSerialPort::WriteChar(CSerialPort* port)
{
	BOOL bWrite = TRUE;
	BOOL bResult = TRUE;

	DWORD BytesSent = 0;

	ResetEvent(port->m_hWriteEvent);

	// Gain ownership of the critical section
	EnterCriticalSection(&port->m_csCommunicationSync);

	if (bWrite)
	{
		// Initailize variables
		port->m_ov.Offset = 0;
		port->m_ov.OffsetHigh = 0;

		// Clear buffer
		PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

		bResult = WriteFile(port->m_hComm,							// Handle to COMM Port
							port->m_szWriteBuffer,					// Pointer to message buffer in calling finction
//							strlen((char*)port->m_szWriteBuffer),	// Length of message to send
							port->m_nWriteSize,	// Length of message to send
							&BytesSent,								// Where to store the number of bytes sent
							&port->m_ov);							// Overlapped structure

		// deal with any error codes
		if (!bResult)  
		{
			DWORD dwError = GetLastError();
			switch (dwError)
			{
				case ERROR_IO_PENDING:
					{
						// continue to GetOverlappedResults()
						BytesSent = 0;
						bWrite = FALSE;
						break;
					}
				default:
					{
						// all other error codes
						port->ProcessErrorMessage("WriteFile()");
					}
			}
		} 
		else
		{
			LeaveCriticalSection(&port->m_csCommunicationSync);
		}
	} // end if(bWrite)

	if (!bWrite)
	{
		bWrite = TRUE;
	
		bResult = GetOverlappedResult(port->m_hComm,	// Handle to COMM port 
									  &port->m_ov,		// Overlapped structure
									  &BytesSent,		// Stores number of bytes sent
									  TRUE); 			// Wait flag

		LeaveCriticalSection(&port->m_csCommunicationSync);

		// deal with the error code 
//		if (!bResult)  
		{
//			port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
		}	
	} // end if (!bWrite)

	// Verify that the data size send equals what we tried to send

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲成av人片| 精品久久99ma| 亚洲欧美日韩系列| 色综合天天综合狠狠| 一区二区日韩电影| 欧美人与禽zozo性伦| 肉色丝袜一区二区| 欧美精品一区二区三区视频| 国产在线日韩欧美| 欧美高清在线一区| 91视频com| 日韩黄色小视频| 久久奇米777| 成人午夜在线播放| 亚洲欧美一区二区三区久本道91| 在线观看91精品国产入口| 麻豆精品一区二区av白丝在线| 国产欧美久久久精品影院| 国产91清纯白嫩初高中在线观看| 中文字幕亚洲一区二区av在线| 91啪亚洲精品| 午夜精品免费在线观看| 久久久久久亚洲综合影院红桃| 色婷婷国产精品| 蜜桃视频在线观看一区| 国产精品毛片a∨一区二区三区 | 日日骚欧美日韩| 欧美成人a视频| 91麻豆高清视频| 免费成人在线播放| 日韩一区欧美一区| 日韩欧美卡一卡二| 不卡的电视剧免费网站有什么| 亚洲va在线va天堂| 国产欧美日韩精品a在线观看| 欧美在线免费播放| 成人夜色视频网站在线观看| 亚洲超丰满肉感bbw| 欧美高清在线精品一区| 337p亚洲精品色噜噜噜| 大陆成人av片| 美日韩一区二区| 亚洲女同ⅹxx女同tv| 亚洲精品在线观| 欧美日韩国产电影| www.欧美日韩国产在线| 精品一二三四区| 午夜一区二区三区在线观看| 国产精品久久久久久亚洲毛片 | 欧美日韩一二三区| 成人综合婷婷国产精品久久免费| 日韩二区三区在线观看| 亚洲精品国产一区二区精华液 | 色www精品视频在线观看| 久久av老司机精品网站导航| 亚洲欧洲一区二区三区| 久久久www成人免费毛片麻豆| 欧美精品自拍偷拍动漫精品| 在线免费不卡电影| www.欧美日韩| 成人福利视频在线看| 国产综合色产在线精品| 久久99精品久久久久婷婷| 偷拍自拍另类欧美| 亚洲综合免费观看高清完整版在线 | 蜜桃av一区二区在线观看| 亚洲最新视频在线播放| 亚洲精品一二三| 亚洲人被黑人高潮完整版| 国产精品区一区二区三区| 国产日产亚洲精品系列| 久久久久久一二三区| 久久综合久久99| 国产亚洲综合在线| 久久伊人蜜桃av一区二区| 精品久久国产字幕高潮| 精品久久久久久最新网址| 日韩美女视频在线| 亚洲精品一区二区三区在线观看| 日韩精品一区二区三区在线播放| 日韩欧美成人一区| www国产亚洲精品久久麻豆| 日韩美女一区二区三区| 精品99999| 国产欧美一区二区三区鸳鸯浴| 国产色婷婷亚洲99精品小说| 亚洲国产成人自拍| 1区2区3区国产精品| 亚洲婷婷在线视频| 一区二区欧美精品| 日韩激情在线观看| 黑人巨大精品欧美黑白配亚洲| 国产一区二区免费看| 成人精品电影在线观看| 色噜噜狠狠成人中文综合| 欧美影视一区在线| 日韩三级精品电影久久久| 欧美精品一区在线观看| 欧美国产综合一区二区| 亚洲美女屁股眼交3| 亚洲成人资源在线| 激情五月婷婷综合网| 成人黄色综合网站| 欧美系列一区二区| 精品精品欲导航| 亚洲欧洲精品一区二区三区不卡| 亚洲欧美另类图片小说| 五月天丁香久久| 国产综合久久久久久鬼色| 成人av免费在线播放| 欧美日韩极品在线观看一区| 日韩一区二区精品葵司在线| 亚洲国产经典视频| 午夜欧美视频在线观看| 国产乱码精品一区二区三区五月婷| 99精品视频在线观看免费| 欧美久久久久久久久中文字幕| 久久综合成人精品亚洲另类欧美| 中文字幕一区二区三区不卡在线| 性感美女极品91精品| 国产一区二区免费看| 在线观看日韩一区| 久久精品欧美一区二区三区麻豆| 亚洲激情六月丁香| 国产永久精品大片wwwapp| 在线观看中文字幕不卡| 26uuu久久天堂性欧美| 亚洲综合色网站| 国产一区二区三区不卡在线观看| 在线看不卡av| 国产精品乱码久久久久久| 日韩中文欧美在线| 91美女蜜桃在线| 久久精品人人做人人爽97| 日韩电影免费在线观看网站| 一本色道久久综合亚洲精品按摩| 久久欧美一区二区| 日韩激情一区二区| 91成人看片片| 国产精品美女久久久久久 | 国产欧美日韩精品a在线观看| 日韩激情视频在线观看| 在线观看视频一区二区| 国产精品久久久久久久久免费丝袜| 麻豆久久久久久| 欧美日韩一区 二区 三区 久久精品| 国产精品传媒入口麻豆| 国产麻豆一精品一av一免费| 欧美一级一区二区| 亚洲第一综合色| 91高清在线观看| 亚洲男女毛片无遮挡| 不卡的av中国片| 中文字幕一区二区三区视频 | 亚洲卡通欧美制服中文| 国产一区二区精品久久| 精品少妇一区二区三区在线视频| 日韩va欧美va亚洲va久久| 欧美在线三级电影| 一区二区三区精品在线| 97久久超碰国产精品电影| 日本一区二区综合亚洲| 风间由美中文字幕在线看视频国产欧美 | 欧美日韩一区在线观看| 亚洲影院免费观看| 欧美性高清videossexo| 夜夜精品视频一区二区| 欧洲国产伦久久久久久久| 亚洲精品伦理在线| 色一情一乱一乱一91av| 亚洲欧美日韩国产综合在线| 91浏览器在线视频| 亚洲国产欧美一区二区三区丁香婷| 在线亚洲+欧美+日本专区| 亚洲一区二区五区| 精品视频在线免费| 日本女优在线视频一区二区| 91精品国产手机| 久久99蜜桃精品| 国产片一区二区三区| 丁香亚洲综合激情啪啪综合| 国产精品福利影院| 日本道精品一区二区三区| 亚洲成年人网站在线观看| 91麻豆精品国产| 国产在线播放一区三区四| 亚洲国产精品t66y| 91国偷自产一区二区三区成为亚洲经典 | 国产精品一区专区| 国产精品久久久久久久久免费樱桃| 一本色道亚洲精品aⅴ| 婷婷丁香久久五月婷婷| 亚洲国产精华液网站w| 99久久精品费精品国产一区二区| 亚洲影视在线播放| 久久一区二区三区四区| av不卡免费在线观看| 日韩一区精品视频| 国产亚洲精品久| 欧美影院一区二区三区|