📄 vb_usb不使用easyd12对端口操作.txt
字号:
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -