亚洲欧美第一页_禁久久精品乱码_粉嫩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>
 
//
// 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()
{
	SetEvent(m_hShutdownEvent);//replace by whj
	m_bThreadAlive = FALSE;
	if(m_Thread)
	{
		TerminateThread(m_Thread,-1);
		m_Thread=NULL;
	}
//	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;
				
				// Kill this thread.  break is not needed, but makes me feel better.
				AfxEndThread(100);
				break;
			}
		case 1:	// read event
			{
				GetCommMask(port->m_hComm, &CommEvent);

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产人妖乱国产精品人妖| 欧美少妇xxx| 日本成人中文字幕| 亚洲国产精品一区二区尤物区| 亚洲国产精品精华液2区45| 久久久久久久久99精品| 国产欧美一区二区精品久导航| 精品国产青草久久久久福利| 精品国产污网站| 久久久av毛片精品| 日本一二三四高清不卡| 亚洲人成网站精品片在线观看| 亚洲视频 欧洲视频| 一区二区欧美在线观看| 午夜影院在线观看欧美| 九色|91porny| 盗摄精品av一区二区三区| 成人免费观看男女羞羞视频| 91日韩精品一区| 欧美亚一区二区| 欧美日本不卡视频| 欧美一区二区三区视频在线观看 | 这里是久久伊人| 久久国产综合精品| 成人av先锋影音| 精品三级av在线| 看电影不卡的网站| 日韩午夜中文字幕| 免费观看日韩电影| 日韩免费一区二区三区在线播放| 午夜精品一区二区三区电影天堂| 日本丶国产丶欧美色综合| 亚洲色欲色欲www在线观看| 不卡的av网站| 亚洲综合999| 欧美最猛性xxxxx直播| 亚洲成人一区二区在线观看| 精品国产一二三| 国产精品亚洲一区二区三区在线| 久久婷婷久久一区二区三区| 国产曰批免费观看久久久| 久久久久久久久久电影| 粉嫩av亚洲一区二区图片| 国产精品嫩草影院av蜜臀| 色综合咪咪久久| 亚洲亚洲精品在线观看| 欧美日本乱大交xxxxx| 另类成人小视频在线| 国产亚洲女人久久久久毛片| av激情综合网| 性久久久久久久久| 2024国产精品| av电影天堂一区二区在线观看| 亚洲精品视频在线观看网站| 欧美日韩免费视频| 国产真实乱子伦精品视频| 国产精品电影院| 欧美日韩高清影院| 国产成人在线色| 一区二区成人在线| 日韩欧美在线不卡| 成人免费视频播放| 日韩在线卡一卡二| 中文字幕欧美日本乱码一线二线 | 欧美激情综合网| 色94色欧美sute亚洲线路二| 蜜臀久久99精品久久久久久9| 久久久激情视频| 欧美日精品一区视频| 国内外成人在线| 亚洲图片自拍偷拍| 国产欧美一区二区三区鸳鸯浴| 色老汉av一区二区三区| 蜜桃av一区二区三区| 亚洲欧美日韩国产手机在线| 日韩欧美高清dvd碟片| 91麻豆6部合集magnet| 九九精品一区二区| 一级日本不卡的影视| 久久久国产一区二区三区四区小说| 色综合天天综合色综合av | 亚洲精品中文字幕乱码三区| 亚洲天天做日日做天天谢日日欢 | 亚洲尤物视频在线| 久久久久久99精品| 91精品综合久久久久久| 91丨九色porny丨蝌蚪| 久久99国产精品成人| 亚洲一区二区三区视频在线播放| 国产人成亚洲第一网站在线播放| 欧美日韩一区二区三区免费看 | 美国av一区二区| 亚洲一区二区在线免费看| 中文字幕成人网| 欧美精品一区二区三区久久久| 欧美在线观看禁18| 99re66热这里只有精品3直播 | 久久久美女艺术照精彩视频福利播放| 91官网在线免费观看| 粉嫩aⅴ一区二区三区四区| 日本成人在线网站| 一区二区在线观看不卡| 成人欧美一区二区三区在线播放| 久久久久久麻豆| 亚洲精品一区在线观看| 日韩色在线观看| 91精品国产综合久久福利软件| 欧美网站一区二区| 欧洲激情一区二区| 欧美日韩一级二级三级| 欧美日韩一区二区三区四区| 欧美午夜精品免费| 在线观看国产日韩| 欧美性三三影院| 欧美日韩亚洲综合一区| 色综合天天综合网国产成人综合天| 成人动漫一区二区在线| 成人性色生活片| av不卡免费在线观看| 成人久久视频在线观看| av午夜一区麻豆| 欧亚一区二区三区| 精品视频1区2区| 欧美一区二区在线观看| 日韩一区二区三免费高清| 精品捆绑美女sm三区| 久久久国际精品| 中文字幕一区二区三| 亚洲色图19p| 国产一区二区福利视频| 福利一区二区在线| av在线播放不卡| 欧洲一区二区三区在线| 欧美一区二区三区爱爱| 精品奇米国产一区二区三区| 国产无遮挡一区二区三区毛片日本| 欧美国产视频在线| 一区二区不卡在线播放 | 午夜久久久久久久久| 免费人成精品欧美精品| 国产毛片精品视频| 99精品久久免费看蜜臀剧情介绍| 在线免费不卡电影| 欧美不卡在线视频| 亚洲国产精品成人综合| 亚洲女同女同女同女同女同69| 五月天一区二区三区| 国精品**一区二区三区在线蜜桃| 成人激情图片网| 在线成人午夜影院| 国产日韩欧美高清在线| 亚洲综合激情另类小说区| 美国十次综合导航| 欧美成人性战久久| 日韩精品在线网站| 久久一区二区三区国产精品| 精品久久久久av影院| 精品久久久久久综合日本欧美 | 国产精品污污网站在线观看| 久久精品夜色噜噜亚洲aⅴ| 精品国产乱码久久久久久久| 精品久久久久一区二区国产| wwwwxxxxx欧美| 日本一区二区三区四区在线视频| 国产欧美日韩精品一区| 国产精品乱码一区二区三区软件| 国产精品色婷婷久久58| 亚洲精品国产高清久久伦理二区| 一二三四社区欧美黄| 五月天欧美精品| 理论电影国产精品| av亚洲精华国产精华| 欧美无人高清视频在线观看| 欧美理论在线播放| 欧美videossexotv100| 国产欧美一区二区三区在线老狼| 中文字幕佐山爱一区二区免费| 亚洲综合自拍偷拍| 美腿丝袜亚洲三区| 成人性生交大片免费看在线播放| 色哟哟一区二区| 精品三级av在线| 悠悠色在线精品| 国产一区二区在线视频| 高清av一区二区| 5566中文字幕一区二区电影| 久久亚洲二区三区| 亚洲免费观看在线观看| 奇米一区二区三区| 99麻豆久久久国产精品免费优播| 欧美日韩精品一区二区三区四区 | 美国毛片一区二区| 91在线视频网址| 精品日韩av一区二区| 中文字幕在线一区| 日韩不卡一区二区三区| 亚洲一区二区中文在线| 91久久一区二区| 亚洲一区二区偷拍精品| 欧美三级电影在线观看|