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

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

?? serialport.cpp

?? 同時可以打開兩個串口的程序。可以進行收發試驗。
?? 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>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
// Constructor
//
CSerialPort::CSerialPort()
{
	m_hComm = NULL;

	// initialize overlapped structure members to zero
	m_ov.Offset = 0;
	m_ov.OffsetHigh = 0;
	m_Thread = NULL;

	// create events
	m_ov.hEvent = NULL;
	m_hWriteEvent = NULL;
	m_hShutdownEvent = NULL;

#ifndef _COMSEND_HEX
	m_szWriteBuffer = NULL;
#else
#ifndef _USE_DEFAULT_BUFSIZ
	ComSendStruct.sendBuf = NULL;
#endif
#endif

	m_bThreadAlive = FALSE;
	m_bThreadRunning = FALSE;
	m_bNeedThreadStop = FALSE;
}

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

	TRACE("Thread ended\n");

#ifndef _COMSEND_HEX
	delete [] m_szWriteBuffer;
#else
#ifndef _USE_DEFAULT_BUFSIZ
	delete [] ComSendStruct.sendBuf;
#endif
#endif
}

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

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

	if (m_hCloseCom != NULL)
		ResetEvent(m_hCloseCom);
	m_hCloseCom = 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;
	m_hEventArray[3] = m_hOpenCom;
	m_hEventArray[4] = m_hCloseCom;

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

#ifndef _COMSEND_HEX
	if (m_szWriteBuffer != NULL)
		delete [] m_szWriteBuffer;
	m_szWriteBuffer = new char[writebuffersize];
#endif

	m_nPortNr = portnr;
#ifdef _USE_DEFAULT_BUFSIZ
	m_nWriteBufferSize = DEFAULT_BUFSIZE;
#else
	m_nWriteBufferSize = writebuffersize;
	m_comWriteBuffer.sendBuf = new BYTE[writebuffersize];
#endif
	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;
}

// close serialport
int CSerialPort::ClosePort()
{
	int nCode = 0;
	if(AfxIsValidAddress(m_Thread, sizeof(CWinThread)))
	{
#if 0
		m_Thread->m_bAutoDelete = TRUE;
		nCode = m_Thread->ExitInstance();
		m_Thread = NULL;
#else
		nCode = 20;
		do
		{
			SetEvent(m_hShutdownEvent);
			Sleep(100);
			nCode --;
			if(nCode < 0)
				break;
		}while(m_bThreadAlive);
		::GetExitCodeThread(m_Thread->m_hThread, (LPDWORD)&nCode);
#endif
	}
	if(m_hComm)
		CloseHandle(m_hComm);
	m_hComm = NULL;
	return nCode;
}

//
//  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;
	port->m_bThreadRunning = 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!

		HANDLE hPortHandle = port->m_hComm;
		bResult = WaitCommEvent(hPortHandle, &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 ERROR_INVALID_HANDLE:
				{
					// For com handle is invalid
					port->ProcessErrorMessage("Invalid COM handle, ");
				}
				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.
				TRACE("About to end thread\n");
				if(port->m_bThreadAlive)
				{
			 		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_EVENT, (WPARAM) WM_COMM_CTS_DETECTED, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_RXFLAG)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_RXFLAG_DETECTED, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_BREAK)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_BREAK_DETECTED, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_ERR)	// may get line error
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_ERR_DETECTED, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_RING)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_RING_DETECTED, (LPARAM) port->m_nPortNr);
				if	(CommEvent & EV_TXEMPTY)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_TXEMPTY_DETECTED, (LPARAM) port->m_nPortNr);
				if	(CommEvent & EV_DSR)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_DSR_DETECTED, (LPARAM) port->m_nPortNr);
				if	(CommEvent & EV_RLSD)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_RLSD_DETECTED, (LPARAM) port->m_nPortNr);
				if	(CommEvent & EV_PERR)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_PERR_DETECTED, (LPARAM) port->m_nPortNr);
				if	(CommEvent & EV_RX80FULL)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_RX80FULL_DETECTED, (LPARAM) port->m_nPortNr);
				if	(CommEvent & EV_EVENT1)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_SPECIFYEVENT1, (LPARAM) port->m_nPortNr);
				if	(CommEvent & EV_EVENT2)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_EVENT, (WPARAM) WM_COMM_SPECIFYEVENT2, (LPARAM) port->m_nPortNr);

				if (CommEvent & EV_RXCHAR)
					// Receive character event from port.
					ReceiveChar(port, comstat);

				ClearCommError(port->m_hComm, &dwError, &comstat);
				CommErrorProc(port->m_pOwner->m_hWnd, dwError, (LPARAM)port->m_nPortNr);
				
				break;
			}  
		case 2: // write event

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
不卡一卡二卡三乱码免费网站| 欧美一区二区三区四区五区 | 国产精品自拍毛片| 91免费在线看| 精品精品国产高清一毛片一天堂| 日韩理论片网站| 国产九色精品成人porny| 欧美性受xxxx黑人xyx性爽| 国产亚洲精品超碰| 午夜精品久久久久久| 99精品国产99久久久久久白柏| 欧美成人一区二区三区在线观看| 久久av资源网| 欧美日韩一区在线观看| 中文字幕一区二区三中文字幕| 蜜桃视频第一区免费观看| 欧美性一级生活| 亚洲色图在线看| 波多野结衣在线一区| 久久嫩草精品久久久精品一| 日韩精品久久久久久| 欧美体内she精视频| 国产精品久久久久久久午夜片| 国内成人精品2018免费看| 欧美日韩免费一区二区三区 | 日韩欧美激情在线| 亚洲6080在线| 欧美日韩成人综合天天影院| 亚洲视频在线观看三级| 成人国产精品免费网站| 中文字幕免费不卡| 成人黄页毛片网站| 国产精品久久久久久久久久久免费看| 国产精品综合在线视频| 久久久美女毛片| 国产福利一区二区三区在线视频| 精品国产乱码久久久久久久| 久久99久久99精品免视看婷婷| 欧美成人精品福利| 国产一区二区免费看| 中文字幕二三区不卡| 成人av免费在线观看| 亚洲色图制服丝袜| 欧美日韩亚州综合| 男女男精品网站| 久久美女高清视频| 成人精品一区二区三区中文字幕| 国产精品免费丝袜| 在线视频亚洲一区| 日韩精品乱码av一区二区| 精品国产免费人成电影在线观看四季 | 亚洲电影在线免费观看| 欧美色图在线观看| 蜜臀av性久久久久蜜臀aⅴ流畅| 欧美大片一区二区| 懂色av噜噜一区二区三区av| 亚洲人吸女人奶水| 欧美一区二区视频在线观看 | 91视频xxxx| 天天亚洲美女在线视频| 精品毛片乱码1区2区3区| 国产福利视频一区二区三区| 亚洲色图都市小说| 91精品国产全国免费观看| 国产麻豆成人精品| 一区二区久久久久久| 日韩精品中午字幕| 不卡的av在线播放| 日韩高清中文字幕一区| 国产日韩v精品一区二区| 91国内精品野花午夜精品| 蜜桃视频免费观看一区| 国产精品久久久久久亚洲伦 | 蜜臀91精品一区二区三区| 久久久久99精品国产片| 欧美日韩一区二区三区高清| 国内一区二区视频| 亚洲一区免费视频| 久久久久久久久久久久电影| 一本到高清视频免费精品| 精品一区二区三区在线观看| 亚洲色图欧美在线| 久久精品欧美一区二区三区不卡 | 日韩综合小视频| 亚洲欧洲精品天堂一级| 欧美va亚洲va| 欧美性受xxxx黑人xyx| 成人激情午夜影院| 裸体在线国模精品偷拍| 亚洲综合丁香婷婷六月香| 中文字幕免费一区| 欧美成人性战久久| 欧美日韩国产a| 91蜜桃婷婷狠狠久久综合9色| 久久精品国产77777蜜臀| 亚洲精品视频免费看| 欧美激情中文字幕一区二区| 日韩欧美资源站| 欧美乱熟臀69xxxxxx| 91美女在线看| 91色九色蝌蚪| jizzjizzjizz欧美| 国产+成+人+亚洲欧洲自线| 极品少妇一区二区三区精品视频| 亚洲成人综合视频| 亚洲国产你懂的| 亚洲最大色网站| 亚洲欧美区自拍先锋| 国产精品丝袜一区| 国产精品乱码人人做人人爱| 国产亚洲一区字幕| 久久久久久久电影| 精品国产乱码久久| 日韩视频在线永久播放| 欧美一区二区三区成人| 国产精品家庭影院| 国产精品乱码一区二区三区软件| 国产精品网站导航| 国产精品成人网| 一区二区视频在线| 亚洲国产欧美日韩另类综合| 亚洲一区二区三区小说| 午夜久久久影院| 日本vs亚洲vs韩国一区三区二区 | 欧美视频一二三区| 欧美高清www午色夜在线视频| 欧美日韩一区二区不卡| 欧美精品自拍偷拍| 欧美大片在线观看| 久久嫩草精品久久久久| 中文成人av在线| 亚洲日本中文字幕区| 亚洲综合免费观看高清完整版 | 日韩二区在线观看| 美女在线视频一区| 国产精品一区在线| caoporn国产精品| 欧美视频一区在线| 日韩一区二区三区免费看 | 欧美色手机在线观看| 91麻豆精品国产| 久久久99久久| 一区二区三区欧美激情| 麻豆精品在线播放| 成人午夜免费视频| 欧美人与禽zozo性伦| 精品粉嫩超白一线天av| ...av二区三区久久精品| 亚洲国产色一区| 国产又黄又大久久| 在线观看不卡一区| 久久久不卡影院| 亚洲午夜日本在线观看| 国产一区二区三区av电影| 91浏览器在线视频| 精品国产一二三| 亚洲一区二区在线免费观看视频| 久久精品国产99久久6| 91美女视频网站| 欧美精品一区二区三区高清aⅴ| 亚洲婷婷综合色高清在线| 蜜桃传媒麻豆第一区在线观看| 国产成人免费视频网站| 欧美嫩在线观看| 国产日韩欧美精品在线| 日韩专区中文字幕一区二区| www.视频一区| 欧美精品一区二区精品网| 亚洲综合丝袜美腿| 岛国一区二区在线观看| 欧美一级片免费看| 亚洲影院久久精品| 成人免费视频app| 精品国产乱码久久久久久老虎| 亚洲v中文字幕| 一本色道久久综合精品竹菊| 久久久久久亚洲综合| 免费高清在线视频一区·| 日本乱人伦aⅴ精品| 国产精品拍天天在线| 极品美女销魂一区二区三区免费| 精品视频在线免费看| 中文字幕一区在线观看视频| 国产成人在线观看| 精品国产1区二区| 轻轻草成人在线| 在线播放亚洲一区| 亚洲国产精品人人做人人爽| 91色porny| 亚洲啪啪综合av一区二区三区| 国产99精品国产| 久久久高清一区二区三区| 麻豆精品视频在线观看视频| 欧美伦理电影网| 日韩精品电影在线观看| 欧美日韩综合不卡| 丝袜亚洲另类丝袜在线| 欧美日韩亚洲综合在线| 亚洲va欧美va人人爽午夜| 欧美自拍偷拍午夜视频|