Page 1 of 5

USB HID Library

Posted: Mon Apr 05, 2010 1:28 pm
by User_Russian
This library allows to gain access to USB HID device, from programs, written in PureBasic.

Download

x86
PureBasic 4.00 — 4.20
PureBasic 4.30 — 4.41
PureBasic 4.50 and 4.51
PureBasic 4.60, 4.61

x64
PureBasic 4.31 x64
PureBasic 4.50 x64
PureBasic 4.60, 4.61 x64

English help


Example:

Code: Select all

#USB_PID=$1234
#USB_VID=$1212
#NO_Device="No HID device"
LoadFont(2,"Arial",22,#PB_Font_Bold)

Procedure FindDevice_Timer()
Shared DeviceHandle
Static Old_Test
Test=HID_Lib_DeviceTest(#USB_PID, #USB_VID)
 If Test<>Old_Test
  Old_Test=Test 
  If Test 
     HID_Lib_CloseDevice(DeviceHandle)
     DeviceHandle=HID_Lib_OpenDevice(#USB_PID, #USB_VID)
     SetGadgetText(0,"Discovered device")
  Else
     HID_Lib_CloseDevice(DeviceHandle)
     DeviceHandle=0
     SetGadgetText(0,#NO_Device)
     SetGadgetText(1,"OFF")
  EndIf
 EndIf
EndProcedure

Procedure ReadDevice_Thread(*x)
 Shared DeviceHandle
 Repeat
   If DeviceHandle
     In.l=0
     HID_Lib_ReadDevice(DeviceHandle, @In, 3)
       In=In>>8
       Result.f=In/16
       If Result<150 And Result>-50
         SetGadgetText(1,StrF(Result,1)+" °C")
       EndIf
   EndIf
   Delay(100)
 ForEver
EndProcedure

OpenWindow(0,0,0,170,70,"USB_Termo", #PB_Window_MinimizeGadget|#PB_Window_Invisible|#PB_Window_ScreenCentered)
 StickyWindow(0,1)
 TextGadget(0,4,4,162,16,#NO_Device,#PB_Text_Center)
 StringGadget(1,16,24,140,40,"OFF",#PB_Text_Center|#PB_String_ReadOnly)
   SetGadgetFont(1,FontID(2))
   SetGadgetColor(1,#PB_Gadget_FrontColor,$DF0000)
FindDevice_Timer()
HideWindow(0,0)

AddWindowTimer(0,1,400)
CreateThread(@ReadDevice_Thread(),0)

Repeat
   Event=WaitWindowEvent()
   If Event=#PB_Event_Timer
       If EventTimer()=1
         FindDevice_Timer()
       EndIf
   EndIf
Until Event=#PB_Event_CloseWindow
Image



Image

Re: HID_Lib - Library

Posted: Mon Apr 05, 2010 3:11 pm
by +18
thanks for sharing :D

Re: HID_Lib - Library

Posted: Tue Apr 06, 2010 3:07 am
by idle
thanks

Re: HID_Lib - Library

Posted: Tue Apr 06, 2010 9:37 am
by SFSxOI
Nice, Thank You. :)

Re: HID_Lib - Library

Posted: Tue Apr 06, 2010 10:17 am
by jamirokwai
Hi User_Russian,

looks promising. But could you provide us a Linux and/or Mac OS X-Lib?
I don't know if it's possible without big changes :-)

Re: HID_Lib - Library

Posted: Tue Apr 06, 2010 10:27 am
by User_Russian
In library were used WinAPI functions from hid.dll and setupapi.dll
In Linux and Mac OS all on other :(

Re: HID_Lib - Library

Posted: Tue Apr 06, 2010 2:53 pm
by jamirokwai
User_Russian wrote:In library were used WinAPI functions from hid.dll and setupapi.dll
In Linux and Mac OS all on other :(
Ok. That's a pity :-)
Hm. Going to search for something similar on Mac OS X now...

Re: HID_Lib - Library

Posted: Tue Apr 06, 2010 3:24 pm
by ColBoy
If I have a USB HID device, like a barcode scanner, does that mean I can read the code scanned directly using this library, or is there another hump that needs to be conquered?

Re: HID_Lib - Library

Posted: Tue Apr 06, 2010 4:12 pm
by User_Russian
ColBoy wrote:If I have a USB HID device, like a barcode scanner, does that mean I can read the code scanned directly using this library, or is there another hump that needs to be conquered?
Probably yes
I did not use the library with scanner of the barcode.

Re: HID_Lib - Library

Posted: Wed Apr 07, 2010 9:03 pm
by DoctorLove
ColBoy wrote:If I have a USB HID device, like a barcode scanner, does that mean I can read the code scanned directly using this library, or is there another hump that needs to be conquered?

Hi colboy,

You can just see the barcode scanner as a keyboard, it acts like one.
just plugin a barcode scanner, open up "notepad" and scan. You'll see what it does.

its typing for example : 3441344341243 and then place an ENTER.

Re: HID_Lib - Library

Posted: Thu Apr 08, 2010 2:10 pm
by ColBoy
Hi doctorlove

Thanks I am aware of that. Sometimes however I want to access a barcode scanner in a serial mode, which many symbol Scanners support, with additional software. If I could do that without the Symbol serial driver that would be great. The problem with running a barcode scanner in wedge mode, is that you need to ensure a control has focus, to receive the data. IT's also much more inflexible.

Colin

Re: HID_Lib - Library

Posted: Thu Apr 08, 2010 4:23 pm
by ABBKlaus
You can use a prefix with your barcodescanner like ALT+I .
And your application then needs a keyboardshortcut to get a note everytime a barcode gets scanned. We are using this method in our factory successfull (Symbol & Datalogic scanners).

BR Klaus

Re: HID_Lib - Library

Posted: Sat May 15, 2010 12:43 pm
by User_Russian
The official website of the program BASCOM (compiler for microcontrollers) are placed several devices, computer programs which are written in PureBasic using the library HID_Lib.

Re: HID_Lib - Library

Posted: Mon Nov 01, 2010 5:45 pm
by User_Russian
Updated list of libraries

Re: USB HID Library

Posted: Sun Jul 24, 2011 9:43 pm
by kemsky_by
hi, thanks for the library, but probably there is a bug:
1. HID_Lib_DeviceTest is 1 means ok
2. HID_Lib_OpenDevice is 0 means failure (by the way GetLastError_() returns 0)

my device is custom usb mouse.

I think this can help:

Why do I receive "Access denied" when attempting to access my HID?

Windows 2000 and later have exclusive read/write access to HIDs that are configured as a system keyboards or mice. An application can obtain a handle to a system keyboard or mouse by not requesting READ or WRITE access with CreateFile. Applications can then use HidD_SetFeature and HidD_GetFeature (if the device supports Feature reports).

see http://www.lvr.com/hidfaq.htm