?? gprsdlg.cpp
字號:
/*! \file GPRSDlg.cpp
\brief 應(yīng)用程序界面實現(xiàn)文件
\author 陳華毅
\version 1.0
用戶通過這個窗口實現(xiàn)了與應(yīng)用程序的交互,通過這個界面控制和使用GPRS模塊。
\defgroup GPRSDLG_IMPLEMENT CGPRSDlg類實現(xiàn)說明
*/
#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 設(shè)為整個屏幕 */
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("通話結(jié)束!"));
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;
}
/* 檢查短消息內(nèi)容是否空,或者超長 */
if (m_strMsg.GetLength() < 1)
{
AfxMessageBox(_T("請輸入消息內(nèi)容!"));
pContentWnd->SetFocus();
pContentWnd->SetSel(-1, 0);
return;
}else if(m_strMsg.GetLength() > 70){ /* 我們決定全部用UCS2編碼,最大70個字符(半角/全角) */
AfxMessageBox(_T("消息內(nèi)容太長,無法發(fā)送!"));
pContentWnd->SetFocus();
pContentWnd->SetSel(-1, 0);
return;
}else{}
SuspendThread(m_hRevThread); /* 暫停讀取線程 */
EnterCriticalSection(&m_csSend); /* 進入發(fā)送短消息關(guān)鍵代碼區(qū) */
NOTIFY(_T("正在發(fā)送!"));
if (m_gprs.gprsSendMessage(m_strNum, m_strMsg)) /* 發(fā)送短消息 */
{
NOTIFY(_T("發(fā)送成功!"));
}else{
NOTIFY(_T("發(fā)送失敗!"));
}
LeaveCriticalSection(&m_csSend); /* 離開發(fā)送短消息關(guān)鍵代碼區(qū) */
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("通話結(jié)束!"));
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串轉(zhuǎn)換成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);
/* 格式化短消息發(fā)送時間輸出串 */
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));
/* 格式化短消息內(nèi)容輸出串 */
CString Content = "";
Content.Format(_T("%s%s"), _T("內(nèi)容 :"), 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 + -