想写个HID通信程序,如下Public Function OpenUSBdevice(NameOfDevice$) As Boolean
' This function searches the system HID tables for NameOfDevice$
' If found then it opens the device and returns TRUE, else it returns FALSE
Dim HidGuid As GUID
Dim Success As Boolean
Dim Openned As Boolean
Dim Buffer(256) As Byte
Dim DeviceInterfaceData As DEVICE_INTERFACE_DATA
Dim FunctionClassDeviceData As DEVICE_INTERFACE_DETAIL_DATA
Dim PnPhandle As Long
Dim HidEntry As Long
Dim i As Long
Dim HidName As String, HidHandle As Long' First, get the HID class identifier
Call HidD_GetHidGuid(HidGuid)
'HidGuid = Application.GUID
' Get a handle for the Plug and Play node, request currently active HID devices
PnPhandle& = SetupDiGetClassDevs(HidGuid, 0, 0, &H12)
If (PnPhandle& = -1) Then
MsgBox ("Could not attach to PnP node")
Exit Function
End IfHidEntry& = 0
HidGuid里面的值是有的,但是PnPhandle总是-1, 大侠们帮忙啊!谢了!
' This function searches the system HID tables for NameOfDevice$
' If found then it opens the device and returns TRUE, else it returns FALSE
Dim HidGuid As GUID
Dim Success As Boolean
Dim Openned As Boolean
Dim Buffer(256) As Byte
Dim DeviceInterfaceData As DEVICE_INTERFACE_DATA
Dim FunctionClassDeviceData As DEVICE_INTERFACE_DETAIL_DATA
Dim PnPhandle As Long
Dim HidEntry As Long
Dim i As Long
Dim HidName As String, HidHandle As Long' First, get the HID class identifier
Call HidD_GetHidGuid(HidGuid)
'HidGuid = Application.GUID
' Get a handle for the Plug and Play node, request currently active HID devices
PnPhandle& = SetupDiGetClassDevs(HidGuid, 0, 0, &H12)
If (PnPhandle& = -1) Then
MsgBox ("Could not attach to PnP node")
Exit Function
End IfHidEntry& = 0
HidGuid里面的值是有的,但是PnPhandle总是-1, 大侠们帮忙啊!谢了!
Lib "setupapi.dll" _
Alias "SetupDiGetClassDevsA" _
(ByRef ClassGuid As GUID, _
ByVal Enumerator As String, _
ByVal hwndParent As Long, _
ByVal Flags As Long) _
As Long
Public Declare Function SetupDiGetDeviceInterfaceDetail _
Lib "setupapi.dll" _
Alias "SetupDiGetDeviceInterfaceDetailA" _
(ByVal DeviceInfoSet As Long, _
ByRef DeviceInterfaceData As DEVICE_INTERFACE_DATA, _
ByVal DeviceInterfaceDetailData As Long, _
ByVal DeviceInterfaceDetailDataSize As Long, _
ByRef RequiredSize As Long, _
ByVal DeviceInfoData As Long) _
As Long
Public 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
Public Declare Function SetupDiGetClassDevs Lib "setupapi" Alias "SetupDiGetClassDevsA" (ClassGuid As GUID, ByVal Enumerator As Long, ByVal hWndParent As Long, ByVal Flags As Long) As Long声明改成这样你再试试
PnPhandle& = SetupDiGetClassDevs(HidGuid, 0, 0, &H12)
多谢高手!
Public Sub A_Open()
' Call USB_HID_Init
Call OpenUSBdevice("AVR")
End SubPublic Function OpenUSBdevice(NameOfDevice$) As Boolean
' This function searches the system HID tables for NameOfDevice$
' If found then it opens the device and returns TRUE, else it returns FALSE
Dim HidGuid As GUID, resultGUID As Long
Dim Success As Boolean
Dim Openned As Boolean
Dim buffer(256) As Byte
Dim DeviceInterfaceData As DEVICE_INTERFACE_DATA
Dim FunctionClassDeviceData As DEVICE_INTERFACE_DETAIL_DATA
Dim SpDeviceInfoData As DEVINFO_DATA
Dim PnPhandle As Long
Dim HidEntry As Long
Dim i As Long, bytesReturned As Long
Dim HidName As String, HidHandle As Long, DeviceName As String
Dim lpFunctionClassDeviceData As Long
Dim HidAttr As HIDD_ATTRIBUTES, lPtr As Long
' First, get the HID class identifier
resultGUID = HidD_GetHidGuid(HidGuid)'ReDim buffer.buffer(256) As Byte' Get a handle for the Plug and Play node, request currently active HID devices
PnPhandle& = SetupDiGetClassDevs(HidGuid, 0, 0, &H12)
If (PnPhandle& = -1) Then
MsgBox ("Could not attach to PnP node")
Exit Function
End IfHidEntry& = 0
Openned = False
DeviceInterfaceData.cbSize = 28 'Length of data structure in bytes
SpDeviceInfoData.cbSize = 28' Look through the table of HID devices
Do While SetupDiEnumDeviceInterfaces(PnPhandle&, 0, HidGuid, HidEntry&, DeviceInterfaceData)
' There is a device here, get it's name'ReDim FunctionClassDeviceData.DevicePath(200) As Byte
FunctionClassDeviceData.cbSize = 5 lpFunctionClassDeviceData = VarPtr(FunctionClassDeviceData)
Success = SetupDiGetDeviceInterfaceDetail(PnPhandle&, DeviceInterfaceData, _
0&, 0&, bytesReturned&, SpDeviceInfoData)
Success = SetupDiGetDeviceInterfaceDetail(PnPhandle&, DeviceInterfaceData, _
lpFunctionClassDeviceData, bytesReturned&, bytesReturned&, SpDeviceInfoData)
If (Success = 0) Then
MsgBox ("Could not attach to PnP node")
Exit Function
End If
' Convert returned C string to Visual Basic String
HidName$ = Empty
i& = 0
Do While FunctionClassDeviceData.DevicePath(i&) <> 0
HidName$ = HidName$ & Chr$(FunctionClassDeviceData.DevicePath(i&)): i& = i& + 1
Loop ' Can now open this HID device
HidHandle& = CreateFile(HidName$, &HC0000000, 3, 0, 3, 0, 0)
If (HidHandle = -1) Then
MsgBox ("System HID detected ")
'Exit Function
GoTo LOOPLABEL
End If ' Is it OUR HID device?
lPtr = VarPtr(buffer(0))
Success = HidD_GetProductString(HidHandle&, lPtr, UBound(buffer)) If HidD_GetAttributes(HidHandle&, HidAttr) Then
DeviceName$ = Empty
i& = 0
Do While buffer(i&) <> 0: DeviceName$ = DeviceName$ & Chr$(buffer(i&))
i& = i& + 2
Loop
If (InStr(DeviceName$, NameOfDevice$) > 0) Then
Openned = True
Exit Do
End If
End If 'HidD_GetProductString
Call CloseHandle(HidHandle&) ' Was not OUR HID device
LOOPLABEL:
HidEntry& = HidEntry& + 1 ' Check next entry
Loop 'SetupDiEnumDeviceInterfaces returns FALSE when there are no more entriesOpenUSBdevice = OpennedEnd Function
中"AVR"是怎么来的?表示什么意思?
初学者求教,别见怪!