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

📄 gprsdlg.cpp

📁 gprs开发使用程序
💻 CPP
字号:
/*!	\file		GPRSDlg.cpp
	\brief		应用程序界面实现文件
	\author		陈华毅
	\version	1.0

	用户通过这个窗口实现了与应用程序的交互,通过这个界面控制和使用GPRS模块。
	\defgroup	GPRSDLG_IMPLEMENT	CGPRSDlg类实现说明
*/
#include "stdafx.h"
#include "GPRS.h"
#include "GPRSDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//@{Group: GPRSDLG_IMPLEMENT


CGPRSDlg::CGPRSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGPRSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGPRSDlg)
	m_strMsg = _T("");
	m_strNum = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_hRevThread = INVALID_HANDLE_VALUE;
	m_hRevThread = CreateThread(NULL,0,(unsigned long (__cdecl *)(void *))CommThread,this,0,&m_dwThreadID);

}

void CGPRSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGPRSDlg)
	DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
	DDX_Text(pDX, IDC_EDIT_NUM, m_strNum);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGPRSDlg, CDialog)
	//{{AFX_MSG_MAP(CGPRSDlg)
	ON_BN_CLICKED(IDC_BUTTON_BACK, OnButtonBack)
	ON_BN_CLICKED(IDC_BUTTON_CALL, OnButtonCall)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON0, OnButton0)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
	ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
	ON_BN_CLICKED(IDC_BUTTON21, OnButton21)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_EN_SETFOCUS(IDC_EDIT_MSG, OnSetfocusEditMsg)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
	ON_BN_CLICKED(IDC_BUTTON_MSGBACK, OnButtonMsgback)
	ON_EN_SETFOCUS(IDC_EDIT_NUM, OnSetfocusEditNum)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGPRSDlg message handlers

BOOL CGPRSDlg::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

	/* 隐藏“开始”栏 */
	HWND lpClassName;
	lpClassName = ::FindWindow(TEXT("HHTaskBar"), NULL);
	::ShowWindow(lpClassName, SW_HIDE);
	
	/* 把work area 设为整个屏幕 */
	INT screenx=GetSystemMetrics(SM_CXSCREEN);
	INT screeny=GetSystemMetrics(SM_CYSCREEN);

	CRect rcWorkArea;
	rcWorkArea.left = 0;
	rcWorkArea.right = screenx;
	rcWorkArea.top = 0;
	rcWorkArea.bottom = screeny;
	::SystemParametersInfo(SPI_SETWORKAREA, 0, &rcWorkArea, SPIF_SENDCHANGE);

	m_iFocus = EDIT_NUM_FOCUS;
	m_gprs.CallState = FALSE;

	if (m_gprs.GPRSInit())
	{
		AfxMessageBox(_T("IntitGPRS模块失败!请重新启动程序!"));
	}

	SetDlgItemText(IDC_EDIT_MSG, _T("欢迎使用深圳市旋极历通科技\r\nGPRS演示程序"));

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

#define NOTIFY(s)	OnNotify(-1, (LPARAM)(s))

afx_msg	LRESULT CGPRSDlg::OnNotify(WPARAM wParam, LPARAM lParam)
{
	CEdit *pOutput = (CEdit *)GetDlgItem(IDC_EDIT_STATE);	
	ASSERT(NULL != pOutput);

	if (0 > (INT)wParam)
	{
		pOutput->SetWindowText((LPCTSTR)lParam);
	}else{
		if (0 == wParam)
		{
			pOutput->ReplaceSel(_T("NULL\r\n"));
		}else{
			UCHAR *lpData = (UCHAR *)lParam;
			CString str, strTemp;
			str.Format(_T("%02X"), lpData[0]);
			for (WORD w = 1; w < wParam; w++)
			{
				strTemp.Format(_T(" %02X"), lpData[w]);
				str += strTemp;
			}
			str += _T("\r\n");

			pOutput->ReplaceSel((LPCTSTR)str);
		}
	}

	return 0;
}

