Results 1 to 7 of 7

Thread: Reading data from usb port

  1. #1
    Join Date
    Apr 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Question Reading data from usb port

    I'd like to read data from a usb-port,
    I've found someone using the following code with a micocontroller that will send data when you push a button, here is the code:

    The Qt code:

    Qt Code:
    1. comPort = new QextSerialPort("COM7");
    2. //set com port settings
    3.  
    4. comPort->setBaudRate(BAUD9600);
    5. comPort->setFlowControl(FLOW_HARDWARE);
    6. comPort->setParity(PAR_NONE);
    7. comPort->setDataBits(DATA_8);
    8. comPort->setStopBits(STOP_1);
    9.  
    10. //read data from com port
    11.  
    12. comPort->open(QIODevice::ReadWrite);
    13. char buff[1024];
    14. int numBytes;
    15. QString msg;
    16. numBytes = comPort->bytesAvailable();
    17. if(numBytes > 0)
    18. {
    19. if(numBytes > 1024){
    20. numBytes = 1024;}
    21. int i = comPort->read(buff, numBytes);
    22. buff[i] = '\0';
    23. msg = buff;
    24. }
    To copy to clipboard, switch view to plain text mode 

    The .pro filethe directions are from someone else ofcourse)

    Qt Code:
    1. //my qextserialport dir is C:/qt/qextserialport
    2. ...
    3. INCLUDEPATH += C:/Qt/qextserialport
    4. LIBS += C:\Qt\qextserialport\build\qextserialport.dll
    5. ..
    To copy to clipboard, switch view to plain text mode 

    My question now is, how do I instal qextserialport? I've downloaded qextserialport-1.2.rar from http://sourceforge.net/projects/qext...a.zip/download , but how do I install it?
    my qt map is located at: C:\Qt\2010.05\

    My qt is based on 4.7 and im running windows 7 x64 home premium.
    Last edited by jantje78; 17th May 2011 at 11:48. Reason: message at the .pro file:(...)

  2. #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: Reading data from usb port

    I'd like to read data from a usb-port,
    Do you want to communicate with a USB port, or with a serial port?
    The code you posted and QextSerialPort is for communicating with a serial port.

    For USB there should be other libs.
    ==========================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.

  3. #3
    Join Date
    Apr 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading data from usb port

    @high_flyer
    I would like to read data from a usb port, let me explain further;
    We are building a project with the purpose to let children learn about robots.
    So we build a little car with diffirent sensors.
    After the car is done driving, you should connect the car with a pc (mini usb --> usb).
    The car should send a string with something like "1 2:15 55" (1=which program/excercise or w/e u would like to call it, 2:15= time, 55=score).
    And I would like to put that data in diffirent QLabels

  4. #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: Reading data from usb port

    The QextSerialPort is not what you want.
    Search in the forum - someone posted few weeks back a Qt wrapper for USB lib.
    But this is really not a Qt issue, you can use any USB lib that will work for you in your Qt project.
    ==========================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.

  5. #5
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reading data from usb port

    In case you are using a usb to serial converter like FTDI which features a driver with a virtual comm port (judging by the high com port number), then you could use qextserialport, however, be aware that the new location for that project is on google code:
    http://code.google.com/p/qextserialport/

    the version you downloaded is old.

    Once you get the proper source, look at qextserialport examples and please refer to the forums/mailing list over at qextserialport on how to use this library.

  6. #6
    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: Reading data from usb port

    In case you are using a usb to serial converter
    If schnitzel is correct, then you want to read from a SERIAL port, not a USB port.
    The fact the serial port is originally a USB port, plays no role for you.
    Your code will only deal with a serial port - in which case the code you posted makes sense.
    So what is it that you need?
    Do you want to read a USB port or a serial port?
    ==========================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.

  7. #7
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reading data from usb port

    I should clarify...

    I'm using one of those FTDI chips on a custom board. The chip is supported by a library (win/mac/linux) available for free from ftdi website. The chip isn't actually a usb to serial converter *but* it features the so called VCP (Virtual COM Port) driver. From ftdi web site:
    For most of these operating systems two types of driver are available: Virtual COM Port (VCP) drivers and direct (D2XX) drivers. The VCP driver emulates a standard PC serial port such that the USB device may be communicated with as a standard RS232 device. The D2XX driver allows direct access to a USB device via a DLL interface.
    I have never used the device with VCP, because the transfer rate is a bit lower compared to using the device with the direct drivers.

    Like you said high_flyer, OP needs to clarify what he needs/uses.

Similar Threads

  1. Replies: 3
    Last Post: 27th April 2011, 09:27
  2. Serial Port Reading problem
    By sfabel in forum Qt Programming
    Replies: 12
    Last Post: 18th February 2010, 14:59
  3. Problem in reading port using QextSerialPort
    By cutie.monkey in forum Qt Programming
    Replies: 6
    Last Post: 2nd July 2009, 02:07
  4. Replies: 1
    Last Post: 1st July 2009, 00:37
  5. Replies: 1
    Last Post: 1st July 2009, 00:36

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.