📄 grpstestdlg.cpp
字号:
// GrpsTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GrpsTest.h"
#include "GrpsTestDlg.h"
#include "Comm.h"
#include "Sms.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGrpsTestDlg dialog
CGrpsTestDlg::CGrpsTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGrpsTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGrpsTestDlg)
m_strPhoneNumber = _T("");
m_strSendMessage = _T("");
m_nState = 1;
m_strPortNumber = _T("");
m_strSendData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGrpsTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGrpsTestDlg)
DDX_Control(pDX, IDC_LIST_SOCKETHANDLE, m_listSocketHandle);
DDX_Control(pDX, IDC_LIST_RECEDATA, m_listReceData);
DDX_Control(pDX, IDC_IPADDRESS1, m_ipAddress);
DDX_Control(pDX, IDC_LIST1, m_listReceMess);
DDX_Text(pDX, IDC_EDIT_PHONENUMBER, m_strPhoneNumber);
DDX_Text(pDX, IDC_EDIT_SENDMESSAGE, m_strSendMessage);
DDX_Radio(pDX, IDC_RADIO1, m_nState);
DDX_Text(pDX, IDC_EDIT_PORTNUMBER, m_strPortNumber);
DDX_Text(pDX, IDC_EDIT_SENDDATA, m_strSendData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGrpsTestDlg, CDialog)
//{{AFX_MSG_MAP(CGrpsTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_BUTTON_SENDMESSAGE, OnButtonSendmessage)
ON_WM_TIMER()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
ON_BN_CLICKED(IDC_BUTTON_SENDDATA, OnButtonSenddata)
ON_NOTIFY(IPN_FIELDCHANGED, IDC_IPADDRESS1, OnFieldchangedIpaddress1)
ON_EN_CHANGE(IDC_EDIT_PORTNUMBER, OnChangeEditPortnumber)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGrpsTestDlg message handlers
BOOL CGrpsTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
nState = m_nState;
m_ntime = 0;
m_strPhoneNumber = "13936256935";
m_strSendMessage = "I love you!";
m_ipAddress.SetAddress(202,118,224,100);
m_strPortNumber = "6800";
// m_listSocketHandle.AddString("202,118,224,100 6800 000");
UpdateData(FALSE);
m_listReceMess.InsertColumn(0,"序号",LVCFMT_CENTER,40);
m_listReceMess.InsertColumn(1,"手机号",LVCFMT_CENTER,80);
m_listReceMess.InsertColumn(2,"接收时间",LVCFMT_CENTER,150);
m_listReceMess.InsertColumn(3,"消息内容",LVCFMT_CENTER,270);
SetTimer(1, 1000, NULL); // 定时读取短消息
threadChangeMode = AfxBeginThread(ChangeGprsMode,this,THREAD_PRIORITY_NORMAL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CGrpsTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CGrpsTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGrpsTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CGrpsTestDlg::OnRadio1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
nState = m_nState;
if(SetMessageType(nState)==FALSE)
{
AfxMessageBox("短消息发送方式设置失败!");
}
}
void CGrpsTestDlg::OnRadio2()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
nState = m_nState;
if(SetMessageType(nState)==FALSE)
{
AfxMessageBox("短消息发送方式设置失败!");
}
}
void CGrpsTestDlg::OnButtonSendmessage()
{
CString strNumber;
UpdateData(TRUE);
if(m_strPhoneNumber.GetLength()!=11)
{
AfxMessageBox("请输入正确手机号!");
return;
}
CString strUnicode;
WCHAR wchar[1024];
int nCount = ::MultiByteToWideChar(CP_ACP, 0, m_strSendMessage, -1, wchar, 1024);
if(nCount <= 1)
{
AfxMessageBox("请输入消息内容!");
GetDlgItem(IDC_EDIT_SENDMESSAGE)->SetFocus();
return;
}
else if(nCount > 70) // 我们决定全部用UCS2编码,最大70个字符(半角/全角)
{
AfxMessageBox("消息内容太长,无法发送!");
GetDlgItem(IDC_EDIT_SENDMESSAGE)->SetFocus();
return;
}
if(AfxMessageBox("确定发送吗?", MB_YESNO) == IDNO) return;
if (nState==0) // 按照Text方式发送短消息
{
if (SendTextMessage(m_strPhoneNumber,m_strSendMessage)==FALSE)
{
AfxMessageBox("短消息发送失败!");
}
}
else if(nState=1) // 按照PDU格式发送消息
{
CString strSmsc,strNumber;
SM_PARAM SmParam;
memset(&SmParam, 0, sizeof(SM_PARAM));
strSmsc = "8613800451500";
strNumber = m_strPhoneNumber;
// 在号码前加"86"
if(strNumber.Left(2) != "86") strNumber = "86" + strNumber;
strcpy(SmParam.SCA, strSmsc);
strcpy(SmParam.TPA, strNumber);
strcpy(SmParam.TP_UD,m_strSendMessage);
SmParam.TP_PID = 0;
SmParam.TP_DCS = GSM_UCS2; // 默认采用UCS2编码
theApp.m_pSmsTraffic->PutSendMessage(&SmParam);
}
}
void CGrpsTestDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent == 1)
{
SM_PARAM SmParam;
SOCKETDATA_PARAM SocketParam;
CString strTime;
CString strNumber;
CString strContent;
// 取接收到的网络数据
if (theApp.m_pSocketTraffic->GetRecvData(&SocketParam))
{
strContent = SocketParam.SEND_DATA;
if (strContent == "CONNECTSUCCESS") // 已经成功建立一个socket连接
{
AfxMessageBox("建立socket连接成功");
strContent = SocketParam.IP_ADDRESS;
strContent += " ";
strContent += SocketParam.PORT_NUMBER;
strContent += " ";
strContent += SocketParam.SOCKET_HANDLE;
m_listSocketHandle.AddString(strContent);
}
else if (strContent=="CONNFAIL") AfxMessageBox("建立socket连接失败");
else if (strContent=="CLOSESUCCESS") AfxMessageBox("关闭socket成功");
else if (strContent=="CLOSEFAIL") AfxMessageBox("关闭socket失败");
else if (strContent=="SENDSUCCESS") AfxMessageBox("发送数据成功");
else if (strContent=="SENDFAIL") AfxMessageBox("发送数据失败");
else
{
CString str;
str.Format("%s:%s",SocketParam.SOCKET_HANDLE,strContent);
m_listReceData.AddString(str);
}
}
// 取接收到的短消息
if(theApp.m_pSmsTraffic->GetRecvMessage(&SmParam))
{
// 取短消息信息
AfxMessageBox("你有新短消息,请注意查收!");
strNumber = SmParam.TPA;
strContent = SmParam.TP_UD;
strTime = "20" + CString(&SmParam.TP_SCTS[0],2) // 读取短消息时间
+ "-" + CString(&SmParam.TP_SCTS[2],2)
+ "-" + CString(&SmParam.TP_SCTS[4],2)
+ " " + CString(&SmParam.TP_SCTS[6],2)
+ ":" + CString(&SmParam.TP_SCTS[8],2)
+ ":" + CString(&SmParam.TP_SCTS[10],2);
// 去掉号码前的"86"
if(strNumber.Left(2) == "86") strNumber = strNumber.Mid(2);
// 最多保留200条
int nItemCount = m_listReceMess.GetItemCount();
if(nItemCount >= 200)
{
m_listReceMess.DeleteItem(0);
nItemCount--;
}
// 插入新消息
CString str;
str.Format("%d",nItemCount+1);
m_listReceMess.InsertItem(nItemCount,str);
m_listReceMess.SetItemText(nItemCount, 1,strNumber);
m_listReceMess.SetItemText(nItemCount, 2, strTime);
m_listReceMess.SetItemText(nItemCount, 3, strContent);
m_listReceMess.EnsureVisible(nItemCount, FALSE);
}
}
CDialog::OnTimer(nIDEvent);
}
void CGrpsTestDlg::OnDestroy()
{
CDialog::OnDestroy();
if (threadChangeMode)
{
delete threadChangeMode;
}
KillTimer(1);
// TODO: Add your message handler code here
}
void CGrpsTestDlg::OnButtonConnect()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_ipAddress.IsBlank()||m_strPortNumber.IsEmpty())
{
AfxMessageBox("请输入需要连接的IP地址和端口号");
return;
}
SOCKET_PARAM SocketParam;
memset(&SocketParam, 0, sizeof(SOCKET_PARAM));
BYTE address[4];
CString str;
m_ipAddress.GetAddress(address[0],address[1],address[2],address[3]);
str.Format("%d.%d.%d.%d",address[0],address[1],address[2],address[3]);
strcpy(SocketParam.IP_ADDRESS,str);
strcpy(SocketParam.PORT_NUMBER,m_strPortNumber);
theApp.m_pSocketTraffic->PutConnectSocket(&SocketParam);
}
void CGrpsTestDlg::OnButtonClose()
{
// TODO: Add your control notification handler code here
char buffer[30];
SOCKET_PARAM SocketParam;
int index = m_listSocketHandle.GetCurSel();
if (index<0)
{
AfxMessageBox("请选中要关闭的socket连接");
return;
}
m_listSocketHandle.GetText(index,buffer);
sscanf(buffer,"%s%s%s",SocketParam.IP_ADDRESS,SocketParam.PORT_NUMBER,SocketParam.SOCKET_HANDLE);
m_listSocketHandle.DeleteString(index);
theApp.m_pSocketTraffic->PutCloseSocket(&SocketParam);
}
void CGrpsTestDlg::OnButtonSenddata()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
SOCKETDATA_PARAM SocketParam;
int index = m_listSocketHandle.GetCurSel();
if (index<0)
{
AfxMessageBox("请选中要发送数据的socket连接");
return;
}
char buffer[30];
m_listSocketHandle.GetText(index,buffer);
sscanf(buffer,"%s%s%s",SocketParam.IP_ADDRESS,SocketParam.PORT_NUMBER,SocketParam.SOCKET_HANDLE);
strcpy(SocketParam.SEND_DATA,m_strSendData);
theApp.m_pSocketTraffic->PutSendData(&SocketParam);
}
UINT CGrpsTestDlg::ChangeGprsMode(LPVOID lParam)
{
CGrpsTestDlg* p = (CGrpsTestDlg *) lParam;
CTime tmOrg,tmNow;
CString str;
BYTE address[4];
SOCKET_PARAM SocketParam;
char buffer[30];
tmOrg = CTime::GetCurrentTime();
while (1)
{
tmNow = CTime::GetCurrentTime();
if ((tmNow-tmOrg)>60&&theApp.m_nRunThread==RUN_SOCKETTHREAD)
{
theApp.m_nRunThread = RUN_MESSAGETHREAD;
for(int i=0;i<p->m_listSocketHandle.GetCount();i++) // 关闭当前已经建立的socket连接
{
p->m_listSocketHandle.GetText(i,buffer);
sscanf(buffer,"%s%s%s",SocketParam.IP_ADDRESS,SocketParam.PORT_NUMBER,SocketParam.SOCKET_HANDLE);
theApp.m_pSocketTraffic->PutCloseSocket(&SocketParam);
}
p->m_listSocketHandle.ResetContent();
theApp.m_pSocketTraffic->SetThreadStatus(FALSE); // 挂起该线程
while (!(theApp.m_pSocketTraffic->GetThreadStatus()));// 等待线程已经挂起
ChangeCommandType(FALSE); // 切换到AT指令集
theApp.m_pSmsTraffic->SetThreadStatus(TRUE); // 运行短消息线程
tmOrg = CTime::GetCurrentTime();
}
else if ((tmNow-tmOrg)>15&&theApp.m_nRunThread==RUN_MESSAGETHREAD)
{
theApp.m_nRunThread = RUN_SOCKETTHREAD;
ChangeCommandType(TRUE); // 切换到AT+I指令集
theApp.m_pSmsTraffic->SetThreadStatus(FALSE);
while((!theApp.m_pSmsTraffic->GetThreadStatus())); // 等待挂起该线程
theApp.m_pSocketTraffic->SetThreadStatus(TRUE); // 运行网络传数县城
p->m_ipAddress.GetAddress(address[0],address[1],address[2],address[3]);
str.Format("%d.%d.%d.%d",address[0],address[1],address[2],address[3]);
strcpy(SocketParam.IP_ADDRESS,str);
strcpy(SocketParam.PORT_NUMBER,p->m_strPortNumber);
theApp.m_pSocketTraffic->PutConnectSocket(&SocketParam);
tmOrg = CTime::GetCurrentTime();
}
}
return 9999;
}
void CGrpsTestDlg::OnFieldchangedIpaddress1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
*pResult = 0;
}
void CGrpsTestDlg::OnChangeEditPortnumber()
{
UpdateData(TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -