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

📄 练习dlg.cpp

📁 此程序能通过串口发送手机短信
💻 CPP
字号:
// 练习Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "练习.h"
#include "练习Dlg.h"
#include "mscomm.h"
#include "ChineseCode1.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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

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

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_READ, m_TAG);
	DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm);
	DDX_Text(pDX, IDC_EDIT1, m_receive);
	DDX_Text(pDX, IDC_CenterNumber, m_CenterNum);
	DDX_Text(pDX, IDC_ToNumber, m_ToNum);
	DDX_Text(pDX, IDC_SEND, m_send);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnLink)
ON_BN_CLICKED(IDC_BUTTON2, OnSend)
ON_BN_CLICKED(IDC_BUTTON3, dail)
ON_BN_CLICKED(IDC_BUTTON4, shutup)
ON_BN_CLICKED(IDC_BUTTON5, listen)
ON_BN_CLICKED(IDC_READ, OnRead)
ON_BN_CLICKED(IDC_BUTTON6, OnEXIT)
ON_BN_CLICKED(IDC_Delete, OnDelete)
ON_BN_CLICKED(IDC_BUTTON7, OnCleanUp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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
	
	CComboBox *combox =(CComboBox *)GetDlgItem(IDC_COMBO2);
	combox->AddString("COM 1");
	combox->AddString("COM 2");
	combox->AddString("COM 3");
	combox->AddString("COM 4");
	combox->AddString("COM 5");
	combox->AddString("COM 6");
	combox->AddString("COM 7");
	combox->SetCurSel(2);
	
	CListCtrl *list=(CListCtrl *)GetDlgItem(IDC_LIST1);
	list->InsertColumn(0,"发送方号码",LVCFMT_LEFT,100);
	list->InsertColumn(1,"接收时间",LVCFMT_LEFT,100);
	list->InsertColumn(2,"短信内容",LVCFMT_LEFT,list->GetColumnWidth(2));
	
	m_SMS_index=1;
	m_CenterNum="13800731500";
	m_ToNum="13467515079";
	
	account=0;
	
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMyDlg::OnLink() 
{
	// TODO: Add your control notification handler code here
	
	m_ctrlComm.SetSettings("9600,n,8,1");
	m_ctrlComm.SetInputMode(1);
	m_ctrlComm.SetRThreshold(1);
	
	m_ctrlComm.SetRTSEnable(TRUE);
	m_ctrlComm.SetDTREnable(TRUE);
	
	m_ctrlComm.SetInputLen(0); //设置当前接收区数据长度为0
	
	m_ctrlComm.SetInBufferCount(0);       //清空接收缓冲区   
    m_ctrlComm.SetOutBufferCount(0);     //清空发送缓冲区   
	
	m_ctrlComm.SetInBufferSize(1024);
	m_ctrlComm.SetOutBufferSize(1024);
	
	if(m_ctrlComm.GetPortOpen()){
		MessageBox("连接要关闭!");
		m_ctrlComm.SetPortOpen(FALSE);
	}
	
	CComboBox *combox =(CComboBox *)GetDlgItem(IDC_COMBO2);
	m_ctrlComm.SetCommPort(combox->GetCurSel()+1);
	
	if(!m_ctrlComm.GetPortOpen()){
		
		m_ctrlComm.SetPortOpen(TRUE);
		
		if(m_ctrlComm.GetPortOpen())
			MessageBox("连接成功!");
		else
			MessageBox("连接失败!");
	}
	else
		MessageBox("无法打开!");
	
	
	m_ctrlComm.GetInput();//先预读缓冲区以清除残留数据
	
	
	
	//设置短消息到达自动提示at+clip=1 
	

	
	//	m_ctrlComm.SetOutput(COleVariant("AT+CNUM\r\n"));
	//	m_ctrlComm.SetOutput(COleVariant(cs));
	
}

void CMyDlg::OnSend() 
{
	// TODO: Add your control notification handler code here//
	
	CString num="089168";
	CString source="";
	CString desty;
	CString command="AT+CMGS=";
	CString temp;
	
	int length;
	int zonglength=14;
	
	UpdateData(TRUE);
	
	temp=Digitcheng.RightToDiverse(m_CenterNum);
	num+=temp;
	num+="11000D9168";
	
	//0891683108701305F011000D91680180F60008A9 0A 00310030003000380036
	temp=Digitcheng.RightToDiverse(m_ToNum);
	num+=temp;
	num+="0008A9";
	
	source=m_send;
	MessageBox(m_send);
	length=CCC.gsmEncodeUCS2(source,desty);
	zonglength+=length+1;
	
	temp.Format("%d",zonglength);
	command+=temp+CString("\r\n");
	//pdu格式发送,而不是text格式发送
	m_ctrlComm.SetOutput(COleVariant("AT+CMGF=0\r\n"));
	Sleep(1000);
	m_ctrlComm.SetOutput(COleVariant(command));
	//XXX表示PDU中TPDU的长度(不包含SMSC地址)
	Sleep(1000);
	
	num+=Digitcheng.Chenge(length);
	num+=desty+CString("\x01a");
	
	m_ctrlComm.SetOutput(COleVariant(num));
	
}

BEGIN_EVENTSINK_MAP(CMyDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMyDlg)
ON_EVENT(CMyDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CMyDlg::OnComm() 
{
	// TODO: Add your control notification handler code here
	VARIANT variant_inp; 
	COleSafeArray safearray_inp; 
	LONG len,k; 
	BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed. 
	CString strtemp; 
	CString recd;
	
	switch(m_ctrlComm.GetCommEvent()) 
	{ 
	case 1: // comEvSend发送数据 
		break; 
		
	case 2: 
		{
			// comEvReceive读取数据 
			// MessageBox(_T("读取数据事件"), _T("TRACE"), MB_OK); 
			variant_inp=m_ctrlComm.GetInput(); //读缓冲区 
			safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
			len=safearray_inp.GetOneDimSize(); //得到有效数据长度 
			//传递参数到read窗口
			Sarray=safearray_inp;
			// 接受数据 
			for(k=0; k<len;k++){ 
				safearray_inp.GetElement(&k,rxdata+k); //转换为BYTE型数组 
				BYTE bt=*(char*)(rxdata+k); //字符型 
				strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放 
				m_receive+=strtemp; 
			} 
			break; 
		}
		
	default: 
		{
			// 传输事件出错 
			
			m_ctrlComm.SetOutBufferCount(0); 
			break; 
		}
	} 
	
//	m_show=m_receive;
	
	UpdateData(FALSE);
	return; 
}


void CMyDlg::dail() 
{
	// TODO: Add your control notification handler code here
	//	拨打手机电话号码
	
	UpdateData(TRUE);
	m_ctrlComm.SetOutput(COleVariant("ATD"+m_ToNum+"\r\n"));
}

void CMyDlg::shutup() 
{
	// TODO: Add your control notification handler code here
	//	挂断手机电话号码
	m_ctrlComm.SetOutput(COleVariant("ATH\r\n"));
}

void CMyDlg::listen() 
{
	// TODO: Add your control notification handler code here
	//	接听手机电话号码
	m_ctrlComm.SetOutput(COleVariant("ATA\r\n"));
}

void CMyDlg::OnRead() 
{
	// TODO: Add your control notification handler code here
	//读取单条短信
	//	m_ctrlComm.SetInBufferCount(0);       //清空接收缓冲区 
		m_ctrlComm.SetOutput(COleVariant("AT+CMGL=1\r\n"));
	//	Sleep(1000);
	/*
	CString SMS_read;
	bool tag=false;
	char c;
	char* temp1;
	char* temp2=new char[1024];

	CString cs;
	cs.Format("%d",account);
//	m_ctrlComm.SetOutput(COleVariant("AT+CMGR="+cs+"\r\n"));
	account++;
	Sleep(1000);
	
	int length=m_receive.GetLength();
	CString ds;
	ds.Format("%d",length);
	MessageBox(ds);
	int i=1;
	
	while((i+62<length-1)&&(m_receive.GetAt(i+62)!='\r')){
		c=m_receive.GetAt(i+62);
		SMS_read+=c;
		i++;
		if(tag==false)
			tag=true;
	}
	if(tag==true){
		SMS_read+="\0";
		
		temp1=SMS_read.GetBuffer(SMS_read.GetLength());
		
		MessageBox(SMS_read);
		CCC.gsmDecodePdu(temp1,temp2);
		SMS_read.ReleaseBuffer();
		
		MessageBox(CString("message ")+temp2);
		Sleep(1000);
		m_receive="";
	}
	*/
}

void CMyDlg::OnEXIT() 
{
	// TODO: Add your control notification handler code here
	if(m_ctrlComm.GetPortOpen()){
		m_ctrlComm.SetPortOpen(FALSE);
		MessageBox("关闭");
	}
	else
		MessageBox("串口已关闭");
	
	CDialog::OnCancel();
}

void CMyDlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	//	PC上的控制软件收到一条短消息并处理后,需要将其在SIM卡上删除,以防止SIM卡饱和。删除短消息的指令为:
	
	
	m_ctrlComm.SetOutput(COleVariant("AT+CMGD=14\r\n"));
}


void CMyDlg::OnCleanUp() 
{
	// TODO: Add your control notification handler code here
	m_receive="";
	
	UpdateData(FALSE);
}


//修改背景颜色

HBRUSH CMyDlg::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)
{
	HBRUSH   hbr   =   CDialog::OnCtlColor(pDC,   pWnd,   nCtlColor);   
	pDC->SetBkMode(TRANSPARENT);//设置为透明模式   
	//   TODO:   Change   any   attributes   of   the   DC   here   
	hbr=CreateSolidBrush(RGB(60,111,255));//创建对话框的底色   
	//   pDC->SetBkColor(RGB(90,249,212));   
	
	//   TODO:   Return   a   different   brush   if   the   default   is   not   desired   
	return   hbr;   
	
}

⌨️ 快捷键说明

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