Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading and writing USB HID FeatureReports async.

Reading and writing USB HID FeatureReports async.

Scheduled Pinned Locked Moved C#
help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BigBenDk
    wrote on last edited by
    #1

    Hi, I am trying to communicate with a HID device, using feature reports. When using normal input and output reports, I use the FileStream to read and write reports. This works great, even with async communication. My problem is that I am using feature reports instead. This means using the HidD_SetFeature and HidD_GetFeature from the Windows hid.dll file. This also works, but the HidD_GetFeature cannot be used async. When called it does not block until there is a report, so I end up having to poll when I expect there to be data. So I am looking for a way to do feature reports async, or have the HidD_GetFeature method to block until there actually is a report. Any insight is appreciated!

    J B 2 Replies Last reply
    0
    • B BigBenDk

      Hi, I am trying to communicate with a HID device, using feature reports. When using normal input and output reports, I use the FileStream to read and write reports. This works great, even with async communication. My problem is that I am using feature reports instead. This means using the HidD_SetFeature and HidD_GetFeature from the Windows hid.dll file. This also works, but the HidD_GetFeature cannot be used async. When called it does not block until there is a report, so I end up having to poll when I expect there to be data. So I am looking for a way to do feature reports async, or have the HidD_GetFeature method to block until there actually is a report. Any insight is appreciated!

      J Offline
      J Offline
      Jason Gleim
      wrote on last edited by
      #2

      I'm curious why you need to constantly get feature information from a HID device since the feature set of a device should be static and fetching it once in response to a connection event I would think would be sufficient. However, it's not for me to judge... ;) If the function doesn't support async there is no way to make it act that way without implementing some sort of polling. Unlike making an async call synchronous, there is no 'await' for functions that don't use a callback and immediately return. That being said, it isn't too hard to implement your own. Setting up a background worker or some other threading solution (even a system.threading.timer can be used) so you check at a given interval is very common. It used to be used a lot with serial comms to check the input buffer. The only gotcha is to make sure you have an abort so your app doesn't hang at close. If you use a timer with a polling interval it will automatically abort when your app shuts down. Also, make sure you handle the cross-thread marshal if anything is going up to the UI.

      B 1 Reply Last reply
      0
      • J Jason Gleim

        I'm curious why you need to constantly get feature information from a HID device since the feature set of a device should be static and fetching it once in response to a connection event I would think would be sufficient. However, it's not for me to judge... ;) If the function doesn't support async there is no way to make it act that way without implementing some sort of polling. Unlike making an async call synchronous, there is no 'await' for functions that don't use a callback and immediately return. That being said, it isn't too hard to implement your own. Setting up a background worker or some other threading solution (even a system.threading.timer can be used) so you check at a given interval is very common. It used to be used a lot with serial comms to check the input buffer. The only gotcha is to make sure you have an abort so your app doesn't hang at close. If you use a timer with a polling interval it will automatically abort when your app shuts down. Also, make sure you handle the cross-thread marshal if anything is going up to the UI.

        B Offline
        B Offline
        BigBenDk
        wrote on last edited by
        #3

        Hi Jason Thank you for your input, appreciated! Unfortunately, I did not write the firmware for the device. It uses feature reports as a 'bidirectional' report instead of input/output reports. I have already implemented a polling solution, with best guess of how long to wait for, and what the polling frequency should be. It works, but burns a lot of clock cycles in an already loaded system. Thanks again and good luck with the fireworks - be careful :)

        J 1 Reply Last reply
        0
        • B BigBenDk

          Hi Jason Thank you for your input, appreciated! Unfortunately, I did not write the firmware for the device. It uses feature reports as a 'bidirectional' report instead of input/output reports. I have already implemented a polling solution, with best guess of how long to wait for, and what the polling frequency should be. It works, but burns a lot of clock cycles in an already loaded system. Thanks again and good luck with the fireworks - be careful :)

          J Offline
          J Offline
          Jason Gleim
          wrote on last edited by
          #4

          Ugh! Why do some vendors insist on doing things their own way instead of the way everyone else in the world does it! Good luck. Polling is always a fine art of balancing responsiveness against overhead. Thanks for the good wishes. We are buried in snow here in the Midwest US and I am VERY ready for fireworks season to return!

          1 Reply Last reply
          0
          • B BigBenDk

            Hi, I am trying to communicate with a HID device, using feature reports. When using normal input and output reports, I use the FileStream to read and write reports. This works great, even with async communication. My problem is that I am using feature reports instead. This means using the HidD_SetFeature and HidD_GetFeature from the Windows hid.dll file. This also works, but the HidD_GetFeature cannot be used async. When called it does not block until there is a report, so I end up having to poll when I expect there to be data. So I am looking for a way to do feature reports async, or have the HidD_GetFeature method to block until there actually is a report. Any insight is appreciated!

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            What about polling in a different thread and adding a custom event when data are available? With the current multi-core computers, that should help to remove load from the main (UI) thread of the application. Beware of Control.Invoke/BeginInvoke when you then update the UI on data received from the device.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups