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

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

?? serialport.cpp

?? 串口編程源代碼.rar
?? 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一区二区三区免费野_久草精品视频
卡一卡二国产精品| 亚洲成人免费在线观看| 国产乱对白刺激视频不卡| 欧美tickling网站挠脚心| 另类综合日韩欧美亚洲| 日韩欧美亚洲国产另类| 久久91精品国产91久久小草| 精品91自产拍在线观看一区| 国产精品1区二区.| 国产精品第一页第二页第三页| 99热精品国产| 亚洲国产中文字幕| 91麻豆精品国产91| 国产毛片精品一区| 国产精品久久久久影院| 色老汉一区二区三区| 午夜精品一区二区三区三上悠亚| 555夜色666亚洲国产免| 精品一区二区三区免费播放| 久久久久99精品国产片| 大美女一区二区三区| 一区二区三区四区在线| 91精品国产免费久久综合| 麻豆中文一区二区| 三级影片在线观看欧美日韩一区二区| 欧美一区二区三区喷汁尤物| 国产剧情在线观看一区二区| 中文字幕亚洲一区二区av在线| 91久久精品日日躁夜夜躁欧美| 午夜不卡av在线| 久久精品一区八戒影视| 在线免费观看成人短视频| 美女视频第一区二区三区免费观看网站| 久久精品人人爽人人爽| 在线观看免费亚洲| 国产九色精品成人porny| 亚洲精品你懂的| 亚洲精品在线观| 色拍拍在线精品视频8848| 蜜桃视频一区二区三区在线观看| 欧美激情艳妇裸体舞| 欧美视频在线一区二区三区| 国产精品一区免费在线观看| 一片黄亚洲嫩模| 久久久精品综合| 欧美精品日韩精品| 99这里都是精品| 美女在线观看视频一区二区| 一区二区不卡在线播放 | 亚洲成人高清在线| 久久新电视剧免费观看| 欧美视频一区在线| 国产成人小视频| 男人的天堂久久精品| 亚洲视频1区2区| 久久综合久色欧美综合狠狠| 欧美日韩国产a| 91丝袜呻吟高潮美腿白嫩在线观看| 久色婷婷小香蕉久久| 午夜电影一区二区| 亚洲欧美另类在线| 国产精品毛片高清在线完整版| 日韩精品自拍偷拍| 7777精品伊人久久久大香线蕉超级流畅| 成人黄色av电影| 国产精品99久久久久久有的能看| 免费久久99精品国产| 一区2区3区在线看| 亚洲另类春色校园小说| 国产精品美女视频| 国产日韩成人精品| 精品国产乱码久久久久久久| 制服丝袜日韩国产| 欧美日韩成人高清| 精品视频在线看| 91麻豆高清视频| 91欧美激情一区二区三区成人| 国产成人免费视频精品含羞草妖精| 久久成人综合网| 免费在线看成人av| 免费看黄色91| 麻豆国产欧美一区二区三区| 92精品国产成人观看免费| 成人av集中营| 不卡一区在线观看| 99精品国产热久久91蜜凸| 成人av网站免费观看| 粉嫩绯色av一区二区在线观看| 国产精品一区不卡| 国产精品77777竹菊影视小说| 国产成人在线看| 成人激情开心网| 91美女在线观看| 色综合激情久久| 欧美系列亚洲系列| 3d动漫精品啪啪| 欧美一级xxx| 337p日本欧洲亚洲大胆精品| 久久久精品免费免费| 欧美激情综合在线| 亚洲人成网站影音先锋播放| 亚洲最大成人综合| 亚洲午夜影视影院在线观看| 日韩高清一区二区| 黄色小说综合网站| 丁香啪啪综合成人亚洲小说| 99re成人在线| 欧美日韩黄色影视| 26uuu国产一区二区三区| 欧美国产97人人爽人人喊| 一区二区三区蜜桃网| 日本系列欧美系列| 国产精品综合一区二区| 99久久综合99久久综合网站| 欧美偷拍一区二区| 欧美xingq一区二区| 免费不卡在线观看| 国产91精品一区二区麻豆亚洲| 91老师片黄在线观看| 91精品国产高清一区二区三区 | 亚洲成人精品影院| 九色|91porny| 91亚洲精品久久久蜜桃网站| 欧美曰成人黄网| 2023国产精品| 亚洲一区二区视频在线观看| 麻豆精品国产91久久久久久| 97aⅴ精品视频一二三区| 欧美精品久久久久久久多人混战| 精品欧美乱码久久久久久1区2区| 国产精品久久毛片av大全日韩| 午夜激情一区二区三区| 国产成人丝袜美腿| 欧美乱熟臀69xxxxxx| 国产精品三级视频| 麻豆精品在线播放| 色婷婷久久久久swag精品| 精品国产一区二区三区久久影院 | 欧美写真视频网站| 国产欧美精品日韩区二区麻豆天美| 亚洲一二三区在线观看| 国产1区2区3区精品美女| 欧美精品久久一区| 自拍偷拍国产亚洲| 国产精品自拍三区| 91精品中文字幕一区二区三区| 国产精品国产三级国产a| 久久爱www久久做| 欧美色综合天天久久综合精品| 久久色.com| 日韩福利电影在线观看| 在线一区二区三区做爰视频网站| 中文字幕二三区不卡| 精品亚洲成a人在线观看| 欧美日韩一区二区欧美激情| 日韩一区中文字幕| 国产激情一区二区三区| 欧美成人一区二区三区| 亚洲电影欧美电影有声小说| 色国产精品一区在线观看| 国产精品视频免费看| 国产一区二区三区精品视频| 日韩视频国产视频| 日本成人在线看| 欧美另类videos死尸| 亚洲香肠在线观看| 日本精品一区二区三区四区的功能| 国产精品久久午夜夜伦鲁鲁| 国产精品中文字幕一区二区三区| 欧美mv和日韩mv国产网站| 日本麻豆一区二区三区视频| 欧美一区二视频| 天堂av在线一区| 在线播放视频一区| 全部av―极品视觉盛宴亚洲| 欧美日韩和欧美的一区二区| 香蕉成人伊视频在线观看| 欧美图片一区二区三区| 亚洲成人激情av| 欧美一区二区三区性视频| 免费观看成人鲁鲁鲁鲁鲁视频| 欧美一级理论性理论a| 日日夜夜免费精品| 日韩欧美一区二区视频| 蜜臀av国产精品久久久久| 2023国产精华国产精品| 国产成人在线色| 亚洲天堂2014| 欧美优质美女网站| 日本欧美加勒比视频| 欧美成人猛片aaaaaaa| 韩国av一区二区三区| 国产欧美日韩在线看| 91一区二区在线观看| 香蕉成人啪国产精品视频综合网| 制服丝袜亚洲色图| 国产成人h网站| 亚洲黄一区二区三区| 777a∨成人精品桃花网| 国产真实乱对白精彩久久|