| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 851 人关注过本帖
标题:VFP API有没有方法 直接读取USB设备的数据。
只看楼主 加入收藏
ljb8864
Rank: 2
等 级:论坛游民
帖 子:171
专家分:14
注 册:2023-6-4
结帖率:2.78%
收藏
已结贴  问题点数:20 回复次数:12 
VFP API有没有方法 直接读取USB设备的数据。
VFP API有没有方法 直接读取USB设备的数据。 ?
搜索更多相关主题的帖子: 设备 读取 API USB 数据 
2023-07-05 07:34
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:5 
USB设备的范围就大了,要什么数据?
2023-07-05 10:21
iswith
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:5
帖 子:448
专家分:582
注 册:2013-5-14
收藏
得分:5 
LibUSB,是很有名的开源usb驱动,其提供的API可以很方便的操作linux或者WIN下的USB设备,非常的方便,关键它是C写的,VFP调用都不用注册。自己百度
2023-07-05 12:22
ljb8864
Rank: 2
等 级:论坛游民
帖 子:171
专家分:14
注 册:2023-6-4
收藏
得分:0 
回复 3楼 iswith
厉害!
SetupDiGetClassDevs 获取一个指定类别或全部类别的所有已安装设备的信息
2023-07-05 13:55
ljb8864
Rank: 2
等 级:论坛游民
帖 子:171
专家分:14
注 册:2023-6-4
收藏
得分:0 
回复 2楼 吹水佬
一般是读USB扫描枪,读取USB设备数据

网上说使用USB口条码枪可以使用一个串口仿真驱动将USB口模拟成串口,

就是有没有直接读USB数据的?

VB与USB有通讯
Option Explicit
Dim EventObject As Long
Dim HIDOverlapped As OVERLAPPED
Dim Capabilities As HIDP_CAPS
Dim DetailData As Long
Dim DetailDataBuffer() As Byte
Dim DeviceAttributes As HIDD_ATTRIBUTES
Dim DevicePathName As String
Dim DeviceInfoSet As Long
Dim HidDevice As Long
Dim RidDevice As Long
'Dim WidDevice As Long
Dim LastDevice As Boolean
Dim Security As SECURITY_ATTRIBUTES
Dim MyDeviceDetected As Boolean
Dim MyDeviceInfoData As SP_DEVINFO_DATA
Dim MyDeviceInterfaceDetailData As SP_DEV IC E_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
Dim DeviceDetected As Boolean
Const MyVendorID = &H8888 '设备ID
Const MyProductID = &H6


Function FindTheHid() As Boolean


Dim Count As Integer
Dim GUIDString As String
Dim HidGuid As GUID
Dim MemberIndex As Long


LastDevice = False
MyDeviceDetected = False
Security.lpSecurityDescriptor = 0
Security.bInheritHandle = True
Security.nLength = Len(Security)
Result = HidD_GetHidGuid(HidGuid) '取得HID群组的GUID
DeviceInfoSet = SetupDiGetClassDevs _
    (HidGuid, _
    vbNullString, _
    0, _
    (DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE)) '取得所有HID信息的结构数组
MemberIndex = 0


Do
    MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData)
    Result = SetupDiEnumDeviceInterfaces _
        (DeviceInfoSet, _
        0, _
        HidGuid, _
        MemberIndex, _
        MyDeviceInterfaceData) '识别每一个HID接口
    If Result = 0 Then LastDevice = True
    If Result <> 0 Then
    MyDeviceInfoData.cbSize = Len(MyDeviceInfoData)
        Result = SetupDiGetDeviceInterfaceDetail _
           (DeviceInfoSet, _
           MyDeviceInterfaceData, _
           0, _
           0, _
           Needed, _
           0) '取得设备路径(为了分配空间)
      
    DetailData = Needed
    MyDeviceInterfaceDetailData.cbSize = _
            Len(MyDeviceInterfaceDetailData)
    ReDim DetailDataBuffer(Needed)
    Call RtlMoveMemory _
            (DetailDataBuffer(0), _
            MyDeviceInterfaceDetailData, _
            4)
    Result = SetupDiGetDeviceInterfaceDetail _
           (DeviceInfoSet, _
           MyDeviceInterfaceData, _
           VarPtr(DetailDataBuffer(0)), _
           DetailData, _
           Needed, _
0)  '取得设备路径
DevicePathName = CStr(DetailDataBuffer())
        DevicePathName = StrConv(DevicePathName, vbUnicode)
        DevicePathName = Right$(DevicePathName, Len(DevicePathName) - 4)


      
    HidDevice = CreateFile _
            (DevicePathName, _
            0, _
            (FILE_SHARE_READ Or FILE_SHARE_WRITE), _
            Security, _
            OPEN_EXISTING, _
            FILE_ATTRIBUTE_NORMAL, _
            0) '取得设备的标示代号
    DeviceAttributes.Size = LenB(DeviceAttributes)
        Result = HidD_GetAttributes _
            (HidDevice, _
            DeviceAttributes) '取得厂商与产品ID
    If (DeviceAttributes.VendorID = MyVendorID) And _
            (DeviceAttributes.ProductID = MyProductID) Then
                MyDeviceDetected = True
        Else
                MyDeviceDetected = False
    Result = CloseHandle _
                    (HidDevice)
        End If
End If
MemberIndex = MemberIndex + 1


Loop Until (LastDevice = True) Or (MyDeviceDetected = True)
Result = SetupDiDestroyDeviceInfoList _
    (DeviceInfoSet)
If MyDeviceDetected = True Then
FindTheHid = True
Call GetDeviceCapabilities '调用获取设备能力
RidDevice = CreateFile _
            (DevicePathName, _
            (GENERIC_READ Or GENERIC_WRITE), _
            (FILE_SHARE_READ Or FILE_SHARE_WRITE), _
            Security, _
            OPEN_EXISTING, _
           FILE_FLAG_OVERLAPPED, _
            0)
  If EventObject = 0 Then
    EventObject = CreateEvent _
        (Security, _
        True, _
        True, _
        "")
End If


HIDOverlapped.Offset = 0
HIDOverlapped.OffsetHigh = 0
HIDOverlapped.hEvent = EventObject
Else
MsgBox "没找到设备!", vbOKOnly
End If
End Function


Private Sub Command1_Click()


    Call ReadAndWriteToDevice
    Timer1.Enabled = True
End Sub


 


Private Sub Command2_Click()
If Command2.Caption = "打开设备" Then
   Command2.Caption = "关闭设备"
DeviceDetected = FindTheHid
Else
  Command2.Caption = "打开设备"
  Timer1.Enabled = False
  End If
  Text1.Text = ""
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Shutdown
End Sub
Private Sub Command3_Click()
Call Shutdown
End
End Sub
Private Sub Shutdown()


Result = CloseHandle _
    (HidDevice)
Result = CloseHandle _
    (RidDevice)
End Sub
Private Sub Form_Load()
Form1.Show
Timer1.Enabled = False
Timer1.Interval = 100
End Sub
Private Sub timer1_Timer()
Call ReadAndWriteToDevice
End Sub
Private Sub ReadAndWriteToDevice()
'Timer1.Enabled = False
If DeviceDetected = True Then
    Call ReadReport
    End If
End Sub
Private Sub GetDeviceCapabilities()
Dim ppData(29) As Byte
Dim ppDataString As Variant
Result = HidD_GetPreparsedData _
    (HidDevice, _
    PreparsedData)
Result = RtlMoveMemory _
    (ppData(0), _
    PreparsedData, _
    30)
ppDataString = ppData()
ppDataString = StrConv(ppDataString, vbUnicode)
Result = HidP_GetCaps _
    (PreparsedData, _
    Capabilities)
Dim ValueCaps(1023) As Byte


Result = HidP_GetValueCaps _
    (HidP_Input, _
    ValueCaps(0), _
    Capabilities.NumberInputValueCaps, _
    PreparsedData)
Result = HidD_FreePreparsedData _
    (PreparsedData)
End Sub
Private Sub ReadReport()
Dim Count
Dim NumberOfBytesRead As Long
Dim ReadBuffer() As Byte
Dim UBoundReadBuffer As Integer
Dim ByteValue As String
Dim length As Long
ReDim ReadBuffer(Capabilities.InputReportByteLength - 1)
'EventObject = CreateEvent _
'(Security, _
'True, _
'True, _
'"")
'HIDOverlapped.hEvent = EventObject
'HIDOverlapped.Offset = 0
'HIDOverlapped.OffsetHigh = 0


Result = ReadFile _
    (RidDevice, _
    ReadBuffer(0), _
    CLng(Capabilities.InputReportByteLength), _
    NumberOfBytesRead, _
    HIDOverlapped)
Result = WaitForSingleObject _
    (EventObject, _
    100)
    Select Case Result
    Case WAIT_TIMEOUT
    Result = CancelIo _
            (RidDevice)
            End Select
            Result = GetOverlappedResult _
            (RidDevice, _
            HIDOverlapped, _
            length, _
            True)
            If length = 9 Then
   Text1.Text = ""
    For Count = 1 To UBound(ReadBuffer)
    If Len(Hex$(ReadBuffer(Count))) < 2 Then
        ByteValue = "0" & Hex$(ReadBuffer(Count))
    Else
        ByteValue = Hex$(ReadBuffer(Count))
    End If
    Text1.SelStart = Len(Text1.Text)
    Text1.SelText = ByteValue & " "
Next Count
End If
Call ResetEvent(EventObject)
'Timer1.Enabled = True
End Sub

[此贴子已经被作者于2023-7-5 14:05编辑过]

2023-07-05 13:56
iswith
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:5
帖 子:448
专家分:582
注 册:2013-5-14
收藏
得分:0 
如果是使用USB口条码枪,一般厂家都会提供SDK的DLL接入,问厂家要驱动,以前玩过都模拟成COM来的完成的!可以无光标接入数据,我还以为你要用USB做什么高端东西呢。
2023-07-05 14:23
laowan001
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:66
帖 子:1090
专家分:2682
注 册:2015-12-30
收藏
得分:5 
usb条码枪不是免驱吗?
难道是什么高级的枪,能说说型号吗?
2023-07-05 14:28
ljb8864
Rank: 2
等 级:论坛游民
帖 子:171
专家分:14
注 册:2023-6-4
收藏
得分:0 
网上找到一个VFP的,但没用
Clear
Local ans, DeviceNumber, DiskGUID, hSetup, Success, i, dia, da, didd
#Define GUID_DEVINTERFACE_USB_DEVICE "A5DCBF10-6530-11D2-901F-00C04FB951ED"
#Define INVALID_HANDLE_VALUE -1
#Define BUFFER_SIZE 2048
#Define REG_SZ 1
#Define GENERIC_WRITE 0x40000000
#Define FILE_SHARE_WRITE 0x2
#Define OPEN_EXISTING 0x3
#Define FILE_ATTRIBUTE_NORMAL 128
#Define DIGCF_PRESENT 0x2
#Define DIGCF_DEVICEINTERFACE 0x10

Declare Integer SetupDiGetClassDevs In "setupapi.dll" ;
String @ClassGuid, ;
Integer Enumerator, ;
Integer hwndParent, ;
Integer Flags
Declare Integer SetupDiEnumDeviceInterfaces In "setupapi.dll" ;
Integer DeviceInfoSet, ;
Integer DeviceInfoData, ;
String InterfaceClassGuid, ;
Integer MemberIndex, ;
String @DeviceInterfaceData
Declare Integer SetupDiDestroyDeviceInfoList In "setupapi.dll" ;
Integer DeviceInfoSet
Declare Integer SetupDiGetDeviceInterfaceDetail In "setupapi.dll" ;
Integer DeviceInfoSet, ;
String DeviceInterfaceData, ;
String @DeviceInterfaceDetailData, ;
Integer DeviceInterfaceDetailDataSize, ;
Integer @RequiredSize, ;
String @DeviceInfoData
Declare INTEGER CreateFile IN kernel32;
STRING lpFileName,INTEGER dwDesiredAccess,INTEGER dwShareMode,;
INTEGER lpSecurityAttr,INTEGER dwCreationDisp,INTEGER dwFlagsAndAttrs,;
INTEGER hTemplateFile
DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject
DECLARE INTEGER DeviceIoControl IN kernel32;
INTEGER hDevice,;
INTEGER dwIoControlCode,;
STRING @ lpInBuffer,;
INTEGER nInBufferSize,;
STRING @ lpOutBuffer,;
INTEGER nOutBufferSize,;
INTEGER @ lpBytesReturned,;
INTEGER lpOverlapped
DECLARE Long GetLastError IN WIN32API

**ÕaÖÖ»ñè¡μÄGUID2»DD
DiskGUID = guid(GUID_DEVINTERFACE_USB_DEVICE)
**ÕaÖÖ»ñè¡μÄGUID¿éòÔ£¬μ«Ö»Äüè¡μ½¼üÅìμÄDÅÏ¢£¬¿éêÇÔõÃ′»ñμÃÆäËûUSBé豸μÄDÅÏ¢ÄØ£¿
**oíòÔÇ°·¢μÄ′úÂëÖ»óDÕaàï2»í¬
DECLARE HidD_GetHidGuid IN HID STRING @ HidGuid
DiskGUID=REPLICATE(CHR(0),16)
HidD_GetHidGuid(@DiskGUID)
hSetup = SetupDiGetClassDevs(DiskGUID, 0, 0, Bitor(DIGCF_PRESENT, DIGCF_DEVICEINTERFACE))
*!*    WINSETUPAPI HDEVINFO SetupDiGetClassDevsW(
*!*      [in, optional] const GUID *ClassGuid,
*!*      [in, optional] PCWSTR     Enumerator,
*!*      [in, optional] HWND       hwndParent,
*!*      [in]           DWORD      Flags
*!*    )

If hSetup <> INVALID_HANDLE_VALUE
i = 0
Success=1
Do While Success<>0
dia = dword2str(28)+Replicate(Chr(0),16)+Replicate(Chr(0),4)+Replicate(Chr(0),4)
Success = SetupDiEnumDeviceInterfaces(hSetup, 0, DiskGUID, i, @dia)
If Success <> 0
da = dword2str(28)+Replicate(Chr(0),16)+Replicate(Chr(0),4)+Replicate(Chr(0),4)
didd = dword2str(5)+Replicate(Chr(0),BUFFER_SIZE)
nBytes = BUFFER_SIZE
nRequiredSize = 0
m.err1 = SetupDiGetDeviceInterfaceDetail(hSetup, dia, @didd, nBytes, @nRequiredSize, @da)
didd=SUBSTR(didd,5)
didd=LEFT(didd,AT(CHR(0),didd)-1)
hhcd=CreateFile(didd, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)

Declare LONG HidD_GetProductString IN "HID.dll" LONG Handle,STRING @BufferPtr, LONG Length
Declare LONG HidD_GetSerialNumberString IN "HID.dll" LONG Handle , STRING @BufferPtr, LONG Length
Declare LONG HidD_GetManufacturerString IN "HID.dll" LONG Handle , STRING @BufferPtr, LONG Length
Declare LONG HidD_GetPhysicalDescriptor IN "HID.dll" LONG Handle , STRING @BufferPtr, LONG Length

STU=REPLICATE(CHR(0),256)
HidD_GetManufacturerString( hHcd,@STU,256)
STU=STRCONV(STU,6)
?LEFT(STU,AT(CHR(0),STU)-1),"1"

STU=REPLICATE(CHR(0),256)
HidD_GetProductString( hHcd,@STU,256)
STU=STRCONV(STU,6)
?LEFT(STU,AT(CHR(0),STU)-1),"2"

STU=REPLICATE(CHR(0),256)
HidD_GetSerialNumberString( hHcd,@STU,256)
?LEFT(STU,AT(CHR(0),STU)-1),"3"

IF hhcd <>INVALID_HANDLE_VALUE
CloseHandle(hHcd)
endif
Endif
i = i+1
Enddo
Endif
SetupDiDestroyDeviceInfoList(hSetup)
Return

Function Guid
Lparameter m.guid
m.guid=Chrtran(m.guid,"{}-","")
Return ;
word2str(Eval("0x"+Substr(m.guid,5,4)))+ ;
word2str(Eval("0x"+Substr(m.guid,1,4)))+ ;
word2str(Eval("0x"+Substr(m.guid,9,4)))+ ;
word2str(Eval("0x"+Substr(m.guid,13,4)))+ ;
chr(Eval("0x"+Substr(m.guid,17,2)))+ ;
chr(Eval("0x"+Substr(m.guid,19,2)))+ ;
chr(Eval("0x"+Substr(m.guid,21,2)))+ ;
chr(Eval("0x"+Substr(m.guid,23,2)))+ ;
chr(Eval("0x"+Substr(m.guid,25,2)))+ ;
chr(Eval("0x"+Substr(m.guid,27,2)))+ ;
chr(Eval("0x"+Substr(m.guid,29,2)))+ ;
chr(Eval("0x"+Substr(m.guid,31,2)))
ENDFUNC


Function dword2str
Lparameter m.wrd
Local m.wrd1, m.st1
m.wrd1=m.wrd%4294967296
Declare RtlMoveMemory In Win32API String @st1, Integer @wrd, Integer Cnt
m.st1=Chr(0)+Chr(0)+Chr(0)+Chr(0)
RtlMoveMemory(@m.st1, @m.wrd, 4)
Return m.st1
ENDFUNC

Function word2str
Lparameter m.wrd
Local m.wrd1, m.st1
m.wrd1=m.wrd%65536
Declare RtlMoveMemory In Win32API String @st1, Integer @wrd, Integer Cnt
m.st1=Chr(0)+Chr(0)
RtlMoveMemory(@m.st1, @m.wrd, 2)
Return m.st1
ENDFUNC

以下是C如何列出设备
DeviceInfoSet = SetupDiGetClassDevs(
                                    NULL,
                                    NULL,
                                    NULL,
                                    DIGCF_ALLCLASSES | DIGCF_PRESENT);

ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA));
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DeviceIndex = 0;
   
while (SetupDiEnumDeviceInfo(
                             DeviceInfoSet,
                             DeviceIndex,
                             &DeviceInfoData)) {
    DeviceIndex++;

    if (!SetupDiGetDeviceProperty(
                                  DeviceInfoSet,
                                  &DeviceInfoData,
                                  &DEVPKEY_Device_Class,
                                  &PropType,
                                  (PBYTE)&DevGuid,
                                  sizeof(GUID),
                                  &Size,
                                  0) || PropType != DEVPROP_TYPE_GUID) {

        Error = GetLastError();

        if (Error == ERROR_NOT_FOUND) {
            \\
            \\ This device has an unknown device setup class.
            \\
            }
        }                 
    }

if (DeviceInfoSet) {
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);
    }
2023-07-05 14:29
iswith
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:5
帖 子:448
专家分:582
注 册:2013-5-14
收藏
得分:0 
先问厂家要驱动仿真成COM口,你直接对接COM口就完事了,不要把问题搞复杂。。。。
2023-07-05 14:38
ljb8864
Rank: 2
等 级:论坛游民
帖 子:171
专家分:14
注 册:2023-6-4
收藏
得分:0 
下面的API 怎么写才有效呢?
 lRet = ReadFile(Handle, Buffer(0), lBufferLength, lBytesRead, 0)  

2023-07-05 17:18
快速回复:VFP API有没有方法 直接读取USB设备的数据。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014401 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved