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

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

?? serialport.cpp

?? 多線程串口自動偵聽程序.rar 采用API多線程技術
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
/*
**	FILENAME			CSerialPort.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
//
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_bThreadAlive = FALSE;
}

//
// Delete dynamic memory
//
CSerialPort::~CSerialPort()
{
	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 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");
	}

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

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
精品国产123| 岛国精品在线观看| 欧美日韩国产精品自在自线| 亚洲电影在线播放| 777午夜精品免费视频| 久久精品免费看| 欧美zozo另类异族| 不卡区在线中文字幕| 亚洲激情校园春色| 欧美一区二区三区在线看| 国产原创一区二区| 中文字幕一区二区日韩精品绯色| 在线观看日产精品| 麻豆国产欧美日韩综合精品二区| 久久久国产精品麻豆| 99久久99久久精品免费观看| 亚洲国产另类av| 欧美精品一区二区高清在线观看| av中文一区二区三区| 首页亚洲欧美制服丝腿| 国产日韩欧美精品综合| 在线这里只有精品| 黄色日韩三级电影| 亚洲一线二线三线久久久| 日韩亚洲欧美中文三级| 成人黄色大片在线观看| 天天亚洲美女在线视频| 国产精品欧美久久久久无广告| 在线一区二区观看| 风间由美一区二区三区在线观看| 亚洲国产精品欧美一二99| 国产婷婷色一区二区三区在线| 91理论电影在线观看| 日本vs亚洲vs韩国一区三区 | 亚洲丶国产丶欧美一区二区三区| 日韩一区二区在线看| 成人动漫在线一区| 爽好久久久欧美精品| 亚洲欧美在线观看| 久久影院午夜论| 欧美日韩国产片| 成人午夜碰碰视频| 精品影视av免费| 最新久久zyz资源站| 26uuu亚洲综合色| 91精品欧美综合在线观看最新| 成人午夜视频网站| 麻豆精品一区二区三区| 午夜一区二区三区视频| |精品福利一区二区三区| 精品国产99国产精品| 欧美日韩一卡二卡三卡 | 色婷婷香蕉在线一区二区| 国内成人免费视频| 日韩av一级电影| 亚洲成av人片在线观看| 亚洲激情在线播放| 亚洲欧洲综合另类在线| 久久久久久久久久久黄色| 日韩欧美电影一二三| 欧美日韩一级二级三级| 色88888久久久久久影院野外| 丁香婷婷综合色啪| 国产主播一区二区| 麻豆精品一区二区综合av| 日韩电影免费在线看| 日韩专区在线视频| 夜夜爽夜夜爽精品视频| 亚洲图片激情小说| 综合色天天鬼久久鬼色| 中文字幕一区二区三区不卡在线| 国产精品视频第一区| 久久久久久久久久久久电影| 欧美一区二区三区播放老司机| 欧美视频在线一区| 欧美日韩第一区日日骚| 在线电影院国产精品| 欧美在线视频你懂得| 欧美亚洲精品一区| 欧美日韩国产综合一区二区三区 | 国产一区二区三区久久久| 看电影不卡的网站| 老司机一区二区| 国内精品久久久久影院色 | 亚洲最大成人综合| 亚洲国产综合人成综合网站| 一区二区三区日韩欧美| 亚洲免费观看高清完整版在线观看熊| 国产精品国产自产拍高清av| 亚洲麻豆国产自偷在线| 亚洲黄色录像片| 亚洲香肠在线观看| 欧美aaa在线| 国产伦精品一区二区三区免费| 国产成人av在线影院| 91在线观看高清| 8x8x8国产精品| 精品久久久久久亚洲综合网| 国产亚洲精品bt天堂精选| 中文字幕在线不卡一区二区三区| 亚洲精品国产a久久久久久| 亚欧色一区w666天堂| 激情国产一区二区| 99精品欧美一区二区蜜桃免费| 在线欧美小视频| 精品国产成人在线影院 | 亚洲国产精品久久不卡毛片| 蜜桃一区二区三区在线| 国产传媒一区在线| 欧美性猛交xxxxxx富婆| 日韩精品中午字幕| 亚洲欧美激情在线| 麻豆精品一二三| 一本色道a无线码一区v| 欧美一区二区视频在线观看2020 | 亚洲电影一级黄| 国产麻豆成人精品| 精品污污网站免费看| 国产午夜精品一区二区三区嫩草| 亚洲天堂a在线| 国内一区二区视频| 精品视频一区二区不卡| 欧美国产禁国产网站cc| 偷拍与自拍一区| 成人精品在线视频观看| 宅男在线国产精品| 亚洲少妇30p| 麻豆免费精品视频| 欧美私人免费视频| 国产精品拍天天在线| 美国毛片一区二区三区| 欧美专区日韩专区| 国产精品三级电影| 国产福利精品导航| 国产揄拍国内精品对白| 欧美日韩免费高清一区色橹橹 | 亚洲一区二区三区四区五区黄| 国产一区在线精品| 欧美一区二区在线视频| 亚洲精品videosex极品| 不卡电影一区二区三区| 久久久久久麻豆| 蜜臀a∨国产成人精品| 在线免费av一区| 日韩一区在线看| 国产成人精品一区二区三区网站观看| 91精品国产91久久久久久最新毛片| 日韩理论在线观看| 成人高清免费在线播放| 精品国产一区二区三区久久久蜜月| 亚洲不卡av一区二区三区| 色视频欧美一区二区三区| 国产精品午夜久久| 国产成人一级电影| 久久午夜免费电影| 国产最新精品免费| 精品国产亚洲一区二区三区在线观看| 视频一区二区欧美| 91麻豆精品国产91久久久| 亚洲国产成人精品视频| 在线中文字幕一区二区| 亚洲日本va午夜在线影院| 91欧美激情一区二区三区成人| 中文字幕精品一区二区精品绿巨人| 国产精品 日产精品 欧美精品| 久久久五月婷婷| 国产成人在线色| 国产欧美日韩卡一| www..com久久爱| 综合欧美亚洲日本| 欧美艳星brazzers| 日韩制服丝袜先锋影音| 日韩欧美一二三区| 国产精一区二区三区| 国产色综合一区| av一区二区不卡| 亚洲一区精品在线| 欧美一级夜夜爽| 国产精品资源站在线| 亚洲欧美在线视频| 欧美日韩一二区| 日韩不卡一区二区| 久久久久久久久久看片| 97精品久久久午夜一区二区三区| 一区二区三区视频在线观看| 欧美精品777| 国产a久久麻豆| 亚洲综合在线第一页| 欧美一区二区三区视频免费| 国产真实乱对白精彩久久| 国产精品的网站| 欧美高清一级片在线| 国模少妇一区二区三区| 中文字幕一区二区在线播放| 欧美调教femdomvk| 精彩视频一区二区| 亚洲伦理在线精品| 日韩美女视频在线| 99国产欧美久久久精品| 三级欧美在线一区|