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

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

?? chuanport.cpp

?? Visual C++串口通信技術與典型事例源代碼—云臺控制
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
/*
**	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 "ChuanPort.h"

#include <assert.h>
 
//
// Constructor
//
CChuanPort::CChuanPort()
{
	m_lComm = 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_LCloseEvent = NULL;

	m_szWriteBuffer = NULL;

	m_LThreadExist = FALSE;
	m_LReBlock=FALSE;

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

//
// Delete dynamic memory
//
CChuanPort::~CChuanPort()
{
	do
	{
		SetEvent(m_LCloseEvent);
	} while (m_LThreadExist);

	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_LThreadExist)
	{
		do
		{
			SetEvent(m_LCloseEvent);
		} while (m_LThreadExist);
		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_LCloseEvent != NULL)
		ResetEvent(m_LCloseEvent);
	m_LCloseEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

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

	// initialize critical section
	InitializeCriticalSection(&m_LCorrespondSync);
	
	// 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_LCommSth = dwCommEvents;

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

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

	// if the port is already opened: close it
	if (m_lComm != NULL)
	{
		CloseHandle(m_lComm);
		m_lComm = 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_lComm = 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_lComm == INVALID_HANDLE_VALUE)
	{
		// port not found
		delete [] szPort;
		delete [] szBaud;

		return FALSE;
	}

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

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

	delete [] szPort;
	delete [] szBaud;

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

	// release critical section
	LeaveCriticalSection(&m_LCorrespondSync);

	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_LThreadExist = 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_lComm)		// check if the port is opened
		PurgeComm(port->m_lComm, 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_LsthArray 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_lComm, &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_lComm, &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_LsthArray, 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_LThreadExist = FALSE;
				
				// Kill this thread.  break is not needed, but makes me feel better.
				AfxEndThread(100);
				break;
			}
		case 1:	// read event
			{
				GetCommMask(port->m_lComm, &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_LReBlock)
						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
//

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲成a人v欧美综合天堂下载 | 成人一区二区三区| 懂色中文一区二区在线播放| 欧美日韩一区二区三区在线| 日本一区二区电影| 免费看日韩精品| 在线一区二区三区四区| 久久久蜜臀国产一区二区| 日韩和欧美一区二区| 一本大道久久a久久精品综合| 精品国产sm最大网站| 亚洲成在线观看| 91久久精品一区二区三| 亚洲国产精品t66y| 国产电影精品久久禁18| 精品久久久久久无| 日韩二区三区四区| 欧美日韩午夜在线| 亚洲综合一区二区精品导航| 成人黄色小视频| 久久久久88色偷偷免费| 麻豆国产一区二区| 欧美大片在线观看| 日本午夜精品一区二区三区电影 | 亚洲男人的天堂一区二区| 国产主播一区二区| 欧美精品一区二区三区视频 | 日韩精品自拍偷拍| 免费不卡在线视频| 91精品在线观看入口| 日本中文字幕一区二区视频| 欧美日产国产精品| 偷拍亚洲欧洲综合| 欧美一区二区在线观看| 免费观看在线色综合| 欧美sm美女调教| 国产精品一区在线观看乱码 | 91在线视频播放地址| 欧美国产精品一区二区| 成人三级在线视频| 亚洲日本欧美天堂| 欧美亚洲综合网| 日韩 欧美一区二区三区| 欧美电影免费观看高清完整版在| 狠狠色狠狠色综合日日91app| 精品日韩在线观看| 高清国产一区二区三区| 中文字幕在线不卡一区| 欧洲色大大久久| 亚洲123区在线观看| 精品久久人人做人人爱| 成人免费观看视频| 亚洲综合视频在线| 欧美成人a视频| 成人看片黄a免费看在线| 亚洲欧美另类在线| 日韩亚洲欧美综合| 成人免费电影视频| 亚洲电影视频在线| 久久久久久久精| 一本大道综合伊人精品热热| 美女爽到高潮91| 国产精品国产自产拍在线| 欧美日韩一区二区三区在线看| 麻豆精品蜜桃视频网站| 亚洲视频一二区| 欧美一区二区三区视频在线| 成人精品小蝌蚪| 天天操天天色综合| 国产视频911| 欧美伦理影视网| 99亚偷拍自图区亚洲| 日韩av在线发布| 亚洲色欲色欲www| 精品毛片乱码1区2区3区 | 国产宾馆实践打屁股91| 亚洲电影欧美电影有声小说| 久久久777精品电影网影网| 欧美性大战久久久久久久| 国产一区二区不卡在线 | 成人激情黄色小说| 日韩高清电影一区| 亚洲精品欧美在线| 中文乱码免费一区二区| 欧美一级高清片| 在线观看国产91| 99这里只有精品| 国产激情一区二区三区四区| 日韩电影免费在线看| 一区二区三区国产精华| 亚洲国产激情av| 久久久综合视频| 欧美va日韩va| 欧美一级二级三级乱码| 欧美高清视频一二三区| 欧美综合色免费| 99精品欧美一区二区蜜桃免费 | 国产麻豆精品在线观看| 日韩电影免费在线看| 亚洲亚洲人成综合网络| 一区二区三区中文字幕| 亚洲欧美一区二区在线观看| 国产欧美日韩在线视频| 久久久久国产精品麻豆| 精品国产亚洲在线| 精品国产乱码久久久久久图片| 91精品国产全国免费观看| 欧美调教femdomvk| 色噜噜狠狠成人中文综合 | 久久99精品久久久久久| 视频在线观看91| 婷婷六月综合亚洲| 亚洲va国产va欧美va观看| 亚洲成人av在线电影| 婷婷成人激情在线网| 婷婷一区二区三区| 日韩国产在线观看一区| 老司机一区二区| 国产乱国产乱300精品| 国产v日产∨综合v精品视频| 成人免费看的视频| gogo大胆日本视频一区| 91小视频在线免费看| 欧美午夜在线一二页| 67194成人在线观看| 亚洲精品在线免费播放| 国产日韩av一区二区| 国产精品久久三区| 亚洲影视在线观看| 青青草精品视频| 国产一区二区在线观看视频| 成人免费电影视频| 欧美三级电影在线看| 91麻豆精品国产91久久久| 久久婷婷色综合| 1024国产精品| 调教+趴+乳夹+国产+精品| 狠狠色丁香婷综合久久| 成人午夜免费av| 欧美视频一区二区三区四区| 日韩午夜精品电影| 亚洲欧美一区二区在线观看| 亚洲1区2区3区4区| 国产白丝精品91爽爽久久| 在线观看一区不卡| 精品国一区二区三区| 亚洲视频一区在线| 免播放器亚洲一区| 成人福利视频在线| 欧美一区二区精美| 亚洲视频一区二区在线观看| 毛片av一区二区| 99riav一区二区三区| 日韩一卡二卡三卡四卡| 国产精品欧美久久久久一区二区| 天天操天天干天天综合网| 成人妖精视频yjsp地址| 欧美一区欧美二区| 亚洲免费毛片网站| 国产精品一区二区三区乱码| 欧美色欧美亚洲另类二区| 国产日产欧美一区二区三区| 日韩av一二三| 色综合天天狠狠| 国产亚洲午夜高清国产拍精品 | 精品国产一区a| 亚洲国产一区二区a毛片| 国产91富婆露脸刺激对白| 在线播放欧美女士性生活| 日韩美女视频一区二区| 极品销魂美女一区二区三区| 欧美久久久久久久久久| 中文字幕佐山爱一区二区免费| 国产一区二区三区免费在线观看 | 色激情天天射综合网| 欧美激情一区三区| 久久精品噜噜噜成人88aⅴ | 日日夜夜免费精品视频| 91浏览器在线视频| 中文字幕亚洲视频| 成人在线综合网站| 欧美激情综合五月色丁香小说| 久久成人羞羞网站| 日韩欧美精品三级| 午夜伊人狠狠久久| 欧美视频三区在线播放| 一区二区三区中文免费| 一本久道久久综合中文字幕| 国产精品国产三级国产有无不卡| 国产一区二区按摩在线观看| 日韩欧美在线一区二区三区| 日本午夜精品一区二区三区电影 | 色悠悠久久综合| 亚洲免费观看视频| 99久久精品国产一区| 亚洲三级电影网站| 99精品国产视频| 一区二区三区在线看| 欧美亚洲国产一区在线观看网站| 亚洲精品国产无天堂网2021|