📄 irdamobiledlg.cpp
字号:
// IrdaMobileDlg.cpp : implementation file
//
#include "stdafx.h"
#include "IrdaMobile.h"
#include "IrdaMobileDlg.h"
#include "IrdaPort.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
#define TIMEOUT 5000 // Timeout for the phone response in milliseconds
/////////////////////////////////////////////////////////////////////////////
// CIrdaMobileDlg dialog
CIrdaMobileDlg::CIrdaMobileDlg(CWnd* pParent /*=NULL*/)
: CDialog(CIrdaMobileDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CIrdaMobileDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CIrdaMobileDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CIrdaMobileDlg)
DDX_Control(pDX, IDC_COMBO1, m_ctrlPort);
DDX_Control(pDX, IDC_BUTTON1, m_btnRead);
DDX_Control(pDX, IDC_LIST1, m_ctrlList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CIrdaMobileDlg, CDialog)
//{{AFX_MSG_MAP(CIrdaMobileDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnRead)
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CIrdaMobileDlg message handlers
BOOL CIrdaMobileDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
m_ctrlPort.ResetContent();
for (int n = 0; n < 255; n++)
{
CString str;
str.Format(_T("COM%d"), n + 1);
m_ctrlPort.InsertString(n,str);
}
const int nPort = CIrdaPort::FindPortIndex();
m_ctrlPort.SetCurSel((nPort > 0 && nPort <= 255) ? (nPort - 1) : 2);
m_ctrlList.InsertColumn(0, _T("Name"), LVCFMT_LEFT);
m_ctrlList.InsertColumn(1, _T("Number"), LVCFMT_LEFT);
CRect rect;
m_ctrlList.GetClientRect(rect);
m_ctrlList.SetColumnWidth(0, rect.Width() / 2);
m_ctrlList.SetColumnWidth(1, LVSCW_AUTOSIZE_USEHEADER);
m_btnRead.SetFont(GetFont());
m_btnRead.SetColor(CCeButtonST::BTNST_COLOR_BK_IN, RGB(192, 192, 192));
m_btnRead.SetColor(CCeButtonST::BTNST_COLOR_BK_OUT, RGB(192, 192, 192));
m_btnRead.SetColor(CCeButtonST::BTNST_COLOR_BK_FOCUS, RGB(192, 192, 192));
m_btnRead.SetFlat(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CIrdaMobileDlg::OnRead()
{
// TODO: Add your control notification handler code here
CString strResponse;
CWaitCursor wait;
///////////////////////////////////////////////////////////////////////////
//
// Try to open the IRDA port.
//
UpdateData(TRUE);
const int nPort = m_ctrlPort.GetCurSel() + 1;
if (nPort <= 0 || nPort > 255)
{
AfxMessageBox(_T("Invalid IRDA port."));
return;
}
CIrdaPort port;
if (!port.Open(nPort))
{
AfxMessageBox(_T("Failed to open IRDA port."));
return;
}
GetDlgItem(IDC_PHONE)->SetWindowText(_T(""));
GetDlgItem(IDC_PHONE)->UpdateWindow();
GetDlgItem(IDC_MODEL)->SetWindowText(_T(""));
GetDlgItem(IDC_MODEL)->UpdateWindow();
GetDlgItem(IDC_ENTRIES)->SetWindowText(_T("0 Entries"));
GetDlgItem(IDC_ENTRIES)->UpdateWindow();
m_ctrlList.DeleteAllItems();
m_ctrlList.Invalidate();
m_ctrlList.UpdateWindow();
///////////////////////////////////////////////////////////////////////////
//
// Try to connect to the Mobile Phone.
//
// Command : AT<CR>
//
// Valid response: <CR><LF>OK<CR><LF>
//
// Error response: <CR><LF>ERROR<CR><LF>
//
// <CR> ... Carriage return
// <LF> ... Line feed
//
if (!port.Send(_T("AT"), TIMEOUT))
{
port.Close();
AfxMessageBox(_T("Failed to write request to device."));
return;
}
if (!port.WaitForResponse(strResponse, TIMEOUT))
{
port.Close();
AfxMessageBox(_T("Timeout, no response."));
return;
}
strResponse.MakeUpper();
if (strResponse.Find(_T("ERROR")) > -1)
{
port.Close();
AfxMessageBox(_T("Invalid command."));
return;
}
else if (strResponse.Find(_T("OK")) == -1)
{
port.Close();
AfxMessageBox(_T("Wrong response."));
return;
}
///////////////////////////////////////////////////////////////////////////
//
// Request the manufacturer identification.
//
// Command : AT+CGMI<CR>
//
// Valid response: <CR><LF>manufacturer<CR><LF>
// <CR><LF>OK<CR><LF>
//
// manufacturer ... manufacturer identification
//
// Error response: <CR><LF>ERROR<CR><LF>
//
// <CR> ... Carriage return
// <LF> ... Line feed
//
if (!port.Send(_T("AT+CGMI"), TIMEOUT))
{
port.Close();
AfxMessageBox(_T("Failed to write request to device."));
return;
}
if (!port.WaitForResponse(strResponse, TIMEOUT))
{
port.Close();
AfxMessageBox(_T("Timeout, no response."));
return;
}
const CString strPhone(strResponse);
strResponse.MakeUpper();
if (strResponse.Find(_T("ERROR")) > -1)
{
port.Close();
AfxMessageBox(_T("Invalid command."));
return;
}
if (!port.WaitForResponse(strResponse, TIMEOUT))
{
port.Close();
AfxMessageBox(_T("Timeout, no response."));
return;
}
strResponse.MakeUpper();
if (strResponse.Find(_T("ERROR")) > -1)
{
port.Close();
AfxMessageBox(_T("Invalid command."));
return;
}
else if (strResponse.Find(_T("OK")) == -1)
{
port.Close();
AfxMessageBox(_T("Wrong response."));
return;
}
GetDlgItem(IDC_PHONE)->SetWindowText(strPhone);
GetDlgItem(IDC_PHONE)->UpdateWindow();
///////////////////////////////////////////////////////////////////////////
//
// Request the model identification.
//
// Command : AT+CGMM<CR>
//
// Valid response: <CR><LF>model<CR><LF>
// <CR><LF>OK<CR><LF>
//
// model ... model identification
//
// Error response: <CR><LF>ERROR<CR><LF>
//
// <CR> ... Carriage return
// <LF> ... Line feed
//
if (!port.Send(_T("AT+CGMM"), TIMEOUT))
{
port.Close();
AfxMessageBox(_T("Failed to write request to device."));
return;
}
if (!port.WaitForResponse(strResponse, TIMEOUT))
{
port.Close();
AfxMessageBox(_T("Timeout, no response."));
return;
}
const CString strModel(strResponse);
strResponse.MakeUpper();
if (strResponse.Find(_T("ERROR")) > -1)
{
port.Close();
AfxMessageBox(_T("Invalid command."));
return;
}
if (!port.WaitForResponse(strResponse, TIMEOUT))
{
port.Close();
AfxMessageBox(_T("Timeout, no response."));
return;
}
strResponse.MakeUpper();
if (strResponse.Find(_T("ERROR")) > -1)
{
port.Close();
AfxMessageBox(_T("Invalid command."));
return;
}
else if (strResponse.Find(_T("OK")) == -1)
{
port.Close();
AfxMessageBox(_T("Wrong response."));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -