📄 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 + -