// DlgBlueToothExc.cpp : implementation file
//
#include "stdafx.h"
#include "BlueToothExc.h"
#include "DlgDataShow.h"
#include "DlgBlueToothExc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CDlgBlueToothExc dialog
CDlgBlueToothExc::CDlgBlueToothExc(CWnd* pParent /*=NULL*/)
: CDialog(CDlgBlueToothExc::IDD, pParent)
, m_pHwCOM ( NULL )
, m_hThread ( NULL )
, m_dwThreadID ( 0 )
, m_hEvtEndThread ( NULL )
, m_hEvtSendDataReq ( NULL )
, m_nBytesForSending ( 0 )
, m_bNoteBeCleared ( FALSE )
, m_bShowCurve(FALSE)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
memset ( m_DataForSending, 0, sizeof(m_DataForSending) );
DeleteFile ( _T("c:\\bthdebug.dat") );
}
void CDlgBlueToothExc::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CDlgBlueToothExc, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON_ScanBluetoothDevice, &CDlgBlueToothExc::OnBnClickedButtonScanbluetoothdevice)
ON_LBN_DBLCLK(IDC_LIST_NearbyBthDev, &CDlgBlueToothExc::OnLbnDblclkListNearbybthdev)
ON_BN_CLICKED(IDC_BUTTON_Test, &CDlgBlueToothExc::OnBnClickedButtonTest)
ON_BN_CLICKED(IDC_BUTTON_Enumerate, &CDlgBlueToothExc::OnBnClickedButtonEnumerate)
ON_BN_CLICKED(IDC_BUTTON_RequestAuthenticate, &CDlgBlueToothExc::OnBnClickedButtonRequestauthenticate)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_Connect, &CDlgBlueToothExc::OnBnClickedButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_Disconnect, &CDlgBlueToothExc::OnBnClickedButtonDisconnect)
ON_BN_CLICKED(IDC_BUTTON_Send, &CDlgBlueToothExc::OnBnClickedButtonSend)
ON_BN_CLICKED(IDC_BUTTON_Clearup, &CDlgBlueToothExc::OnBnClickedButtonClearup)
ON_BN_CLICKED(IDC_BUTTON_Test1, &CDlgBlueToothExc::OnBnClickedButtonTest1)
ON_EN_SETFOCUS(IDC_SENDTEXT, &CDlgBlueToothExc::OnEnSetfocusSendtext)
ON_BN_CLICKED(IDC_BUTTON_Receive, &CDlgBlueToothExc::OnBnClickedButtonReceive)
ON_BN_CLICKED(IDC_BUTTON_Finished, &CDlgBlueToothExc::OnBnClickedButtonFinished)
// ON_LBN_SELCHANGE(IDC_LIST_NearbyBthDev, &CDlgBlueToothExc::OnLbnSelchangeListNearbybthdev)
ON_BN_CLICKED(IDC_CHECK_ReceivedOnTimer, &CDlgBlueToothExc::OnBnClickedCheckReceivedontimer)
ON_WM_TIMER()
END_MESSAGE_MAP()
// CDlgBlueToothExc message handlers
BOOL CDlgBlueToothExc::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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// 初始化控件
( (CButton*)GetDlgItem(IDC_CHECK_ShowRemembered) )->SetCheck ( TRUE );
CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_COM);
ASSERT ( pCombo );
for ( int i=0; i<32; i++ )
{
CString csCom;
csCom.Format ( L"COM%d", i+1 );
pCombo->AddString ( csCom );
}
pCombo->SetCurSel ( 1-1 ); // 默认在控件中显示的端口号
GetDlgItem(IDC_EDIT_Baud)->SetWindowText ( L"115200" );
EnableWindow_ConnCOM ( FALSE );
( (CButton*)GetDlgItem(IDC_CHECK_Hex) )->SetCheck ( TRUE );
// SetDlgItemText ( IDC_EDIT_ReceivedData_Hex, Data2HexString ( (char*)NOTE_Bluetooth, (int)wcslen(NOTE_Bluetooth) ) );
// GetDlgItem(IDC_EDIT_ReceivedData_Hex)->SendMessage ( WM_VSCROLL, (WPARAM)SB_BOTTOM, (LPARAM)NULL ); // 滚动在最底行
// SetDlgItemText ( IDC_EDIT_ReceivedData_String, NOTE_Bluetooth );
// GetDlgItem(IDC_EDIT_ReceivedData_String)->SendMessage ( WM_VSCROLL, (WPARAM)SB_BOTTOM, (LPARAM)NULL ); // 滚动在最底行
SetDlgItemText ( IDC_STATIC_ReceivedData_Hex_Length, _T("") );
SetDlgItemText ( IDC_STATIC_ReceivedData_String_Length, _T("") );
pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_Segment);
ASSERT ( pCombo );
pCombo->SetCurSel ( 0 );
pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_Section);
ASSERT ( pCombo );
pCombo->SetCurSel ( 0 );
// 初始化显示心电数据的框
CDlgDataShow *pDlgDataShow = CDlgDataShow::GetInstance ();
if ( pDlgDataShow )
{
CRect rcClient;
pDlgDataShow->GetWindowRect ( &rcClient );
CSize szArea = GetWorkAreaSize ();
CRect rcDataShow ( 0, 0, szArea.cx, rcClient.Height() );
pDlgDataShow->MoveWindow ( rcDataShow );
CRect rcMainWnd;
CenterWindow ();
GetWindowRect ( &rcMainWnd );
int nMainWndHeight = rcMainWnd.Height();
rcMainWnd.top = rcDataShow.bottom;
rcMainWnd.bottom = rcMainWnd.top + nMainWndHeight;
MoveWindow ( &rcMainWnd );
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CDlgBlueToothExc::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 CDlgBlueToothExc::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDlgBlueToothExc::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CDlgBlueToothExc::OnBnClickedButtonScanbluetoothdevice()
{
// 先确定本地蓝牙设备,如果还没有就查找
t_LocalRadioInfo LocalRadioInfo;
if ( !GetCur_LocalRadioInfo ( LocalRadioInfo ) ) return;
// 搜索附近的蓝牙设备,并添加到列表中
CListBox *pListBox_NearbyBthDev = (CListBox*)GetDlgItem ( IDC_LIST_NearbyBthDev );
ASSERT ( pListBox_NearbyBthDev );
pListBox_NearbyBthDev->ResetContent ();
BOOL fReturnRemembered = ( (CButton*)GetDlgItem(IDC_CHECK_ShowRemembered) )->GetCheck ();
BOOL fIssueInquiry = ( (CButton*)GetDlgItem(IDC_CHECK_IssueInquiry) )->GetCheck ();
int nCount = 0;
if ( ( (CButton*)GetDlgItem(IDC_CHECK_Wizard) )->GetCheck () )
{
nCount = m_BlueTooth.ScanNearbyBthDev_Wizard ( GetSafeHwnd(), LocalRadioInfo.hRadio, _T("我现在正在搜索周边的蓝牙设备,您别打扰我! -- 谢红伟"),
FALSE, TRUE, fReturnRemembered, TRUE, TRUE, TRUE );
}
else
{
nCount = m_BlueTooth.ScanNearbyBthDev ( LocalRadioInfo.hRadio, TRUE, fReturnRemembered, TRUE, TRUE, fIssueInquiry );
}
if ( nCount <= 0 )
{
if ( nCount < 0 ) AfxMessageBox ( _T("Scan nearby bluetooth device failed") );
return;
}
for ( int i=0; i<nCount; i++ )
{
t_RemoteBthDevInfo &RemoteBthDevInfo = m_BlueTooth.m_Ary_RemoteBthDevInfo.GetAt(i);
CString csItemString;
csItemString.Format ( _T("%s ( %s )"), RemoteBthDevInfo.szName, m_BlueTooth.FormatBthAddress ( RemoteBt