View Full Version : data from serial port

24th April 2009, 11:48
hi all,

i'm newbie in qt, and i have to make a program in qt to read data from usb device, digital compass to be more precisely

and this is the part of my code

in .h file

char buf[256];
QString line

in .cpp

while(running) {
res = read(fd, buf, 255);
if(res > 1) {
buf[res-1] = 0;
line = buf;
emit newData(line);

but when i try to run it i got message : "The program has unexpectedly finished"
when i try to comment the "emit newData(line)" with "qDebug() << line;" it'll show the data that i want.
even when i try to use mid() function on line like this "line.mid(2,5)" it'll show the same message, but not with "line.length()"

my purpose it to emit the data, and parse in other class outside this file

anybody can help me please

thanks in advance


24th April 2009, 13:26
I think the problem is emitting a signal with a char*argument.
Try to use a const QByteArray& or const QString&

24th April 2009, 16:16
I think the problem is emitting a signal with a char*argument.
Try to use a const QByteArray& or const QString&

do you mean i should use QByteArray instead of QString for the argument of the "newData"?
can you give any example or URL that show about it?
still a little bit confuse to use QT, but i like QT anyway :)

27th April 2009, 11:52
My solution is use Qextserialport (http://qextserialport.sourceforge.net/qextserialport-1.1.x/)

I use winXP, usb-serial converter and QtCreator 4.5.1

i have tested this with simple microcontroller program which sends data when button is pressed..

When using in "real" situation you need to use QThread to set reader active if you want to maintain programs functionality.

Here is simply code example
comPort = new QextSerialPort("COM7");
//set com port settings


//read data from com port

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;

And here is .pro

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


3rd May 2009, 10:19
thanks for mcosta, I've solved with const QString&
and Jeo_, thanks also, I'll try later :)