Attribute VB_Name = "BulkXfer"
Option Explicit
' = = = = W I N A P I = = = = 这些在DELPHI都不用理
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long
Public Declare Function GetLastError Lib "kernel32" () As LongPublic Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_SHARE_READ = &H1
Public Const FILE_SHARE_WRITE = &H2
Public Const OPEN_EXISTING = 3Public Const METHOD_BUFFERED = 0
Public Const METHOD_IN_DIRECT = 1
Public Const METHOD_OUT_DIRECT = 2Public Const MAX_PIPES = 16
Public Const MAX_USB_DEV_NUMBER = 32Enum ErrorEnum
eBadParam = -1
eBadDriver = -2
eBadPipe = -3
End EnumEnum EZ_ReadOrWrite
eWrite = 1
eRead = 0
End EnumPublic Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End TypePrivate Const Ezusb_IOCTL_INDEX = &H800Public Const IOCTL_Ezusb_GET_PIPE_INFO = _
&H220000 + METHOD_BUFFERED + (Ezusb_IOCTL_INDEX + 0) * 4Private Const IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR = _
&H220000 + METHOD_BUFFERED + (Ezusb_IOCTL_INDEX + 1) * 4Public Const IOCTL_EZUSB_BULK_READ = _
&H220000 + METHOD_OUT_DIRECT + (Ezusb_IOCTL_INDEX + 19) * 4Public Const IOCTL_EZUSB_BULK_WRITE = _
&H220000 + METHOD_IN_DIRECT + (Ezusb_IOCTL_INDEX + 20) * 4////////////////////////////////////
Public Type USBDeviceDescriptorType
bDescriptorLength As Byte
bDescriptor As Byte
iSpecRelease As Integer
bDeviceClass As Byte
bDeviceSubClass As Byte
bDeviceProtocol As Byte
bMaxPacketSize As Byte
iVendorID As Integer
iProductID As Integer
iDeviceRelease As Integer
bManufacturer As Byte
bProduct As Byte
bSerialNumber As Byte
bNumberConfigurations As Byte
fill(128) As Byte
End TypePublic Type BulkTransferControlType
lPipeNum As Long
End Type
////////////////////////////////////
////////////////////////////////////
Public Enum USBDPipeEnum
eUsbdPipeTypeControl = 0
eUsbdPipeTypeIsochronous
eUsbdPipeTypeBulk
eUsbdPipeTypeInterrupt
End Enum
////////////////////////////////////
////////////////////////////////////
Public Type USBDPipeInformationTypeiMaximumPacketSize As Integer
bEndpointAddress As Byte bInterval As Byte PipeType As USBDPipeEnum
lPipeHandle As LonglMaximumTransferSize As Long lPipeFlags As Long
End Type////////////////////////////////////
////////////////////////////////////
Public Type USBDInterfaceInformationType
iLength As Integer bInterfaceNumber As Byte
bAlternateSetting As BytebClass As Byte
bSubClass As Byte
bProtocol As Byte
bReserved As BytelInterfaceHandle As Long
lNumberOfPipes As LongPipes(MAX_PIPES) As USBDPipeInformationType
End Type////////////////////////////////////
主要就是帮我把下面这段翻译一下。
////////////////////////////////////
Function DoBulkXfer(strDriver As String, pipe As Integer, ByRef buffer() As Byte, ByRef dataLen As Long) As LongDim hDriverHandle As Long
Dim result As Long
Dim btc As BulkTransferControlType
Dim pi As USBDInterfaceInformationType
Dim usbDD As USBDeviceDescriptorType
Dim lBytesReturned As LonghDriverHandle = OpenDriver(strDriver)
'
If hDriverHandle > 0 Thenresult = DeviceIoControl(hDriverHandle, IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR, usbDD, Len(usbDD), usbDD, Len(usbDD), lBytesReturned, 0)If result = 0 Then: DoBulkXfer = resultIf usbDD.iVendorID <> &H547 And usbDD.iProductID <> &H1002 Then
DoBulkXfer = eBadPipe
Exit Function
End Ifresult = GetPipeInfo(strDriver, pi)If result = 0 Then
DoBulkXfer = result
Exit Function
End IfIf pi.lNumberOfPipes <= pipe Then
DoBulkXfer = eBadPipe
Exit Function
Else
btc.lPipeNum = pipe
End IfIf (pi.Pipes(pipe).bEndpointAddress > 128) Then
result = DeviceIoControl(hDriverHandle, IOCTL_EZUSB_BULK_READ, btc, Len(btc), buffer(0), dataLen, dataLen, 0)
Else
result = DeviceIoControl(hDriverHandle, IOCTL_EZUSB_BULK_WRITE, btc, Len(btc), buffer(0), dataLen, dataLen, 0)
End IfCloseHandle (hDriverHandle)DoBulkXfer = resultElseDoBulkXfer = eBadDriverEnd IfEnd Function////////////////////////////////////
////////////////////////////////////
Function GetPipeInfo(strDriver As String, pi As USBDInterfaceInformationType) As LongDim result As Long
Dim hDriverHandle As Long
Dim lBytesReturned As LonghDriverHandle = OpenDriver(strDriver)GetPipeInfo = 0If hDriverHandle > 0 Thenresult = DeviceIoControl(hDriverHandle, IOCTL_Ezusb_GET_PIPE_INFO, pi, Len(pi), pi, Len(pi), lBytesReturned, 0)
CloseHandle (hDriverHandle)
End IfGetPipeInfo = resultEnd Function////////////////////////////////////
这个我已经写好了。
////////////////////////////////////
Function OpenDriver(sDriverName As String) As LongDim result As Long
Dim driverName As StringdriverName = "\\.\" & sDriverNameresult = CreateFile(driverName, (GENERIC_READ Or GENERIC_WRITE), (FILE_SHARE_READ Or FILE_SHARE_WRITE), ByVal 0, OPEN_EXISTING, 0&, 0)If result < 0 Then
result = GetLastError()
End If
OpenDriver = resultEnd Function////////////////////////////////////
////////////////////////////////////
Sub ErrMsg(err As ErrorEnum)Select Case errCase eBadDriver
MsgBox "Selected EZ-USB Device Driver was not found. Perhaps no device is connected.", vbOKOnly + vbCritical, "BulkXFer Error"
Case eBadPipe
MsgBox "Correct Pipe not found. Perhaps ""Ep-pair.hex"" was not downloaded to a development board.", vbOKOnly + vbCritical, "BulkXFer Error"
Case Else
MsgBox "Unknown Error.", vbOKOnly + vbCritical, "BulkXFer Error"
End SelectEnd Sub
Option Explicit
' = = = = W I N A P I = = = = 这些在DELPHI都不用理
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long
Public Declare Function GetLastError Lib "kernel32" () As LongPublic Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_SHARE_READ = &H1
Public Const FILE_SHARE_WRITE = &H2
Public Const OPEN_EXISTING = 3Public Const METHOD_BUFFERED = 0
Public Const METHOD_IN_DIRECT = 1
Public Const METHOD_OUT_DIRECT = 2Public Const MAX_PIPES = 16
Public Const MAX_USB_DEV_NUMBER = 32Enum ErrorEnum
eBadParam = -1
eBadDriver = -2
eBadPipe = -3
End EnumEnum EZ_ReadOrWrite
eWrite = 1
eRead = 0
End EnumPublic Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End TypePrivate Const Ezusb_IOCTL_INDEX = &H800Public Const IOCTL_Ezusb_GET_PIPE_INFO = _
&H220000 + METHOD_BUFFERED + (Ezusb_IOCTL_INDEX + 0) * 4Private Const IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR = _
&H220000 + METHOD_BUFFERED + (Ezusb_IOCTL_INDEX + 1) * 4Public Const IOCTL_EZUSB_BULK_READ = _
&H220000 + METHOD_OUT_DIRECT + (Ezusb_IOCTL_INDEX + 19) * 4Public Const IOCTL_EZUSB_BULK_WRITE = _
&H220000 + METHOD_IN_DIRECT + (Ezusb_IOCTL_INDEX + 20) * 4////////////////////////////////////
Public Type USBDeviceDescriptorType
bDescriptorLength As Byte
bDescriptor As Byte
iSpecRelease As Integer
bDeviceClass As Byte
bDeviceSubClass As Byte
bDeviceProtocol As Byte
bMaxPacketSize As Byte
iVendorID As Integer
iProductID As Integer
iDeviceRelease As Integer
bManufacturer As Byte
bProduct As Byte
bSerialNumber As Byte
bNumberConfigurations As Byte
fill(128) As Byte
End TypePublic Type BulkTransferControlType
lPipeNum As Long
End Type
////////////////////////////////////
////////////////////////////////////
Public Enum USBDPipeEnum
eUsbdPipeTypeControl = 0
eUsbdPipeTypeIsochronous
eUsbdPipeTypeBulk
eUsbdPipeTypeInterrupt
End Enum
////////////////////////////////////
////////////////////////////////////
Public Type USBDPipeInformationTypeiMaximumPacketSize As Integer
bEndpointAddress As Byte bInterval As Byte PipeType As USBDPipeEnum
lPipeHandle As LonglMaximumTransferSize As Long lPipeFlags As Long
End Type////////////////////////////////////
////////////////////////////////////
Public Type USBDInterfaceInformationType
iLength As Integer bInterfaceNumber As Byte
bAlternateSetting As BytebClass As Byte
bSubClass As Byte
bProtocol As Byte
bReserved As BytelInterfaceHandle As Long
lNumberOfPipes As LongPipes(MAX_PIPES) As USBDPipeInformationType
End Type////////////////////////////////////
主要就是帮我把下面这段翻译一下。
////////////////////////////////////
Function DoBulkXfer(strDriver As String, pipe As Integer, ByRef buffer() As Byte, ByRef dataLen As Long) As LongDim hDriverHandle As Long
Dim result As Long
Dim btc As BulkTransferControlType
Dim pi As USBDInterfaceInformationType
Dim usbDD As USBDeviceDescriptorType
Dim lBytesReturned As LonghDriverHandle = OpenDriver(strDriver)
'
If hDriverHandle > 0 Thenresult = DeviceIoControl(hDriverHandle, IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR, usbDD, Len(usbDD), usbDD, Len(usbDD), lBytesReturned, 0)If result = 0 Then: DoBulkXfer = resultIf usbDD.iVendorID <> &H547 And usbDD.iProductID <> &H1002 Then
DoBulkXfer = eBadPipe
Exit Function
End Ifresult = GetPipeInfo(strDriver, pi)If result = 0 Then
DoBulkXfer = result
Exit Function
End IfIf pi.lNumberOfPipes <= pipe Then
DoBulkXfer = eBadPipe
Exit Function
Else
btc.lPipeNum = pipe
End IfIf (pi.Pipes(pipe).bEndpointAddress > 128) Then
result = DeviceIoControl(hDriverHandle, IOCTL_EZUSB_BULK_READ, btc, Len(btc), buffer(0), dataLen, dataLen, 0)
Else
result = DeviceIoControl(hDriverHandle, IOCTL_EZUSB_BULK_WRITE, btc, Len(btc), buffer(0), dataLen, dataLen, 0)
End IfCloseHandle (hDriverHandle)DoBulkXfer = resultElseDoBulkXfer = eBadDriverEnd IfEnd Function////////////////////////////////////
////////////////////////////////////
Function GetPipeInfo(strDriver As String, pi As USBDInterfaceInformationType) As LongDim result As Long
Dim hDriverHandle As Long
Dim lBytesReturned As LonghDriverHandle = OpenDriver(strDriver)GetPipeInfo = 0If hDriverHandle > 0 Thenresult = DeviceIoControl(hDriverHandle, IOCTL_Ezusb_GET_PIPE_INFO, pi, Len(pi), pi, Len(pi), lBytesReturned, 0)
CloseHandle (hDriverHandle)
End IfGetPipeInfo = resultEnd Function////////////////////////////////////
这个我已经写好了。
////////////////////////////////////
Function OpenDriver(sDriverName As String) As LongDim result As Long
Dim driverName As StringdriverName = "\\.\" & sDriverNameresult = CreateFile(driverName, (GENERIC_READ Or GENERIC_WRITE), (FILE_SHARE_READ Or FILE_SHARE_WRITE), ByVal 0, OPEN_EXISTING, 0&, 0)If result < 0 Then
result = GetLastError()
End If
OpenDriver = resultEnd Function////////////////////////////////////
////////////////////////////////////
Sub ErrMsg(err As ErrorEnum)Select Case errCase eBadDriver
MsgBox "Selected EZ-USB Device Driver was not found. Perhaps no device is connected.", vbOKOnly + vbCritical, "BulkXFer Error"
Case eBadPipe
MsgBox "Correct Pipe not found. Perhaps ""Ep-pair.hex"" was not downloaded to a development board.", vbOKOnly + vbCritical, "BulkXFer Error"
Case Else
MsgBox "Unknown Error.", vbOKOnly + vbCritical, "BulkXFer Error"
End SelectEnd Sub
'下面这些是在FRM里面摘出来的文件
好像是类似于delphi的formCreate之类的吧。///////////////////////////////////Attribute VB_Name = "frmBulk"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = FalseOption ExplicitPublic strBuffer As String////////////////////////////////////
////////////////////////////////////
Private Sub Form_Load()
Dim Index As Integer
Dim sDriverName As String
Dim hDriver As LongFor Index = 0 To MAX_USB_DEV_NUMBER - 1
sDriverName = "Ezusb-" & Index ////调用EZUSB-0或-1
hDriver = OpenDriver(sDriverName)
If hDriver > 0 Then
cmbDriverName.AddItem sDriverName
CloseHandle hDriver
End If
NextIf cmbDriverName.ListCount > 0 Then
cmbDriverName.Text = cmbDriverName.List(0)
Else
ErrMsg (eBadDriver)
End
End If
End Sub////////////////////////////////////
////////////////////////////////////
Private Sub txtIn_Change()Dim buffer(63) As Byte
Dim result As Long
Dim i As Integer
Dim lDataLen As Long
Dim sDriverName As StringstrBuffer = strBuffer + Right(txtIn, 1)If Len(strBuffer) = Val(txtBlkSize.Text) ThenlDataLen = Len(strBuffer)
For i = 1 To lDataLen
buffer(i - 1) = Asc(Mid(strBuffer, i, 1))
Next
strBuffer = ""sDriverName = cmbDriverName.Textresult = DoBulkXfer(sDriverName, eWrite, buffer, lDataLen)
If result <> 1 Then: ErrMsg (result): Exit SubFor i = 0 To 63 ' no rabbits up my sleeve
buffer(i) = 0
Nextresult = DoBulkXfer(sDriverName, eRead, buffer, lDataLen)
If result <> 1 Then: ErrMsg (result): Exit SubFor i = 1 To lDataLen
txtOut.Text = txtOut.Text + Chr(buffer(i - 1))
NextEnd If ' xfer trigger reachedEnd Sub////////////////////////////////////
////////////////////////////////////
Private Sub txtBlkSize_Change()
Dim temp As Integertemp = Val(txtBlkSize)If temp < 0 Or temp > 64 Then
MsgBox "Enter a valid Bulk Transfer block size between 1 and 64.", vbInformation, "Input Error"
txtBlkSize.SelStart = 0
txtBlkSize.SelLength = 3
End If
End SubPrivate Sub cmdClearIn_Click()
txtIn.Text = ""
End SubPrivate Sub cmdClearOut_Click()
txtOut.Text = ""
End SubPrivate Sub Form_Activate()
txtBlkSize.SetFocus
End Sub
bDescriptorLength As Byte
bDescriptor As Byte
iSpecRelease As Integer
bDeviceClass As Byte
bDeviceSubClass As Byte
bDeviceProtocol As Byte
bMaxPacketSize As Byte
iVendorID As Integer
iProductID As Integer
iDeviceRelease As Integer
bManufacturer As Byte
bProduct As Byte
bSerialNumber As Byte
bNumberConfigurations As Byte
fill(128) As Byte
End Type那其它不要理啦,上面这一段在delphi要如何写呀。