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

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

?? serialport.cpp

?? Visual C++串口通信技術(shù)與典型事例源代碼—云臺(tái)控制
?? CPP
?? 第 1 頁(yè) / 共 2 頁(yè)
字號(hào):
/*
**	FILENAME			CChuanPort.cpp
**
**	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		15-09-1997
**	LAST MODIFICATION	12-11-1997
**
**	AUTHOR				Remon Spekreijse
**
**
*/

#include "stdafx.h"
#include "SerialPort.h"

#include <assert.h>
 
//
// Constructor
//
CChuanPort::CChuanPort()
{
	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_bThreadAlive = FALSE;
	m_bBlockRead=FALSE;

	m_dcb.BaudRate=19200;
	m_dcb.ByteSize=8;
	m_dcb.StopBits=1;
	m_dcb.Parity=NOPARITY;
}

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

	TRACE("Thread ended\n");

	delete [] m_szWriteBuffer;
}

//
// Initialize the port. This can be port 1 to 4.
//
BOOL CChuanPort::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");
	}

	// 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 BYTE[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()");
					m_dcb.StopBits=stopbits;
				}
				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 CChuanPort::CommThread(LPVOID pParam)
{
	// Cast the void pointer passed to the thread back to
	// a pointer of CChuanPort class
	CChuanPort *port = (CChuanPort*)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)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_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);
				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_RXCHAR)
					// Receive character event from port.
					if(!port->m_bBlockRead)
						ReceiveChar(port, comstat);
					
				break;
			}  
		case 2: // write event
			{
				// Write character event from port
				WriteChar(port);
				break;
			}

		} // end switch

	} // close forever loop

	return 0;
}

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

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


//
// Suspend the comm thread
//

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产不卡视频一区二区三区| 91在线视频播放地址| 日本一区二区久久| 91精品国产91久久久久久最新毛片| 大尺度一区二区| 蜜臀av在线播放一区二区三区| 亚洲欧美日韩国产综合在线| 欧美性xxxxx极品少妇| 免费成人性网站| 国产亚洲精品福利| 欧美精选在线播放| 91蜜桃免费观看视频| 国产乱码精品1区2区3区| 五月天婷婷综合| 亚洲人成网站色在线观看| 久久久精品欧美丰满| 欧美一二三区在线观看| 欧美亚洲动漫制服丝袜| 99久久久精品免费观看国产蜜| 国产综合色视频| 久久69国产一区二区蜜臀| 午夜精品久久久久| 亚洲精品va在线观看| 国产精品国产馆在线真实露脸 | 国产婷婷色一区二区三区四区| 欧美日韩另类一区| 性做久久久久久| 久久久噜噜噜久噜久久综合| 欧美videos大乳护士334| 欧美日韩激情一区二区| 欧美在线制服丝袜| 在线观看亚洲一区| 色婷婷av一区二区| 91久久人澡人人添人人爽欧美| aaa亚洲精品| 91在线码无精品| 色综合中文字幕国产| 国产99精品视频| 成人黄色软件下载| 成人丝袜视频网| eeuss影院一区二区三区| 99国内精品久久| 在线欧美日韩国产| 欧美视频一区二区三区四区| 欧美性大战xxxxx久久久| 欧美日韩高清在线播放| 欧美一区日本一区韩国一区| 日韩欧美国产高清| 久久综合久色欧美综合狠狠| 国产午夜精品一区二区三区嫩草| 国产精品网友自拍| 亚洲色图另类专区| 亚洲国产综合色| 欧美日韩不卡一区二区| 色综合天天综合给合国产| 99国产精品久久久久久久久久| 91免费观看在线| 欧美久久久久久久久久| 精品国产污网站| 国产欧美精品一区二区色综合 | 亚洲欧洲精品一区二区三区不卡| 亚洲婷婷在线视频| 亚洲一区二区四区蜜桃| 久久www免费人成看片高清| 国产精品一区二区久激情瑜伽| av一本久道久久综合久久鬼色| 欧美中文字幕不卡| 精品久久免费看| 国产精品高潮呻吟| 日韩精品国产欧美| 粉嫩aⅴ一区二区三区四区五区 | 91精品国产综合久久精品图片| 精品国产99国产精品| 亚洲日本va在线观看| 日本女优在线视频一区二区| 国产精品一区二区三区网站| 欧洲国产伦久久久久久久| 精品美女一区二区| 一区二区三区在线视频免费| 免费一级片91| 色综合久久天天综合网| 欧美videossexotv100| 一区二区三区中文在线观看| 极品瑜伽女神91| 在线视频你懂得一区| 精品国产髙清在线看国产毛片| 亚洲色图欧洲色图婷婷| 激情综合网最新| 欧美在线观看一区| 国产无人区一区二区三区| 丝袜国产日韩另类美女| zzijzzij亚洲日本少妇熟睡| 日韩一级视频免费观看在线| 亚洲乱码国产乱码精品精小说| 精品一区二区三区久久| 在线亚洲欧美专区二区| 国产三级三级三级精品8ⅰ区| 婷婷综合久久一区二区三区| 91在线云播放| 中文字幕欧美国产| 久久99国产精品麻豆| 欧美日产在线观看| 一区二区三区精品视频| 国产一区二区三区美女| 5月丁香婷婷综合| 亚洲精品乱码久久久久| av色综合久久天堂av综合| 久久久久久久电影| 喷白浆一区二区| 欧美日韩国产一级| 亚洲日本va午夜在线影院| 国产不卡视频一区二区三区| 2021国产精品久久精品 | 色悠久久久久综合欧美99| 久久精品一区二区| 激情综合网最新| 精品国产乱码久久久久久浪潮 | 国产麻豆欧美日韩一区| 91精品国产全国免费观看| 五月综合激情网| 欧美性受xxxx| 一区二区三区高清不卡| 色999日韩国产欧美一区二区| 国产精品久久久久毛片软件| 成人晚上爱看视频| 国产精品区一区二区三| 成人小视频在线观看| 国产日本一区二区| 国产v综合v亚洲欧| 久久久久国产精品麻豆| 国产一区二区三区蝌蚪| 国产亚洲精品久| 粉嫩av亚洲一区二区图片| 国产精品免费视频观看| a4yy欧美一区二区三区| 亚洲欧美另类久久久精品2019| 91论坛在线播放| 亚洲国产精品久久不卡毛片 | 国产精品久久午夜| 成人av网站大全| 伊人开心综合网| 日本精品裸体写真集在线观看| 亚洲综合男人的天堂| 欧美午夜精品免费| 日韩激情中文字幕| 日韩精品一区二区三区蜜臀| 精品一区二区免费看| 国产调教视频一区| 成人短视频下载| 樱桃视频在线观看一区| 欧美喷潮久久久xxxxx| 美女视频网站久久| 国产拍揄自揄精品视频麻豆| 色中色一区二区| 日韩和欧美一区二区三区| 久久一日本道色综合| 成人免费高清在线| 亚洲第一福利一区| 2023国产一二三区日本精品2022| 成人黄色在线看| 亚洲国产婷婷综合在线精品| 日韩欧美国产精品一区| 成人午夜视频福利| 亚洲成a人v欧美综合天堂下载| 精品剧情在线观看| 91亚洲精品久久久蜜桃| 手机精品视频在线观看| 26uuu精品一区二区| 91丨porny丨首页| 日韩成人免费在线| 日本一区二区动态图| 欧美日韩精品欧美日韩精品一综合| 经典三级视频一区| 亚洲综合在线免费观看| 欧美电影免费观看高清完整版在线 | 日本系列欧美系列| 欧美激情综合五月色丁香| 欧美性受xxxx| 国产a视频精品免费观看| 亚洲国产精品久久不卡毛片 | 亚洲免费高清视频在线| 91精品国产麻豆国产自产在线| 福利电影一区二区三区| 亚洲va欧美va人人爽午夜| 国产欧美日本一区视频| 这里只有精品免费| 99热在这里有精品免费| 久久不见久久见免费视频7| 一区二区欧美视频| 国产偷国产偷亚洲高清人白洁| 欧美女孩性生活视频| 国产成人午夜视频| 日韩二区在线观看| 亚洲欧美一区二区三区久本道91| 精品成人在线观看| 欧美日韩一区久久| 91网站视频在线观看| 国产激情精品久久久第一区二区| 日韩二区三区在线观看| 一区二区三区中文在线|