现在的位置: 首页 > 综合 > 正文

BioUsb-程序执行流程分析

2014年03月23日 ⁄ 综合 ⁄ 共 9436字 ⁄ 字号 评论关闭

1void CMainFrame::OnDeviceConnect() 连接设备
//定时器作用:每隔一段读缓存(从设备端点读取数据)
//每隔一段时间(10ms)执行mycallback()回调函数,以实现连续采集。
::timeSetEvent (10,0,mycallback,(DWORD)this,TIME_PERIODIC);  

2、执行mycallback回调函数

3、执行Execute_ReadFile()函数

4、待hEvent_Read置为有信号状态后,ThreadExecute_Read(PVOID pContext)waitForSingleObject(```)后执行,开始将设备端点数据读取至计算机中。


WaitForSingleObject()函数介绍详见 hj的多线程程序设计P93

引用:事实上,win32中大部分以HANDLE表示的对象都能够作为WaitForSingleObject()的等待目标。视你所拥有的对象不同,操作系统等待的事情也不太一样。形式上来说,系统等待着这一对象“被激发”。

Ps: ThreadExecute_Read是线程函数,在CBioUSBAPI类的构造函数中被CreateThread启动。

 

Api函数

IRP主要功能代码

说明

CreateFile

IRP_MJ_CREATE

打开设备

ReadFile

IRP_MJ_READ

从设备获取数据

WriteFile

IRP_MJ_WRITE

向设备发送数据

CloseHandle

IRP_MJ_CLOSE

关闭设备

DeviceIoControl

IRP_MJ_DEVICE_CONTROL

控制操作

 

 

 

 

 

 

 

usb设备进行通信之前,首先需要得到usb设备的句柄。通过获取设备GUID、获取设备信息集、获取设备路径名可查找到设备并得到所要打开设备的路径名。然后通过API函数CreateFile来打开设备,当成功打开后,CreateFile函数会返回一个其他API函数可用来与设备交换数据的句柄。打开设备之后,应用程序就可以使用ReadFileWriteFile函数和设备进行数据交换了。当读写usb设备结束可以断开设备时候,就可以调用CloseHandle函数关闭设备。

4.1 CreateFile


在以下函数OpenDeviceInterface中被调用

4.2 ReadFile

在以下<FONT style="FONT-SIZE: 10.5pt" face=""">ThreadExecute_Read函数中被调用

ReadFile

The ReadFile function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually
read, unless the file handle is created
with the overlapped attribute. If the file handle is created for overlapped input and output (I/O), the application must adjust the position of the file pointer
after the read operation.

BOOL ReadFile(
  HANDLE hFile,                // handle of file to read
  LPVOID lpBuffer,             // pointer to buffer that receives data
  DWORD nNumberOfBytesToRead,  // number of bytes to read
  LPDWORD lpNumberOfBytesRead, // pointer to number of bytes read
  LPOVERLAPPED lpOverlapped    // pointer to structure for data
);

 

hFile

Handle to the file to be read. The file handle must have been created with GENERIC_READ access to the file.

lpBuffer

Pointer to the buffer that receives the data read from the file.

程序中:

ReadFile(pUSBAPI->stRead.hDevice,

                                  
 pUSBAPI->stRead.pInBuffer,

                                  
 pUSBAPI->stRead.dwInSize,

                                  
 pUSBAPI->stRead.lpBytesReturned,

                                  
 &olRead))

pUSBAPI定义:

CBioUSBAPI* pUSBAPI = (CBioUSBAPI *)pContext;

是一个指向CBioUSBAPI类的指针。

 

stRead是类CBioUSBAPI的成员变量,

class CBioUSBAPI 

{···

STRUCT_IO stRead;    
//
类型为结构体类型(即STRUCT_IO

····

}

 

STRUCT_IO定义:

typedef struct _STRUCT_IO

{

      
HWND hTargetWnd;

      
HANDLE hDevice;

      
DWORD dwIoControlCode;

      
PBYTE pInBuffer;

      
DWORD dwInSize;

      
PBYTE pOutBuffer;

      
DWORD dwOutSize;

      
LPDWORD lpBytesReturned;

      
DWORD   dwTimeOut;

}STRUCT_IO,*PSTRUCT_IO;

 

 

 

SendMessage(pUSBAPI->stRead.hTargetWnd,

                                  
MSG_READ_COMPLETION,

                                  
0,

                                  
(LPARAM)&pUSBAPI->stRead);

作用:读取端点数据后,发消息MSG_READ_COMPLETION通知窗口过程函数。

 

stRead作为SendMessage()函数的第四个参数被传送给消息处理函数。

5、待采集线程函数ThreadExecute_Read(PVOID
pContext)
通过SendMessage(····)函数发送“读完成”消息后,WindowProc(····)被调用。其作用主要是对采集到保存在缓冲区的数据pUSBAPI->stRead.pInBuffer进行处理。


虚函数WindowProc()定义在类CMainFrame中:


 

【上篇】
【下篇】

抱歉!评论已关闭.