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

📄 gsmtouartdlg.cpp

📁 GSM模块的串口操作源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// GsmToUartDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GsmToUart.h"
#include "GsmToUartDlg.h"
#include "UartSettingDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CGsmToUartDlg dialog

CGsmToUartDlg::CGsmToUartDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGsmToUartDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGsmToUartDlg)
	m_nViewSmsRead = _T("");
	m_nViewSmsSend = _T("");
	m_nPhoneNumber = _T("");
	m_nResultAt = _T("");
	m_nCommandAt = _T("");
	m_nSignalintensity = _T("");
	m_nSmsSerNumber = _T("");
	m_nSmsSendNumber = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	ComOpenFlag=0;
	ComUseEnable=1;
}

void CGsmToUartDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGsmToUartDlg)
	DDX_Text(pDX, IDC_VIEWSMS_READ, m_nViewSmsRead);
	DDX_Text(pDX, IDC_VIEWSMS_SEND, m_nViewSmsSend);
	DDX_Text(pDX, IDC_PHONE_NUMBER, m_nPhoneNumber);
	DDX_Text(pDX, IDC_RESULT_AT, m_nResultAt);
	DDX_Text(pDX, IDC_COMMAND_AT, m_nCommandAt);
	DDX_Text(pDX, IDC_SIGNALINTENSITY, m_nSignalintensity);
	DDX_Text(pDX, IDC_SMSSERNUMBER, m_nSmsSerNumber);
	DDX_Text(pDX, IDC_SMSSENDNUMBER, m_nSmsSendNumber);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGsmToUartDlg, CDialog)
	//{{AFX_MSG_MAP(CGsmToUartDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RUN_AT, OnRunAt)
	ON_BN_CLICKED(IDC_SEND_SMS, OnSendSms)
	ON_BN_CLICKED(IDC_READ_SMS, OnReadSms)
	ON_BN_CLICKED(IDC_CLOSE_PHONE, OnClosePhone)
	ON_BN_CLICKED(IDC_ONLINE_PHONE, OnOnlinePhone)
	ON_BN_CLICKED(IDC_UART_SETTING, OnUartSetting)
	ON_BN_CLICKED(IDC_OPEN_UART, OnOpenUart)
	ON_BN_CLICKED(IDC_CLOSE_UART, OnCloseUart)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_HELP_AT, OnHelpAt)
	ON_BN_CLICKED(IDC_LOADSMS, OnLoadsms)
	ON_BN_CLICKED(IDC_SAVESMS, OnSavesms)
	ON_BN_CLICKED(IDC_VIEWNOTREADSMS, OnViewnotreadsms)
	ON_BN_CLICKED(IDC_DELETESMS, OnDeletesms)
	ON_BN_CLICKED(IDC_SETSMSSERNUMBER, OnSetsmssernumber)
	ON_BN_CLICKED(IDC_GETSMSSERNUMBER, OnGetsmssernumber)
	ON_BN_CLICKED(IDC_CLEARATRESULT, OnClearatresult)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGsmToUartDlg message handlers

BOOL CGsmToUartDlg::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);

	char* pFileName = "TmpSetting.dat";
	CStdioFile ComSetting;
	if (!ComSetting.Open ("TmpSetting.dat", CFile::modeRead | CFile::typeText))
	{
		AfxMessageBox("打开文件TmpSetting.dat出错!");
		return FALSE;
	}

	ComSetting.ReadString(nComBaud);
	ComSetting.ReadString(nComSel);
	ComSetting.ReadString(nComData);
	CString	tmptring;
	ComSetting.ReadString(tmptring);
	sscanf(tmptring,"%d", &nComStop);
	ComSetting.ReadString(tmptring);
	sscanf(tmptring,"%d", &nComParity);
	ComSetting.ReadString(tmptring);
	sscanf(tmptring,"%d", &nComShake);
	ComSetting.Close();
	ComOpenFlag=0;
	ComUseEnable=1;
	SetTimer(100,1000,NULL);


	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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CGsmToUartDlg::OnRunAt() 
{
	// TODO: Add your control notification handler code here
	CString tmpSend; 
	int nLength;
	char ReceiveData[128]; 

	if(!ComOpenFlag)
	{
	AfxMessageBox(_T("串口未打开!"));
	return ;
	}
	if(!ComUseEnable)
	{
	AfxMessageBox(_T("串口在使用中!"));
	return ;
	}
	ComUseEnable=0;
	UpdateData(FALSE);
	tmpSend=m_nCommandAt;
	nLength=tmpSend.GetLength();
	WriteComm(tmpSend,nLength);

	nLength = ReadComm(ReceiveData,128);   
	ReceiveData[nLength+1]='\0';
	m_nResultAt=ReceiveData;
	m_nResultAt+="\r\n";
	UpdateData(FALSE);
	ComUseEnable=1;	

}

void CGsmToUartDlg::OnSendSms() 
{
	// TODO: Add your control notification handler code here

}

void CGsmToUartDlg::OnReadSms() 
{
	// TODO: Add your control notification handler code here
	
}

void CGsmToUartDlg::OnClosePhone() 
{
	// TODO: Add your control notification handler code here

}

void CGsmToUartDlg::OnOnlinePhone() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

}

void CGsmToUartDlg::OnUartSetting() 
{
	// TODO: Add your control notification handler code here
	CUartSettingDlg dlg;

	dlg.m_nComSel=nComSel;
	dlg.m_nComData=nComData;
	dlg.m_nComBaud=nComBaud;
	dlg.m_nComParity=nComParity;
	dlg.m_nComShake=nComShake;
	dlg.m_nComStop=nComStop;

	if(dlg.DoModal()==IDOK)
	{
		OnCloseUart();
		nComSel=dlg.m_nComSel;
		nComData=dlg.m_nComData;
		nComBaud=dlg.m_nComBaud;
		nComParity=dlg.m_nComParity;
		nComShake=dlg.m_nComShake;
		nComStop=dlg.m_nComStop;
		OnOpenUart();
	}	
}

void CGsmToUartDlg::OnOpenUart() 
{
	// TODO: Add your control notification handler code here
	sscanf(nComBaud,"%d", &dwBaud);
	sscanf(nComData,"%d", &dwData);
	CString strCom;
	CString TestData;
	if(ComOpenFlag)
	{
		AfxMessageBox(_T("串口已经打开!"));
	}
	else
	{
		strCom.Format(nComSel);

		// 打开指定的串口
		hCom1 = ::CreateFile(strCom,GENERIC_READ | GENERIC_WRITE,0,NULL,
			OPEN_EXISTING,0,NULL);

		// 获得串口的配置信息
		COMMCONFIG ComCfg;
		DWORD dwSize2 = sizeof(ComCfg);
		if (!::GetCommConfig(hCom1,&ComCfg,&dwSize2))
		{
			AfxMessageBox("读取串口配置失败!");
			::CloseHandle(hCom1);
			return;
		}

		// 获得当前的串口配置状态
		DCB dcb;
		if (!GetCommState(hCom1,&dcb))
		{
			AfxMessageBox(_T("读取串口状态失败!"));
			::CloseHandle(hCom1);
			return;
		}
	
		// 设置奇偶校验方式
		switch (nComParity)
		{
		case 0:
			// 不校验
			dcb.Parity = NOPARITY;    
			break;	
		case 1:
			// 奇校验
			dcb.Parity = ODDPARITY;   
			break;
		case 2:
			// 偶校验
			dcb.Parity = EVENPARITY;  
			break;
		case 3:
			// MARK方式
			dcb.Parity = MARKPARITY;  
			break;
		case 4:
			// SPACE方式
			dcb.Parity = SPACEPARITY; 
			break;
		}
	
		// 设置数据位格式
		dcb.ByteSize = dwData;

		// 设置波特率
		dcb.BaudRate = dwBaud;

		// 设置停止位方式
		switch (nComStop)
		{
		case 0:
			// 停止位为1位
			dcb.StopBits = ONESTOPBIT;
			break;
		case 1:
			// 停止位为1.5位
			dcb.StopBits = ONE5STOPBITS; 
			break;
		case 2:
			// 停止位为2位
			dcb.StopBits = TWOSTOPBITS;  
			break;
		}

		// 设置灵敏度
		dcb.fDsrSensitivity = FALSE;

⌨️ 快捷键说明

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