⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smsexampledlg.cpp

📁 VC6.0控制GSM模块实现收发短信
💻 CPP
字号:
// SmsExampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SmsExample.h"
#include "SmsExampleDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CSmsExampleDlg dialog

CSmsExampleDlg::CSmsExampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSmsExampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSmsExampleDlg)
	m_strNumber = _T("");
	m_strContent = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSmsExampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSmsExampleDlg)
	DDX_Text(pDX, IDC_DESNUMBER, m_strNumber);
	DDX_Text(pDX, IDC_CONTENT, m_strContent);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSmsExampleDlg, CDialog)
	//{{AFX_MSG_MAP(CSmsExampleDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPENBUTTON, OnOpenButton)
	ON_BN_CLICKED(IDC_CLOSEBUTTON, OnCloseButton)
	ON_BN_CLICKED(IDC_CLEARBUTTON, OnClearButton)
	ON_BN_CLICKED(IDC_SENDBUTTON, OnSendButton)
	ON_BN_CLICKED(IDC_RECEIVEBUTTON, OnReceiveButton)
	ON_BN_CLICKED(IDC_DELALLBUTTON, OnDelAllButton)
	ON_BN_CLICKED(IDC_UNREADBUTTON, OnUnreadButton)
	ON_BN_CLICKED(IDC_GETCENTER, OnGetCenter)
	ON_BN_CLICKED(IDC_SETCENTER, OnSetCenter)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_EN_CHANGE(IDC_DESNUMBER, OnChangeDesnumber)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSmsExampleDlg message handlers

BOOL CSmsExampleDlg::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); 
	//Identifies a copy of the Control menu if bRevert is FALSE  
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
//Reads a Windows string resource, identified by nID, into an existing CString object
    	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
	//初始化COMBOX串口框
	CComboBox* cbCom=(CComboBox*)GetDlgItem(IDC_COMBO1);
/*	for(int i=1;i<10;i++)
	{
		CString temp;
		temp.Format("%d",i); 
		cbCom->AddString(temp); 
	}
	cbCom->SetWindowText("1"); */
	cbCom->SetCurSel(0);

	CButton* pCloseButton=(CButton*)GetDlgItem(IDC_CLOSEBUTTON);
	CButton* pSendButton=(CButton*)GetDlgItem(IDC_SENDBUTTON);
	CButton* pReceiveButton=(CButton*)GetDlgItem(IDC_RECEIVEBUTTON);
	CButton* pDelAllButton=(CButton*)GetDlgItem(IDC_DELALLBUTTON);
	CButton* pUnreadButton=(CButton*)GetDlgItem(IDC_UNREADBUTTON);
	CButton* pSetButton=(CButton*)GetDlgItem(IDC_SETCENTER);
	CButton* pGetButton=(CButton*)GetDlgItem(IDC_GETCENTER);
	pCloseButton->EnableWindow(FALSE);
	pSendButton->EnableWindow(FALSE);
	pReceiveButton->EnableWindow(FALSE);
	pDelAllButton->EnableWindow(FALSE);
	pUnreadButton->EnableWindow(FALSE);
	pSetButton->EnableWindow(FALSE);
	pGetButton->EnableWindow(FALSE);
	
	//初始化短消息接收列表框
	CListCtrl* pListCtrl=(CListCtrl*)GetDlgItem(IDC_LIST1);
	DWORD dwStyle=GetWindowLong(pListCtrl->m_hWnd,GWL_STYLE);
	SetWindowLong(pListCtrl->m_hWnd,GWL_STYLE,dwStyle|LVS_REPORT);
	dwStyle=pListCtrl->GetExtendedStyle();
	pListCtrl->SetExtendedStyle(dwStyle|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	
	TCHAR Title[3][16]={_T("发送方号码"),_T("发送时间"),_T("发送内容")};
	LVCOLUMN lvColumn;
	CRect rect;
	pListCtrl->GetWindowRect(&rect);
	for(int j=0;j<3;j++)
	{
		lvColumn.mask=LVCF_FMT|LVCF_SUBITEM|LVCF_TEXT|LVCF_WIDTH|LVCF_ORDER;
		lvColumn.fmt=LVCFMT_LEFT;
		lvColumn.pszText=Title[j];
		lvColumn.iSubItem=j;
		lvColumn.iOrder=j;
		lvColumn.cx=rect.Width()/3;
		pListCtrl->InsertColumn(j,&lvColumn); 

	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSmsExampleDlg::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 CSmsExampleDlg::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 CSmsExampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSmsExampleDlg::OnOpenButton() 
{
	// TODO: Add your control notification handler code here
	CComboBox* pCB=(CComboBox*)GetDlgItem(IDC_COMBO1);//	有下来菜单选择的那一个
	CButton* pOpenButton=(CButton*)GetDlgItem(IDC_OPENBUTTON);//打开串口的那个按钮
	CButton* pCloseButton=(CButton*)GetDlgItem(IDC_CLOSEBUTTON);//关闭串口的那个按钮
	CButton* pSendButton=(CButton*)GetDlgItem(IDC_SENDBUTTON);//发送消息的按钮
	CButton* pReceiveButton=(CButton*)GetDlgItem(IDC_RECEIVEBUTTON);//显示已读消息
	CButton* pDelAllButton=(CButton*)GetDlgItem(IDC_DELALLBUTTON);//删除所有短信
	CButton* pUnreadButton=(CButton*)GetDlgItem(IDC_UNREADBUTTON);//显示未读短信
	CButton* pSetButton=(CButton*)GetDlgItem(IDC_SETCENTER);//设置短信中心号码
	CButton* pGetButton=(CButton*)GetDlgItem(IDC_GETCENTER);//获取短信中心号码
	
	CString strCom;
//	CString temp="";
//	pCB->GetWindowText(temp);
	//pCB->GetCurSel();
//	strCom+=temp;
	pCB->GetLBText(pCB->GetCurSel(),strCom);//得到下标对应的字符串,第一个参数为给定的下标,第二个参数为接收字符字符串的指针
	if(OpenComm(strCom,CBR_9600,NOPARITY,8, ONESTOPBIT))
	{
		
//		if(!CheckNo())
//		{
//			MessageBox("非本公司产品,程序将终止运行","序列号验证",MB_OK);
//			CloseComm();
//			return;
//		}
		SetSendModule(0);//
		pOpenButton->EnableWindow(FALSE); 
		pCloseButton->EnableWindow(TRUE);
		pSendButton->EnableWindow(TRUE);
 		pReceiveButton->EnableWindow(TRUE);
		pDelAllButton->EnableWindow(TRUE);
		pUnreadButton->EnableWindow(TRUE);
		pSetButton->EnableWindow(TRUE);
		pGetButton->EnableWindow(TRUE);
	}
}


void CSmsExampleDlg::OnCloseButton() 
{
	// TODO: Add your control notification handler code here
	CButton* pOpenButton=(CButton*)GetDlgItem(IDC_OPENBUTTON);
	CButton* pCloseButton=(CButton*)GetDlgItem(IDC_CLOSEBUTTON);
	CButton* pSendButton=(CButton*)GetDlgItem(IDC_SENDBUTTON);
	CButton* pReceiveButton=(CButton*)GetDlgItem(IDC_RECEIVEBUTTON);
	CButton* pDelAllButton=(CButton*)GetDlgItem(IDC_DELALLBUTTON);
	CButton* pUnreadButton=(CButton*)GetDlgItem(IDC_UNREADBUTTON);
	CButton* pSetButton=(CButton*)GetDlgItem(IDC_SETCENTER);
	CButton* pGetButton=(CButton*)GetDlgItem(IDC_GETCENTER);
		
	if(CloseComm())
	{
		pOpenButton->EnableWindow(TRUE); 
		pCloseButton->EnableWindow(FALSE);
		pSendButton->EnableWindow(FALSE);
 		pReceiveButton->EnableWindow(FALSE);
		pDelAllButton->EnableWindow(FALSE);
		pUnreadButton->EnableWindow(FALSE);
		pSetButton->EnableWindow(FALSE);
		pGetButton->EnableWindow(FALSE);
	}
}

void CSmsExampleDlg::OnClearButton() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_strNumber="";
	m_strContent="";
	UpdateData(FALSE);
}

void CSmsExampleDlg::OnSendButton() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);//把对话框的内容传到变量
	SM_PARAM *sm_param_temp;   
	sm_param_temp= new SM_PARAM;//开辟空间
	CEdit* pEB=(CEdit*)GetDlgItem(IDC_EDIT1);
	SetCursor(LoadCursor(NULL,IDC_WAIT));//
	int nResult=SendShortMessage(m_strNumber,m_strContent,sm_param_temp);
	//sm_param_temp 貌似没有初始化
	if(nResult==0)
		pEB->SetWindowText("发送成功!!!");
	else if(nResult==1)
	{
		pEB->SetWindowText("发送失败!!!");
		MessageBox("可能在发送短消息同时接收到新的短消息!!!","发送失败",MB_OK);
	}
	else if(nResult==2)
	{
		pEB->SetWindowText("发送失败!!!");
		MessageBox("无链路连接或没有收到短消息中心的确认,或没有收到基站的确认!!!","发送失败",MB_OK);
	}
	else if(nResult==3)
	{
		pEB->SetWindowText("发送失败!!!");
		MessageBox("短消息号码错误或目的号码错误!!!","发送失败",MB_OK);
	}
	else if(nResult==4)
	{
		pEB->SetWindowText("发送失败!!!");
		MessageBox("在指令执行过程中又接受新的指令!!!","发送失败",MB_OK);
	}
	SetCursor(LoadCursor(NULL,IDC_ARROW
		));
	delete sm_param_temp;
	UpdateData(FALSE);
//	OnClearButton();
}


void CSmsExampleDlg::OnReceiveButton() //已读短信
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CListCtrl* pListCtrl=(CListCtrl*)GetDlgItem(IDC_LIST1);
	SetCursor(LoadCursor(NULL,IDC_WAIT));
	pListCtrl->DeleteAllItems(); //先删除原来的
	SM_PARAM *sm_param_temp=new SM_PARAM[30];//最大存储30条短信么
	int temp=ReceiveAllMessage(sm_param_temp);
	int i=0;
	while(i<temp)
	{													//目标号码
		pListCtrl->InsertItem(LVIF_TEXT|LVIF_STATE,i,sm_param_temp->TPA,(i%2)==0?LVIS_SELECTED:0,LVIS_SELECTED,0,0); 
		pListCtrl->SetItemText(i,1, InvertTime(sm_param_temp->TP_SCTS));//服务时间戳
		pListCtrl->SetItemText(i,2, sm_param_temp->TP_UD);	//原始用户信息
		sm_param_temp++;
		i++;
	}
	SetCursor(LoadCursor(NULL,IDC_ARROW));
	UpdateData(FALSE);
}



void CSmsExampleDlg::OnDelAllButton() //删除所有短信
{
	// TODO: Add your control notification handler code here
	SetCursor(LoadCursor(NULL,IDC_WAIT));	
	if(!DeleteAllMessage())
		{
			MessageBox("删除失败","删除所有短消息",MB_OK);
			return;
		}
	OnReceiveButton();
	SetCursor(LoadCursor(NULL,IDC_WAIT));
}

/*
void CSmsExampleDlg::OnDelOneButton() 
{
	// TODO: Add your control notification handler code here
	if(Fcomm!=NULL)
	{
		CListCtrl* pListCtrl=(CListCtrl*)GetDlgItem(IDC_LIST1);
		if(pListCtrl->GetItemCount()==0)
		{
			MessageBox("没有选中要删除的短消息!!!","提示",MB_OK);
			return;
		}
		int i=pListCtrl->GetSelectedCount();
		if(!DeleteMessage(Fcomm,i+1))
		{
			MessageBox("删除失败","删除所有短消息",MB_OK);
			return;
		}
		OnReceiveButton();
	}
}
*/
/***************************************************
干什么用的
*/
CString CSmsExampleDlg::InvertTime(char temp[])
{
	CString strResult="20";
	CString temp1;
	int i;
	for(i=0;i<6;i=i+2)
	{
		temp1.Format("%c",temp[i]);
		strResult+=temp1;
		temp1.Format("%c",temp[i+1]);
		strResult+=temp1;
		if(i<4)
			strResult+="-";
	}
	strResult+=" ";
	for(i=6;i<14;i=i+2)
	{
		temp1.Format("%c",temp[i]);
		strResult+=temp1;
		temp1.Format("%c",temp[i+1]);
		strResult+=temp1;
		if(i<12)
			strResult+=":";
	}
	return strResult;
}


void CSmsExampleDlg::OnUnreadButton() //显示未读短信
{
	// TODO: Add your control notification handler code here
	CListCtrl* pListCtrl=(CListCtrl*)GetDlgItem(IDC_LIST1);
	pListCtrl->DeleteAllItems(); 
	SM_PARAM *sm_param_temp=new SM_PARAM[10];
	SetCursor(LoadCursor(NULL,IDC_WAIT));
	int temp=ReceiveUnreadMessage(sm_param_temp);
	if(temp==0)
	{
		MessageBox("没有未读短消息!!!","提示",MB_OK);
		return;
	}
	int i=0;
	while(i<temp)
	{
		pListCtrl->InsertItem(LVIF_TEXT|LVIF_STATE,i,sm_param_temp->TPA,(i%2)==0?LVIS_SELECTED:0,LVIS_SELECTED,0,0); 
		pListCtrl->SetItemText(i,1, InvertTime(sm_param_temp->TP_SCTS));
		pListCtrl->SetItemText(i,2, sm_param_temp->TP_UD);	
		sm_param_temp++;
		i++;
	}
	SetCursor(LoadCursor(NULL,IDC_ARROW));
}

/*
void CSmsExampleDlg::OnGetNumber() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	char *strCenterNumber=new char[15];
	if(!GetCenterNumber(Fcomm,strCenterNumber))
		strCenterNumber="wrong!";
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT2);
	pEdit->SetWindowText(strCenterNumber); 
	delete strCenterNumber;
	UpdateData(FALSE);

}

void CSmsExampleDlg::OnSetNumber() 
{
	// TODO: Add your control notification handler code here
	
	CString strCenterNumber="+86";
	CString temp;
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT2);
	pEdit->GetWindowText(temp);
	strCenterNumber+=temp;
	if(Fcomm==NULL)
		return;
	if(!SetCenterNumber(Fcomm,&strCenterNumber))
		AfxMessageBox("wrong!");
	delete strCenterNumber;
}
*/
void CSmsExampleDlg::OnGetCenter() 
{
	// TODO: Add your control notification handler code here
	char* strCenterNumber=new char[16];
	GetCenterNumber(strCenterNumber);
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT2);
	pEdit->SetWindowText(strCenterNumber); 
	delete strCenterNumber;
	UpdateData(FALSE);
}

void CSmsExampleDlg::OnSetCenter() 
{
	// TODO: Add your control notification handler code here
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT2);
	CString strCenterNumber;
	pEdit->GetWindowText(strCenterNumber);
	SetCenterNumber(strCenterNumber);
}

void CSmsExampleDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -