USB HID Library

Developed or developing a new product in PureBasic? Tell the world about it.
User_Russian
Addict
Addict
Posts: 1441
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

USB HID Library

Post 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
Last edited by User_Russian on Fri Aug 03, 2012 8:39 pm, edited 11 times in total.
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

Re: HID_Lib - Library

Post by +18 »

thanks for sharing :D
User avatar
idle
Always Here
Always Here
Posts: 5018
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: HID_Lib - Library

Post by idle »

thanks
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: HID_Lib - Library

Post by SFSxOI »

Nice, Thank You. :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
jamirokwai
Enthusiast
Enthusiast
Posts: 771
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: HID_Lib - Library

Post 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 :-)
Regards,
JamiroKwai
User_Russian
Addict
Addict
Posts: 1441
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: HID_Lib - Library

Post by User_Russian »

In library were used WinAPI functions from hid.dll and setupapi.dll
In Linux and Mac OS all on other :(
jamirokwai
Enthusiast
Enthusiast
Posts: 771
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: HID_Lib - Library

Post 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...
Regards,
JamiroKwai
ColBoy
Enthusiast
Enthusiast
Posts: 143
Joined: Fri Feb 13, 2004 2:37 pm
Location: Ottawa, Canada
Contact:

Re: HID_Lib - Library

Post 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?
Colin
User_Russian
Addict
Addict
Posts: 1441
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: HID_Lib - Library

Post 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.
DoctorLove
User
User
Posts: 85
Joined: Sat Mar 06, 2010 2:55 pm

Re: HID_Lib - Library

Post 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.
ColBoy
Enthusiast
Enthusiast
Posts: 143
Joined: Fri Feb 13, 2004 2:37 pm
Location: Ottawa, Canada
Contact:

Re: HID_Lib - Library

Post 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
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Re: HID_Lib - Library

Post 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
User_Russian
Addict
Addict
Posts: 1441
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: HID_Lib - Library

Post 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.
User_Russian
Addict
Addict
Posts: 1441
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: HID_Lib - Library

Post by User_Russian »

Updated list of libraries
kemsky_by
User
User
Posts: 14
Joined: Sun Jul 24, 2011 9:35 pm

Re: USB HID Library

Post 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
Post Reply