DlgBlueToothExc.cpp
上传用户:shmeyee
上传日期:2015-06-28
资源大小:108k
文件大小:20k
源码类别:

通讯编程

开发平台:

Visual C++

  1. // DlgBlueToothExc.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "BlueToothExc.h"
  5. #include "DlgDataShow.h"
  6. #include "DlgBlueToothExc.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. // CAboutDlg dialog used for App About
  11. class CAboutDlg : public CDialog
  12. {
  13. public:
  14. CAboutDlg();
  15. // Dialog Data
  16. enum { IDD = IDD_ABOUTBOX };
  17. protected:
  18. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  19. // Implementation
  20. protected:
  21. DECLARE_MESSAGE_MAP()
  22. };
  23. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  24. {
  25. }
  26. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  27. {
  28. CDialog::DoDataExchange(pDX);
  29. }
  30. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  31. END_MESSAGE_MAP()
  32. // CDlgBlueToothExc dialog
  33. CDlgBlueToothExc::CDlgBlueToothExc(CWnd* pParent /*=NULL*/)
  34. : CDialog(CDlgBlueToothExc::IDD, pParent)
  35. , m_pHwCOM ( NULL )
  36. , m_hThread ( NULL )
  37. , m_dwThreadID ( 0 )
  38. , m_hEvtEndThread ( NULL )
  39. , m_hEvtSendDataReq ( NULL )
  40. , m_nBytesForSending ( 0 )
  41. , m_bNoteBeCleared ( FALSE )
  42. , m_bShowCurve(FALSE)
  43. {
  44. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  45. memset ( m_DataForSending, 0, sizeof(m_DataForSending) );
  46. DeleteFile ( _T("c:\bthdebug.dat") );
  47. }
  48. void CDlgBlueToothExc::DoDataExchange(CDataExchange* pDX)
  49. {
  50. CDialog::DoDataExchange(pDX);
  51. }
  52. BEGIN_MESSAGE_MAP(CDlgBlueToothExc, CDialog)
  53. ON_WM_SYSCOMMAND()
  54. ON_WM_PAINT()
  55. ON_WM_QUERYDRAGICON()
  56. //}}AFX_MSG_MAP
  57. ON_BN_CLICKED(IDC_BUTTON_ScanBluetoothDevice, &CDlgBlueToothExc::OnBnClickedButtonScanbluetoothdevice)
  58. ON_LBN_DBLCLK(IDC_LIST_NearbyBthDev, &CDlgBlueToothExc::OnLbnDblclkListNearbybthdev)
  59. ON_BN_CLICKED(IDC_BUTTON_Test, &CDlgBlueToothExc::OnBnClickedButtonTest)
  60. ON_BN_CLICKED(IDC_BUTTON_Enumerate, &CDlgBlueToothExc::OnBnClickedButtonEnumerate)
  61. ON_BN_CLICKED(IDC_BUTTON_RequestAuthenticate, &CDlgBlueToothExc::OnBnClickedButtonRequestauthenticate)
  62. ON_WM_DESTROY()
  63. ON_BN_CLICKED(IDC_BUTTON_Connect, &CDlgBlueToothExc::OnBnClickedButtonConnect)
  64. ON_BN_CLICKED(IDC_BUTTON_Disconnect, &CDlgBlueToothExc::OnBnClickedButtonDisconnect)
  65. ON_BN_CLICKED(IDC_BUTTON_Send, &CDlgBlueToothExc::OnBnClickedButtonSend)
  66. ON_BN_CLICKED(IDC_BUTTON_Clearup, &CDlgBlueToothExc::OnBnClickedButtonClearup)
  67. ON_BN_CLICKED(IDC_BUTTON_Test1, &CDlgBlueToothExc::OnBnClickedButtonTest1)
  68. ON_EN_SETFOCUS(IDC_SENDTEXT, &CDlgBlueToothExc::OnEnSetfocusSendtext)
  69. ON_BN_CLICKED(IDC_BUTTON_Receive, &CDlgBlueToothExc::OnBnClickedButtonReceive)
  70. ON_BN_CLICKED(IDC_BUTTON_Finished, &CDlgBlueToothExc::OnBnClickedButtonFinished)
  71. // ON_LBN_SELCHANGE(IDC_LIST_NearbyBthDev, &CDlgBlueToothExc::OnLbnSelchangeListNearbybthdev)
  72. ON_BN_CLICKED(IDC_CHECK_ReceivedOnTimer, &CDlgBlueToothExc::OnBnClickedCheckReceivedontimer)
  73. ON_WM_TIMER()
  74. END_MESSAGE_MAP()
  75. // CDlgBlueToothExc message handlers
  76. BOOL CDlgBlueToothExc::OnInitDialog()
  77. {
  78. CDialog::OnInitDialog();
  79. // Add "About..." menu item to system menu.
  80. // IDM_ABOUTBOX must be in the system command range.
  81. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  82. ASSERT(IDM_ABOUTBOX < 0xF000);
  83. CMenu* pSysMenu = GetSystemMenu(FALSE);
  84. if (pSysMenu != NULL)
  85. {
  86. CString strAboutMenu;
  87. strAboutMenu.LoadString(IDS_ABOUTBOX);
  88. if (!strAboutMenu.IsEmpty())
  89. {
  90. pSysMenu->AppendMenu(MF_SEPARATOR);
  91. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  92. }
  93. }
  94. SetIcon(m_hIcon, TRUE); // Set big icon
  95. SetIcon(m_hIcon, FALSE); // Set small icon
  96. // 初始化控件
  97. ( (CButton*)GetDlgItem(IDC_CHECK_ShowRemembered) )->SetCheck ( TRUE );
  98. CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_COM);
  99. ASSERT ( pCombo );
  100. for ( int i=0; i<32; i++ )
  101. {
  102. CString csCom;
  103. csCom.Format ( L"COM%d", i+1 );
  104. pCombo->AddString ( csCom );
  105. }
  106. pCombo->SetCurSel ( 1-1 ); // 默认在控件中显示的端口号
  107. GetDlgItem(IDC_EDIT_Baud)->SetWindowText ( L"115200" );
  108. EnableWindow_ConnCOM ( FALSE );
  109. ( (CButton*)GetDlgItem(IDC_CHECK_Hex) )->SetCheck ( TRUE );
  110. // SetDlgItemText ( IDC_EDIT_ReceivedData_Hex, Data2HexString ( (char*)NOTE_Bluetooth, (int)wcslen(NOTE_Bluetooth) ) );
  111. // GetDlgItem(IDC_EDIT_ReceivedData_Hex)->SendMessage ( WM_VSCROLL, (WPARAM)SB_BOTTOM, (LPARAM)NULL ); // 滚动在最底行
  112. // SetDlgItemText ( IDC_EDIT_ReceivedData_String, NOTE_Bluetooth );
  113. // GetDlgItem(IDC_EDIT_ReceivedData_String)->SendMessage ( WM_VSCROLL, (WPARAM)SB_BOTTOM, (LPARAM)NULL ); // 滚动在最底行
  114. SetDlgItemText ( IDC_STATIC_ReceivedData_Hex_Length, _T("") );
  115. SetDlgItemText ( IDC_STATIC_ReceivedData_String_Length, _T("") );
  116. pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_Segment);
  117. ASSERT ( pCombo );
  118. pCombo->SetCurSel ( 0 );
  119. pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_Section);
  120. ASSERT ( pCombo );
  121. pCombo->SetCurSel ( 0 );
  122. // 初始化显示心电数据的框
  123. CDlgDataShow *pDlgDataShow = CDlgDataShow::GetInstance ();
  124. if ( pDlgDataShow )
  125. {
  126. CRect rcClient;
  127. pDlgDataShow->GetWindowRect ( &rcClient );
  128. CSize szArea = GetWorkAreaSize ();
  129. CRect rcDataShow ( 0, 0, szArea.cx, rcClient.Height() );
  130. pDlgDataShow->MoveWindow ( rcDataShow );
  131. CRect rcMainWnd;
  132. CenterWindow ();
  133. GetWindowRect ( &rcMainWnd );
  134. int nMainWndHeight = rcMainWnd.Height();
  135. rcMainWnd.top = rcDataShow.bottom;
  136. rcMainWnd.bottom = rcMainWnd.top + nMainWndHeight;
  137. MoveWindow ( &rcMainWnd );
  138. }
  139. return TRUE;  // return TRUE  unless you set the focus to a control
  140. }
  141. void CDlgBlueToothExc::OnSysCommand(UINT nID, LPARAM lParam)
  142. {
  143. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  144. {
  145. CAboutDlg dlgAbout;
  146. dlgAbout.DoModal();
  147. }
  148. else
  149. {
  150. CDialog::OnSysCommand(nID, lParam);
  151. }
  152. }
  153. // If you add a minimize button to your dialog, you will need the code below
  154. //  to draw the icon.  For MFC applications using the document/view model,
  155. //  this is automatically done for you by the framework.
  156. void CDlgBlueToothExc::OnPaint()
  157. {
  158. if (IsIconic())
  159. {
  160. CPaintDC dc(this); // device context for painting
  161. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  162. // Center icon in client rectangle
  163. int cxIcon = GetSystemMetrics(SM_CXICON);
  164. int cyIcon = GetSystemMetrics(SM_CYICON);
  165. CRect rect;
  166. GetClientRect(&rect);
  167. int x = (rect.Width() - cxIcon + 1) / 2;
  168. int y = (rect.Height() - cyIcon + 1) / 2;
  169. // Draw the icon
  170. dc.DrawIcon(x, y, m_hIcon);
  171. }
  172. else
  173. {
  174. CDialog::OnPaint();
  175. }
  176. }
  177. // The system calls this function to obtain the cursor to display while the user drags
  178. //  the minimized window.
  179. HCURSOR CDlgBlueToothExc::OnQueryDragIcon()
  180. {
  181. return static_cast<HCURSOR>(m_hIcon);
  182. }
  183. void CDlgBlueToothExc::OnBnClickedButtonScanbluetoothdevice()
  184. {
  185. // 先确定本地蓝牙设备,如果还没有就查找
  186. t_LocalRadioInfo LocalRadioInfo;
  187. if ( !GetCur_LocalRadioInfo ( LocalRadioInfo ) ) return;
  188. // 搜索附近的蓝牙设备,并添加到列表中
  189. CListBox *pListBox_NearbyBthDev = (CListBox*)GetDlgItem ( IDC_LIST_NearbyBthDev );
  190. ASSERT ( pListBox_NearbyBthDev );
  191. pListBox_NearbyBthDev->ResetContent ();
  192. BOOL fReturnRemembered = ( (CButton*)GetDlgItem(IDC_CHECK_ShowRemembered) )->GetCheck ();
  193. BOOL fIssueInquiry = ( (CButton*)GetDlgItem(IDC_CHECK_IssueInquiry) )->GetCheck ();
  194. int nCount = 0;
  195. if ( ( (CButton*)GetDlgItem(IDC_CHECK_Wizard) )->GetCheck () )
  196. {
  197. nCount = m_BlueTooth.ScanNearbyBthDev_Wizard ( GetSafeHwnd(), LocalRadioInfo.hRadio, _T("我现在正在搜索周边的蓝牙设备,您别打扰我! -- 谢红伟"),
  198. FALSE, TRUE, fReturnRemembered, TRUE, TRUE, TRUE );
  199. }
  200. else
  201. {
  202. nCount = m_BlueTooth.ScanNearbyBthDev ( LocalRadioInfo.hRadio, TRUE, fReturnRemembered, TRUE, TRUE, fIssueInquiry );
  203. }
  204. if ( nCount <= 0 )
  205. {
  206. if ( nCount < 0 ) AfxMessageBox ( _T("Scan nearby bluetooth device failed") );
  207. return;
  208. }
  209. for ( int i=0; i<nCount; i++ )
  210. {
  211. t_RemoteBthDevInfo &RemoteBthDevInfo = m_BlueTooth.m_Ary_RemoteBthDevInfo.GetAt(i);
  212. CString csItemString;
  213. csItemString.Format ( _T("%s ( %s )"), RemoteBthDevInfo.szName, m_BlueTooth.FormatBthAddress ( RemoteBthDevInfo.Address.rgBytes ) );
  214. pListBox_NearbyBthDev->AddString ( csItemString );
  215. }
  216. }
  217. void CDlgBlueToothExc::OnLbnDblclkListNearbybthdev()
  218. {
  219. int nSel = GetListCurSel ( IDC_LIST_NearbyBthDev, _T("Please select remote bluetooth device") );
  220. if ( nSel < 0  ) return;
  221. m_BlueTooth.ShowPropertySheet ( nSel, GetSafeHwnd() );
  222. }
  223. void CDlgBlueToothExc::OnBnClickedButtonTest()
  224. {
  225. CString csFileName = L"C:\UHolter_multi\3.txt";
  226. int nFileSize = hwGetFileAttr ( csFileName );
  227. if ( nFileSize < 1 ) return;
  228. char* strdata = new char[nFileSize];
  229. if ( !strdata ) return;
  230. char *data = new char[nFileSize];
  231. wchar_t *wdata = new wchar_t[nFileSize*2];
  232. int nReadBytes = ReadDataFromFile ( csFileName, strdata, nFileSize );
  233. if ( nReadBytes > 1 )
  234. {
  235. MultiByteToWideChar(CP_ACP, 0, (char*)strdata, nReadBytes, wdata, 2*nFileSize);
  236. int nDataSize = ConvStringToHexData ( wdata, (BYTE*)data, nFileSize );
  237. WriteDataToFile ( L"C:\UHolter_multi\data.dat", data, nDataSize, L"wb" );
  238. }
  239. // m_BlueTooth.Test ();
  240. }
  241. void CDlgBlueToothExc::OnBnClickedButtonEnumerate()
  242. {
  243. CListBox *pListBox = (CListBox*)GetDlgItem ( IDC_LIST_LocalBluetoothRadios );
  244. ASSERT ( pListBox );
  245. pListBox->ResetContent ();
  246. int nCount = m_BlueTooth.EnumerateLocalRadios ();
  247. if ( nCount <= 0 )
  248. {
  249. AfxMessageBox ( _T("None any local bluetooth radios") );
  250. return;
  251. }
  252. for ( int i=0; i<nCount; i++ )
  253. {
  254. t_LocalRadioInfo &LocalRadioInfo = m_BlueTooth.m_Ary_LocalRadioInfo.GetAt(i);
  255. CString csItemString;
  256. csItemString.Format ( _T("%s ( %s )"), LocalRadioInfo.RadioInfo.szName, m_BlueTooth.FormatBthAddress ( LocalRadioInfo.RadioInfo.address.rgBytes ) );
  257. pListBox->AddString ( csItemString );
  258. }
  259. }
  260. void CDlgBlueToothExc::OnBnClickedButtonRequestauthenticate()
  261. {
  262. // 先确定本地蓝牙设备,如果还没有就查找
  263. t_LocalRadioInfo LocalRadioInfo;
  264. if ( !GetCur_LocalRadioInfo ( LocalRadioInfo ) ) return;
  265. // 当前选择的远程蓝牙设备
  266. int nSel = GetListCurSel ( IDC_LIST_NearbyBthDev, _T("Please select remote bluetooth device") );
  267. if ( nSel < 0  ) return;
  268. DWORD result = ERROR_SUCCESS;
  269. if ( !m_BlueTooth.RequestAuthenticateDevice ( nSel, GetSafeHwnd(), &result ) )
  270. {
  271. CString csMsg;
  272. csMsg.Format ( _T("Authenticate failed using passkey [%s] : %s"), AUTHENTICATION_PASSKEY, hwFormatMessage(result) );
  273. AfxMessageBox ( csMsg );
  274. }
  275. }
  276. BOOL CDlgBlueToothExc::GetCur_LocalRadioInfo(t_LocalRadioInfo& LocalRadioInfo)
  277. {
  278. // 先确定本地蓝牙设备,如果还没有就查找
  279. CListBox *pListBox_LocalBluetoothRadios = (CListBox*)GetDlgItem ( IDC_LIST_LocalBluetoothRadios );
  280. ASSERT ( pListBox_LocalBluetoothRadios );
  281. int nSel_LocalBluetoothRadios = 0;
  282. if ( m_BlueTooth.m_Ary_LocalRadioInfo.GetSize() < 1 )
  283. {
  284. OnBnClickedButtonEnumerate ();
  285. if ( m_BlueTooth.m_Ary_LocalRadioInfo.GetSize() < 1 )
  286. {
  287. return FALSE;
  288. }
  289. pListBox_LocalBluetoothRadios->SetCurSel ( nSel_LocalBluetoothRadios );
  290. }
  291. nSel_LocalBluetoothRadios = pListBox_LocalBluetoothRadios->GetCurSel ();
  292. if ( nSel_LocalBluetoothRadios < 0 || nSel_LocalBluetoothRadios >= pListBox_LocalBluetoothRadios->GetCount() )
  293. {
  294. nSel_LocalBluetoothRadios = 0;
  295. pListBox_LocalBluetoothRadios->SetCurSel ( nSel_LocalBluetoothRadios );
  296. }
  297. ASSERT ( m_BlueTooth.m_Ary_LocalRadioInfo.GetSize() == pListBox_LocalBluetoothRadios->GetCount() );
  298. t_LocalRadioInfo& LocalRadioInfo_Ref = m_BlueTooth.m_Ary_LocalRadioInfo.GetAt(nSel_LocalBluetoothRadios);
  299. memcpy ( &LocalRadioInfo, &LocalRadioInfo_Ref, sizeof(t_LocalRadioInfo) );
  300. return TRUE;
  301. }
  302. int CDlgBlueToothExc::GetListCurSel(UINT nID, LPCTSTR lpszMsg)
  303. {
  304. CListBox *pListBox = (CListBox*)GetDlgItem ( nID );
  305. if ( !pListBox ) return -1;
  306. int nSel = pListBox->GetCurSel ();
  307. if ( nSel < 0 || nSel >= pListBox->GetCount() )
  308. {
  309. if ( lpszMsg ) AfxMessageBox ( lpszMsg );
  310. return -1;
  311. }
  312. return nSel;
  313. }
  314. void CDlgBlueToothExc::OnDestroy()
  315. {
  316. CDialog::OnDestroy();
  317. CDlgDataShow::DeleteInstance ();
  318. ExitCOMThread ();
  319. SAFE_CLOSE_HANDLE ( m_hEvtEndThread );
  320. SAFE_CLOSE_HANDLE ( m_hEvtSendDataReq );
  321. DeleteCOMObject ();
  322. }
  323. void CDlgBlueToothExc::DeleteCOMObject(void)
  324. {
  325. if ( m_pHwCOM )
  326. {
  327. delete m_pHwCOM;
  328. m_pHwCOM = NULL;
  329. }
  330. }
  331. DWORD WINAPI ThreadProc_COM(
  332.   LPVOID lpParameter   // thread data
  333. )
  334. {
  335. CDlgBlueToothExc *pDlgBlueToothExc = reinterpret_cast<CDlgBlueToothExc*>(lpParameter);
  336. if ( pDlgBlueToothExc )
  337. pDlgBlueToothExc->ThreadProc_COM ();
  338. return TRUE;
  339. }
  340. void CDlgBlueToothExc::OnBnClickedButtonConnect()
  341. {
  342. CWaitCursor WaitCurSor;
  343. CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_COM);
  344. ASSERT ( pCombo );
  345. int nSel = pCombo->GetCurSel ();
  346. if ( nSel < 0 || nSel >= pCombo->GetCount () )
  347. {
  348. AfxMessageBox ( L"Please select COM" );
  349. return;
  350. }
  351. int nCOM = nSel + 1;
  352. int nBaudRate = (int)GetDlgItemInt ( IDC_EDIT_Baud, NULL, FALSE );
  353. DeleteCOMObject ();
  354. m_pHwCOM = new CHwCOM;
  355. if ( !m_pHwCOM ) return;
  356. if ( !m_pHwCOM->OpenCOM ( nCOM, nBaudRate, TRUE ) )
  357. {
  358. DeleteCOMObject ();
  359. CString csMsg;
  360. csMsg.Format ( L"Open COM%d failed : %s", nCOM, hwFormatMessage ( GetLastError() ) );
  361. AfxMessageBox ( csMsg );
  362. return;
  363. }
  364. m_pHwCOM->SetCommMask ( EV_RXCHAR );
  365. ExitCOMThread ();
  366. if ( !HANDLE_IS_VALID(m_hEvtEndThread) )
  367. m_hEvtEndThread = ::CreateEvent ( NULL, TRUE, FALSE, NULL );
  368. if ( !HANDLE_IS_VALID(m_hEvtSendDataReq) )
  369. m_hEvtSendDataReq = ::CreateEvent ( NULL, FALSE, FALSE, NULL );
  370. if ( !HANDLE_IS_VALID(m_hThread) )
  371. m_hThread = ::CreateThread ( NULL, 0, ::ThreadProc_COM, this, 0, &m_dwThreadID );
  372. if ( !HANDLE_IS_VALID(m_hThread) || !HANDLE_IS_VALID(m_hEvtEndThread) || !HANDLE_IS_VALID(m_hEvtSendDataReq) )
  373. return;
  374. GetDlgItem(IDC_BUTTON_Connect)->EnableWindow ( FALSE );
  375. EnableWindow_ConnCOM ( TRUE );
  376. }
  377. //
  378. // 数据处理的线程
  379. //
  380. void CDlgBlueToothExc::ThreadProc_COM(void)
  381. {
  382. HANDLE hAryEvt[] = { m_hEvtEndThread, m_hEvtSendDataReq };
  383. while ( TRUE )
  384. {
  385. DWORD dwRet = ::WaitForMultipleObjects ( LENGTH(hAryEvt), hAryEvt, FALSE, 100 ) - WAIT_OBJECT_0;
  386. // 请求线程终止
  387. if ( dwRet == 0 )
  388. return;
  389. // 发送数据到串口
  390. else if ( dwRet == 1 )
  391. {
  392. if ( m_pHwCOM )
  393. {
  394. m_CSFor_DataForSending.Lock();
  395. char DataForSending[MAX_COM_BUFSIZE] = {0};
  396. int nBytesForSending = m_nBytesForSending;
  397. memcpy ( DataForSending, m_DataForSending, nBytesForSending );
  398. memset ( m_DataForSending, 0, sizeof(m_DataForSending) );
  399. m_nBytesForSending = 0;
  400. m_CSFor_DataForSending.Unlock ();
  401. if ( m_pHwCOM->Write ( DataForSending, nBytesForSending ) != nBytesForSending )
  402. {
  403. AfxMessageBox ( _T("Send data failed") );
  404. }
  405. }
  406. }
  407. // 从串口读取数据
  408. else
  409. {
  410. ReadDataAndDisplay ();
  411. }
  412. }
  413. }
  414. void CDlgBlueToothExc::ExitCOMThread(void)
  415. {
  416. if ( HANDLE_IS_VALID(m_hEvtEndThread) )
  417. ::SetEvent ( m_hEvtEndThread );
  418. WaitForThreadEnd ( &m_hThread );
  419. }
  420. void CDlgBlueToothExc::OnBnClickedButtonDisconnect()
  421. {
  422. ExitCOMThread ();
  423. DeleteCOMObject ();
  424. GetDlgItem(IDC_BUTTON_Connect)->EnableWindow ( TRUE );
  425. EnableWindow_ConnCOM ( FALSE );
  426. }
  427. void CDlgBlueToothExc::ReadDataAndDisplay(void)
  428. {
  429. BYTE data[MAX_COM_BUFSIZE] = {0};
  430. if ( !m_pHwCOM ) return;
  431. DWORD dwEvtMask = 0;
  432. int nReadBytes = m_pHwCOM->Read ( (char*)data, sizeof(data)-1 );
  433. if ( nReadBytes > 0 )
  434. {
  435. // 将收到的数据保存到临时文件
  436. WriteDataToFile ( _T("c:\bthdebug.dat"), (char*)data, nReadBytes, _T("ab+") );
  437. // 将数据显示成曲线
  438. if ( m_bShowCurve && nReadBytes > 20 )
  439. {
  440. CDlgDataShow *pDlgDataShow = CDlgDataShow::GetInstance ();
  441. if ( pDlgDataShow )
  442. {
  443. pDlgDataShow->DrawDataCurve ( data, nReadBytes );
  444. }
  445. m_bShowCurve = FALSE;
  446. }
  447. CEdit *pEdit_Hex = (CEdit*)GetDlgItem(IDC_EDIT_ReceivedData_Hex);
  448. ASSERT ( pEdit_Hex );
  449. if ( !m_bNoteBeCleared ) pEdit_Hex->SetWindowText ( _T("") );
  450. pEdit_Hex->SetSel ( -1, -1 );
  451. pEdit_Hex->ReplaceSel ( Data2HexString((char*)data,nReadBytes) );
  452. pEdit_Hex->SetSel ( -1, -1 );
  453. pEdit_Hex->ReplaceSel ( L"rn" );
  454. CalcAndShowDataLength ( IDC_EDIT_ReceivedData_Hex, IDC_STATIC_ReceivedData_Hex_Length, TRUE );
  455. CEdit *pEdit_String = (CEdit*)GetDlgItem(IDC_EDIT_ReceivedData_String);
  456. ASSERT ( pEdit_String );
  457. if ( !m_bNoteBeCleared ) pEdit_String->SetWindowText ( _T("") );
  458. pEdit_String->SetSel ( -1, -1 );
  459. data[nReadBytes] = '';
  460. CString csData;
  461. #ifdef UNICODE
  462. WCHAR wchar[MAX_COM_BUFSIZE] = {0}; // UNICODE串缓冲区
  463. MultiByteToWideChar(CP_ACP, 0, (char*)data, nReadBytes, wchar, MAX_COM_BUFSIZE);
  464. csData.Format ( _T("%s"), wchar );
  465. #else
  466. csData = (LPCTSTR)data;
  467. #endif
  468. pEdit_String->ReplaceSel ( csData );
  469. pEdit_String->SetSel ( -1, -1 );
  470. pEdit_String->ReplaceSel ( L"rn" );
  471. m_bNoteBeCleared = TRUE;
  472. }
  473. }
  474. void CDlgBlueToothExc::OnBnClickedButtonSend()
  475. {
  476. if ( !HANDLE_IS_VALID(m_hEvtSendDataReq) ) return;
  477. CString csData;
  478. GetDlgItemText ( IDC_SENDTEXT, csData );
  479. if ( csData.IsEmpty() ) return;
  480. if ( ( (CButton*)GetDlgItem(IDC_CHECK_Hex) )->GetCheck () )
  481. {
  482. m_nBytesForSending = ConvStringToHexData ( csData, (BYTE*)m_DataForSending, (int)sizeof(m_DataForSending) );
  483. }
  484. else
  485. {
  486. #ifdef UNICODE
  487. m_nBytesForSending = WideCharToMultiByte ( CP_ACP, 0, csData, csData.GetLength(), (LPSTR)m_DataForSending, sizeof(m_DataForSending), NULL, NULL );
  488. #else
  489. m_nBytesForSending = _sntprintf ( m_DataForSending, sizeof(m_DataForSending), "%s", csData );
  490. #endif
  491. }
  492. SendData ( m_DataForSending, m_nBytesForSending );
  493. }
  494. void CDlgBlueToothExc::OnBnClickedButtonClearup()
  495. {
  496. CEdit *pEdit_Hex = (CEdit*)GetDlgItem(IDC_EDIT_ReceivedData_Hex);
  497. ASSERT ( pEdit_Hex );
  498. pEdit_Hex->SetWindowText ( _T("") );
  499. CEdit *pEdit_String = (CEdit*)GetDlgItem(IDC_EDIT_ReceivedData_String);
  500. ASSERT ( pEdit_String );
  501. pEdit_String->SetWindowText ( _T("") );
  502. CalcAndShowDataLength ( IDC_EDIT_ReceivedData_Hex, IDC_STATIC_ReceivedData_Hex_Length, TRUE );
  503. }
  504. void CDlgBlueToothExc::CalcAndShowDataLength(UINT nEditID, UINT nStaticID, bool bHex)
  505. {
  506. CString csData;
  507. GetDlgItemText ( nEditID, csData );
  508. csData.Trim ();
  509. csData.Replace ( _T("rn"), _T("") );
  510. csData.Replace ( _T("r"), _T("") );
  511. csData.Replace ( _T("n"), _T("") );
  512. int nLength = csData.GetLength ();
  513. if ( bHex )
  514. {
  515. nLength = (nLength+1)/3;
  516. }
  517. CString csLength;
  518. csLength.Format ( _T("%d byte(s)"), nLength );
  519. SetDlgItemText ( nStaticID, csLength );
  520. }
  521. void CDlgBlueToothExc::OnBnClickedButtonTest1()
  522. {
  523. CDlgDataShow *pDlgDataShow = CDlgDataShow::GetInstance ();
  524. // static int nPos = 0;
  525. // nPos += 10;
  526. // pDlgDataShow->m_staticCurve.SetCurStartPos ( nPos );
  527. pDlgDataShow->m_staticCurve.Dynamic ( 200 );
  528. }
  529. void CDlgBlueToothExc::OnEnSetfocusSendtext()
  530. {
  531. CEdit *pEdit = (CEdit*)GetDlgItem(IDC_SENDTEXT);
  532. ASSERT ( pEdit );
  533. pEdit->SetSel ( 0, -1 );
  534. }
  535. void CDlgBlueToothExc::OnBnClickedButtonReceive()
  536. {
  537. CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_Segment);
  538. ASSERT ( pCombo );
  539. int nSegment = pCombo->GetCurSel ();
  540. pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_Section);
  541. ASSERT ( pCombo );
  542. int nSection = pCombo->GetCurSel ();
  543. BYTE data[] = {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0x03,0xA3,0x00,0x01};
  544. data[LENGTH(data)-1] = nSegment*4+nSection+1;
  545. m_bShowCurve = TRUE;
  546. SendData ( data, LENGTH(data) );
  547. }
  548. BOOL CDlgBlueToothExc::SendData(BYTE* data, int size)
  549. {
  550. if ( !data || size<1 ) return FALSE;
  551. m_CSFor_DataForSending.Lock ();
  552. m_nBytesForSending = size;
  553. memcpy ( m_DataForSending, data, size );
  554. m_CSFor_DataForSending.Unlock ();
  555. ::SetEvent ( m_hEvtSendDataReq );
  556. return TRUE;
  557. }
  558. void CDlgBlueToothExc::OnBnClickedButtonFinished()
  559. {
  560. BYTE data[] = {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0x01,0xA7};
  561. SendData ( data, LENGTH(data) );
  562. }
  563. void CDlgBlueToothExc::EnableWindow_ConnCOM(bool bEnable)
  564. {
  565. GetDlgItem(IDC_BUTTON_Disconnect)->EnableWindow ( bEnable );
  566. GetDlgItem(IDC_BUTTON_Receive)->EnableWindow ( bEnable );
  567. GetDlgItem(IDC_CHECK_ReceivedOnTimer)->EnableWindow ( bEnable );
  568. GetDlgItem(IDC_BUTTON_Finished)->EnableWindow ( bEnable );
  569. GetDlgItem(IDC_COMBO_Segment)->EnableWindow ( bEnable );
  570. GetDlgItem(IDC_COMBO_Section)->EnableWindow ( bEnable );
  571. GetDlgItem(IDC_SENDTEXT)->EnableWindow ( bEnable );
  572. GetDlgItem(IDC_CHECK_Hex)->EnableWindow ( bEnable );
  573. GetDlgItem(IDC_BUTTON_Send)->EnableWindow ( bEnable );
  574. }
  575. //void CDlgBlueToothExc::OnLbnSelchangeListNearbybthdev()
  576. //{
  577. // // TODO: 在此添加控件通知处理程序代码
  578. //}
  579. void CDlgBlueToothExc::OnBnClickedCheckReceivedontimer()
  580. {
  581. if ( ((CButton*)GetDlgItem(IDC_CHECK_ReceivedOnTimer))->GetCheck() )
  582. {
  583. SetTimer ( 1, 300, NULL );
  584. }
  585. else
  586. {
  587. KillTimer ( 1 );
  588. }
  589. }
  590. void CDlgBlueToothExc::OnTimer(UINT_PTR nIDEvent)
  591. {
  592. if ( nIDEvent == 1 )
  593. {
  594. PostMessage ( WM_COMMAND, IDC_BUTTON_Receive );
  595. }
  596. CDialog::OnTimer(nIDEvent);
  597. }