void CGPRSDlg::OnButtonBack() 
{
	// TODO: Add your control notification handler code here
	CString strEdit;
	GetDlgItemText(IDC_EDIT_NUM, strEdit);
	strEdit = strEdit.Left(strEdit.GetLength() - 1);
	SetDlgItemText(IDC_EDIT_NUM, strEdit);
}

void CGPRSDlg::OnButtonCall() 
{
	// TODO: Add your control notification handler code here
	CEdit* pNumberWnd=(CEdit*)GetDlgItem(IDC_EDIT_NUM);
	ASSERT(NULL != pNumberWnd);

	CString strBtnCall;
	GetDlgItemText(IDC_BUTTON_CALL, strBtnCall);

	if (_T("拨号") == strBtnCall)
	{
		NOTIFY(_T("打电话!"));
		SetDlgItemText(IDC_BUTTON_CALL, _T("挂机"));
		UpdateData(TRUE);
		m_gprs.gprsCall(m_strNum);
		m_gprs.CallState = TRUE;
	}else{
		SetDlgItemText(IDC_BUTTON_CALL, _T("拨号"));
		m_gprs.gprsHang();
		NOTIFY(_T("通话结束!"));
		m_gprs.CallState = FALSE;
	}

//	m_gprs.m_serial.WriteComm((UCHAR*)"AT&F\r", 4);
}

//DEL void CGPRSDlg::OnButtonHang() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	m_gprs.gprsHang();
//DEL }

void CGPRSDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	
	CEdit* pNumberWnd=(CEdit*)GetDlgItem(IDC_EDIT_NUM);
	ASSERT(NULL != pNumberWnd);
	CEdit* pContentWnd=(CEdit*)GetDlgItem(IDC_EDIT_MSG);
	ASSERT(NULL != pContentWnd);

	/* 检查号码 */
	UpdateData(TRUE);
	if(m_strNum.GetLength() < 11)
	{
		AfxMessageBox(_T("请输入正确的号码!"));
		pNumberWnd->SetFocus();
		pNumberWnd->SetSel(-1, 0);
		return;
	}

	/* 检查短消息内容是否空,或者超长 */
	if (m_strMsg.GetLength() < 1)
	{
		AfxMessageBox(_T("请输入消息内容!"));
		pContentWnd->SetFocus();
		pContentWnd->SetSel(-1, 0);
		return;
	}else if(m_strMsg.GetLength() > 70){		/* 我们决定全部用UCS2编码,最大70个字符(半角/全角) */
		AfxMessageBox(_T("消息内容太长,无法发送!"));
		pContentWnd->SetFocus();
		pContentWnd->SetSel(-1, 0);
		return;
	}else{}

	SuspendThread(m_hRevThread);		/* 暂停读取线程 */
	EnterCriticalSection(&m_csSend);	/* 进入发送短消息关键代码区 */
	NOTIFY(_T("正在发送!"));
	if (m_gprs.gprsSendMessage(m_strNum, m_strMsg))		/* 发送短消息 */
	{
		NOTIFY(_T("发送成功!"));
	}else{
		NOTIFY(_T("发送失败!"));
	}
	LeaveCriticalSection(&m_csSend);	/* 离开发送短消息关键代码区 */
	ResumeThread(m_hRevThread);			/* 恢复读取线程 */
}

void CGPRSDlg::OnButton0() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("0"));	
}

void CGPRSDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("1"));		 	
}

void CGPRSDlg::OnButton10() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("*"));
}

void CGPRSDlg::OnButton11() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("#"));
}

//DEL void CGPRSDlg::OnButton2() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL //	ShowText(CString("0"));
//DEL }

void CGPRSDlg::OnButton21() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("2"));
}

void CGPRSDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("3"));
}

void CGPRSDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("4"));
}

void CGPRSDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("5"));
}

void CGPRSDlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("6"));
}

void CGPRSDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("7"));
}

void CGPRSDlg::OnButton8() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("8"));
}

void CGPRSDlg::OnButton9() 
{
	// TODO: Add your control notification handler code here
	ShowText(CString("9"));
}

