阅读:1836回复:6
那位朋友能帮我把EZUSB例程中的VB程序写成DELPHI呀。
我看了一下,应该不是很难,只可怜我不懂VB。好像就只有二个function在调用。或者那位好心的朋友给我一个DELPHI的例程,感激不尽呀。。。AN2131--Delphi
|
|
沙发#
发布于:2003-09-17 13:42
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 Long \' = = = = C O N S T A N T S = = = = Public 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 = 3 Public Const METHOD_BUFFERED = 0 Public Const METHOD_IN_DIRECT = 1 Public Const METHOD_OUT_DIRECT = 2 Public Const MAX_PIPES = 16 Public Const MAX_USB_DEV_NUMBER = 32 Enum ErrorEnum eBadParam = -1 eBadDriver = -2 eBadPipe = -3 End Enum Enum EZ_ReadOrWrite eWrite = 1 eRead = 0 End Enum Public Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Boolean End Type Private Const Ezusb_IOCTL_INDEX = &H800 Public Const IOCTL_Ezusb_GET_PIPE_INFO = _ &H220000 + METHOD_BUFFERED + (Ezusb_IOCTL_INDEX + 0) * 4 Private Const IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR = _ &H220000 + METHOD_BUFFERED + (Ezusb_IOCTL_INDEX + 1) * 4 Public Const IOCTL_EZUSB_BULK_READ = _ &H220000 + METHOD_OUT_DIRECT + (Ezusb_IOCTL_INDEX + 19) * 4 Public 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 Type Public Type BulkTransferControlType lPipeNum As Long End Type //////////////////////////////////// //////////////////////////////////// Public Enum USBDPipeEnum eUsbdPipeTypeControl = 0 eUsbdPipeTypeIsochronous eUsbdPipeTypeBulk eUsbdPipeTypeInterrupt End Enum //////////////////////////////////// //////////////////////////////////// Public Type USBDPipeInformationType iMaximumPacketSize As Integer bEndpointAddress As Byte bInterval As Byte PipeType As USBDPipeEnum lPipeHandle As Long lMaximumTransferSize As Long lPipeFlags As Long End Type //////////////////////////////////// //////////////////////////////////// Public Type USBDInterfaceInformationType iLength As Integer bInterfaceNumber As Byte bAlternateSetting As Byte bClass As Byte bSubClass As Byte bProtocol As Byte bReserved As Byte lInterfaceHandle As Long lNumberOfPipes As Long Pipes(MAX_PIPES) As USBDPipeInformationType End Type //////////////////////////////////// 主要就是帮我把下面这段翻译一下。 //////////////////////////////////// Function DoBulkXfer(strDriver As String, pipe As Integer, ByRef buffer() As Byte, ByRef dataLen As Long) As Long Dim hDriverHandle As Long Dim result As Long Dim btc As BulkTransferControlType Dim pi As USBDInterfaceInformationType Dim usbDD As USBDeviceDescriptorType Dim lBytesReturned As Long hDriverHandle = OpenDriver(strDriver) \' If hDriverHandle > 0 Then result = DeviceIoControl(hDriverHandle, IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR, usbDD, Len(usbDD), usbDD, Len(usbDD), lBytesReturned, 0) If result = 0 Then: DoBulkXfer = result If usbDD.iVendorID <> &H547 And usbDD.iProductID <> &H1002 Then DoBulkXfer = eBadPipe Exit Function End If result = GetPipeInfo(strDriver, pi) If result = 0 Then DoBulkXfer = result Exit Function End If If pi.lNumberOfPipes <= pipe Then DoBulkXfer = eBadPipe Exit Function Else btc.lPipeNum = pipe End If If (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 If CloseHandle (hDriverHandle) DoBulkXfer = result Else DoBulkXfer = eBadDriver End If End Function //////////////////////////////////// //////////////////////////////////// Function GetPipeInfo(strDriver As String, pi As USBDInterfaceInformationType) As Long Dim result As Long Dim hDriverHandle As Long Dim lBytesReturned As Long hDriverHandle = OpenDriver(strDriver) GetPipeInfo = 0 If hDriverHandle > 0 Then result = DeviceIoControl(hDriverHandle, IOCTL_Ezusb_GET_PIPE_INFO, pi, Len(pi), pi, Len(pi), lBytesReturned, 0) CloseHandle (hDriverHandle) End If GetPipeInfo = result End Function //////////////////////////////////// 这个我已经写好了。 //////////////////////////////////// Function OpenDriver(sDriverName As String) As Long Dim result As Long Dim driverName As String driverName = \"\\\\.\\\" & sDriverName result = 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 = result End Function //////////////////////////////////// //////////////////////////////////// Sub ErrMsg(err As ErrorEnum) Select Case err Case 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 Select End Sub /////////////////////////////////// /////////////////////////////////// /////////////////////////////////// /////////////////////////////////// \'下面这些是在FRM里面摘出来的文件 好像是类似于delphi的formCreate之类的吧。 /////////////////////////////////// Attribute VB_Name = \"frmBulk\" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit Public strBuffer As String //////////////////////////////////// //////////////////////////////////// Private Sub Form_Load() Dim Index As Integer Dim sDriverName As String Dim hDriver As Long For 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 Next If 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 String strBuffer = strBuffer + Right(txtIn, 1) If Len(strBuffer) = Val(txtBlkSize.Text) Then lDataLen = Len(strBuffer) For i = 1 To lDataLen buffer(i - 1) = Asc(Mid(strBuffer, i, 1)) Next strBuffer = \"\" sDriverName = cmbDriverName.Text result = DoBulkXfer(sDriverName, eWrite, buffer, lDataLen) If result <> 1 Then: ErrMsg (result): Exit Sub For i = 0 To 63 \' no rabbits up my sleeve buffer(i) = 0 Next result = DoBulkXfer(sDriverName, eRead, buffer, lDataLen) If result <> 1 Then: ErrMsg (result): Exit Sub For i = 1 To lDataLen txtOut.Text = txtOut.Text + Chr(buffer(i - 1)) Next End If \' xfer trigger reached End Sub //////////////////////////////////// //////////////////////////////////// Private Sub txtBlkSize_Change() Dim temp As Integer temp = 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 Sub Private Sub cmdClearIn_Click() txtIn.Text = \"\" End Sub Private Sub cmdClearOut_Click() txtOut.Text = \"\" End Sub Private Sub Form_Activate() txtBlkSize.SetFocus End Sub |
|
板凳#
发布于:2003-09-17 13:44
Private Const Ezusb_IOCTL_INDEX = &H800
Public Const IOCTL_Ezusb_GET_PIPE_INFO = _ &H220000 + METHOD_BUFFERED + (Ezusb_IOCTL_INDEX + 0) * 4 Private Const IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR = _ &H220000 + METHOD_BUFFERED + (Ezusb_IOCTL_INDEX + 1) * 4 Public Const IOCTL_EZUSB_BULK_READ = _ &H220000 + METHOD_OUT_DIRECT + (Ezusb_IOCTL_INDEX + 19) * 4 Public Const IOCTL_EZUSB_BULK_WRITE = _ &H220000 + METHOD_IN_DIRECT + (Ezusb_IOCTL_INDEX + 20) * 4 请问一下,上面这些在delphi上要如何写呀。..... |
|
地板#
发布于:2003-09-18 09:18
这应该是公有、私有的定量定义。
|
|
地下室#
发布于:2003-09-18 10:21
谢谢你。
天呀,那位朋友有delphi的代码呀,只要一小段就行了。本人对VB不懂呀。。痛哭ing........... |
|
5楼#
发布于:2004-01-31 13:58
完全用delphi写的控制ezusb已经写好了。全pascal语言。an2131系列芯片。
|
|
6楼#
发布于:2010-03-25 21:32
回 5楼(kimlon) 的帖子
我也在开发中,可以把你的例子发到我的邮箱吗,我的邮箱是indexhome@163.com,谢谢你啊 |
|