⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wzdprtio.cpp

📁 E:Visual_C__MFC扩展编程实例 实例52:使用串行或并行I/O
💻 CPP
字号:
// PortIO.cpp: implementation of the CPortIO class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WzdPrtIO.h"

//////////////////////////////////////////////////////////////////////
// Open Printer Port
//////////////////////////////////////////////////////////////////////

BOOL CWzdPortIO::OpenLPT(int n,CFileException *e)
{
	CString portName;
	portName.Format("LPT%d:",n);
 	return Open( portName, CFile::modeReadWrite, e);
}
 
//////////////////////////////////////////////////////////////////////
// Open Serial Port
//////////////////////////////////////////////////////////////////////

BOOL CWzdPortIO::OpenCOM(int n, CFileException *e, int baud, int parity, int databits, int stopbits)
{
	CString portName;
	portName.Format("COM%d:",n);
 	if (Open( portName, CFile::modeReadWrite, e))
	{
	 	DCB dcb;
	 	::GetCommState( (HANDLE)m_hFile, &dcb );
	 	if (baud!=-1)     dcb.BaudRate = baud;
	 	if (databits!=-1) dcb.ByteSize = databits;
	 	if (stopbits!=-1) dcb.StopBits = stopbits;
	 	if (parity!=-1)   dcb.Parity = parity;
	 	::SetCommState( (HANDLE)m_hFile, &dcb );
		return(TRUE);
	}
	return(FALSE);
}

//////////////////////////////////////////////////////////////////////
// Send to Port
//////////////////////////////////////////////////////////////////////

void CWzdPortIO::Send(LPSTR lpBuf, int len)
{
	// initialize the structure we will pass to thread
	m_SendData.pFile=this;
	m_SendData.lpBuf=lpBuf;
	m_SendData.len=len;

	// start the thread
	AfxBeginThread(SendThread,&m_SendData);
}

UINT SendThread( LPVOID pParam )
{
	// get data from thread creator
	SENDDATA *pSend=(SENDDATA *)pParam;

	// do the write
 	pSend->pFile->Write( pSend->lpBuf, pSend->len);

	return 0;
}


//////////////////////////////////////////////////////////////////////
// Listen to Port
//////////////////////////////////////////////////////////////////////

void CWzdPortIO::Listen(int hdrSz, int bodyPos, CWzdQueue *pQueue, CWnd *pWnd, UINT msg, UINT id)
{
	// cancel timeouts!	we want to wait forever until next message comes in
 	COMMTIMEOUTS cto;
 	::GetCommTimeouts( (HANDLE)m_hFile, &cto );
 	cto.ReadIntervalTimeout = 0;
 	cto.WriteTotalTimeoutMultiplier = 0;
 	cto.WriteTotalTimeoutConstant = 0;
     ::SetCommTimeouts( (HANDLE)m_hFile, &cto );

	// initialize the structure we will pass to thread
	m_RecvData.pFile=this;
	m_RecvData.hdrSz=hdrSz;
	m_RecvData.bodyPos=bodyPos;
	m_RecvData.pQueue=pQueue;
	m_RecvData.pWnd=pWnd;
	m_RecvData.msg=msg;
	m_RecvData.id=id;

	// start the thread
	AfxBeginThread(RecvThread,&m_RecvData);
}

UINT RecvThread( LPVOID pParam )
{
	// get data from thread creator
	RECVDATA *pRecv=(RECVDATA *)pParam;

	while (TRUE) //forever
	{
		// read the header
		int len;
		int error=0;
		char *pHdr=new char[pRecv->hdrSz];
		try
		{
		 	len=pRecv->pFile->Read( pHdr, pRecv->hdrSz);
		}
		catch (CFileException *e)
		{
			error=e->m_cause;
			e->Delete();
		}

		// read the body???
		char *pBody=NULL;
		if (!error && pRecv->bodyPos!=-1)
		{
			int bodyLen=*((short *)pHdr+pRecv->bodyPos);
			pBody=new char[bodyLen];
			try
			{
			 	len+=pRecv->pFile->Read( pBody, bodyLen );
			}
			catch (CFileException *e)
			{
				error=e->m_cause;
				e->Delete();
			}
		}

		// put message in queue
		pRecv->pQueue->Add(new CWzdMsg(pRecv->id,pHdr,pBody,len,error));

		// post message to window to process this new message
		pRecv->pWnd->PostMessage(pRecv->msg);
	}

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -