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

📄 cdmasmssdlg.cpp

📁 短消息模块EM4的调试程序
💻 CPP
字号:
// CDMASMSSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CDMASMSS.h"
#include "CDMASMSSDlg.h"
#include "MyConnect.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
static UINT BASED_CODE indicators[] =
{
	ID_INDICATOR_USE
};

CCDMASMSSDlg * CDMASMSSDlg=NULL;
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()

/////////////////////////////////////////////////////////////////////////////
// CCDMASMSSDlg dialog

CCDMASMSSDlg::CCDMASMSSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCDMASMSSDlg::IDD, pParent)
{
m_sendprepare2=m_sendprepare1=m_sending=false;
m_sendnumber=m_instruct="";
	count=0;
	tempch="";
	m_receive="";
	m_receiveASC="";
	m_bOpen=false;rchar_temp=chFlag=' ';
	//{{AFX_DATA_INIT(CCDMASMSSDlg)
	m_receiveAT = _T("");
	m_ASC = _T("");
	m_char1 = _T("");
	m_asc1 = _T("");
	m_char2 = _T("");
	m_asc2 =0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCDMASMSSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCDMASMSSDlg)
	DDX_Control(pDX, IDC_EDIT2, m_controlASC);
	DDX_Control(pDX, IDC_EDIT1, m_controlAT);
	DDX_Control(pDX, IDC_TAB1, m_sheet);
	DDX_Text(pDX, IDC_EDIT1, m_receiveAT);
	DDX_Text(pDX, IDC_EDIT2, m_ASC);
	DDX_Text(pDX, IDC_EDIT3, m_char1);
	DDX_Text(pDX, IDC_EDIT4, m_asc1);
	DDX_Text(pDX, IDC_EDIT6, m_char2);
	DDX_Text(pDX, IDC_EDIT5, m_asc2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCDMASMSSDlg, CDialog)
	//{{AFX_MSG_MAP(CCDMASMSSDlg)
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_OpenPort, OnOpenPort)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_COMMAND(ID_ClosePort, OnClosePort)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCDMASMSSDlg message handlers

BOOL CCDMASMSSDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
CDMASMSSDlg=this;
	// 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
if (!m_wndtoolbar.CreateEx( this,TBSTYLE_FLAT ,  WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS,
    CRect(4,4,0,0)) ||	!m_wndtoolbar.LoadToolBar(IDR_TOOLBAR1) )
{
	TRACE0("failed to create toolbar\n");
	return FALSE;
}
m_wndtoolbar.SetHeight(20);
m_wndtoolbar.ShowWindow(SW_SHOW);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
//////////////////////////////////////////////////
	m_bar.Create(this);	
	
	m_bar.SetIndicators(indicators,1);	

	CRect rect;
	GetClientRect(&rect);
	m_bar.SetPaneInfo(0,ID_INDICATOR_USE,SBPS_NORMAL,rect.Width()-100);
//	m_bar.SetPaneInfo(1,ID_INDICATOR_TIME,SBPS_STRETCH ,0);


//	m_bar.GetStatusBarCtrl().SetBkColor(RGB(180,180,180));
 font.CreateFont(16,           //字体字符的逻辑高度
			        0,                //字符平均宽度取默认值
				0,                //文本行角度为0,水平
				0,                //字符角度为0,正立
				FW_MEDIUM,        //正常字体
						 FALSE,            //不倾斜 
						 FALSE,            //不加下划线
						 FALSE,            //不加删除线
						 ANSI_CHARSET,       
						 OUT_DEFAULT_PRECIS,
						 CLIP_DEFAULT_PRECIS,
						 DEFAULT_QUALITY,
						 DEFAULT_PITCH|FF_MODERN,
						 "宋体");
m_bar.GetStatusBarCtrl().SetFont(&font,true);
m_bar.GetStatusBarCtrl().SetMinHeight(28);		

	RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,
		ID_INDICATOR_USE);
/////////////////////////////////////////////////////////////////////
	m_sheet.AddPage("发送短消息", &m_sendpage, IDD_DIALOG_Send);
   m_sheet.AddPage("测试指令", &m_testpage, IDD_DIALOG_Test);
	m_sheet.Show();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CCDMASMSSDlg::OnOpenPort() 
{
	// TODO: Add your command handler code here
	CMyConnect a;
int ret=a.DoModal();
//================================== 全局串口初始化 ==================================
	if(ret==IDOK)
	{
		if( m_bOpen )		//已经打开过串口
		{
	
			m_ComPort.ClosePort();		//关闭该串口			
			m_bOpen=false;
		}
		if (m_ComPort.InitPort(this,theApp.m_port,theApp.m_bit,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
	{
			m_ComPort.StartMonitoring();   //启动串口监视线程
			m_bOpen = true;
			CString infor="串口:COM";
			CString a="";
			a.Format("%d",theApp.m_port);
			infor+=a;
			infor+="; 波特率:";
            a.Format("%d",theApp.m_bit);
            infor+=a;
			m_bar.SetPaneText(0,infor);
		}
		else	
		{
			CString str;
			str.Format("COM%d 没有发现,或被其它设备占用",theApp.m_port);
			AfxMessageBox(str);
		}
	}
//================================================================================
}
LONG CCDMASMSSDlg::OnCommunication(WPARAM ch, LPARAM port)
{
	tempch.Format("%d",ch);
	count++;
	m_receiveASC+=tempch;
	if(count%9==0)
	m_receiveASC+="\r\n";
	else
	m_receiveASC+=" ";
	rchar_temp = chFlag;
	chFlag = ( char ) ch;
	m_receive += ( char ) ch;
	if(rchar_temp=='\r'&&chFlag=='\n')
{
m_receiveAT+=m_receive;
m_ASC=m_receiveASC;
// 
		m_controlAT.SetWindowText(m_receiveAT);
		m_controlAT.SetFocus (); 
		int nLen=m_controlAT.GetWindowTextLength();
		m_controlAT.SetSel (nLen, nLen);
		int EditCount = m_controlAT.GetLineCount();
		m_controlAT.LineScroll( EditCount,0);
// 
    	int nReceive=m_receive.Find("+CAD: 1");
       if(nReceive!=-1)
	   {
           m_sendprepare1=true;
	   }
	   	nReceive = m_receive.Find("OK");
		if(nReceive!=-1)
		{
				if(m_sendprepare2==true)
				{
		CString instruct=0x07;
		int length=27;
		length+=m_instruct.GetLength();
		instruct+=length;
		instruct+=m_sendnumber;
		for(int i=0;i<15;i++)
		instruct+=0x20;
		instruct+=m_instruct;
		m_ComPort.WriteToPort(instruct);
		m_sendprepare1=false;
		m_sendprepare2=false;
			}
			if(m_sendprepare1==true&&m_sendprepare2==false)
			{
				CString instruct="AT+SMSG=1";
					instruct+="\r";
			        m_ComPort.WriteToPort(instruct);
					m_sendprepare2=true;
			}
	
		}
UpdateData(false);
m_receive="";
}

	return 0;
}

void CCDMASMSSDlg::SendAT(CString AT)
{
m_ComPort.WriteToPort(AT);
}

void CCDMASMSSDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
m_receive="";
m_receiveAT="";
m_ASC=m_receiveASC="";
count=0;
UpdateData(false);	
}

void CCDMASMSSDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	char ch=' ';
	if(m_char1!="")
	ch=m_char1.GetAt(0);
	m_asc1.Format("%d",ch);
	UpdateData(false);
}

void CCDMASMSSDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	char ch=' ';
	ch=m_asc2;
	m_char2=ch;
	UpdateData(false);
}

void CCDMASMSSDlg::SendShortMessage(CString number, CString content)
{
    CString send="";
	send="at+cad?";
	send+="\r";
   m_sending=true;
   m_ComPort.WriteToPort(send);
   m_instruct=content;
   m_sendnumber=number;
}

void CCDMASMSSDlg::OnClosePort() 
{
	// TODO: Add your command handler code here
		m_ComPort.ClosePort();		//关闭该串口			
			m_bOpen=false;
			CString infor="串口关闭";
				m_bar.SetPaneText(0,infor);
}


⌨️ 快捷键说明

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