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

📄 modem.cpp

📁 MudMaster 2000 的C++源码
💻 CPP
字号:
/************************************************************************************
	Copyright (c) 2000 Aaron O'Neil
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

		1) Redistributions of source code must retain the above copyright notice, 
				this list of conditions and the following disclaimer.
		2) Redistributions in binary form must reproduce the above copyright notice, 
				this list of conditions and the following disclaimer in the documentation
				and/or other materials provided with the distribution.
		3) Redistributions in binary form must reproduce the above copyright notice on
				program startup. Additional credits for program modification are acceptable
				but original copyright and credits must be visible at startup.
		4) You may charge a reasonable copying fee for any distribution of Mud Master. 
				You may charge any fee you choose for support of Mud Master. You may not 
				charge a fee for Mud Master itself.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************************/

#include "StdAfx.h"
#include "Modem.h"
#include "extern.h"

CModem::CModem()
{
	m_hModem = INVALID_HANDLE_VALUE;
}

CModem::~CModem()
{
}

void CModem::Close()
{
	CloseHandle(m_hModem);
	m_hModem = INVALID_HANDLE_VALUE;
}

BOOL CModem::Open(const char *pszPort, const char *pszSpeed,
	const char *pszDataBits, const char *pszParity, const char *pszStop)
{
	CString strPort(pszPort);
	strPort.MakeUpper();
	CString strPortName("\\\\.\\");
	strPortName += strPort;
	strPortName.MakeUpper();
	m_hModem = CreateFile(
		strPortName,
		GENERIC_READ|GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		0,
		NULL);
/*	m_hModem = CreateFile(
		strPortName,
		GENERIC_READ|GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
		NULL); */

	if (m_hModem == INVALID_HANDLE_VALUE)
	{
		CString strText;
		strText.Format("CreateFile failed: %s",(const char *)strPortName);
		PrintMessage(strText);
		return(FALSE);
	}

	// Put a bogus baud rate it, just to get the function to fill
	// in the other params.
	CString strTemp;
	strTemp.Format("%s: 12,%c,%s,%s",
		(const char *)strPort,
		*pszParity,
		pszDataBits,
		pszStop);

	if (!BuildCommDCB(strTemp,&m_dcb))
	{
		CString strText;
		strText.Format("BuildCommDCB failed: %s",(const char *)strTemp);
		PrintMessage(strText);
		return(FALSE);
	}

	// Now we can actually set the baud rate.
	UINT nBaudSetting;
	long nBaudRate = atol(pszSpeed);
	switch(nBaudRate)
	{
		case 300 :
			nBaudSetting = 300;
			break;
			
		case 1200 :
			nBaudSetting = 1200;
			break;
			
		case 2400 :
			nBaudSetting = 2400;
			break;                            
			
		case 4800 :
			nBaudSetting = 4800;
			break;
			
		case 9600 :
			nBaudSetting = CBR_9600;
			break;
			
		case 19200 :
			nBaudSetting = CBR_19200;
			break;
			
		case 38400 :
			nBaudSetting = CBR_38400;
			break;
			
		case 57600 :
			nBaudSetting = CBR_56000;
			break;
			
		case 115000 :
			nBaudSetting = CBR_128000;
			break;
			
		default :
			nBaudSetting = 2400;
			break;
	}
	
	m_dcb.BaudRate = nBaudSetting;

	// Make sure it is in binary mode.
	m_dcb.fBinary = 1;

	if (SetCommState(m_hModem,&m_dcb) < 0)
	{
		PrintMessage("SetCommState failed.");
		return(FALSE);
	}

	// Set the timeout values.  We don't want the ReadFile
	// function to hang out waiting for chars.
	COMMTIMEOUTS ct;
	ct.ReadIntervalTimeout = MAXDWORD;
	ct.ReadTotalTimeoutMultiplier = 0;
	ct.ReadTotalTimeoutConstant = 0;
	ct.WriteTotalTimeoutMultiplier = 0;
	ct.WriteTotalTimeoutConstant = 0;
	if (!SetCommTimeouts(m_hModem,&ct))
	{
		PrintMessage("SetCommTimeouts failed.");
		return(FALSE);
	}


	return(m_hModem != INVALID_HANDLE_VALUE);
}

int CModem::Send(const char *pszText)
{
	DWORD wNumWritten;
	WriteFile(m_hModem,pszText,strlen(pszText),&wNumWritten,NULL);
	return(wNumWritten);
}

int CModem::Read(char *pBuf, int nMaxToRead)
{
	DWORD wNumRead;
	if (!ReadFile(m_hModem,pBuf,nMaxToRead,&wNumRead,NULL))
		return(-1);
	return(wNumRead);
}

void CModem::Hangup()
{
	EscapeCommFunction(m_hModem,CLRDTR);
	time_t timeFirst, timeNow;
	time(&timeFirst);
	timeNow = timeFirst;
	while(timeNow - timeFirst < 2)
		time(&timeNow);
	EscapeCommFunction(m_hModem,SETDTR);
}

⌨️ 快捷键说明

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