junki
驱动牛犊
驱动牛犊
  • 注册日期2004-09-02
  • 最后登录2007-11-30
  • 粉丝0
  • 关注0
  • 积分124分
  • 威望13点
  • 贡献值0点
  • 好评度10点
  • 原创分0分
  • 专家分0分
阅读:2446回复:4

USB--WriteFile时出错,请各位大侠帮助我

楼主#
更多 发布于:2005-03-17 11:52

这是一个从网上下载的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

打搅大家了,万分抱歉
只要有帮助,再加帖给分!
AllenZh
驱动老牛
驱动老牛
  • 注册日期2001-08-19
  • 最后登录2015-11-27
  • 粉丝19
  • 关注10
  • 积分1316分
  • 威望2387点
  • 贡献值7点
  • 好评度321点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2005-03-17 12:06
帖出VC的代码来吧,
6年没有看VB的代码了,看起来费劲,并且你的代码也不完整
问题的根源应该好似你的Capabilities有问题导致的。
你怎么的到Capabilities的?



这是一个从网上下载的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

打搅大家了,万分抱歉
只要有帮助,再加帖给分!
 
1,承接Windows下驱动/应用开发 2,本人原创虚拟鼠标/键盘,触摸屏,虚拟显卡,Mirror驱动,XP无盘的SCSI虚拟磁盘驱动等 3,windows下有尝技术服务(包括BUG调试,员工培训等) 欢迎深圳和海外企业联系.msn:mfczmh@sina.com
coolwuya
驱动牛犊
驱动牛犊
  • 注册日期2004-06-29
  • 最后登录2007-08-24
  • 粉丝0
  • 关注0
  • 积分178分
  • 威望21点
  • 贡献值0点
  • 好评度18点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2005-03-17 13:02
帖出VC的代码来吧,
6年没有看VB的代码了,看起来费劲
 
USB开发交流
junki
驱动牛犊
驱动牛犊
  • 注册日期2004-09-02
  • 最后登录2007-11-30
  • 粉丝0
  • 关注0
  • 积分124分
  • 威望13点
  • 贡献值0点
  • 好评度10点
  • 原创分0分
  • 专家分0分
地板#
发布于: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
junki
驱动牛犊
驱动牛犊
  • 注册日期2004-09-02
  • 最后登录2007-11-30
  • 粉丝0
  • 关注0
  • 积分124分
  • 威望13点
  • 贡献值0点
  • 好评度10点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2005-03-17 14:10
没有VC的啊,只有VB的
在这里:
Capabilities.InputReportByteLength 和Capabilities.OutputReportByteLength
都为0,不知道为什么,好郁闷???

请帮助我看一看...
游客

返回顶部