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

📄 comports.cpp

📁 通过发送AT命令实现发送和接受短信的功能
💻 CPP
字号:
// SerialPort.cpp: implementation of the CSerialPort class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "SMS.h"
#include "ComPorts.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CComPorts::CComPorts()
{
	m_hCom = NULL;
}

CComPorts::~CComPorts()
{
	CloseHandle(m_hCom);
	delete m_hCom;
}

BOOL CComPorts::Open(int portnum, int baud)
{
	TCHAR szPort[15];
	wsprintf( szPort, "COM%d", portnum );
	m_hCom = CreateFile( szPort,
							GENERIC_READ | GENERIC_WRITE,
							0,    // comm devices must be opened w/exclusive-access 
							NULL, // no security attributes 
							OPEN_EXISTING, // comm devices must use OPEN_EXISTING 
							0,    // not overlapped I/O 
							NULL  // hTemplate must be N`ULL for comm devices 
							);

	if (m_hCom == INVALID_HANDLE_VALUE) 
	{
		//DWORD error = GetLastError();
		//AfxMessageBox("open com error");
		return FALSE;
	}	

	DCB dcb;
	GetCommState(m_hCom, &dcb);
	dcb.BaudRate = baud;
	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;
	dcb.fDtrControl = DTR_CONTROL_ENABLE;
	dcb.fRtsControl = DTR_CONTROL_ENABLE;
	dcb.fParity = 0;
	dcb.fInX = 0;
	dcb.fOutX = 0;
	dcb.fDsrSensitivity = 0;
	dcb.fTXContinueOnXoff  = 0;
	dcb.fOutxDsrFlow = 0;
	dcb.fOutxCtsFlow = 0;
	if (SetCommState(m_hCom, &dcb)==0)
	{
		//AfxMessageBox("set commstate error");
		return FALSE;
	}
	return TRUE;
}

BOOL CComPorts::Close()
{
	if (!CloseHandle (m_hCom))
	{
		DWORD dwError = GetLastError ();
		return FALSE;
	}
	return TRUE;
}


int CComPorts::Output(void *pData, int nLength)
{
	DWORD dwNumWrite;	// 串口发出的数据长度
	WriteFile(m_hCom, pData, (DWORD)nLength, &dwNumWrite, NULL);
	return (int)dwNumWrite;
}

int CComPorts::GetInput(void *pData, int nLength)
{
	DWORD dwNumRead;	// 串口收到的数据长度
	ReadFile(m_hCom, pData, (DWORD)nLength, &dwNumRead, NULL);
	return (int)dwNumRead;
}

int CComPorts::ReadData( char *data )
{
	char Byte;
	DWORD dwCommModemStatus,dwBytesTransferred;
	int len = 0;
	
	SetCommMask (m_hCom, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD );
	
	if (m_hCom != INVALID_HANDLE_VALUE) 
	{
		WaitCommEvent (m_hCom, &dwCommModemStatus, 0);
		
		
		SetCommMask (m_hCom, EV_RXCHAR | EV_CTS | EV_DSR );
		
		if (dwCommModemStatus & EV_RXCHAR) 
		{
			// Loop for waiting for the data.
			do 
			{
				// Read the data from the serial port.
				ReadFile (m_hCom,
					&Byte, 
					1, 
					&dwBytesTransferred,
					0
					);
				
				// Display the data read.
				if (dwBytesTransferred == 1)
				{
					data[len] = Byte;
					data[len+1] = 0;
					len++;
				}
			}
			while (dwBytesTransferred == 1);
		}   
	}
	return len;
}

⌨️ 快捷键说明

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