Results 1 to 4 of 4

Thread: How to open a device for Asynchronous IO in QT4

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to open a device for Asynchronous IO in QT4

    Hi all

    I'm new to Qt and an intermediate C++ (well I think so anyways) dev.
    I'm trying to port some code over from C# to do with driver development for a HiD board and trying to use Qt and C++ data types as opposed to the MFC ones like, QFile instead of HANDLE and unsigned long instead of DWORD. So far so good I have managed to get to the point of opening the device which is where I've hit an impasse.
    Qt Code:
    1. bool hid_device::init_device(QString devPathname)
    2. {
    3. QString errorMessage;
    4. QMessageBox msgBox;
    5. HIDD_ATTRIBUTES mDeviceAttribs;
    6.  
    7. qDebug() << devPathname;
    8. //devicehandle is of type QFILE
    9. deviceHandle.setFileName(devPathname);
    10. if(!deviceHandle.open(QIODevice::ReadWrite))
    11. {
    12. unsigned long lastErr = GetLastError(); //ulong = DWORD
    13. errorMessage = errorMessage.setNum(lastErr);
    14.  
    15. msgBox.setText("Unable to Open the Device for IO operations. Error code: " +QString::number(lastErr));
    16. msgBox.exec();
    17. return false;
    18. }
    19.  
    20. HidD_GetAttributes(&deviceHandle, &mDeviceAttribs);
    21. this->ProductID = mDeviceAttribs.ProductID;
    22. this->VendorID = mDeviceAttribs.VendorID;
    23. this->VersionNumber = mDeviceAttribs.VersionNumber;
    24.  
    25. deviceHandle.close();
    26. return true;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Attempting to open the device always returns an error: ERROR_PATH_NOT_FOUND.
    I know the QString I'm passing to the device is the one returned from the setupDi* DDK function so it's valid. (I have anyone done HiD dev in Qt and can point me anywhere? Please remember I'm new to Qt and I'm just porting some code over as a way of learning the framework. I'll tidy up my programming later when I start getting used to thinking in QT.

    Thanks for your time.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to open a device for Asynchronous IO in QT4

    I'm not familiar with Windows device driver coding but since Windows does not abide the notion of device-as-file like UNIXes it may be that QFile cannot open the "file" returned. What does devPathName actually contain? Does that exist as a file?

    Mixing the Windows API GetLastError() with the Qt QFile class does not strike me that it will be particularly reliable. Who knows what the last Windows API call that QFile made was. What does QFile::error() return.

  3. #3
    Join Date
    Aug 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to open a device for Asynchronous IO in QT4

    The problem here is that the QFile is based on C runtime low-level I/O file descriptors (just like on unix) and the HidD_* functions need a Windows API HANDLE (which QFile::handle() does not returns). There are two conversion functions between HANDLE and fd: _get_osfhandle (fd->HANDLE) and _open_osfhandle (HANDLE->fd). I didn't get the latter to do anything useful on my CreateFile(...) handles for HID devices.

  4. #4
    Join Date
    Jul 2009
    Location
    Sao Paulo / Brazil
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: How to open a device for Asynchronous IO in QT4

    What simar0at said is what you should check out, WDT. If QFile uses the native c runtime file reading functions (instead of the CreateFile()/WriteFile()/ReadFile() Windows API functions) then, passing the file name (device path) of your HID device in the same way that CreateFile() expects, will not work at all.
    Just as a coincidence I also have a user level HID program using Qt, but the whole HID part of it was done using the native Windows API functions.

    Just to shower some light on the issue here are a couple things you could check:
    1 - QSocketNotifier seems like a very nice solution for monitoring read and write operations over file descriptors. The problem is that (and the Qt documentation for the QSocketNotifier class slounds like that) is that, on Windows, it really works only for network socket descriptors (not handles returned by CreateFile()), but on *nix it works for network sockets, and maybe even for serial ports and file descriptors of any kind (maybe even a HID one). It's just my guess, but I will try answering this question soon.

    2 - Second option is cumbersome, but it's a way and that's what I did here at work: Subclass QIODevice, then when using the CreateFile and Read/WriteFile functions with Overlapped IO, if you find yourself asking how you are going to mix the WaitForSingleObject (or multiple objects) with the main thread event loop (so you don't have to use a separated thread to avoid freezing the Ui) just look at the : <QtCore/private/qwineventnotifier_p.h>, it solves that problem.

Similar Threads

  1. Replies: 3
    Last Post: 25th August 2010, 12:39
  2. Not able to open Qt Mobility applications on Nokia N97 Device!
    By rajeevsahu in forum Installation and Deployment
    Replies: 0
    Last Post: 16th July 2010, 06:50
  3. Why QSqlDatabase::open() returns open?
    By gboelter in forum Newbie
    Replies: 7
    Last Post: 27th August 2009, 18:52
  4. Problem whis open device in Windows
    By Pavka in forum Qt Programming
    Replies: 3
    Last Post: 19th November 2007, 10:13
  5. Handheld Device
    By freelyfallers in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 12:48

Tags for this Thread

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.