//DEL void CGPRSDlg::OnSetfocusEditCallnum() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	m_iFocus = EDIT_NUM_FOCUS;	
//DEL }

void CGPRSDlg::OnSetfocusEditMsg() 
{
	// TODO: Add your control notification handler code here
	m_iFocus = EDIT_MSG_FOCUS;	
}

//DEL void CGPRSDlg::OnSetfocusEditMsgnum() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	m_iFocus = EDIT_MSGNUM_FOCUS;	
//DEL }

VOID CGPRSDlg::ShowText(CString str)
{
	if (EDIT_NUM_FOCUS == m_iFocus)
	{
		CString strEdit;
		GetDlgItemText(IDC_EDIT_NUM, strEdit);
		strEdit += str;
		SetDlgItemText(IDC_EDIT_NUM, strEdit);
	}else{
		CString strEdit;
		GetDlgItemText(IDC_EDIT_MSG, strEdit);
		strEdit += str;
		SetDlgItemText(IDC_EDIT_MSG, strEdit);
	}
}

void CGPRSDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here

	/* 显示“开始”栏 */
	HWND lpClassName;
	lpClassName = ::FindWindow(TEXT("HHTaskBar"), NULL);
	::ShowWindow(lpClassName, SW_SHOW);
}

VOID CGPRSDlg::CommThread(LPVOID pParam)
{
	CGPRSDlg* pDlg = (CGPRSDlg*)(pParam);
	CHAR ReadBuffer[512] = {0};
	DWORD dwCommModemStatus;
	DWORD dwBytesRead = 0;
	INT nLength = 0;
	CString msg;
	SetCommMask(pDlg->m_gprs.m_serial.m_hFile, EV_RXCHAR);

	while(INVALID_HANDLE_VALUE != pDlg->m_gprs.m_serial.m_hFile)
	{		
		WaitCommEvent(pDlg->m_gprs.m_serial.m_hFile, &dwCommModemStatus, NULL);
		SetCommMask(pDlg->m_gprs.m_serial.m_hFile, EV_RXCHAR);
		if (dwCommModemStatus & EV_RXCHAR)
		{
			Sleep(20);
			nLength = pDlg->m_gprs.m_serial.ReadComm((unsigned char *)ReadBuffer,512);	
			msg = CString(ReadBuffer);
	//		pDlg->ShowMsg(msg);
			
			if (strstr(ReadBuffer, "NO"))
			{
				pDlg->m_gprs.CallState = FALSE;
				pDlg->SetDlgItemText(IDC_BUTTON_CALL, _T("拨号"));
			}

			if (strstr(ReadBuffer, "RING"))
			{
				if (!pDlg->m_gprs.CallState)
				{
					pDlg->SetDlgItemText(IDC_EDIT_STATE, _T("有来电!"));
					if(AfxMessageBox(_T("有来电!按\"是\"接听,按\"否\"拒接!"), MB_YESNO) == IDYES)
					{
						pDlg->m_gprs.m_serial.WriteComm((UCHAR*)"ata\r", 4);
						pDlg->SetDlgItemText(IDC_EDIT_STATE, _T("通话中!"));
						pDlg->SetDlgItemText(IDC_BUTTON_CALL, _T("挂机"));
						pDlg->m_gprs.CallState = TRUE;
						memset(&ReadBuffer, 0, sizeof(ReadBuffer));
					}else{
						pDlg->m_gprs.m_serial.WriteComm((UCHAR*)"ath\r", 4);
						pDlg->m_gprs.m_serial.ReadComm((unsigned char *)ReadBuffer,512);
						memset(&ReadBuffer, 0, sizeof(ReadBuffer));
						pDlg->SetDlgItemText(IDC_EDIT_STATE, _T("通话结束!"));
						pDlg->m_gprs.CallState = FALSE;
					}
				}
			
			}

			if ((strstr(ReadBuffer, "MTI:")) && (strstr(ReadBuffer, "SM")))
			{	
				pDlg->SetDlgItemText(IDC_EDIT_STATE, _T("有新短消息!"));
				if(AfxMessageBox(_T("有新短消息!按\"是\"读取,按\"否\"返回!"), MB_YESNO) == IDYES)
				{
					/* 读取新短消息存放序号 */
					CString Index = "";
					INT count = 0;
					for (INT i = 0; i < msg.GetLength(); i++)
					{
						if (',' == msg[i])
						{
							Index = msg.Mid(i + 1);
							break;
						}		
					}

					/* 按序号读取短消息 */
					UCHAR sendbuf[256];
					CString cmd = "AT+CMGR=" + Index + "\r"; 

					for (INT i1 = 0; i1 < cmd.GetLength(); i1++)
					{
						sendbuf[i1] = cmd[i1]; 
					}

					pDlg->SetDlgItemText(IDC_EDIT_STATE, _T("读取短消息!"));
					pDlg->m_gprs.m_serial.WriteComm(sendbuf, cmd.GetLength());	
					Sleep(1000);
					pDlg->m_gprs.m_serial.ReadComm((UCHAR *)ReadBuffer,512);

					/* 从ReadBuffer提取pdu串 */
					CHAR pdu[512] = {0};
					DWORD nLength = 0;
					pDlg->m_gprs.gprsGetPdu(ReadBuffer, pdu); 

					/* pdu串转换成Unicode字符填充SmParam */
					SM_PARAM SmParam;
					memset(&SmParam, 0, sizeof(SM_PARAM));
					pDlg->m_gprs.gprsDecodePdu(pdu, &SmParam);
					CString strFrom = CString(SmParam.TPA);
					CString strTime = CString(SmParam.TP_SCTS);
					CString strContent = CString(SmParam.TP_UD);

					/* 格式化对方号码输出串 */
					CString From = "";
					From.Format(_T("%s%s"), _T("来自 :"), strFrom);

					/* 格式化短消息发送时间输出串 */
					CString Time = "";
					Time.Format(_T("%s%s%s%s%s%s%s%s%s%s%s%s%s"), _T("时间 :"), _T("20"),
						strTime.Left(2), _T("-"), strTime.Mid(2,2), _T("-"), strTime.Mid(4,2),
						_T(" "), strTime.Mid(6,2), _T(":"), strTime.Mid(8,2), _T(":"), strTime.Mid(10,2));

					/* 格式化短消息内容输出串 */
					CString Content = "";
					Content.Format(_T("%s%s"), _T("内容 :"), strContent);

					/* 输出格式化的短消息 */
					CString Msg = "";
					Msg.Format(_T("%s%s%s%s%s"), From, _T("\r\n"), Time, _T("\r\n"), Content);
					pDlg->ShowMsg(Msg);
					pDlg->SetDlgItemText(IDC_EDIT_STATE, _T("读取短消息成功!"));
					Sleep(500);			
				}else{
					pDlg->m_gprs.m_serial.ReadComm((UCHAR *)ReadBuffer,512);
					memset(&ReadBuffer, 0, sizeof(ReadBuffer));
					pDlg->SetDlgItemText(IDC_EDIT_STATE, _T("有新短消息末读!"));
				}
			}
		}	
	}
}

VOID CGPRSDlg::ShowMsg(CString str)
{
	CEdit *pOutput = (CEdit *)GetDlgItem(IDC_EDIT_MSG);
	pOutput->SetWindowText(str);
}

void CGPRSDlg::OnButtonReset() 
{
	// TODO: Add your control notification handler code here
	SetDlgItemText(IDC_EDIT_MSG, _T(""));
}

void CGPRSDlg::OnButtonMsgback() 
{
	// TODO: Add your control notification handler code here
	CString strEdit;
	GetDlgItemText(IDC_EDIT_MSG, strEdit);
	strEdit = strEdit.Left(strEdit.GetLength() - 1);
	SetDlgItemText(IDC_EDIT_MSG, strEdit);
}

void CGPRSDlg::OnSetfocusEditNum() 
{
	// TODO: Add your control notification handler code here
	m_iFocus = EDIT_NUM_FOCUS;
}

⌨️ 快捷键说明

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