实验23 实现应用程序和流接口驱动的通信.doc
实验2.3实现应用程序和流接口驱动的通信一、在Visual Studio 2005中”File->New->Project”新建一个工程。二、在“Project types”中选择“Smart Device”,在“Templates”中选择“MFC Smart Device Application”,在”Name”中输入工程名称:DriverTest.再点击“OK”。三、选择正确的SDK:XSBase270.四、在应用程序类型中,选择“Dialog based”,其他采用默认。五、在”Generated classes”中选择“CDriverTestDlg”。再点击“Finish”。六、在”Visual Studio”中选择“Resource View”,打开“Dialog”,双击“IDD_DRIVERTEST_DIALOG“,删除掉“DriverTest”窗口中的“TODO:Place dialog control here”。七、在“DriverTest”窗口中五个“Button”控件。八、在按钮控件中右击,打属性窗口。九、把各个按钮控件的名称改为如下图所示。十、右击“Open”控件,选择“Add Event Handler”添加事件句柄。十一、在”Message type”中选择“BN_CLICKED”,在“Clash lish”中选择“CDriverTestDlg”. 在“Function handle name”把名称修改为“OnBnClickedOpen”.再点击“Add and Edit”。十二、在控件关联的函数中就可以来实现控件的现实功能。十三、按相同的步骤添加其他四个控件的事件句柄。十四、把下列的函数实现复制到工程中相应的函数中。void CDriverAppDlg:OnBnClickedOpen()/ Open the stings driver/-hStr = CreateFile ( TEXT ( "STR1:" ), GENERIC_READ | GENERIC_WRITE, 0 , NULL, OPEN_EXISTING, 0 ,0 );if ( INVALID_HANDLE_VALUE = hStr ):MessageBox( NULL, _T("Cannot open STR1:"), _T( "StringApp" ), MB_OK);else :MessageBox( NULL, _T("Open STR1:Succeed"), _T( "StringApp" ), MB_OK);void CDriverAppDlg:OnBnClickedIOControl():DeviceIoControl (hStr, NULL, NULL, NULL, NULL, NULL, NULL, NULL);void CDriverAppDlg:OnBnClickedRead()/ Read a string from the driver/-WCHAR wchBUFFER_SIZE;DWORD dwBytesRead = BUFFER_SIZE;memset(&wch, '0', BUFFER_SIZE*sizeof ( WCHAR ) );:ReadFile(hStr, wch, sizeof ( wch ), &dwBytesRead, NULL); :MessageBox( NULL, wch, TEXT( "StringApp" ), MB_OK);void CDriverAppDlg:OnBnClickedWrite()/ Write a string to the driver/-DWORD dwWritten = 0;WCHAR* pString = TEXT ( "This is a test of the String Driver" );:WriteFile (hStr, pString, (_tcslen (pString) + 1), &dwWritten, NULL); void CDriverAppDlg:OnBnClickedClose()/ Disconnect from the driver/-if ( hStr != NULL )CloseHandle ( hStr );hStr = NULL;十五、在”DriverTestDlg.cpp”文件的开头添加“HANDLE hStr = NULL;”。十六、在“DriverTestDlg.h”文件中添加“const int BUFFER_SIZE = 128;”。十七、确定一下,需build是一个Release版本。十八、选择“Build->Build DriverTest”开始编译工程。十九、把编译好的“DriverTest.exe”用Activesync下载到目标平台上。先复制“DriverTest.exe”。二十、在ActiveSync中点击“浏览”。二十一、在移动设备中,右击选择“粘贴”。二十二、在目标平台上运行“DriverTest”. 二十三、确定串口线是连接好的,打开超级终端。点击目标平台上的各个控件,在超级终端可以看到相应的输出信息。