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

📄 aa1dlg.cpp

📁 GPRS拨号程序
💻 CPP
字号:
// aa1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "aa1.h"
#include "aa1Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE hCom;  //全局变量,串口句柄
/////////////////////////////////////////////////////////////////////////////
// CAa1Dlg dialog

CAa1Dlg::CAa1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAa1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAa1Dlg)
	m_disp = _T("");
	m_send = _T("AT");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAa1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAa1Dlg)
	DDX_Text(pDX, IDC_DISP, m_disp);
	DDX_Text(pDX, IDC_SEND_EDIT, m_send);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAa1Dlg, CDialog)
	//{{AFX_MSG_MAP(CAa1Dlg)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_RECEIVE, OnReceive)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAa1Dlg message handlers

BOOL CAa1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here

	hCom=CreateFile(_T("COM1:"),//COM1口
		GENERIC_READ|GENERIC_WRITE, //允许读和写
		0, //独占方式
		NULL,
		OPEN_EXISTING, //打开而不是创建
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, //重叠方式
		NULL);
	if(hCom==(HANDLE)-1)
	{
		AfxMessageBox(_T("打开COM失败!"));
		return FALSE;
	}

	SetupComm(hCom,512,512); //输入缓冲区和输出缓冲区的大小都是512

	COMMTIMEOUTS TimeOuts;
	//设定读超时
	TimeOuts.ReadIntervalTimeout=MAXDWORD;
	TimeOuts.ReadTotalTimeoutMultiplier=0;
	TimeOuts.ReadTotalTimeoutConstant=0;
	//在读一次输入缓冲区的内容后读操作就立即返回,
	//而不管是否读入了要求的字符。


	//设定写超时
	TimeOuts.WriteTotalTimeoutMultiplier=100;
	TimeOuts.WriteTotalTimeoutConstant=500;
	SetCommTimeouts(hCom,&TimeOuts); //设置超时

	DCB dcb;
	GetCommState(hCom,&dcb);
	dcb.BaudRate=9600; //波特率为9600
	dcb.ByteSize=8; //每个字节有8位
	dcb.Parity=NOPARITY; //无奇偶校验位
	dcb.StopBits=ONESTOPBIT; //两个停止位

	dcb.fOutxCtsFlow = TRUE; //串行端口的输出由CTS线控制 
	dcb.fOutxDsrFlow = FALSE; //关闭串行端口的DSR流控制 
	dcb.fDtrControl = DTR_CONTROL_ENABLE; //启用DTR线 
	//dcb.fDsrSensitivity = FALSE; //如果设为TRUE将忽略任何输入的字节,除非DSR线被启用 
	dcb.fTXContinueOnXoff = FALSE; //当为TRUE时,如果接收缓冲区已满且驱动程序已传送XOFF字符,将使驱动程序停止传输字符 
	dcb.fTXContinueOnXoff = FALSE; 
	dcb.fOutX = FALSE; //设为TRUE指定XON/XOFF控制被用于控制串行输出 
	dcb.fInX = FALSE; //设为TRUE指定XON/XOFF控制被用于控制串行输入 
	dcb.fErrorChar = FALSE; //WINCE串行驱动程序的默认执行将忽略这个字段 
	dcb.fNull = FALSE; //设为TRUE将使串行驱动程序忽略收到的空字节 
	dcb.fRtsControl = RTS_CONTROL_ENABLE; //启用RTS线 
	
	SetCommState(hCom,&dcb);


	PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
	

	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CAa1Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
}

void CAa1Dlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	OVERLAPPED m_osWrite;
	memset(&m_osWrite,0,sizeof(OVERLAPPED));
	m_osWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);

	DWORD dwBytesWrite=0;
	COMSTAT ComStat;
	DWORD dwErrorFlags;
	BOOL bWriteStat;
	CString temp;
	UpdateData(TRUE);

	TCHAR lpOutBuffer[256];

	EscapeCommFunction (hCom, SETDTR); 
	EscapeCommFunction (hCom, SETRTS); 

/*
	strcpy(lpOutBuffer,"AT+CMGS=?\r\n");
    dwBytesWrite=strlen(lpOutBuffer);
*/

	ClearCommError(hCom,&dwErrorFlags,&ComStat);
    temp = m_send+"\r\n";
	dwBytesWrite = temp.GetLength()*sizeof(TCHAR);
	
	bWriteStat=WriteFile(hCom,LPCTSTR(temp),dwBytesWrite,
		& dwBytesWrite,&m_osWrite);

//	dwBytesWrite = 4;
//	bWriteStat=WriteFile(hCom,"AT\r\n",dwBytesWrite,
//		& dwBytesWrite,&m_osWrite);

//	bWriteStat=WriteFile(hCom,lpOutBuffer,
//		dwBytesWrite,& dwBytesWrite,&m_osWrite);

	if(!bWriteStat)
	{
		if(GetLastError()==ERROR_IO_PENDING)
		{
			WaitForSingleObject(m_osWrite.hEvent,1000);
		}
	}
	UpdateData(FALSE);
	
}

void CAa1Dlg::OnReceive() 
{
	// TODO: Add your control notification handler code here
	OVERLAPPED m_osRead;
	memset(&m_osRead,0,sizeof(OVERLAPPED));
	m_osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);

	COMSTAT ComStat;
	DWORD dwErrorFlags;
	
	char str[100];
	memset(str,0,100);
	DWORD dwBytesRead=100;//读取的字节数
	BOOL bReadStat;

	ClearCommError(hCom,&dwErrorFlags,&ComStat);
	dwBytesRead=min(dwBytesRead, (DWORD)ComStat.cbInQue);
	bReadStat=ReadFile(hCom,str,
		dwBytesRead,&dwBytesRead,&m_osRead);
	if(!bReadStat)
	{
		if(GetLastError()==ERROR_IO_PENDING)
	    //GetLastError()函数返回ERROR_IO_PENDING,表明串口正在进行读操作
		{
			WaitForSingleObject(m_osRead.hEvent,2000);
		    //使用WaitForSingleObject函数等待,直到读操作完成或延时已达到2秒钟
		    //当串口读操作进行完毕后,m_osRead的hEvent事件会变为有信号
			
		
		}
	}

   	m_disp=str;
	PurgeComm(hCom, PURGE_TXABORT|
		PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
	UpdateData(FALSE);	
}

⌨️ 快捷键说明

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