如何在VB下实现USB口数据的传送与读取
1个回答
展开全部
VERSION 5.00
Begin VB.Form frmMain
Caption = "USB Complete"
ClientHeight = 4392
ClientLeft = 252
ClientTop = 336
ClientWidth = 6132
LinkTopic = "Form1"
ScaleHeight = 4392
ScaleWidth = 6132
Begin VB.Timer tmrContinuousDataCollect
Left = 120
Top = 3960
End
Begin VB.Frame fraSendAndReceive
Caption = "Send and Receive Data"
Height = 1692
Left = 3960
TabIndex = 7
Top = 120
Width = 2052
Begin VB.CommandButton cmdContinuous
Caption = "Continuous"
Height = 372
Left = 360
TabIndex = 9
Top = 1080
Width = 1452
End
Begin VB.CommandButton cmdOnce
Caption = "Once"
Height = 372
Left = 360
TabIndex = 8
Top = 360
Width = 1452
End
End
Begin VB.Frame fraBytesReceived
Caption = "Bytes Received"
Height = 1692
Left = 2400
TabIndex = 4
Top = 120
Width = 1452
Begin VB.TextBox txtBytesReceived
Height = 732
Left = 360
MultiLine = -1 'True
TabIndex = 5
Top = 600
Width = 732
End
End
Begin VB.Frame fraBytesToSend
Caption = "Bytes to Send"
Height = 1692
Left = 120
TabIndex = 1
Top = 120
Width = 2172
Begin VB.CheckBox chkAutoincrement
Caption = "Autoincrement values"
Height = 372
Left = 240
TabIndex = 6
Top = 1200
Width = 2412
End
Begin VB.ComboBox cboByte1
Height = 288
Left = 240
Style = 2 'Dropdown List
TabIndex = 3
Top = 840
Width = 1212
End
Begin VB.ComboBox cboByte0
Height = 288
Left = 240
Style = 2 'Dropdown List
TabIndex = 2
Top = 360
Width = 1212
End
End
Begin VB.Timer tmrDelay
Enabled = 0 'False
Left = 120
Top = 11400
End
Begin VB.ListBox lstResults
Height = 2352
Left = 120
TabIndex = 0
Top = 1920
Width = 5892
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'Project: usbhidio.vbp
'Version: 1.1
'Date: 11/20/99
'Copyright 1999 by Jan Axelson (jan@lvr.com)
'
'Purpose: demonstrates USB communications with an HID-class device
'Description:
'Finds an attached device that matches specific vendor and product IDs.
'Retrieves the device's capabilities.
'Sends two bytes to the device using Input reports.
'Receives two bytes from the device in Output reports.
'(For testing, the current device firmware adds 1 to the received bytes
'and sends them back.)
'A list box displays the data sent and received,
'along with error and status messages.
'Combo boxes enable you to select data to send, and to select 1-time or
'continuous transfers.
'The companion device firmware is usbhidio.asm,
'for Cypress Semiconductor's CY7C63001 USB Microcontroller.
'For more information, visit Lakeview Research at http://www.lvr.com .
'Send comments, bug reports, etc. to jan@lvr.com .
'Changes and updates:
'11/20/99. Revised a few of the comments.
'v1.1 added Else statement in InitializeDisplay routine
'so both combo boxes have all of the values.
Dim Capabilities As HIDP_CAPS
Dim DataString As String
Dim DetailData As Long
Dim DetailDataBuffer() As Byte
Dim DeviceAttributes As HIDD_ATTRIBUTES
Dim DevicePathName As String
Dim DeviceInfoSet As Long
Dim ErrorString As String
Dim HidDevice As Long
Dim LastDevice As Boolean
Dim MyDeviceDetected As Boolean
Dim MyDeviceInfoData As SP_DEVINFO_DATA
Dim MyDeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA
Dim MyDeviceInterfaceData As SP_DEVICE_INTERFACE_DATA
Dim Needed As Long
Dim OutputReportData(7) As Byte
Dim PreparsedData As Long
Dim Result As Long
Dim Timeout As Boolean
'Set these to match the values in the device's firmware and INF file.
Const MyVendorID = &H925
Const MyProductID = &H1234
Function FindTheHid() As Boolean
'Makes a series of API calls to locate the desired HID-class device.
'Returns True if the device is detected, False if not detected.
Dim Count As Integer
Dim GUIDString As String
Dim HidGuid As GUID
Dim MemberIndex As Long
LastDevice = False
MyDeviceDetected = False
'******************************************************************************
'HidD_GetHidGuid
'Get the GUID for all system HIDs.
'Returns: the GUID in HidGuid.
'The routine doesn't return a value in Result
'but the routine is declared as a function for consistency with the other API calls.
'******************************************************************************
Result = HidD_GetHidGuid(HidGuid)
Call DisplayResultOfAPICall("GetHidGuid")
'Display the GUID.
GUIDString = _
Hex$(HidGuid.Data1) & "-" & _
Hex$(HidGuid.Data2) & "-" & _
Hex$(HidGuid.Data3) & "-"
For Count = 0 To 7
'Ensure that each of the 8 bytes in the GUID displays two characters.
If HidGuid.Data4(Count) >= &H10 Then
GUIDString = GUIDString & Hex$(HidGuid.Data4(Count)) & " "
Else
GUIDString = GUIDString & "0" & Hex$(HidGuid.Data4(Count)) & " "
End If
Next Count
lstResults.AddItem " GUID for system HIDs: " & GUIDString
'******************************************************************************
'SetupDiGetClassDevs
'Returns: a handle to a device information set for all installed devices.
'Requires: the HidGuid returned in GetHidGuid.
'******************************************************************************
DeviceInfoSet = SetupDiGetClassDevs _
(HidGuid, _
vbNullString, _
0, _
(DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE))
Call DisplayResultOfAPICall("SetupDiClassDevs")
DataString = GetDataString(DeviceInfoSet, 32)
'******************************************************************************
'SetupDiEnumDeviceInterfaces
'On return, MyDeviceInterfaceData contains the handle to a
'SP_DEVICE_INTERFACE_DATA structure for a detected device.
'Requires:
'the DeviceInfoSet returned in SetupDiGetClassDevs.
'the HidGuid returned in GetHidGuid.
'An index to specify a device.
'******************************************************************************
'Begin with 0 and increment until no more devices are detected.
MemberIndex = 0
Do
'The cbSize element of the MyDeviceInterfaceData structure must be set to
'the structure's size in bytes. The size is 28 bytes.
MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData)
Result = SetupDiEnumDeviceInterfaces _
(DeviceInfoSet, _
0, _
HidGuid, _
MemberIndex, _
MyDeviceInterfaceData)
Call DisplayResultOfAPICall("SetupDiEnumDeviceInterfaces")
If Result = 0 Then LastDevice = True
'If a device exists, display the information returned.
If Result <> 0 Then
lstResults.AddItem " DeviceInfoSet for device #" & CStr(MemberIndex) & ": "
lstResults.AddItem " cbSize = " & CStr(MyDeviceInterfaceData.cbSize)
lstResults.AddItem _
" InterfaceClassGuid.Data1 = " & Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data1)
lstResults.AddItem _
" InterfaceClassGuid.Data2 = " & Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data2)
lstResults.AddItem _
" InterfaceClassGuid.Data3 = " & Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data3)
lstResults.AddItem _
" Flags = " & Hex$(MyDeviceInterfaceData.Flags)
Begin VB.Form frmMain
Caption = "USB Complete"
ClientHeight = 4392
ClientLeft = 252
ClientTop = 336
ClientWidth = 6132
LinkTopic = "Form1"
ScaleHeight = 4392
ScaleWidth = 6132
Begin VB.Timer tmrContinuousDataCollect
Left = 120
Top = 3960
End
Begin VB.Frame fraSendAndReceive
Caption = "Send and Receive Data"
Height = 1692
Left = 3960
TabIndex = 7
Top = 120
Width = 2052
Begin VB.CommandButton cmdContinuous
Caption = "Continuous"
Height = 372
Left = 360
TabIndex = 9
Top = 1080
Width = 1452
End
Begin VB.CommandButton cmdOnce
Caption = "Once"
Height = 372
Left = 360
TabIndex = 8
Top = 360
Width = 1452
End
End
Begin VB.Frame fraBytesReceived
Caption = "Bytes Received"
Height = 1692
Left = 2400
TabIndex = 4
Top = 120
Width = 1452
Begin VB.TextBox txtBytesReceived
Height = 732
Left = 360
MultiLine = -1 'True
TabIndex = 5
Top = 600
Width = 732
End
End
Begin VB.Frame fraBytesToSend
Caption = "Bytes to Send"
Height = 1692
Left = 120
TabIndex = 1
Top = 120
Width = 2172
Begin VB.CheckBox chkAutoincrement
Caption = "Autoincrement values"
Height = 372
Left = 240
TabIndex = 6
Top = 1200
Width = 2412
End
Begin VB.ComboBox cboByte1
Height = 288
Left = 240
Style = 2 'Dropdown List
TabIndex = 3
Top = 840
Width = 1212
End
Begin VB.ComboBox cboByte0
Height = 288
Left = 240
Style = 2 'Dropdown List
TabIndex = 2
Top = 360
Width = 1212
End
End
Begin VB.Timer tmrDelay
Enabled = 0 'False
Left = 120
Top = 11400
End
Begin VB.ListBox lstResults
Height = 2352
Left = 120
TabIndex = 0
Top = 1920
Width = 5892
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'Project: usbhidio.vbp
'Version: 1.1
'Date: 11/20/99
'Copyright 1999 by Jan Axelson (jan@lvr.com)
'
'Purpose: demonstrates USB communications with an HID-class device
'Description:
'Finds an attached device that matches specific vendor and product IDs.
'Retrieves the device's capabilities.
'Sends two bytes to the device using Input reports.
'Receives two bytes from the device in Output reports.
'(For testing, the current device firmware adds 1 to the received bytes
'and sends them back.)
'A list box displays the data sent and received,
'along with error and status messages.
'Combo boxes enable you to select data to send, and to select 1-time or
'continuous transfers.
'The companion device firmware is usbhidio.asm,
'for Cypress Semiconductor's CY7C63001 USB Microcontroller.
'For more information, visit Lakeview Research at http://www.lvr.com .
'Send comments, bug reports, etc. to jan@lvr.com .
'Changes and updates:
'11/20/99. Revised a few of the comments.
'v1.1 added Else statement in InitializeDisplay routine
'so both combo boxes have all of the values.
Dim Capabilities As HIDP_CAPS
Dim DataString As String
Dim DetailData As Long
Dim DetailDataBuffer() As Byte
Dim DeviceAttributes As HIDD_ATTRIBUTES
Dim DevicePathName As String
Dim DeviceInfoSet As Long
Dim ErrorString As String
Dim HidDevice As Long
Dim LastDevice As Boolean
Dim MyDeviceDetected As Boolean
Dim MyDeviceInfoData As SP_DEVINFO_DATA
Dim MyDeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA
Dim MyDeviceInterfaceData As SP_DEVICE_INTERFACE_DATA
Dim Needed As Long
Dim OutputReportData(7) As Byte
Dim PreparsedData As Long
Dim Result As Long
Dim Timeout As Boolean
'Set these to match the values in the device's firmware and INF file.
Const MyVendorID = &H925
Const MyProductID = &H1234
Function FindTheHid() As Boolean
'Makes a series of API calls to locate the desired HID-class device.
'Returns True if the device is detected, False if not detected.
Dim Count As Integer
Dim GUIDString As String
Dim HidGuid As GUID
Dim MemberIndex As Long
LastDevice = False
MyDeviceDetected = False
'******************************************************************************
'HidD_GetHidGuid
'Get the GUID for all system HIDs.
'Returns: the GUID in HidGuid.
'The routine doesn't return a value in Result
'but the routine is declared as a function for consistency with the other API calls.
'******************************************************************************
Result = HidD_GetHidGuid(HidGuid)
Call DisplayResultOfAPICall("GetHidGuid")
'Display the GUID.
GUIDString = _
Hex$(HidGuid.Data1) & "-" & _
Hex$(HidGuid.Data2) & "-" & _
Hex$(HidGuid.Data3) & "-"
For Count = 0 To 7
'Ensure that each of the 8 bytes in the GUID displays two characters.
If HidGuid.Data4(Count) >= &H10 Then
GUIDString = GUIDString & Hex$(HidGuid.Data4(Count)) & " "
Else
GUIDString = GUIDString & "0" & Hex$(HidGuid.Data4(Count)) & " "
End If
Next Count
lstResults.AddItem " GUID for system HIDs: " & GUIDString
'******************************************************************************
'SetupDiGetClassDevs
'Returns: a handle to a device information set for all installed devices.
'Requires: the HidGuid returned in GetHidGuid.
'******************************************************************************
DeviceInfoSet = SetupDiGetClassDevs _
(HidGuid, _
vbNullString, _
0, _
(DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE))
Call DisplayResultOfAPICall("SetupDiClassDevs")
DataString = GetDataString(DeviceInfoSet, 32)
'******************************************************************************
'SetupDiEnumDeviceInterfaces
'On return, MyDeviceInterfaceData contains the handle to a
'SP_DEVICE_INTERFACE_DATA structure for a detected device.
'Requires:
'the DeviceInfoSet returned in SetupDiGetClassDevs.
'the HidGuid returned in GetHidGuid.
'An index to specify a device.
'******************************************************************************
'Begin with 0 and increment until no more devices are detected.
MemberIndex = 0
Do
'The cbSize element of the MyDeviceInterfaceData structure must be set to
'the structure's size in bytes. The size is 28 bytes.
MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData)
Result = SetupDiEnumDeviceInterfaces _
(DeviceInfoSet, _
0, _
HidGuid, _
MemberIndex, _
MyDeviceInterfaceData)
Call DisplayResultOfAPICall("SetupDiEnumDeviceInterfaces")
If Result = 0 Then LastDevice = True
'If a device exists, display the information returned.
If Result <> 0 Then
lstResults.AddItem " DeviceInfoSet for device #" & CStr(MemberIndex) & ": "
lstResults.AddItem " cbSize = " & CStr(MyDeviceInterfaceData.cbSize)
lstResults.AddItem _
" InterfaceClassGuid.Data1 = " & Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data1)
lstResults.AddItem _
" InterfaceClassGuid.Data2 = " & Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data2)
lstResults.AddItem _
" InterfaceClassGuid.Data3 = " & Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data3)
lstResults.AddItem _
" Flags = " & Hex$(MyDeviceInterfaceData.Flags)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询