BTserial C++ Code
Microsoft eMbedded Visual C++ (Application Development for Pocket PC)
MFC Dialog-based Application
Go back to the
Handheld Ultrasound Imaging Device
// BTserialDlg.cpp : implementation file #include "stdafx.h" #include "BTserial.h" #include "BTserialDlg.h" #include
using std::string; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CBTserialDlg dialog CBTserialDlg::CBTserialDlg(CWnd* pParent /*=NULL*/) : CDialog(CBTserialDlg::IDD, pParent) { //{{AFX_DATA_INIT(CBTserialDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CBTserialDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CBTserialDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CBTserialDlg, CDialog) //{{AFX_MSG_MAP(CBTserialDlg) ON_BN_CLICKED(IDC_BUTTON1, OnButtonRx) ON_BN_CLICKED(IDC_BUTTON2, OnButtonTx) ON_BN_CLICKED(IDC_BUTTON3, InitBTconnections) ON_BN_CLICKED(IDC_BUTTON4, CloseBT) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CBTserialDlg message handlers BOOL CBTserialDlg::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 return TRUE; // return TRUE unless you set the focus to a control } void CBTserialDlg::OnButtonRx() { DWORD iBytesRead; BOOL bReadRC; char sBuffer[128] = ""; // Read some Bytes! PrintDebug(_T("Reading From COM port")); bReadRC = ReadFile(m_hComTx, &sBuffer, 128, &iBytesRead, NULL); //&sBuffer PrintLastErrorDebug(); //PrintDebug(_T("BytesRead=")); //PrintDebugChars(sBuffer); char * ASCI_BytesRead = ""; //_itoa((int)iBytesRead, tester, 10); PrintDebug(_T("Bytes Read")); string newdata = sBuffer; string newdata_sub = newdata.substr(0,(int)iBytesRead); PrintDebugChars((char *)newdata_sub.c_str()); // newdata_sub is a string that only contains the new valid // information received from the BT read. PrintDebug(_T("\r\n")); } void CBTserialDlg::OnButtonTx() { DWORD iBytesWritten; BOOL bWriteRC; // Write some bytes! // the form: return = WriteFile(Handle, bytes, (# of bytes), &iBytesWritten, NULL); PrintDebug(_T("Writting to COM port")); bWriteRC = WriteFile(m_hComTx, "Writing To Bluetooth!",21,&iBytesWritten,NULL); PrintLastErrorDebug(); } void CBTserialDlg::PrintDebug(LPCTSTR debug_info) { CWnd* pWnd = GetDlgItem(IDC_DEBUG_TEXT); CString old_text; pWnd->GetWindowText(old_text); pWnd->SetWindowText(old_text + " - " + debug_info); pWnd->UpdateWindow(); SetLastError((DWORD) 0); } void CBTserialDlg::PrintLastErrorDebug() { if(GetLastError() != (DWORD)0){ LPTSTR lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); CWnd* pWnd = GetDlgItem(IDC_DEBUG_TEXT); CString old_text; pWnd->GetWindowText(old_text); pWnd->SetWindowText(old_text + " - err::" + lpMsgBuf); pWnd->UpdateWindow(); LocalFree( lpMsgBuf ); // free the memory SetLastError((DWORD) 0); // Clear out error so we won't keep printing old messages } else{ PrintDebug(_T("ok\r\n")); } } void CBTserialDlg::InitBTconnections() { // Init Bluetooth Tx PrintDebug(_T("Opening Bluetooth Tx and Rx\r\n")); // variables used with the com port BOOL m_bPortReady; //HANDLE m_hCom; CString m_sComPort; DCB m_dcb; COMMTIMEOUTS m_CommTimeouts; BOOL bWriteRC; BOOL bReadRC; DWORD iBytesWritten; DWORD iBytesRead; char sBuffer[128]; SetLastError((DWORD) 0); // clear out any error messages. // Pocket PC's Bluetooth Rx on COM8 and Tx on COM7 // // --- COM7 can be opened for Read AND Write access to the BT module --- // m_sComPort = _T("COM7:"); //Notice the strange form!! PrintDebug(_T("Opening:" + m_sComPort)); m_hComTx = CreateFile(m_sComPort, //BT: Read on COM7 and Write to COM8 GENERIC_READ | GENERIC_WRITE, 0, // was 0 --> FILE_SHARE_READ | FILE_SHARE_WRITE NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, //FILE_ATTRIBUTE_NORMAL NULL); PrintLastErrorDebug(); // shows access is denied, but still allows write.. hmm.. PrintDebug(_T("Setting COM buffer sizes")); //Check Returned Handle for INVALID_HANDLE_VALUE and then write m_bPortReady = SetupComm(m_hComTx, 128, 128); // set buffer sizes PrintLastErrorDebug(); //Port settings are specified in a Data Communication Block (DCB). //The easiest way to initialize a DCB is to call GetCommState to //fill in its default values, override the values that you want to //change and then call SetCommState to set the values. m_bPortReady = GetCommState(m_hComTx, &m_dcb); m_dcb.BaudRate = 115200; m_dcb.ByteSize = 8; m_dcb.Parity = NOPARITY; m_dcb.StopBits = ONESTOPBIT; m_dcb.fAbortOnError = TRUE; PrintDebug(_T("Setting COM port DCB(baud/etc)")); m_bPortReady = SetCommState(m_hComTx, &m_dcb); // Set it! PrintLastErrorDebug(); //Communication timeouts are optional but can be set similarly to DCB values: m_bPortReady = GetCommTimeouts(m_hComTx, &m_CommTimeouts); m_CommTimeouts.ReadIntervalTimeout = 10; // was 50 m_CommTimeouts.ReadTotalTimeoutConstant = 10; // was 50 m_CommTimeouts.ReadTotalTimeoutMultiplier = 10; m_CommTimeouts.WriteTotalTimeoutConstant = 50; m_CommTimeouts.WriteTotalTimeoutMultiplier = 10; PrintDebug(_T("Setting Up COM timeouts")); m_bPortReady = SetCommTimeouts (m_hComTx, &m_CommTimeouts); PrintLastErrorDebug(); /*// Init Bluetooth Rx PrintDebug(_T("Opening Bluetooth Rx")); SetLastError((DWORD) 0); // clear out any error messages. // Pocket PC's Bluetooth Rx on COM8 and Tx on COM7 m_sComPort = _T("COM8:"); //Notice the strange form!! PrintDebug(_T("Opening:" + m_sComPort)); m_hComRx = CreateFile(m_sComPort, //BT: Read on COM7 and Write to COM8 GENERIC_READ, // GENERIC_READ | GENERIC_WRITE 0, // was 0 --> FILE_SHARE_READ | FILE_SHARE_WRITE NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, //FILE_ATTRIBUTE_NORMAL NULL); PrintLastErrorDebug(); // shows access is denied, but still allows write.. hmm.. PrintDebug(_T("Setting Up COM port")); //Check Returned Handle for INVALID_HANDLE_VALUE and then write m_bPortReady = SetupComm(m_hComRx, 128, 128); // set buffer sizes PrintLastErrorDebug(); //Port settings are specified in a Data Communication Block (DCB). //The easiest way to initialize a DCB is to call GetCommState to //fill in its default values, override the values that you want to //change and then call SetCommState to set the values. m_bPortReady = GetCommState(m_hComRx, &m_dcb); m_dcb.BaudRate = 115200; m_dcb.ByteSize = 8; m_dcb.Parity = NOPARITY; m_dcb.StopBits = ONESTOPBIT; m_dcb.fAbortOnError = TRUE; m_bPortReady = SetCommState(m_hComRx, &m_dcb); // Set it! //Communication timeouts are optional but can be set similarly to DCB values: m_bPortReady = GetCommTimeouts(m_hComRx, &m_CommTimeouts); m_CommTimeouts.ReadIntervalTimeout = 50; m_CommTimeouts.ReadTotalTimeoutConstant = 50; m_CommTimeouts.ReadTotalTimeoutMultiplier = 10; m_CommTimeouts.WriteTotalTimeoutConstant = 50; m_CommTimeouts.WriteTotalTimeoutMultiplier = 10; m_bPortReady = SetCommTimeouts (m_hComRx, &m_CommTimeouts); */ // Rx portion of code not needed since Tx COM port allows both. (hmmm...) PrintDebug(_T("COM ports now open\r\n")); return; } void CBTserialDlg::PrintDebugChars(char *info) { int c = 0; c = MultiByteToWideChar(CP_UTF8,0,info, -1,NULL,0); TCHAR * uChars = new TCHAR[c]; MultiByteToWideChar(CP_UTF8, 0, info, -1, uChars, c); PrintDebug((LPCTSTR)uChars); } void CBTserialDlg::CloseBT() { //Close the Serial Communication Handle... //(This closes the BT connection, don't do this for now) PrintDebug(_T("Closing COM ports and BT connection.")); CloseHandle(m_hComTx); PrintLastErrorDebug(); //CloseHandle(m_hComRx); //PrintLastErrorDebug(); }
Adam Kumpf @ MIT
Cambridge, MA 02142
e-mail:
kumpf@mit.edu