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

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

?? serialport.cpp

?? 串口通信源程序
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
#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_nWriteSize=1;

	m_bThreadAlive = FALSE;
}

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

	
	// if the port is still opened: close it 
	if (m_hComm != NULL)
	{
		CloseHandle(m_hComm);
		m_hComm = NULL;
	}
	// Close Handles  
	if(m_hShutdownEvent!=NULL)
		CloseHandle( m_hShutdownEvent); 
	if(m_ov.hEvent!=NULL)
		CloseHandle( m_ov.hEvent ); 
	if(m_hWriteEvent!=NULL)
		CloseHandle( m_hWriteEvent ); 

	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);
	else
		m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	if (m_hWriteEvent != NULL)
		ResetEvent(m_hWriteEvent);
	else
		m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
	
	if (m_hShutdownEvent != NULL)
		ResetEvent(m_hShutdownEvent);
	else
		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.EvtChar = 'q';
				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.
				CloseHandle(port->m_hComm);
				port->m_hComm=NULL;
				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_RXCHAR)
					// Receive character event from port.
					ReceiveChar(port, comstat);
				if (CommEvent & EV_CTS)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_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_RXFLAG)
					::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
					
				break;
			}  
		case 2: // write event
			{
				// Write character event from port
				WriteChar(port);
				break;
			}

		} // end switch

	} // close forever loop

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日韩欧美美女一区二区三区| 欧美日本一区二区三区| 亚洲欧美另类久久久精品| 欧美美女bb生活片| 国产**成人网毛片九色 | 99re亚洲国产精品| 亚洲福利国产精品| 国产精品污网站| 欧美日产国产精品| 99精品欧美一区二区三区小说| 这里只有精品视频在线观看| 综合久久综合久久| 欧美大胆人体bbbb| 色呦呦日韩精品| 国产一区二区在线看| 亚洲国产精品影院| 亚洲视频一区二区在线观看| 日韩欧美一级二级| 欧美精品久久天天躁| 色综合色综合色综合色综合色综合| 国产真实乱偷精品视频免| 五月天精品一区二区三区| 最新日韩av在线| 久久久久久97三级| 精品国产百合女同互慰| 欧美日韩综合一区| 91视频国产观看| 成人综合婷婷国产精品久久| 久久99国产精品尤物| 青娱乐精品视频| 亚洲国产视频直播| 又紧又大又爽精品一区二区| 国产欧美日本一区二区三区| 欧美变态口味重另类| 91精品在线一区二区| 欧美日韩成人综合在线一区二区| 国产成人综合亚洲网站| 精品亚洲免费视频| 日韩av在线免费观看不卡| 亚洲综合免费观看高清完整版在线| 亚洲欧洲性图库| 欧美国产一区视频在线观看| 久久精品夜夜夜夜久久| 亚洲精品一区二区三区精华液 | 欧美午夜片在线观看| 91丨porny丨国产| 99麻豆久久久国产精品免费| 成人av一区二区三区| 国产 日韩 欧美大片| 成人污污视频在线观看| www.色精品| 色激情天天射综合网| 欧美综合在线视频| 欧美理论电影在线| 欧美一区二区三区在线看| 日韩免费成人网| 久久精品在线观看| 成人欧美一区二区三区小说 | 亚洲尤物在线视频观看| 亚洲二区在线视频| 日韩av网站免费在线| 美女视频一区二区| 国产成人精品午夜视频免费| 成人h动漫精品一区二| av一区二区三区四区| 色香蕉成人二区免费| 欧美色网站导航| 日韩精品专区在线影院重磅| 久久综合狠狠综合| 18成人在线观看| 午夜电影网一区| 国产一区二区三区蝌蚪| 99免费精品在线| 欧美二区三区91| 2020国产精品自拍| 国产精品短视频| 日韩国产精品久久久久久亚洲| 国产一区二区三区在线看麻豆| 成人爽a毛片一区二区免费| 色欧美日韩亚洲| 欧美一区二区三区人| 国产精品水嫩水嫩| 午夜电影久久久| 国产+成+人+亚洲欧洲自线| 色偷偷久久人人79超碰人人澡| 欧美日本免费一区二区三区| 久久久久久夜精品精品免费| 自拍偷拍欧美精品| 日本不卡视频在线观看| 成人av在线一区二区三区| 欧美剧情片在线观看| 久久久久久久久久久黄色| 一二三四社区欧美黄| 看片的网站亚洲| 色猫猫国产区一区二在线视频| 欧美mv和日韩mv国产网站| 亚洲图片你懂的| 青青国产91久久久久久| 91视频你懂的| 久久女同性恋中文字幕| 亚洲一区二区三区四区中文字幕| 国产精品一区二区黑丝| 3d成人动漫网站| 亚洲欧美另类久久久精品2019| 精品一区二区三区久久| 色999日韩国产欧美一区二区| 精品国产三级a在线观看| 亚洲国产精品一区二区www| 国产成人在线视频播放| 欧美日韩精品三区| 亚洲免费观看高清完整| 国产不卡一区视频| 日韩午夜在线影院| 亚洲国产中文字幕| 91影院在线免费观看| 久久久久久亚洲综合| 免费成人在线播放| 精品视频一区二区三区免费| 中文字幕在线视频一区| 国产精品一区二区无线| 日韩一区二区高清| 午夜精品视频在线观看| 99riav一区二区三区| 国产欧美日韩一区二区三区在线观看| 免费的成人av| 91精品久久久久久久久99蜜臂| 亚洲理论在线观看| kk眼镜猥琐国模调教系列一区二区 | 精品国产第一区二区三区观看体验| 亚洲国产成人tv| 91麻豆免费在线观看| 国产精品每日更新在线播放网址| 国产精品1区2区| 日本一区免费视频| 国产成人欧美日韩在线电影| 精品99久久久久久| 捆绑调教美女网站视频一区| 91精品国产aⅴ一区二区| 亚洲一区视频在线| 在线观看www91| 亚洲综合区在线| 欧美色图在线观看| 天天色天天操综合| 日韩一二三区视频| 精品在线一区二区| 亚洲精品在线免费播放| 国产一区激情在线| 日本一区二区三区视频视频| 国产丶欧美丶日本不卡视频| 国产日产精品1区| 春色校园综合激情亚洲| 国产精品久久二区二区| 91啪在线观看| 亚洲一区二区欧美| 91麻豆精品国产自产在线| 日韩中文字幕亚洲一区二区va在线 | 国产精品第四页| 在线亚洲高清视频| 日日夜夜精品视频天天综合网| 日韩一区二区不卡| 国产精品影视网| 日韩毛片精品高清免费| 日本韩国一区二区三区| 三级欧美在线一区| 欧美精品一区二区三区在线播放 | 99久免费精品视频在线观看| 亚洲视频 欧洲视频| 欧美日韩不卡一区二区| 久久99精品国产麻豆不卡| 亚洲国产精品传媒在线观看| 99久久综合狠狠综合久久| 亚洲一区在线看| 日韩精品资源二区在线| 国产69精品久久99不卡| 一级女性全黄久久生活片免费| 日韩一级片在线观看| 国产老妇另类xxxxx| 亚洲精品国产第一综合99久久| 91精品国产综合久久精品麻豆| 国产一区二区电影| 亚洲色图欧洲色图婷婷| 91麻豆精品国产自产在线观看一区| 国内精品在线播放| 一区二区三区丝袜| 26uuu国产日韩综合| 在线观看视频91| 久久超碰97人人做人人爱| 国产精品久久夜| 欧美一级欧美三级| 色哟哟亚洲精品| 国产一区二区三区在线观看免费视频| 亚洲女人的天堂| 久久亚洲一区二区三区四区| 在线看国产一区| 国产一区二区影院| 亚洲一卡二卡三卡四卡无卡久久| 精品久久久网站| 欧美日韩精品电影| 91在线视频观看| 黄色日韩网站视频|