想用VB 做个接受界面,用于接收从USB设备传来的数据,下面是部分代码,运行总是错误,我认为是PC没有打开设备,单不知问题所在,请大家帮我看看,帮我改正一下,谢谢了
HidDevice = CreateFile( _
DevicePathName, _
GENERIC_READ Or GENERIC_WRITE, _
(FILE_SHARE_READ Or FILE_SHARE_WRITE), _
0, _
OPEN_EXISTING, _
0, _
0) If HidDevice = INVALID_HANDLE_VALUE Then GoTo DO_Next
Result = HidD_GetPreparsedData( _
HidDevice, _
PreparsedData)
If Result = 0 Then GoTo DO_Next
Result = HidP_GetCaps( _
PreparsedData, _
Capabilities)
If (Capabilities.UsagePage = &HFF00) And (Capabilities.Usage = &H1) Then
ADEVICE.devicepath(ADEVICE.total) = DevicePathName
ADEVICE.total = ADEVICE.total + 1
End If
DO_Next:
Loop Until NumMax < ADEVICE.total
Call ReadFile(HidDevice, szStore(0), 4, dwCount, 0)Dim adr_l As Integer
Dim adr_h As Integer
Dim length As Integer
Dim STR As String
STR = ReadUSB(adr_l, adr_h, length)Text1 = STREnd Sub
HidDevice = CreateFile( _
DevicePathName, _
GENERIC_READ Or GENERIC_WRITE, _
(FILE_SHARE_READ Or FILE_SHARE_WRITE), _
0, _
OPEN_EXISTING, _
0, _
0) If HidDevice = INVALID_HANDLE_VALUE Then GoTo DO_Next
Result = HidD_GetPreparsedData( _
HidDevice, _
PreparsedData)
If Result = 0 Then GoTo DO_Next
Result = HidP_GetCaps( _
PreparsedData, _
Capabilities)
If (Capabilities.UsagePage = &HFF00) And (Capabilities.Usage = &H1) Then
ADEVICE.devicepath(ADEVICE.total) = DevicePathName
ADEVICE.total = ADEVICE.total + 1
End If
DO_Next:
Loop Until NumMax < ADEVICE.total
Call ReadFile(HidDevice, szStore(0), 4, dwCount, 0)Dim adr_l As Integer
Dim adr_h As Integer
Dim length As Integer
Dim STR As String
STR = ReadUSB(adr_l, adr_h, length)Text1 = STREnd Sub
以下是程序代码:
Option Explicit'**********************************************************************'API constants, listed alphabetically
'**********************************************************************
'from setupapi.h
Private Const DIGCF_PRESENT = &H2
Private Const DIGCF_DEVICEINTERFACE = &H10Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2'Typedef enum defines a set of integer constants for HidP_Report_Type
'Remember to declare these as integers (16 bits)
Private Const HidP_Input = 0
Private Const HidP_Output = 1
Private Const HidP_Feature = 2Private Const OPEN_EXISTING = 3
Private Const INVALID_HANDLE_VALUE = -1'**********************************************************************'User-defined types for API calls, listed alphabetically
'**********************************************************************
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End TypePrivate Type HIDD_ATTRIBUTES
Size As Long
VendorID As Integer
ProductID As Integer
VersionNumber As Integer
End Type'Windows 98 DDK documentation is incomplete.
'Use the structure defined in hidpi.h
Private Type HIDP_CAPS
Usage As Integer
UsagePage As Integer
InputReportByteLength As Integer
OutputReportByteLength As Integer
FeatureReportByteLength As Integer
Reserved(16) As Integer
NumberLinkCollectionNodes As Integer
NumberInputButtonCaps As Integer
NumberInputvalueCaps As Integer
NumberInputDataIndices As Integer
NumberOutputButtonCaps As Integer
NumberOutputvalueCaps As Integer
NumberOutputDataIndices As Integer
NumberFeatureButtonCaps As Integer
NumberFeaturevalueCaps As Integer
NumberFeatureDataIndices As Integer
End Type'If IsRange is false, UsageMin is the Usage and UsageMax is unused.
'If IsStringRange is false, StringMin is the string index and StringMax is unused.
'If IsDesignatorRange is false, DesignatorMin is the designator index and DesignatorMax is unused.
Private Type HidP_value_Caps
UsagePage As Integer
ReportID As Byte
IsAlias As Long
BitField As Integer
LinkCollection As Integer
LinkUsage As Integer
LinkUsagePage As Integer
IsRange As Long
IsStringRange As Long
IsDesignatorRange As Long
IsAbsolute As Long
HasNull As Long
Reserved As Byte
BitSize As Integer
ReportCount As Integer
Reserved2 As Integer
Reserved3 As Integer
Reserved4 As Integer
Reserved5 As Integer
Reserved6 As Integer
LogicalMin As Long
LogicalMax As Long
PhysicalMin As Long
PhysicalMax As Long
UsageMin As Integer
UsageMax As Integer
StringMin As Integer
StringMax As Integer
DesignatorMin As Integer
DesignatorMax As Integer
DataIndexMin As Integer
DataIndexMax As Integer
End TypePrivate Type SP_DEVICE_INTERFACE_DATA
cbSize As Long
InterfaceClassGuid As GUID
Flags As Long
Reserved As Long
End TypePrivate Type SP_DEVICE_INTERFACE_DETAIL_DATA
cbSize As Long
devicepath As Byte
End TypePrivate Type SP_DEVINFO_DATA
cbSize As Long
ClassGuid As GUID
DevInst As Long
Reserved As Long
End TypePrivate Type ADEVICE
total As Integer
devicepath() As Integer
End Type
Private Type OVERLAPPED 'readfile用
Internal As Long
InternalHigh As Long
Offset As Long
OffsetHigh As Long
hEvent As String
End Type
Private Declare Function CloseHandle _
Lib "kernel32" _
(ByVal hObject As Long) _
As LongPrivate Declare Function CreateFile _
Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByRef lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) _
As LongPrivate Declare Function FormatMessage _
Lib "kernel32" _
Alias "FormatMessageA" _
(ByVal dwFlags As Long, _
ByRef lpSource As Any, _
ByVal dwMessageId As Long, _
ByVal dwLanguageZId As Long, _
ByVal lpBuffer As String, _
ByVal nSize As Long, _
ByVal Arguments As Long) _
As LongPrivate Declare Function HidD_FreePreparsedData _
Lib "hid.dll" _
(ByRef PreparsedData As Long) _
As LongPrivate Declare Function HidD_GetAttributes _
Lib "hid.dll" _
(ByVal HidDeviceObject As Long, _
ByRef Attributes As HIDD_ATTRIBUTES) _
As Long'Declared as a function for consistency,
'but returns nothing. (Ignore the returned value.)
Private Declare Function HidD_GetHidGuid _
Lib "hid.dll" _
(ByRef HidGuid As GUID) _
As LongPrivate Declare Function HidD_GetPreparsedData _
Lib "hid.dll" _
(ByVal HidDeviceObject As Long, _
ByRef PreparsedData As Long) _
As LongPrivate Declare Function HidP_GetCaps _
Lib "hid.dll" _
(ByVal PreparsedData As Long, _
ByRef Capabilities As HIDP_CAPS) _
As LongPrivate Declare Function HidP_GetvalueCaps _
Lib "hid.dll" _
(ByVal ReportType As Integer, _
ByRef valueCaps As Byte, _
ByRef valueCapsLength As Integer, _
ByVal PreparsedData As Long) _
As Long
Private Declare Function lstrcpy _
Lib "kernel32" _
Alias "lstrcpyA" _
(ByVal dest As String, _
ByVal source As Long) _
As StringPrivate Declare Function lstrlen _
Lib "kernel32" _
Alias "lstrlenA" _
(ByVal source As Long) _
As LongPrivate Declare Function ReadFile _
Lib "kernel32" _
(ByVal hFile As Long, _
ByRef lpBuffer As Byte, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Long) _
As LongPrivate Declare Function RtlMoveMemory _
Lib "kernel32" _
(dest As Any, _
src As Any, _
ByVal Count As Long) _
As LongPrivate Declare Function SetupDiCreateDeviceInfoList _
Lib "setupapi.dll" _
(ByRef ClassGuid As GUID, _
ByVal hwndParent As Long) _
As LongPrivate Declare Function SetupDiDestroyDeviceInfoList _
Lib "setupapi.dll" _
(ByVal DeviceInfoSet As Long) _
As LongPrivate Declare Function SetupDiEnumDeviceInterfaces _
Lib "setupapi.dll" _
(ByVal DeviceInfoSet As Long, _
ByVal DeviceInfoData As Long, _
ByRef InterfaceClassGuid As GUID, _
ByVal MemberIndex As Long, _
ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA) _
As LongPrivate Declare Function SetupDiGetClassDevs _
Lib "setupapi.dll" _
Alias "SetupDiGetClassDevsA" _
(ByRef ClassGuid As GUID, _
ByVal Enumerator As String, _
ByVal hwndParent As Long, _
ByVal Flags As Long) _
As LongPrivate Declare Function SetupDiGetDeviceInterfaceDetail _
Lib "setupapi.dll" _
Alias "SetupDiGetDeviceInterfaceDetailA" _
(ByVal DeviceInfoSet As Long, _
ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, _
ByVal DeviceInterfaceDetailData As Long, _
ByVal DeviceInterfaceDetailDataSize As Long, _
ByRef RequiredSize As Long, _
ByVal DeviceInfoData As Long) _
As Long
Private Declare Function WriteFile _
Lib "kernel32" _
(ByVal hFile As Long, _
ByRef lpBuffer As Byte, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long) _
As Long
Private Declare Function ReadPort1 Lib "EasyD12.dll" (ByRef pData As _
Byte, ByVal iLen As Integer) As Integer
Private Declare Function ReadPort2 Lib "EasyD12.dll" (ByRef pData As _
Byte, ByVal iLen As Integer) As Integer
Private Declare Function WritePort1 Lib "EasyD12.dll" (ByRef pData As _
Byte, ByVal iLen As Integer) As Integer
Private Declare Function WritePort2 Lib "EasyD12.dll" (ByRef pData As _
Byte, ByVal iLen As Integer) As IntegerFunction ReadUSB(ByVal Bgnadr_L As Integer, ByVal _
Bgnadr_H As Integer, ByVal nlen As Integer) As StringDim nframeLen As Integer
Dim nframeNum As Integer
Dim nframeLeave As Integer
Dim cAFrameAck(63) As Byte
Dim ComBuf(7) As Byte
Dim nBgnAdr_l As Integer
Dim nBgnAdr_h As Integer
Dim STR As String
Dim st As Integer
Dim j As Integer
ReadUSB = ""
STR = ""
nframeLen = 64
nBgnAdr_l = Bgnadr_L
nBgnAdr_h = Bgnadr_H
If nlen > 0 Then
nframeNum = Int(nlen / nframeLen)
nframeLeave = nlen Mod nframeLen
End If
ComBuf(0) = Asc("R")
ComBuf(1) = 0
ComBuf(2) = 0
ComBuf(3) = nBgnAdr_l
ComBuf(4) = nBgnAdr_h
ComBuf(5) = nframeLen
ComBuf(6) = 0
ComBuf(7) = 0
For j = 0 To 6
ComBuf(7) = ComBuf(7) Xor ComBuf(j)
Next j st = ReadPort1(ComBuf(0), 2)
If st <> 0 Then
MsgBox "读端口1数据出错", vbInformation, "提示"
Exit Function
End If
For j = 1 To nframeLen
STR = STR + Chr(cAFrameAck(j - 1))
Next j
ReadUSB = STREnd Function