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

📄 testdlg.cpp

📁 用于在串口上开发手机短信的工具包
💻 CPP
字号:
// testDlg.cpp : implementation file

#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include "SMSCOM.h"
#include <string.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define SMS_INVALID		-1
#define SMS_IDLE		0
#define SMS_SNDING		1
#define SMS_RCVING		2
#define SMS_EXIT		3

CRITICAL_SECTION criSnd_Rcv_Flag;
int	iSndRcvFlag=0;  //1发送 

HANDLE hComm;
int	   iTmp;
STRUCommInfo CommInfo;			
char szErrInfo[30];
SM_RCV_STRU  RcvSms;

UINT nIDEvent;

int iTestSMSFormat=0; //0 PDU模式收发短信 1-Text模式收发短信

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

//获取收发状态
int iGetSndRcvState(void)
{
    int iRet=0;
    EnterCriticalSection(&criSnd_Rcv_Flag);
    iRet=iSndRcvFlag;
    LeaveCriticalSection(&criSnd_Rcv_Flag);
    return iRet;
}

//设置收发状态
int iSetSndRcvState(int iNewVal)
{
    int iRet=0;
    EnterCriticalSection(&criSnd_Rcv_Flag);
    iSndRcvFlag=iNewVal;
    LeaveCriticalSection(&criSnd_Rcv_Flag);
    return iRet;
}

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()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
	m_SCA = _T("");
	m_Result = _T("");
	m_Msg = _T("");
	m_COM_Switch = _T("");
	m_State = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Control(pDX, IDC_CMB_SMS_FORMAT, m_SMSFormat);
	DDX_Control(pDX, IDC_COM, m_COM);
	DDX_Control(pDX, IDC_SND_PH, m_SndPh);
	DDX_Control(pDX, IDC_SND_MSG, m_SndMsg);
	DDX_Control(pDX, IDC_LIST_RCV, m_ListRcv);
	DDX_Text(pDX, IDC_SCA, m_SCA);
	DDX_Text(pDX, IDC_COM_SWITCH, m_COM_Switch);
	DDX_Text(pDX, IDC_STATE, m_State);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_OPEN, OnBtnOpen)
	ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
	ON_BN_CLICKED(ID_BTN_SND, OnBtnSnd)
	ON_BN_CLICKED(ID_BTN_RCV, OnBtnRcv)
	ON_CBN_SELCHANGE(IDC_CMB_SMS_FORMAT, OnSelchangeCmbSmsFormat)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

BOOL CTestDlg::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
    memset(&criSnd_Rcv_Flag,0,sizeof(criSnd_Rcv_Flag));  //定义线路状态临界区
    InitializeCriticalSection(&criSnd_Rcv_Flag);
	iSetSndRcvState(SMS_INVALID); 
	
	m_SMSFormat.SetCurSel(iTestSMSFormat);
	m_COM.SetWindowText("COM4");
	m_COM_Switch="关闭";
	m_SCA="";
	if(iTestSMSFormat==1)
	{
		m_SndMsg.SetWindowText("TestMsg");
	}
	else
	{
		m_SndMsg.SetWindowText("测试短信");
	}
	m_SndPh.SetWindowText("13910673682");
	m_State="";

	m_ListRcv.InsertColumn(0,"手机号码",LVCFMT_LEFT,80,-1);
	m_ListRcv.InsertColumn(0,"接收时间",LVCFMT_LEFT,100,-1);
	m_ListRcv.InsertColumn(0,"接收短信内容",LVCFMT_LEFT,220,-1);
	
	::SetTimer(m_hWnd,WM_TIMER,5000,NULL);		//3000每三秒钟查库一次
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CTestDlg::OnOK() 
{
	CDialog::OnOK();
}

void CTestDlg::OnBtnOpen() 
{
	bool bRet=false;
	int  iErrCode=-255;
	char szVerInfo[30]; //版本信息
	CString strTmp="";
	
	// TODO: Add your control notification handler code here
	memset(szVerInfo,0,sizeof(szVerInfo));
	GetVerInfo(&szVerInfo[0]);  //获取版本信息

	//设置DLL序列号
	//iErrCode=1密码正确; iErrCode=-100密码错误; 
	iErrCode=iSetSerialNo("北京方环电子公司","注册码");		//中文输入
	//iErrCode=iSetSerialNo("www.netlabchina.com","SN");  //英文输入
    m_COM.GetWindowText(strTmp);

	//打开串口
	hComm=hOpenComm(strTmp);

	if(hComm!=NULL)
	{
		m_COM_Switch="打开成功";

		//获取SIM卡信息
		bRet=bAutoInit(hComm,&CommInfo,iTestSMSFormat,szErrInfo);
		m_SCA=CommInfo.szSCA;

		iSetSndRcvState(SMS_IDLE);
	}
	else
	{
		m_COM_Switch="打开失败";
	}
	m_State="";
	UpdateData(false);
}

void CTestDlg::OnBtnClose() 
{
	BOOL BRet;
	int iCurrState;

	// TODO: Add your control notification handler code here
	iCurrState=iGetSndRcvState();
	iSetSndRcvState(SMS_INVALID); 	
	
	//关闭串口
	BRet=bCloseComm(hComm);
	if(BRet==TRUE)
	{
		m_COM_Switch="关闭成功";
		hComm=NULL;
	}
	else
	{
		m_COM_Switch="关闭失败";
		iSetSndRcvState(iCurrState); 
	}
	m_State="";
	UpdateData(false);
}

