Results 1 to 20 of 47

Thread: Windows USB example

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Italy
    Posts
    5
    Thanks
    1
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Windows USB example

    This program is an example of how to use an USB device in Qt.
    It is limited only to the HID devices class (joystick, mouse, keyboard...)
    It uses the standard Microsoft HID driver. If you use another driver you must know how it works.
    It demonstrates how to:
    -recognize a device by VID&PID
    -read/write
    -attach/detach notification
    Two examples are provided:
    -first example reads the XY position of a joystick
    -second example sends a message to the device and waits for the answer.

    See comments in source for details.

    Licence: GPL.

    I hope that this program can be useful to someone.
    Attached Files Attached Files

  2. The following 4 users say thank you to gavagai for this useful post:

    ASUKA_521 (25th March 2012), baetaekwon (19th February 2013), caodungviet (28th April 2011), Gamer (12th October 2013)

  3. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Windows USB example

    Excellent!
    It might be a good idea if you'd add it to the examples section in the wiki, here it gets lost among the many other threads...
    http://www.qtcentre.org/wiki/index.p...egory:Examples
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Windows USB example

    I don't like the idea of using global variables. And threads access those variables without a proper protection. This is bound to blow up sooner or later.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Windows USB example

    I haven't looked at the code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Windows USB example

    When you say:

    Qt Code:
    1. //to find a better way for this...
    2. while(lpdbv->dbcc_name[i] != 0)
    3. {
    4. s.append(lpdbv->dbcc_name[i]);
    5. i++;
    6. }
    7. s = s.toUpper();
    To copy to clipboard, switch view to plain text mode 

    Have you tried:

    Qt Code:
    1. QString s = QString::fromWCharArray(lpdbv->dbcc_name).toUpper();
    To copy to clipboard, switch view to plain text mode 

    Don't know if it'll work, it's just something that sprung into mind when I read your code/comment.

  7. The following user says thank you to squidge for this useful post:

    gavagai (25th March 2011)

  8. #6
    Join Date
    Mar 2011
    Location
    Italy
    Posts
    5
    Thanks
    1
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Windows USB example

    hi squidge,
    thanks for your suggestion. Yes, it works!
    I had not seen this static function to convert wchar_t in QString.
    Thanks again.

  9. #7
    Join Date
    Jul 2010
    Location
    Ahmedabad,Gujarat,India
    Posts
    25
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Windows USB example

    Thanks for Share awesome Example for USB Communication...
    but i have problem to devide...... The "Device Class GUID" is {6bdd1fc6-810f-11d0-bec7-08002be2092f}
    can u please explain me more.

    i'll try this type #define HID_CLASSGUID {0x6bdd1fc6, 0x810f, 0x11d0,{ 0x08, 0x00, 0x2b, 0xe2, 0x09, 0x2f }}
    but result is same not connected ....

    please help me, i m waiting for u r kindly reply...
    thanks in advance.

  10. #8
    Join Date
    Mar 2011
    Location
    Italy
    Posts
    5
    Thanks
    1
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Windows USB example

    Don't use the class GUID returned by device manager, it don't works. It is the device SETUP GUID class. We need the device INTERFACE GUID class (see http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx).
    Try this:
    - be sure that your device is installed and recognized in the system
    - start regedit and search for your vid&pid (VID_XXXX&PID_XXXX) in the following key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contro l\DeviceClasses. You can get more of a result.
    - Try to use the parent class GUID where the descriptor starts with the type of device (##?#HID#VID... or ##?#IMAGE#VID...).
    For more informations see the examples in http://msdn.microsoft.com/en-us/library/ff551069.aspx

    Your device seems to be an image device. I do not know if endpoint size (InputBuffer and OutputBuffer size) is correct for this device. Depends on the driver.

    Good luck

  11. The following user says thank you to gavagai for this useful post:

    pavanbarot (1st April 2011)

  12. #9
    Join Date
    Jul 2010
    Location
    Ahmedabad,Gujarat,India
    Posts
    25
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Thumbs up Re: Windows USB example

    Thanks a lot it's works fine....
    Attached Images Attached Images

  13. #10
    Join Date
    Mar 2011
    Location
    Italy
    Posts
    5
    Thanks
    1
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Windows USB example

    ok, it works but I think it is not exactly correct.
    Your image shows a generic class GUID for all USB devices (##?# USB VID # ...).
    Better to use a GUID for a specific class of devices.
    Search again to find something like # #? # IMAGE # VID_XXXX&PID_XXXX and uses its GUID.
    Maybe I'm wrong, but I think for read / write operations you must use a more specific GUID.

  14. #11
    Join Date
    Mar 2011
    Location
    Bangalore
    Posts
    3

    Default Re: Windows USB example

    I am new member to this forum. So this information is useful for me. I will try this.

  15. #12
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Windows USB example

    sorry but i don't understand about The "Device Class GUID", please you help me?? for example,
    #define HID_CLASSGUID {0x6bdd1fc6, 0x810f, 0x11d0,{ 0x08, 0x00, 0x2b, 0xe2, 0x09, 0x2f }}
    so :{ 0x08, 0x00, 0x2b, 0xe2, 0x09, 0x2f }.what is it??
    please explain for me,i don't understand about it. what it means?? sorry ,because my english is not good

  16. #13
    Join Date
    Jul 2011
    Location
    Pune, India
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Windows USB example

    Quote Originally Posted by gavagai View Post
    Don't use the class GUID returned by device manager, it don't works. It is the device SETUP GUID class. We need the device INTERFACE GUID class (see http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx).
    Try this:
    - be sure that your device is installed and recognized in the system
    - start regedit and search for your vid&pid (VID_XXXX&PID_XXXX) in the following key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contro l\DeviceClasses. You can get more of a result.
    - Try to use the parent class GUID where the descriptor starts with the type of device (##?#HID#VID... or ##?#IMAGE#VID...).
    For more informations see the examples in http://msdn.microsoft.com/en-us/library/ff551069.aspx

    Your device seems to be an image device. I do not know if endpoint size (InputBuffer and OutputBuffer size) is correct for this device. Depends on the driver.

    Good luck
    Hi gavagai,
    First of all thanks for the great upload.
    I am tryint to build qt application to detect a usb device(Not necessarily a HID) when plugged in.
    Now, i have two HIDs connected to my PC a keyboard and a mouse.
    how do i know which VID/PID is for which device? Coz its really confusing even when i navigate to registry location you mentioned above.
    how does HID differs from Mass storage device in terms of VID/PID?
    Just want to put me in the right direction.
    Thnx.

Similar Threads

  1. Replies: 2
    Last Post: 6th September 2010, 15:22
  2. Replies: 3
    Last Post: 12th July 2010, 07:25
  3. Program runs on Windows 7, crashes on Windows XP
    By JovianGhost in forum General Programming
    Replies: 8
    Last Post: 7th June 2010, 01:55
  4. Deployment Procedure On Windows On Linux and Windows
    By Harshith J.V. in forum Installation and Deployment
    Replies: 4
    Last Post: 9th July 2009, 12:27
  5. Replies: 10
    Last Post: 25th February 2007, 01:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.