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

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

?? serialport.cpp

?? 使用多線程對串口進行數(shù)據(jù)傳輸程序。本代碼主要是對串口的測試。其中SerialPort類提供對串口的讀寫
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
/*
**	FILENAME			CSerialPort.h
**
**	PURPOSE				This class can read, write and watch one serial port.
**						It sends messages to its owner when something happends on the port
**						The class creates a thread for reading and writing so the main
**						program is not blocked.
**
**	CREATION DATE		3-27-2000
**	LAST MODIFICATION	4-8-2000
**	LAST MODIFICATION	11-5-2000
**
**	譚昌永 
**
*/
#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;
	m_strRecieved.Empty();
	// create events
	m_ov.hEvent = NULL;
	m_hWriteEvent = NULL;
	m_hShutdownEvent = NULL;
	m_Thread = NULL;
	m_szWriteBuffer = NULL;
	m_bCanRecieved = FALSE;
	m_bThreadAlive = FALSE;
	m_nSendCharLen = 0;
}

//
// Delete dynamic memory
//
CSerialPort::~CSerialPort()
{
	m_strRecieved.Empty();
	if(m_Thread != NULL)
	{
		DWORD dwSuspendCount;

		do
		{
		   dwSuspendCount = m_Thread->ResumeThread();
		}while((dwSuspendCount != 0) && (dwSuspendCount != 0xffffffff) );

		do
		{
		   SetEvent(m_hShutdownEvent);
		} while (m_bThreadAlive);
		TRACE("Thread ended\n");
	}
	CloseHandle(m_ov.hEvent);
	CloseHandle(m_hWriteEvent);
	CloseHandle(m_hComm);
	CloseHandle(m_hShutdownEvent);
	m_hComm = NULL;
	m_ov.hEvent = NULL;
	m_hWriteEvent = NULL;
	m_hShutdownEvent = NULL;

	TRACE("Thread ended\n");

	if (m_szWriteBuffer != NULL)
		delete [] m_szWriteBuffer;
	if(m_hComm)DeleteCriticalSection(&m_csCommunicationSync);
}

//
// Initialize the port. This can be port 1 to 4.
//
BOOL CSerialPort::InitPort(CWnd* pPortOwner,	// the owner (CWnd) of the port (receives message)
						   UINT  portnr,		// portnumber (1..4)
						   UINT  baud,			// baudrate
						   char  parity,		// parity 
						   UINT  databits,		// databits 
						   UINT  stopbits,		// stopbits 
						   DWORD dwCommEvents,	// EV_RXCHAR, EV_CTS etc
						   UINT  writebuffersize)	// size to the writebuffer
{
//	assert(portnr > 0 && portnr < 5);
	assert(pPortOwner != NULL);

	// if the thread is alive: Kill
	if (m_bThreadAlive)
	{
		do
		{
			SetEvent(m_hShutdownEvent);
		} while (m_bThreadAlive);
		TRACE("Thread ended\n");
	}
	m_Thread = NULL;
	// create events
	if (m_ov.hEvent != NULL)
		ResetEvent(m_ov.hEvent);
	m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	if (m_hWriteEvent != NULL)
		ResetEvent(m_hWriteEvent);
	m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
	
	if (m_hShutdownEvent != NULL)
		ResetEvent(m_hShutdownEvent);
	m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	// initialize the event objects
	m_hEventArray[0] = m_hShutdownEvent;	// highest priority
	m_hEventArray[1] = m_ov.hEvent;
	m_hEventArray[2] = m_hWriteEvent;

	// initialize critical section
	InitializeCriticalSection(&m_csCommunicationSync);
	
	// set buffersize for writing and save the owner
	m_pOwner = pPortOwner;

	if (m_szWriteBuffer != NULL)
		delete [] m_szWriteBuffer;
	m_szWriteBuffer = new char[writebuffersize];

	m_nPortNr = portnr;

	m_nWriteBufferSize = writebuffersize;
	m_dwCommEvents = dwCommEvents;

	BOOL bResult = FALSE;
	char *szPort = new char[50];
	char *szBaud = new char[50];

	// now it critical!
	EnterCriticalSection(&m_csCommunicationSync);

	// if the port is already opened: close it
	if (m_hComm != NULL)
	{
		CloseHandle(m_hComm);
		m_hComm = NULL;
	}

	// prepare port strings
	sprintf(szPort, "COM%d", portnr);
	sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);

	// get a handle to the port
	m_hComm = CreateFile(szPort,						// communication port string (COMX)
					     GENERIC_READ | GENERIC_WRITE,	// read/write types
					     0,								// comm devices must be opened with exclusive access
					     NULL,							// no security attributes
					     OPEN_EXISTING,					// comm devices must use OPEN_EXISTING
					     FILE_FLAG_OVERLAPPED,			// Async I/O
					     0);							// template must be 0 for comm devices

	if (m_hComm == INVALID_HANDLE_VALUE)
	{
		// port not found
		delete [] szPort;
		delete [] szBaud;

		return FALSE;
	}

	// set the timeout values
	m_CommTimeouts.ReadIntervalTimeout = 1000;
	m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
	m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
	m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
	m_CommTimeouts.WriteTotalTimeoutConstant = 1000;

	// configure
	if (SetCommTimeouts(m_hComm, &m_CommTimeouts))
	{						   
		if (SetCommMask(m_hComm, dwCommEvents))
		{
			if (GetCommState(m_hComm, &m_dcb))
			{
				m_dcb.fRtsControl = RTS_CONTROL_ENABLE;		// set RTS bit high!
				if (BuildCommDCB(szBaud, &m_dcb))
				{
					if (SetCommState(m_hComm, &m_dcb))
						; // normal operation... continue
					else
						ProcessErrorMessage("SetCommState()");
				}
				else
					ProcessErrorMessage("BuildCommDCB()");
			}
			else
				ProcessErrorMessage("GetCommState()");
		}
		else
			ProcessErrorMessage("SetCommMask()");
	}
	else
		ProcessErrorMessage("SetCommTimeouts()");

	delete [] szPort;
	delete [] szBaud;

	// flush the port
	PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

	// release critical section
	LeaveCriticalSection(&m_csCommunicationSync);

	TRACE("Initialisation for communicationport %d completed.\nUse Startmonitor to communicate.\n", portnr);

	return TRUE;
}

//
//  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.

			 	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_CTS)
					::PostMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_RXFLAG)
					::PostMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_BREAK)
					::PostMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_ERR)
					::PostMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_RING)
					::PostMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				
				if (CommEvent & EV_RXCHAR)
					// Receive character event from port.
					ReceiveChar(port, comstat);
					

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美日韩美少妇| 国产v日产∨综合v精品视频| 欧美在线一二三| 亚洲精品国久久99热| 99精品视频一区二区| 亚洲素人一区二区| 欧美日精品一区视频| 亚洲成av人片一区二区梦乃| 7777精品久久久大香线蕉| 强制捆绑调教一区二区| 欧美tickling网站挠脚心| 国产一区二区影院| 亚洲欧美综合色| 欧美丝袜自拍制服另类| 免费av网站大全久久| 欧美tickling网站挠脚心| 成人在线一区二区三区| 一区二区三区美女视频| 欧美一卡二卡在线| 国产精品亚洲а∨天堂免在线| 国产精品情趣视频| 欧美艳星brazzers| 精品一区二区三区免费视频| 日本一区二区三区四区| 欧美揉bbbbb揉bbbbb| 国内精品国产三级国产a久久| 国产精品素人一区二区| 在线观看一区二区视频| 精品一区二区综合| 亚洲另类一区二区| 精品美女在线观看| 色婷婷av一区二区三区软件| 免费成人结看片| 亚洲男人的天堂在线aⅴ视频| 制服丝袜av成人在线看| 成人黄色电影在线| 免费人成在线不卡| 亚洲视频一二三| 3751色影院一区二区三区| 国产成人在线视频网址| 亚洲成人av一区二区| 国产女人18水真多18精品一级做| 色狠狠一区二区| 国产精品一区专区| 日韩中文字幕麻豆| 国产精品视频免费看| 日韩写真欧美这视频| 91麻豆福利精品推荐| 激情国产一区二区| 婷婷国产在线综合| 亚洲欧美精品午睡沙发| 国产三级精品视频| 日韩西西人体444www| 欧美亚洲高清一区| 成人av资源在线| 国产呦萝稀缺另类资源| 天天综合天天综合色| 亚洲欧美另类久久久精品2019| 久久午夜老司机| 日韩精品一区二区三区蜜臀| 欧美性猛片aaaaaaa做受| 99久久精品国产一区二区三区 | 精品一区二区三区免费观看 | 一区二区三区中文在线| 国产清纯在线一区二区www| 欧美一级理论片| 欧美色老头old∨ideo| 色综合中文字幕国产| 国产又粗又猛又爽又黄91精品| 蜜桃视频一区二区| 日本不卡高清视频| 日韩不卡一区二区三区| 石原莉奈一区二区三区在线观看 | 91国产成人在线| 99re成人精品视频| 99久久99久久久精品齐齐| 国产成人精品亚洲日本在线桃色| 精品在线免费观看| 国产一区不卡视频| 国产福利一区在线| 高清av一区二区| 国产成人免费视频| 成人免费黄色在线| 99视频一区二区| 99精品国产一区二区三区不卡| 成人av先锋影音| 99精品视频一区二区| 91免费国产在线| 欧美亚洲综合久久| 欧美福利一区二区| 欧美xxxxxxxx| 国产精品污网站| 亚洲免费视频成人| 午夜日韩在线电影| 美女爽到高潮91| 国产精品一区二区黑丝| 成人妖精视频yjsp地址| 99在线热播精品免费| 欧美影片第一页| 日韩午夜av一区| 国产亚洲一二三区| 久久不见久久见免费视频7| 国产真实精品久久二三区| 国产 日韩 欧美大片| 91久久国产最好的精华液| 欧美精品日韩一区| 久久精品人人做人人爽97| 国产精品成人一区二区三区夜夜夜| 国产精品欧美综合在线| 亚洲午夜精品久久久久久久久| 日韩综合一区二区| 国产91富婆露脸刺激对白| 91免费看视频| 日韩欧美一区二区三区在线| 国产日本一区二区| 一区二区在线免费观看| 毛片av一区二区| 99视频精品免费视频| 91精品国产品国语在线不卡| 国产女人18水真多18精品一级做| 亚洲国产综合91精品麻豆| 激情久久久久久久久久久久久久久久| av一区二区三区在线| 欧美一级免费观看| 中文字幕制服丝袜成人av| 日韩1区2区3区| 成人av电影免费在线播放| 在线成人av网站| 国产精品久久一级| 日本在线观看不卡视频| 99久久精品国产导航| 精品日韩成人av| 亚洲综合一区二区精品导航| 国产一区 二区 三区一级| 欧美日韩免费观看一区二区三区 | 国产激情一区二区三区| 精品视频免费看| 国产精品麻豆视频| 精品一区二区av| 欧美写真视频网站| 国产精品免费久久久久| 久久精品99国产精品| 欧美亚一区二区| 综合色天天鬼久久鬼色| 国产精品1区二区.| 日韩精品影音先锋| 91啪在线观看| 欧美电影免费观看高清完整版在线 | 欧美午夜精品一区| 国产精品你懂的在线| 激情综合网天天干| 在线电影一区二区三区| 悠悠色在线精品| www.亚洲激情.com| 国产色综合久久| 九九久久精品视频| 欧美一区二区成人| 日本中文一区二区三区| 欧美日韩夫妻久久| 亚洲一区二区在线视频| 一本久久综合亚洲鲁鲁五月天 | 国产综合久久久久影院| 日韩欧美一级二级| 日本成人在线电影网| 欧美性猛交xxxxxx富婆| 一区二区三区在线视频免费| 99久久婷婷国产综合精品| 国产精品天美传媒| 丰满白嫩尤物一区二区| 日本一区二区成人在线| 国产精品 欧美精品| 国产午夜精品在线观看| 粉嫩av一区二区三区在线播放| 久久久不卡影院| 国产成人av福利| 国产精品拍天天在线| 97国产一区二区| 亚洲精品中文在线| 欧美亚洲动漫精品| 天堂影院一区二区| 精品久久国产老人久久综合| 蜜桃视频一区二区三区| 精品日韩99亚洲| 国产 欧美在线| 亚洲品质自拍视频| 欧美精品视频www在线观看 | 成人性生交大合| 国产精品护士白丝一区av| 91视频91自| 亚洲电影一区二区三区| 日韩一区二区电影在线| 国产精一区二区三区| 国产精品你懂的| 在线精品观看国产| 美腿丝袜亚洲色图| 久久精品在这里| 99精品久久免费看蜜臀剧情介绍| 亚洲国产精品一区二区久久恐怖片| 在线播放视频一区| 国产成人精品免费在线|