void CTestDlg::OnBtnSnd() 
{
	bool bRet=false;
	char szErrInfo[300];
	char szTmpMsg[2048],szTmpSndPh[30],szTmpSCA[30],szTmpNextMsg[1024];
	CString strTmpSndMsg,strTmpSndPh="";
	int iTmp=0;

	// TODO: Add your control notification handler code here
	m_SndMsg.GetWindowText(strTmpSndMsg);
	m_SndPh.GetWindowText(strTmpSndPh);

	iSetSndRcvState(SMS_SNDING);

	m_State="正在发送...";
	UpdateData(false);

	memset(szErrInfo,0,sizeof(szErrInfo));
	memset(szTmpMsg,0,sizeof(szTmpMsg));
	memset(szTmpSndPh,0,sizeof(szTmpSndPh));
	memset(szTmpSCA,0,sizeof(szTmpSCA));

	strcpy(szTmpMsg,strTmpSndMsg);
	strcpy(szTmpSndPh,strTmpSndPh);
	strcpy(szTmpSCA,m_SCA);

	while(strlen(szTmpMsg)>0)
	{
		memset(szTmpNextMsg,0,sizeof(szTmpNextMsg));

		//发送短信
		bRet=bSendMsg(hComm,szTmpMsg,szTmpSndPh,szTmpSCA,&(szTmpNextMsg[0]),iTestSMSFormat,szErrInfo);
		
		memset(szTmpMsg,0,sizeof(szTmpMsg));
		iTmp=strlen(szTmpNextMsg);
		strcpy(szTmpMsg,szTmpNextMsg);	
	}

	iSetSndRcvState(SMS_IDLE); 
	
	if(bRet)
	{
		m_State="发送成功";
	}
	else
	{
		m_State="发送失败";
	}
	UpdateData(false);

}

void CTestDlg::OnBtnRcv() 
{
	int i,iTmpRcvSmsTotal=0;
	iTestSMSFormat=1;

	// TODO: Add your control notification handler code here
	m_State="正在接收...";
	UpdateData(false);

	RcvSms.iSmsTotal=0;

	//接收短信
	iTmpRcvSmsTotal=iRecvMsg(hComm,&RcvSms,iTestSMSFormat,szErrInfo);
	if(iTmpRcvSmsTotal<=0)
	{
		m_State="";
		UpdateData(false);
		return;
	}
	for(i=0;i<iTmpRcvSmsTotal;i++)
	{
		m_ListRcv.InsertItem(0,"");
		m_ListRcv.SetItemText(0,0,RcvSms.SMS[i].TP_UD);
		m_ListRcv.SetItemText(0,1,RcvSms.SMS[i].TP_SCTS);
		m_ListRcv.SetItemText(0,2,RcvSms.SMS[i].TPA);
	}
	m_State="接收完毕";
	UpdateData(false);

}

void CTestDlg::OnSelchangeCmbSmsFormat() 
{
	BOOL BRet;

	// TODO: Add your control notification handler code here
	iTestSMSFormat=m_SMSFormat.GetCurSel();
	if(iTestSMSFormat==1)
	{
		m_SndMsg.SetWindowText("TestMsg");
	}
	else
	{
		m_SndMsg.SetWindowText("测试短信");
	}
	
	//关闭串口
	if(hComm!=NULL)
	{
		BRet=bCloseComm(hComm);
		if(BRet==TRUE)
		{
			m_COM_Switch="关闭成功";
			hComm=NULL;
		}
		else
		{
			m_COM_Switch="关闭失败";
		}
	}
	m_State="";
	UpdateData(false);

}

void CTestDlg::OnTimer(UINT nIDEvent) 
{
	int i,iTmpRcvSmsTotal=0;
	int	iCurrState;

	// TODO: Add your control notification handler code here

	iCurrState=iGetSndRcvState(); 
	if((iCurrState==SMS_SNDING)||(iCurrState==SMS_INVALID))
	{
		return ;
	}

	iSetSndRcvState(SMS_RCVING); 

	m_State="正在接收...";
	UpdateData(false);

	RcvSms.iSmsTotal=0;
	iTmpRcvSmsTotal=iRecvMsg(hComm,&RcvSms,iTestSMSFormat,szErrInfo);
	if(iTmpRcvSmsTotal<=0)
	{
		iSetSndRcvState(SMS_IDLE); 
		m_State="";
		UpdateData(false);
		return;
	}
	for(i=0;i<iTmpRcvSmsTotal;i++)
	{
		m_ListRcv.InsertItem(0,"");
		m_ListRcv.SetItemText(0,0,RcvSms.SMS[i].TP_UD);
		m_ListRcv.SetItemText(0,1,RcvSms.SMS[i].TP_SCTS);
		m_ListRcv.SetItemText(0,2,RcvSms.SMS[i].TPA);
	}

	iSetSndRcvState(SMS_IDLE); 

	CDialog::OnTimer(nIDEvent);}

⌨️ 快捷键说明

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