?? monitordlg.cpp
字號:
// MonitorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GSMTest.h"
#include "MonitorDlg.h"
#include "PDU.h"//pdu編解碼類
#include "transjs.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMonitorDlg dialog
CMonitorDlg::CMonitorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMonitorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMonitorDlg)
m_sContent = _T("");
m_sCommand = _T("");
m_sPhoneNum = _T("");
m_sStatus1 = _T("");
m_sStatus2 = _T("");
//}}AFX_DATA_INIT
}
void CMonitorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMonitorDlg)
DDX_Control(pDX, IDC_GSMCOMM, m_ctrGsm);
DDX_Text(pDX, IDC_CONTENT, m_sContent);
DDV_MaxChars(pDX, m_sContent, 120);
DDX_Text(pDX, IDC_COMMAND, m_sCommand);
DDX_Text(pDX, IDC_PHONE, m_sPhoneNum);
DDX_Text(pDX, IDC_STATUS1, m_sStatus1);
DDX_Text(pDX, IDC_STATUS2, m_sStatus2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMonitorDlg, CDialog)
//{{AFX_MSG_MAP(CMonitorDlg)
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_MANAGE, OnManage)
ON_BN_CLICKED(IDC_EXE, OnExe)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMonitorDlg message handlers
void CMonitorDlg::OnStart()
{
try
{
if(!InitCom())
AfxMessageBox("操作失敗!");
m_sStatus2="";
m_ctrGsm.SetOutput(COleVariant("AT\r"));
Sleep(1000);
OnOnCommGsmcomm();
m_ctrGsm.SetOutput(COleVariant("AT+CNMI=2,2,0,1,1\r"));
m_ctrGsm.SetOutput(COleVariant("AT+CMGF=1\r"));
//得到SIM卡基本信息
//短信中心
//卡號
m_sStatus2="";
if(m_sStatus1.Find("OK")>=0)
{m_sStatus1="連接成功";UpdateData(FALSE);}
else m_sStatus1="連接失敗";
}
catch (...) //...代表捕捉所有異常
{
//AfxMessageBox(ex.what());
AfxMessageBox("初始化失敗!");
}
}
bool CMonitorDlg::InitCom()
{
if (m_ctrGsm.GetPortOpen())
m_ctrGsm.SetPortOpen(false);//如果是打開的,則先關閉串口
m_ctrGsm.SetCommPort(1); //選擇COM1
m_ctrGsm.SetInBufferSize(1024);//設置輸入緩沖區大小
m_ctrGsm.SetOutBufferSize(1024);//設置輸出緩沖區大小
m_ctrGsm.SetInputLen(0);//設置當前接收區數據長度為0,表示全部讀取
m_ctrGsm.SetInputMode(1);//以二進制方式讀寫數據
m_ctrGsm.SetRThreshold(1);//當接收緩沖區有1個及1個以上字符時,將引發接收數據的OnComm事件
m_ctrGsm.SetSettings("9600,n,8,1");//波特率9600,無校驗位,8個數據位,1個停止位
if(!m_ctrGsm.GetPortOpen())
m_ctrGsm.SetPortOpen(TRUE);
else
m_ctrGsm.SetInBufferCount(0);
// SetTimer(1,10,NULL);
// InBuffer.bstrVal=new unsigned short[MESSAGELENGTH];
// OutBuffer.bstrVal=new unsigned short[MESSAGELENGTH];
// OutBuffer.vt=VT_BSTR;
//m_ctrGsm.SetCommPort(1);//設置為com1
// m_ctrGsm.SetOutput(COleVariant("AT\r"));
return true;
}
void CMonitorDlg::OnSend()
{
//發送短信
UpdateData(true);
if(!m_ctrGsm.GetPortOpen())
{
AfxMessageBox("端口未打開!");
return;
}
if(m_sPhoneNum.IsEmpty())
{
AfxMessageBox("請輸入手機號碼!");
return;
}
if(m_sContent.IsEmpty())
{
AfxMessageBox("請輸入短信內容!");
return;
}
string strNum=m_sPhoneNum;
char* strContent="";
strContent=m_sContent.GetBuffer(m_sContent.GetLength());
m_sStatus2="";
PDU p;
string x=p.smsEncodedsms("8613800230500",strNum,strContent);
CString str;
str.Format("AT+CMGS=%d\r",p.nLength);
m_ctrGsm.SetOutput(COleVariant(str));
Sleep(1000);
OnOnCommGsmcomm();
if(m_sStatus2.Find("> ")>=0)
{
str.Format("%s%c",x.c_str(),0x1a);
str.MakeUpper();
//m_sStatus2="";
m_ctrGsm.SetOutput(COleVariant(str));
}
//AfxMessageBox("發送失敗!"); // TODO: Add your control notification handler code here
}
void CMonitorDlg::OnManage()
{
CTransJS dlg;
dlg.DoModal();
CDialog::OnCancel();
//OnCancel();
}
void CMonitorDlg::OnExe()
{
if(!m_ctrGsm.GetPortOpen())
{
AfxMessageBox("端口未打開!");
return;
}
UpdateData(true);
if(m_sCommand=="")
{
AfxMessageBox("命令不能為空!");
return;
}
m_sStatus2="";
m_ctrGsm.SetOutput(COleVariant(m_sCommand+"\r"));
//GetDlgItemText(IDC_STATUS2,m_sStatus2);
//UpdateData(false);
//AfxMessageBox(m_sStatus2); // TODO: Add your control notification handler code here
}
BEGIN_EVENTSINK_MAP(CMonitorDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMonitorDlg)
ON_EVENT(CMonitorDlg, IDC_GSMCOMM, 1 /* OnComm */, OnOnCommGsmcomm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CMonitorDlg::OnOnCommGsmcomm()
{
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048];
CString strtemp;
switch(m_ctrGsm.GetCommEvent())
{
case 1: //comEvSend發送數據
break;
case 2: //comEvReceive讀取數據
variant_inp=m_ctrGsm.GetInput();//讀緩沖區
safearray_inp=variant_inp;//VARIANT型變量轉換為COleSafeArray型變量
len=safearray_inp.GetOneDimSize();//得到有效數據長度
//接收數據
for(k=0;k<len;k++)
{
safearray_inp.GetElement(&k,rxdata+k);//轉換為BYTE型數組
BYTE bt=*(char *)(rxdata+k);//字符型
strtemp.Format("%c",bt);//將字符送入臨時變量strtemp存放
m_sStatus2 += strtemp;
}
if(m_sStatus2.Find("OK")>=0)
m_sStatus1="成功";
UpdateData(FALSE);//回顯
break;
default:
m_ctrGsm.SetOutBufferCount(0);
break;
}
}
void CMonitorDlg::OnCancel()
{
//if(AfxMessageBox("確實要退出監控嗎?",MB_OKCANCEL)==IDOK)
//{
CDialog::OnCancel();
//}
}
BOOL CMonitorDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//ShowWindow(SW_MAXIMIZE);
int nWidth = GetSystemMetrics(SM_CXSCREEN);
int nHeight = GetSystemMetrics(SM_CYSCREEN);
SetWindowPos(NULL,nWidth/2,nHeight/2,60,60,SWP_NOZORDER|SWP_NOMOVE);
SetTimer(1,200,NULL);
ShowWindow(SW_MAXIMIZE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CMonitorDlg::OnTimer(UINT nIDEvent)
{
KillTimer(1);
OnStart();
// OnManage();
CDialog::OnTimer(nIDEvent);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -