亚洲欧美第一页_禁久久精品乱码_粉嫩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_szWriteBuffer = new BYTE[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

	return 0;
}

//
// start comm watching

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产精品一区二区你懂的| 亚洲人123区| 大尺度一区二区| 亚洲国产一区二区三区青草影视| 欧美久久久久免费| 国产盗摄精品一区二区三区在线| 亚洲你懂的在线视频| 中文天堂在线一区| 日韩欧美在线123| 99久久婷婷国产综合精品电影 | 视频在线观看国产精品| 久久精品一区二区三区不卡牛牛| 欧洲亚洲精品在线| 国产成人自拍高清视频在线免费播放| 麻豆91在线观看| 亚洲综合免费观看高清完整版| 精品国产sm最大网站免费看| 色噜噜狠狠色综合中国| 国产乱码精品一品二品| 国产精品18久久久久久久久| 国产高清一区日本| 不卡的av网站| 国产精品一二三四区| 国v精品久久久网| 男人操女人的视频在线观看欧美| 亚洲裸体xxx| 久久综合九色综合久久久精品综合| 欧美午夜精品久久久| 99riav一区二区三区| 色呦呦国产精品| 99久久免费精品| 国产精品久久久久一区二区三区共| 欧美一区二区三区男人的天堂| 91丨九色porny丨蝌蚪| 国产999精品久久| 91在线观看一区二区| 欧美日韩一区二区三区不卡| 91国产视频在线观看| 欧美一级日韩一级| 国产欧美日韩不卡| 国产亚洲成av人在线观看导航| 国产精品国产三级国产aⅴ无密码 国产精品国产三级国产aⅴ原创 | 欧美成人官网二区| 91看片淫黄大片一级在线观看| 91在线精品秘密一区二区| 色88888久久久久久影院野外| 99视频在线精品| 3d动漫精品啪啪一区二区竹菊| 美日韩黄色大片| 国产一区二区三区观看| 无码av中文一区二区三区桃花岛| 久久久www免费人成精品| 91麻豆精品91久久久久同性| 日韩欧美一级在线播放| 欧美日韩中文字幕精品| 欧美一级淫片007| 久久久久久一二三区| 国产精品卡一卡二| 亚洲影院在线观看| 久久超级碰视频| 麻豆精品视频在线| 国产999精品久久| 99精品国产热久久91蜜凸| 欧美精品久久久久久久多人混战 | 极品少妇xxxx精品少妇| 日本特黄久久久高潮| 偷拍日韩校园综合在线| 激情综合网天天干| 国产美女久久久久| 欧美日本在线一区| 本田岬高潮一区二区三区| 欧美色图天堂网| 国产亚洲一区二区在线观看| 一区二区三区四区五区视频在线观看 | 风流少妇一区二区| 欧美男人的天堂一二区| 国产午夜精品理论片a级大结局| 一二三区精品视频| 国产精品自拍网站| 欧美亚洲综合另类| 亚洲国产精品成人综合| 国产精品久久久久婷婷| 亚洲成av人片一区二区三区| 国产成都精品91一区二区三| 欧美精品精品一区| 久久精品夜色噜噜亚洲a∨| 一区二区三区在线视频免费| 国产一区二区伦理片| 欧美精品v日韩精品v韩国精品v| 国产精品天干天干在线综合| 日韩国产在线一| 一本一道综合狠狠老| 久久免费视频一区| 日韩二区在线观看| 色又黄又爽网站www久久| 中文字幕欧美日本乱码一线二线| 亚洲一区二区中文在线| 99视频精品全部免费在线| 久久尤物电影视频在线观看| 五月激情六月综合| 日本精品裸体写真集在线观看| 日本一区二区三区免费乱视频| 美脚の诱脚舐め脚责91| 欧美天堂一区二区三区| 国产精品免费aⅴ片在线观看| 日韩精品久久久久久| 欧美三级一区二区| 亚洲欧美视频一区| www.激情成人| 国产精品天美传媒| 国产成人亚洲综合a∨婷婷图片 | 日韩视频一区二区三区在线播放| 伊人色综合久久天天人手人婷| 成人国产精品免费网站| 久久综合久久久久88| 久久99久久久久| 日韩一二在线观看| 全国精品久久少妇| 日韩一区二区三区电影在线观看 | 婷婷开心久久网| 欧美日韩色综合| 亚洲制服欧美中文字幕中文字幕| 91在线看国产| 一区二区三区在线视频免费观看| 91美女片黄在线观看| 久久精品欧美一区二区三区不卡 | 欧日韩精品视频| 中文字幕亚洲电影| 99免费精品在线| 亚洲精品免费电影| 色噜噜狠狠色综合欧洲selulu| 亚洲猫色日本管| 欧美色视频一区| 视频一区欧美日韩| 欧美一区二区三区啪啪| 久久精品72免费观看| 日韩一区二区免费在线观看| 日韩黄色片在线观看| 欧美一级日韩一级| 狠狠色狠狠色综合| 国产夜色精品一区二区av| 国产白丝精品91爽爽久久| 中文av一区二区| 国产v综合v亚洲欧| 亚洲视频你懂的| 欧美日韩在线播放| 美腿丝袜亚洲一区| 久久九九久久九九| 国产盗摄一区二区| 国产精品久久久久久久蜜臀| 色综合久久天天| 五月天婷婷综合| 欧美成人福利视频| av不卡免费电影| 亚洲v日本v欧美v久久精品| 日韩欧美国产综合一区 | 久久精品亚洲一区二区三区浴池| 国产福利一区二区| 亚洲高清视频的网址| 亚洲精品一区二区三区香蕉| voyeur盗摄精品| 午夜久久久影院| 久久免费精品国产久精品久久久久| gogo大胆日本视频一区| 午夜一区二区三区视频| 精品999久久久| 日本道在线观看一区二区| 亚洲大片在线观看| 2023国产精品| 色婷婷国产精品综合在线观看| 日韩国产高清在线| 国产日韩一级二级三级| 欧美日韩一区三区| 国产福利精品一区二区| 亚洲第一av色| 精品剧情在线观看| 欧美色国产精品| 国产精品性做久久久久久| 一区二区高清在线| 久久久精品2019中文字幕之3| 欧美在线免费观看亚洲| 国产成人综合在线播放| 性久久久久久久久| 亚洲欧美综合网| 欧美成人综合网站| 91福利社在线观看| 国产福利一区二区三区在线视频| 欧美色图一区二区三区| 国产mv日韩mv欧美| 奇米一区二区三区av| 国产午夜精品福利| 欧美在线一区二区三区| 成人精品小蝌蚪| 久久99精品久久只有精品| 亚洲不卡一区二区三区| 中文字幕日韩精品一区| 欧美tickle裸体挠脚心vk| 精品视频资源站| 成人黄色在线视频| 国产精品77777|