PDA

View Full Version : Reading data from usb port



jantje78
17th May 2011, 11:44
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:


comPort = new QextSerialPort("COM7");
//set com port settings

comPort->setBaudRate(BAUD9600);
comPort->setFlowControl(FLOW_HARDWARE);
comPort->setParity(PAR_NONE);
comPort->setDataBits(DATA_8);
comPort->setStopBits(STOP_1);

//read data from com port

comPort->open(QIODevice::ReadWrite);
char buff[1024];
int numBytes;
QString msg;
numBytes = comPort->bytesAvailable();
if(numBytes > 0)
{
if(numBytes > 1024){
numBytes = 1024;}
int i = comPort->read(buff, numBytes);
buff[i] = '\0';
msg = buff;
}


The .pro file:(the directions are from someone else ofcourse)


//my qextserialport dir is C:/qt/qextserialport
...
INCLUDEPATH += C:/Qt/qextserialport
LIBS += C:\Qt\qextserialport\build\qextserialport.dll
..

My question now is, how do I instal qextserialport? I've downloaded qextserialport-1.2.rar from http://sourceforge.net/projects/qextserialport/files/qextserialport%20alpha/1.2win-alpha/qextserialport-1.2win-alpha.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.

high_flyer
17th May 2011, 11:50
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.

jantje78
17th May 2011, 12:04
@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

high_flyer
17th May 2011, 12:58
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.

schnitzel
25th May 2011, 20:13
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.

high_flyer
26th May 2011, 08:51
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?

schnitzel
26th May 2011, 17:21
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.