阅读:3708回复:8
不用EasyD12.dll怎么读写usb端口,在vb下?
在vb下可以用api函数直接调用吗?能给出读写例子吗?
|
|
沙发#
发布于:2003-08-20 08:53
可以,例子在周立功的书或软件包都有。
|
|
板凳#
发布于:2003-08-20 17:55
书我看了,也是要用到EasyD12.dll的,我的意思是说直接在vb下用createfile、writefile、readfile等实现。
|
|
地板#
发布于:2003-08-21 08:56
既然你看了,肯定没看清楚。书上就有使用api不用easyd12.dll的例子,按部就班编写,我就是参考那个做的,没问题的。
|
|
地下室#
发布于:2003-08-21 17:16
clementzhao:
那个是vc的,我看不太懂,而且我对api向来头疼。你若愿意提供vb编的api函数,我奉上50分! |
|
5楼#
发布于:2003-08-22 08:51
嗯,我有,不过要等一下。程序不在我这,同事还没回来。其实主要是声明一下使用api,其他跟书上都是一样的。
|
|
6楼#
发布于:2003-08-22 15:56
好了,原程(能用的):
Public Sub Colse_Dev(iDev As DevType) CloseHandle iDev.rhDev CloseHandle iDev.whDev CloseHandle iDev.hDev iDev.bhDev = False End Sub Public Function WritePort2(iDev As DevType, ByRef pData As Byte, ByRef iLen As Integer) As Long Dim lRet As Long WriteFile iDev.whDev, pData, iLen, lRet, 0& If lRet <> 0 Then WritePort2 = 0 Else WritePort2 = -1 End If End Function Public Function ReadPort2(iDev As DevType, ByRef pData As Byte, ByRef iLen As Integer) As Long Dim lRet As Long ReadFile iDev.rhDev, pData, iLen, lRet, 0& If lRet <> 0 Then ReadPort2 = 0 Else ReadPort2 = -1 End If End Function Public Function Open_Dev() As DevType Dim DeviceInfoSet As Long Dim LinkGuid As GUID Dim MyDeviceInfoData As SP_DEVINFO_DATA Dim MyDeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA Dim MyDeviceInterfaceData As SP_DEVICE_INTERFACE_DATA Dim LastDevice As Boolean Dim MyDeviceDetected As Boolean Dim UsbDevice As Long Dim Needed As Long Dim DetailDataBuffer() As Byte Dim DevicePathName As String Dim Result As Long Dim MemberIndex As Long LinkGuid.Data1 = &H77F49320 LinkGuid.Data2 = &H16EF LinkGuid.Data3 = &H11D2 LinkGuid.Data4(0) = &HAD LinkGuid.Data4(1) = &H51 LinkGuid.Data4(2) = &H0 LinkGuid.Data4(3) = &H60 LinkGuid.Data4(4) = &H97 LinkGuid.Data4(5) = &HB5 LinkGuid.Data4(6) = &H14 LinkGuid.Data4(7) = &HDD \'DEFINE_GUID(GUID_CLASS_D12_BULK, \'0x77f49320, 0x16ef, 0x11d2, 0xad, 0x51, 0x0, 0x60, 0x97, 0xb5, 0x14, 0xdd); DeviceInfoSet = SetupDiGetClassDevs _ (LinkGuid, _ vbNullString, _ 0, _ (DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE)) MemberIndex = 0 MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData) Result = SetupDiEnumDeviceInterfaces _ (DeviceInfoSet, _ 0, _ LinkGuid, _ MemberIndex, _ MyDeviceInterfaceData) If Result <> 1 Then Open_Dev.bhDev = False Exit Function End If Result = SetupDiGetDeviceInterfaceDetail _ (DeviceInfoSet, _ MyDeviceInterfaceData, _ 0, _ 0, _ Needed, _ 0) DetailData = Needed If Result <> 0 Then Open_Dev.bhDev = False Exit Function End If MyDeviceInterfaceDetailData.cbSize = _ Len(MyDeviceInterfaceDetailData) ReDim DetailDataBuffer(Needed) \'Store cbSize in the first four bytes of the array. Call RtlMoveMemory _ (DetailDataBuffer(0), _ MyDeviceInterfaceDetailData, _ 4) \'Call SetupDiGetDeviceInterfaceDetail again. \'This time, pass the address of the first element of DetailDataBuffer \'and the returned required buffer size in DetailData. Result = SetupDiGetDeviceInterfaceDetail _ (DeviceInfoSet, _ MyDeviceInterfaceData, _ VarPtr(DetailDataBuffer(0)), _ DetailData, _ Needed, _ 0) If Result <> 1 Then Open_Dev.bhDev = False Exit Function End If DevicePathName = CStr(DetailDataBuffer()) DevicePathName = StrConv(DevicePathName, vbUnicode) \'Strip cbSize (4 bytes) from the beginning. DevicePathName = right$(DevicePathName, Len(DevicePathName) - 4) DevicePathName = left$(DevicePathName, Len(DevicePathName) - 2) UsbDevice = CreateFile _ (DevicePathName, _ GENERIC_READ Or GENERIC_WRITE, _ (FILE_SHARE_READ Or FILE_SHARE_WRITE), _ 0&, _ OPEN_EXISTING, _ 0, _ 0) If UsbDevice = -1 Then Open_Dev.bhDev = False CloseHandle (UsbDevice) Exit Function End If Dim whFile, ohFile, lRet As Long whFile = CreateFile _ (DevicePathName + \"\\PIPE03\", _ GENERIC_READ Or GENERIC_WRITE, _ (FILE_SHARE_READ Or FILE_SHARE_WRITE), _ 0&, _ OPEN_EXISTING, _ 0, _ 0) If whFile = -1 Then Open_Dev.bhDev = False CloseHandle whFile CloseHandle (UsbDevice) Exit Function End If rhFile = CreateFile _ (DevicePathName + \"\\PIPE02\", _ GENERIC_READ Or GENERIC_WRITE, _ (FILE_SHARE_READ Or FILE_SHARE_WRITE), _ 0&, _ OPEN_EXISTING, _ 0, _ 0) If rhFile = -1 Then Open_Dev.bhDev = False CloseHandle rhFile CloseHandle whFile CloseHandle (UsbDevice) Exit Function End If Open_Dev.bhDev = True Open_Dev.hDev = UsbDevice Open_Dev.rhDev = rhFile Open_Dev.whDev = whFile End Function 慢慢看,因为vb下不是我写的,可能需要修改。 [编辑 - 8/22/03 by clementzhao] |
|
7楼#
发布于:2003-08-25 11:02
首先,你要把USB做成一个HID设备。然后就可以了,最近会有资料update在www.eyeteck.com.
|
|
8楼#
发布于:2010-05-19 11:49
回 6楼(clementzhao) 的帖子
您好,可以留下QQ吗?需要向您请教问题,我的QQ:317620361 (如果您愿意可以加我QQ,验证信息填写“USB驱动”)谢谢:) |
|