📄 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
threadChangeMode = NULL;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGrpsTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGrpsTestDlg)
DDX_Control(pDX, IDC_IPADDRESSGPRS, m_gprsAddress);
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
int a[4];
char ans[80];
Sleep(500);
for(int i=0;i<3;i++) // 建立g20和GPRS网络连接,接收一个IP地址
{
WriteComm("AT+MIPCALL=1,cmnet\r\n",20); // 建立连接
Sleep(1000);
ReadComm(ans,50);
if (strstr(ans, "OK") == NULL) // 返回OK,说明设置成功
{
if(i==2)
{
AfxMessageBox("建立连接错误");
return FALSE;
}
Sleep(500);
}
else
{
break;
}
}
theApp.m_pSocketTraffic->m_bBeginFlag = TRUE;
char *ip;
ip = strstr(ans,"MIPCALL");
ip += 9;
sscanf(ip,"%d.%d.%d.%d",&a[0],&a[1],&a[2],&a[3]);
nState = m_nState;
m_ntime = 0;
m_strPhoneNumber = "13936256935";
m_strSendMessage = "I love you!";
m_gprsAddress.SetAddress(a[0],a[1],a[2],a[3]);
m_ipAddress.SetAddress(220,192,219,170);
m_strPortNumber = "3000";
// 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); // 定时读取短消息
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_pSocketTraffic->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.SOCKET_ID;
strContent += " ";
m_listSocketHandle.AddString(strContent);
}
else if (strContent=="CONNFAIL") AfxMessageBox("建立socket连接失败");
else if (strContent=="CLOSESUCCESS")
{
AfxMessageBox("关闭socket成功");
m_listSocketHandle.DeleteString(0);
}
else if (strContent=="CLOSEFAIL") AfxMessageBox("关闭socket失败");
else if (strContent=="SENDSUCCESS") AfxMessageBox("发送数据成功");
else if (strContent=="SENDFAIL") AfxMessageBox("发送数据失败");
else
{
}
}
// 取接收到的短消息
if(theApp.m_pSocketTraffic->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;
CString sport = "2000"; // 源端口默认设置为3000
CString socketId = "1";
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.DPORT_NUMBER,m_strPortNumber);
strcpy(SocketParam.SPORT_NUMBER,sport);
strcpy(SocketParam.SOCKET_ID,socketId);
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",SocketParam.IP_ADDRESS,SocketParam.SOCKET_ID);
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%",SocketParam.IP_ADDRESS,SocketParam.SOCKET_ID);
CString str = m_strSendData;
memset(buffer,0,sizeof(buffer));
Char2Ascii(str.GetBuffer(MAX_PATH),buffer);
strcpy(SocketParam.SEND_DATA,buffer);
theApp.m_pSocketTraffic->PutSendData(&SocketParam);
}
void CGrpsTestDlg::OnFieldchangedIpaddress1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
*pResult = 0;
}
void CGrpsTestDlg::OnChangeEditPortnumber()
{
UpdateData(TRUE);
}
void CGrpsTestDlg::OnOK()
{
// TODO: Add extra validation here
char ans[60];
theApp.m_pSocketTraffic->m_bBeginFlag = FALSE;
for(int i=0;i<3;i++) // 建立g20和GPRS网络连接,接收一个IP地址
{
WriteComm("AT+MIPCALL=0\r",13); // 断开连接
Sleep(1000);
ReadComm(ans,50);
if (strstr(ans, "OK") == NULL) // 返回OK,说明设置成功
{
if(i==2)
{
AfxMessageBox("关闭连接错误");
return;
}
Sleep(500);
}
else
{
break;
}
}
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -