LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Controlling a USB HID device (Joystick, Keyboard, Mouse) using LabView

Not a problem, but I thought I'd post a solution that I stumbled across. And which, as usual (unfortunately) is not documented anywhere. I've been trying to figure out how to read a USB Joystick (a LogiTech Attack 3, FYI) via LabView to control a pan/tilt unit. After experimenting with the Windows sdk dinput.dll library (which is a right-royal pain to work with) I stumbled across the fact that LabView already has the dinput library built in! Buried in the "Advanced VI's and Functions" / "Input Device Control" menu there are some nice little VI's. "Query Input Devices.vi" will enumerate all of the USB joysticks, keyboards, and mice attached to the system. You then have to look through the names to find the index of the one you're looking for. (See attached). The "Initialize Joystick/Mouse/Keyboard" vi's attach to the particular device and return an ID reference. "Acquire Input Data.vi" reads the current state of the joystick/keyboard/mouse. Finally, "Close Input Devices.vi" detaches from the USB link. 

 

There. Now the next time I need this, a search will actually find something.

Message 1 of 13
(19,369 Views)

 


@pblase wrote:

Not a problem, but I thought I'd post a solution that I stumbled across. And which, as usual (unfortunately) is not documented anywhere. I


 

The Input Device VIs are documented. VI and Function Reference -> Connectivity VIs and Functions -> Input Device Control VIs. Also, if you search the functions palette for "joystick" (as an example) you'll get to those VIs. And each of those VIs is fully documented in the LabVIEW Help. There is also an example that ships with LabVIEW called "Keyboard Puzzle Game", which can be found either by (a) searching in the Example Finder for "input" or "keyboard"; or (b) looking at the help page for "Init Keyboard" and clicking the "Open Example" button.

 

How much more documentation were you looking for?

 

 

EDIT: Oh, and there's also the stuff available on the NI site, like this: http://digital.ni.com/public.nsf/allkb/CA411647F224787B86256DD000669EFE.

Message 2 of 13
(19,364 Views)

Well, it's all there in the palette, right. 😄

 

A few comments:

 

  • In newer LabVIEW versions (you are using 7.0, which is almost prehistoric ;)) you could use a FOR loop with a conditional terminal for the first loop, significantly simplifying the code.
  • Why do you need to measure the array size with each iteration of the loop? Once before the loop is sufficient. (The modern LabVIEW compiler will actually recognize this as "loop invariant code" and treat it as being outside, but I am not sure if LabVIEW 7.0 is that advanced.)
  • You can save one iteration by tapping into the output from "match pattern" for the "not found" boolean and change the logic a bit (e.g. invert the outcome or label the LED as "Found!" and swap the cases).
  • The "close input device" belongs inside the case structure, else if the device is not found, you are trying to close an nonexistent device.
  • Your inner loop consumes all CPU it can get, trying to spin as fast as it can. Place a reasonable delay (e.g. 1ms). Nobody needs nanosecond response on a joystick and you might be starving other loops that need CPU too..

 

0 Kudos
Message 3 of 13
(19,352 Views)

Thanks for posting some usable information.  I was trying to use the NI-VISA Driver Wizard to create a drive so I could access the device.  You implementation is much simpler. Smiley Wink

Matthew Fitzsimons

Certified LabVIEW Architect
LabVIEW 6.1 ... 2013, LVOOP, GOOP, TestStand, DAQ, and Vison
0 Kudos
Message 4 of 13
(19,319 Views)

Sure, if you know to look under those things in help. You have to know it's there first before you look in the help file. It's also not filed under "HID" or "Microsoft SDK"

0 Kudos
Message 5 of 13
(19,288 Views)

This was simpy a test to see if I could get it to work at all. I'll keep your advice in mind for the real code.

Thanks

0 Kudos
Message 6 of 13
(19,285 Views)

 


@pblase wrote:

Sure, if you know to look under those things in help. You have to know it's there first before you look in the help file. It's also not filed under "HID" or "Microsoft SDK"


You don't need to know it's there - that's why there's a search capability for the help in addition to the index. Just open the help and search for "joystick". Searching for HID is too vague and searching for acronyms isn't always a good idea, and searching for "Microsoft SDK" would be relatively meaningless as that's WAY too vague.

 

0 Kudos
Message 7 of 13
(19,250 Views)

That assumes that I'd even think of looking in the Help system for "joystick". This, IMHO, is the main problem with most help systems - not just NI's. They're meant for people that are intimately familiar with the system and just need reminders, they are not meant for people that need to find a capability that may or may not be there. I started out using LabWindows/CVI, with limited success, using the "dinput.dll" facility for reading HID USB devices. That was just a temporary thing, until I could get LabView reinstalled on my main computer (long story there). So, I know that a joystick is an HID device; all of the documentation calls it an HID device; Microsoft has a driver facility for HID devices and all of their documentation calls it an HID device, all of my searches on this website come up with interfacing to the HID system (including searches on "joystick") why the heck should I look for "joystick" in the LabView help file?

0 Kudos
Message 8 of 13
(19,240 Views)

Joystick and HID are not at all synonomous and the only documentation that would relate a joystick to the HID driver is a joystick with a USB connection. You can, of course still find joysticks that connect to a dedicated game port (which is what the HID driver emulates) just like yu can find keyboards and mice for the dedicated ports you still see on the back of pc's. Not doing a search for joystick when it is a joystick (not a mouse, keyboard, or any number of other devices that the HID class can emulate) seems rather obtuse.

0 Kudos
Message 9 of 13
(19,220 Views)

Hi Blase and Everyone,

   I have the same problem of reading joystick through LabVIEW, through my Laptop. I use the Logitech joystick-ATK 3. When I connected that to the USB port, the joystick was recognised in Windows- , but the joystick was not readable from LabVIEW. I used the query input devices.vi to find the number of my joystick, it was at "3" , but when I wired the number "3" to the device ID of the initialize joystick. vi  and run it, it was not recognised and no response too.

 

    But when I worked on the desktop, everything was fine, and the device ID was the default value of "0". Your help would be greatly appreciated as I need to get this done asap. I would also like to know what are the different reasons for the problems which can arise and solutions.

 

    Thank you,

 

   ---Neehar

0 Kudos
Message 10 of 13
(18,179 Views)