阅读:2446回复:4
USB--WriteFile时出错,请各位大侠帮助我这是一个从网上下载的USB通讯代码,有三个问题请教,请帮我看一看: Private Sub WriteReport() \'Send data to the device. Dim Count As Integer Dim NumberOfBytesRead As Long Dim NumberOfBytesToSend As Long Dim NumberOfBytesWritten As Long Dim SendBuffer() As Byte \'1. 前面已经找到USB设备,CreateFile也已返回USB Handle \'但Capabilities.OutputReportByteLength总是为0,Redim定义 \'错误,不知道为什么? ReDim SendBuffer(Capabilities.OutputReportByteLength - 1) \'The first byte is the Report ID \'2. 不知道Report ID是什么意思,一定要设为0吗? SendBuffer(0) = 0 \'The next bytes are data For Count = 1 To Capabilities.OutputReportByteLength - 1 SendBuffer(Count) = OutputReportData(Count - 1) Next Count NumberOfBytesWritten = 0 \'3. 为什么有时WriteFile返回参数错误? Result = WriteFile _ (HidDevice, _ SendBuffer(0), _ CLng(Capabilities.OutputReportByteLength), _ NumberOfBytesWritten, _ 0) Call DisplayResultOfAPICall(\"WriteFile\") lstResults.AddItem \" OutputReportByteLength = \" & Capabilities.OutputReportByteLength lstResults.AddItem \" NumberOfBytesWritten = \" & NumberOfBytesWritten lstResults.AddItem \" Report ID: \" & SendBuffer(0) lstResults.AddItem \" Report Data:\" For Count = 1 To UBound(SendBuffer) lstResults.AddItem \" \" & Hex$(SendBuffer(Count)) Next Count End Sub 打搅大家了,万分抱歉 只要有帮助,再加帖给分! |
|
沙发#
发布于:2005-03-17 12:06
帖出VC的代码来吧,
6年没有看VB的代码了,看起来费劲,并且你的代码也不完整 问题的根源应该好似你的Capabilities有问题导致的。 你怎么的到Capabilities的?
|
|
|
板凳#
发布于:2005-03-17 13:02
帖出VC的代码来吧, |
|
|
地板#
发布于:2005-03-17 14:07
\'HidDevice为USB Handle,下面是得到Capabilities的结构体
Private Sub GetDeviceCapabilities() \'****************************************************************************** \'HidD_GetPreparsedData \'Returns: a pointer to a buffer containing information about the device\'s capabilities. \'Requires: A handle returned by CreateFile. \'There\'s no need to access the buffer directly, \'but HidP_GetCaps and other API functions require a pointer to the buffer. \'****************************************************************************** Dim ppData(29) As Byte Dim ppDataString As Variant \'Preparsed Data is a pointer to a routine-allocated buffer. Result = HidD_GetPreparsedData _ (HidDevice, _ PreparsedData) \'Copy the data at PreparsedData into a byte array. Result = RtlMoveMemory _ (ppData(0), _ PreparsedData, _ 30) ppDataString = CStr(ppData()) \'Convert the data to Unicode. ppDataString = StrConv(ppDataString, vbUnicode) \'****************************************************************************** \'HidP_GetCaps \'Find out the device\'s capabilities. \'For standard devices such as joysticks, you can find out the specific \'capabilities of the device. \'For a custom device, the software will probably know what the device is capable of, \'so this call only verifies the information. \'Requires: The pointer to a buffer containing the information. \'The pointer is returned by HidD_GetPreparsedData. \'Returns: a Capabilites structure containing the information. \'****************************************************************************** Result = HidP_GetCaps _ (PreparsedData, _ Capabilities) Call DisplayResultOfAPICall(\"HidP_GetCaps\") lstResults.AddItem \" Last error: \" & ErrorString lstResults.AddItem \" Usage: \" & Hex$(Capabilities.Usage) lstResults.AddItem \" Usage Page: \" & Hex$(Capabilities.UsagePage) lstResults.AddItem \" Input Report Byte Length: \" & Capabilities.InputReportByteLength lstResults.AddItem \" Output Report Byte Length: \" & Capabilities.OutputReportByteLength lstResults.AddItem \" Feature Report Byte Length: \" & Capabilities.FeatureReportByteLength lstResults.AddItem \" Number of Link Collection Nodes: \" & Capabilities.NumberLinkCollectionNodes lstResults.AddItem \" Number of Input Button Caps: \" & Capabilities.NumberInputButtonCaps lstResults.AddItem \" Number of Input Value Caps: \" & Capabilities.NumberInputValueCaps lstResults.AddItem \" Number of Input Data Indices: \" & Capabilities.NumberInputDataIndices lstResults.AddItem \" Number of Output Button Caps: \" & Capabilities.NumberOutputButtonCaps lstResults.AddItem \" Number of Output Value Caps: \" & Capabilities.NumberOutputValueCaps lstResults.AddItem \" Number of Output Data Indices: \" & Capabilities.NumberOutputDataIndices lstResults.AddItem \" Number of Feature Button Caps: \" & Capabilities.NumberFeatureButtonCaps lstResults.AddItem \" Number of Feature Value Caps: \" & Capabilities.NumberFeatureValueCaps lstResults.AddItem \" Number of Feature Data Indices: \" & Capabilities.NumberFeatureDataIndices \'****************************************************************************** \'HidP_GetValueCaps \'Returns a buffer containing an array of HidP_ValueCaps structures. \'Each structure defines the capabilities of one value. \'This application doesn\'t use this data. \'****************************************************************************** \'This is a guess. The byte array holds the structures. Dim ValueCaps(1023) As Byte Result = HidP_GetValueCaps _ (HidP_Input, _ ValueCaps(0), _ Capabilities.NumberInputValueCaps, _ PreparsedData) End Sub |
|
地下室#
发布于:2005-03-17 14:10
没有VC的啊,只有VB的
在这里: Capabilities.InputReportByteLength 和Capabilities.OutputReportByteLength 都为0,不知道为什么,好郁闷??? 请帮助我看一看... |
|