USB Communication Using Qt



  • I am starting on a project that requires information to be transmitted between a linux / windows based desktop PC and an external microcontroller circuit. The ideal would be to use USB but I am unable to find any information or book explaining in detail how to communicate with the PC's USB ports from my Qt application. I really like Qt for it's GUI design but it wouldn't be of any use starting the project using Qt if I cannot find any information on this crucial requirement. Can someone indicate if this is possible with a reference to a good book or link to a website or suggest a different (C++) approach? (The microcontroller part is not a problem)



  • I´m working exactly the same project, if somebody could help us I will be very grateful.



  • Some useful links that might help that I have found so far:

    http://www.microchip.com/forums/m340892.aspx
    http://libusb.sourceforge.net/api-1.0/index.html

    You may need to piece together the info you need from the microchip thread on how to use libusb with Qt but it seems to be informative.



  • Thank You So Much!



  • Dear Candiman,

    you might want to have a look at "Waiting for Friday":http://www.waitingforfriday.com/index.php/Building_a_PIC18F_USB_device

    You'll find an example, containing hardware based on a Pic 18f4550 (cheap and free if you ask microchip for samples), firmware code (using a free as "free-beer" compiler), a base class to help you deal with the USB communication and a full visual application sample.

    From my point of view, should be fairly easy to hide your internal communication in usb in a few classes, then provide a nice shell object with signals and slots to deal with your hardware.
    This way, you split nicely the code and you can easily test your UI without bothering with hardware (and test your hardware without bothering with UI ;) ) Of course, the HW communication layer will most-probably be platform specific whereas your UI will be generic. You could even think of using plugins to extend the functionality of your UI with HW... I have had a similar project in mind for quite a while, just can't find time to start it....



  • USB and other hardware peripheral communication means (excepting networking) are not part of the Qt modules, as far as I know. Best course of action would be to go for some cross platform library (e.g. libusb, which you already found) and use that in your Qt program.



  • Thank you for the reply Florent.Revelut and Franzk.

    Would be great if you could finally do that project you had in mind and document it. Then you can sell it to the unintelectuals like me ;)


  • Moderators

    Hi Candyman,
    there can be found some source code based on Qt for serial connections. You will across a couple of derivatives obviously orginating from the same starting point.
    "See for instance":http://qextserialport.sourceforge.net/
    It can be used with USB-serial (PC-device). What the differences for USB-USB are, I am blank. Would be great to know, how to do it.



  • Yes we use the usb-serial approach by way of an FT-245 convertor chip on the comms side of our devices. This just marshalls between usb and standard RS-232 comms which allows us to use qextserialport on /dev/ttyUSB0 or COM1 for e.g. Without such a chip, libusb is the way to go but Qt can still be used for other aspects of the application though.


  • Moderators

    @ZapB How would you address usb on windows systems? Your example is for linux.



  • On windows you refer to serial devices as \.\COM1, \.\COM2 etc. Which in C++ translates to

    @
    "\\.\COM1"
    @

    when you escape all those backslashes.


  • Moderators

    Thanks ZapB
    However, the serial com part is easy.
    How does
    @
    "/dev/ttyUSB0"
    @
    translate on a windows system?
    Is it
    @"\\.\USB0"@ ?



  • Could you not use QExtSerialPort to simply list the available ports, instead of guessing their names?



  • /dev/ttyUSB0 is simply the name that the kernel (or udev or whatever else you are using) assigns to serial over USB devices ie when using a usb-serial convertor. So when using this type of device under windows the corresponding device is COM1.

    If you are not using a usb-serial convertor then you have to use the libusb functionality for communicating - that is you cannot simply use COM1 etc.


  • Moderators

    [quote author="Andre" date="1303983404"]Could you not use QExtSerialPort to simply list the available ports, instead of guessing their names?[/quote]

    Not that I am aware off. Probably, you have to do it brute force by checking all com ports.

    [quote author="ZapB" date="1303983794"]If you are not using a usb-serial convertor then you have to use the libusb functionality for communicating - that is you cannot simply use COM1 etc.[/quote]

    That gives me something to chew on...

    Thanks for your valuable responses



  • [quote author="koahnig" date="1303984402"]
    [quote author="Andre" date="1303983404"]Could you not use QExtSerialPort to simply list the available ports, instead of guessing their names?[/quote]

    Not that I am aware off. Probably, you have to do it brute force by checking all com ports.

    [/quote]

    There is an enumerator in the source code: http://qextserialport.cvs.sourceforge.net/viewvc/qextserialport/qextserialport/


  • Moderators

    [quote author="Andre" date="1303985184"]
    There is an enumerator in the source code: http://qextserialport.cvs.sourceforge.net/viewvc/qextserialport/qextserialport/
    [/quote]

    you mean the enum on NamingConvention ?
    I did not come across this yet. But it is nowherelse referenced. So it could be dead source.



  • No, there is a device enumeration function in qextserialport that probes what serial devices you have available.





  • BTW the sourceforge project is no longer active. The new home is "here":http://code.google.com/p/qextserialport/


  • Moderators

    Got it!
    Thanks again. Will try to use it. Apperently, it was not part of the example I have come across.



  • @Koahnig, if I don't come right with libusb I can fall back on serial to USB as you suggested. Tnx for the link to qextserport.
    @ZapB, tnx for the info serial to USB converter chip. If required I will modify my hardware and go the serial route.
    I will post here again on completion of the project with info on how to do it for others that might find it useful.



  • OK. For info, the QStateMachine framework can be very handy for controlling hardware devices so it may be worth a look in you case. Good luck with your project.


  • Moderators

    @Candiman Thx for feedback. Good to know that it helped. Good luck with your project. Please make sure to post a link in this thread. I am interested to see the outcome.
    @ZapB Thx for updated link. Wasn't aware of this. There seem to be a couple of derivatives of the source floating around. Actually I started out with one, but some things required reversal. So it is good to know where the original source code is. Have fun.



  • @all

    Hi, I have found this thread because I have a similar problem.

    I have to communicate with a serial external device folowing a simple yet precise protocol. I have tried a fast test to develop the protocol using RXTCComm java library that works not so good and now I was searching for a Qt approach that is the final goal of the other side of the device.

    I try to put all your advices together and see if a working stuff can be done. If it maybe useful I put the project on the project area so who is interested can follow. As the project is ready and setup it will be on projects.forum.nokia.com/mill-machine I will announce it on t the forum appropriate area and in the Qt embedded group.

    Cheers.

    Anticipating many thanks to all.



  • Great. Tnx Alicemirror.



  • Hi. I have used rs232 communication in visual c++ 6.0
    which work very well. But because of my problem with
    transmit images trough socket in VC++ which is
    too complicated than Qt implementation ( very simple ).
    I need to implement rs232 comunication.
    My interface have to use TCP socket for internet comunication and rs232 for comunicate with microcontroller
    and electrical devices.



  • Hi dan_geo, let me know if you get your RS232 comms working with Qt. I have had to shift focus to a different project but during the downtime over the December holidays and from Jan 2012 I will be putting effort into the USB via Qt project again since many of my projects can benefit from having a GUI that I can design with Qt. I decided not to go with the RS232 option because so many PCs these days don't have a serial port. USB to serial converters are cheap, but if I can communicate directly with my device using USB then I don't need any additional hardware / drivers etc.
    Good luck!



  • I have not get rs232 comm. Next days i will try to
    implement them in Qt. If you are a source, please
    sent to me. About usb, this task i will do later
    with PIC18F4455. At usb i understand that
    for make a host usb is more complicated than
    implement a usb client for example a controller.




  • Moderators

    According to "this post":http://qt-project.org/forums/viewthread/15580/#90074 there is a wiki on "QtSerialPort":http://qt-project.org/wiki/QtSerialPort Apparently, the source is inherited from QSerialDevice.



  • It's possible to use the QtSerialPort on QT4? On this page (see: http://qt-project.org/wiki/Building-Qt-5-from-Git#15e7b6ad3df43ff719f8388ab4afb1cc) show thats is possible but he only explains to build in qt5!

    Anyone is using on QT4? The example there show only to build on Windows, not on linux...



  • Well, I compile using QT Creator 4.8.1, now I copy the compile folder "QtAddOnSerialPort" to directory:

    "../bin/QtSDK/Desktop/Qt/4.8.1/gcc/include/."

    This will be enough to work with SerialPort/USB on QT?

    Thanks all!



  • Possible.

    Build and install process:

    
    bq. $mkdir serialport-build
    $cd  serialport-build
    $qmake ../qtserialport/serialport.pro
    $make (or nmake)
    $make install (or nmake install)
    
    Note: But in Windows, this build and install only the Debug version. To build and install Release version - just repeat the steps again, with:
    
    bq. $qmake ../qtserialport/serialport.pro CONFIG+=release
    
    
    use:
    ~~~
    
    your custom *.pro file:
    
    bq. CONFIG += serialport
    
    your *.cpp file:
    
    @#include <QtAddOnSerialPort/serialport.h>
    #include <QtAddOnSerialPort/serialportinfo.h>
     
    QT_USE_NAMESPACE_SERIALPORT@
    


  • hi kuzulis, from QtCreator I build and appears to works fine... Doing the make as you said give me an error...
    I have already copy and past the folder "QtAddOnSerialPorta" to “../bin/QtSDK/Desktop/Qt/4.8.1/gcc/include/.” and QtCreator find the includes:

    @
    #include <QtAddOnSerialPort/serialport.h>
    #include <QtAddOnSerialPort/serialportinfo.h
    @

    I'm trying to run the example on that page but doesn't works.... the errors:

    "Undefined reference to 'QtAddOn::SerialPort::SerialPortInfo::avaliablePorts()'"
    ... (more others 14 similar erros)



  • Sorry I made some mistake:

    When I try to compile on Qt using 4.6.1 or using the make on shell It gives me this error:

    moc_serialport.cpp:55:16: error: ‘SerialPort’ was not declared in this scope

    On the QT 4.8.1 the Qt Creator gives the message:
    ...
    #error (moc changes too much)
    ...



  • I try again, reopening the project and now works... but now I'm very confused!



  • Remove (clear) away all that you previously copied by hand and do as I have indicated in the "above":http://qt-project.org/forums/viewreply/90605/



  • Will not compile... the errors at code...



  • See the errors:

    @
    ...
    ...
    ...
    /home/dvl/SOURCE_CODE/qtserialport/src -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I/home/dvl/SOURCE_CODE/qtserialport/src -I. -I/home/dvl/SOURCE_CODE/qtserialport/src -I. -o moc_serialport.o moc_serialport.cpp
    moc_serialport.cpp:55:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:56:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:57:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:58:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:59:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:60:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:61:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:62:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:63:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:64:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:65:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:66:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:67:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:68:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:69:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:70:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:71:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:72:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:73:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:74:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:75:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:76:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:77:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:78:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:79:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:80:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:81:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:82:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:83:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:84:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:85:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:86:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:87:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:88:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:89:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:90:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:91:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:92:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:93:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:94:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:95:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:96:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:97:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:98:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:99:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:100:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:101:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:102:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:103:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:104:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:105:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:106:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:107:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:108:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:109:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:110:16: error: ‘SerialPort’ was not declared in this scope
    moc_serialport.cpp:142:19: error: ‘SerialPort’ has not been declared
    moc_serialport.cpp:151:20: error: ‘SerialPort’ has not been declared
    moc_serialport.cpp:151:45: error: non-member function ‘const QMetaObject* metaObject()’ cannot have cv-qualifier
    /usr/include/QtCore/qobject.h: In function ‘const QMetaObject* metaObject()’:
    /usr/include/QtCore/qobject.h:296:33: error: ‘QScopedPointer<QObjectData> QObject::d_ptr’ is protected
    ...
    ...
    ...
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.