PDA

View Full Version : Problem in reading port using QextSerialPort



cutie.monkey
30th June 2009, 11:08
hi all, i'm currently doing an application that will send and receive SMS messages using GSM Modem and by sending AT Commands. I used the QextSerialPort class. I connect to the port using this code:


port = new QextSerialPort("COM1");
port->setBaudRate(BAUD19200);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_2);
port->open(QIODevice::ReadWrite);


then send AT COMMANDS:



QString command ="AT\r\n"; //sample command - in Hyperterminal this returns OK
QByteArray byte;
byte.clear();
byte.append(command);
port->write(byte);
port->waitForBytesWritten (500);


then try to read the response:


char buff[1024];
int numBytes;
numBytes = port->bytesAvailable(); //always returns -1
if(numBytes > 0)
{
if(numBytes > 1024)
numBytes = 1024;
int i = port->read(buff, numBytes);
buff[i] = '\0';
QString msg = buff;
qDebug(msg);
qDebug("bytes available: %d", numBytes);
qDebug("received: %d", i);
}


i'm not sure if im doing it right cause when i read the response the bytesAvailable() always returns -1. Is there anybody here can help me or give me a piece of code on how to do it right? thnks in advance...

nish
30th June 2009, 11:24
did you checked what port->open(QIODevice::ReadWrite); is returning?

^NyAw^
30th June 2009, 11:44
Hi,

Are you executing the write and read sequentially?
You have to wait some time until there is data on the port. You can use a timer that will call a slot where you have to read the data or use a QThread to perfrom read and write with a "msleep" that will wait some time.
The sleep time needed can be calculated with the baud rate.

kuzulis
30th June 2009, 14:33
2 cutie.monkey, use QSerialDevice :-)

this library work in asynchronous mode and support method: waitForReadyRead(msecs)

this library is alternative QextSerialPort

in this library is /examples/reader and /examples/writer ... for testing use it.

PS:
in the library may contain errors!
you can fix them myself! :-)

cutie.monkey
1st July 2009, 04:13
did you checked what port->open(QIODevice::ReadWrite); is returning?
yes, this returns true.. but what i encountered here is this, i removed the modem so nothing is connected in COM1 then try to run my app, it also returns true.


Are you executing the write and read sequentially?
No, i have a button to write command and a button to read its response. I have tried to wait for some seconds before trying get its response but nothing happens. Still the port->bytesAvailable() returns -1


2 cutie.monkey, use QSerialDevice :-)
ok, i'll try to use QSerialDevice . hope this one will help..

thanks:)

^NyAw^
1st July 2009, 10:43
Hi,

Are you sure that the modem is writing somthing? Try using a Serial sniffer to see what are you writing and what is the modem writing. Search "free serial port monitor" on your favorite web search engine.

cutie.monkey
2nd July 2009, 03:07
hi, instead of QextSerialPort , im now using QSerialDevice which kuzulis suggessted(tnks to kuzulis), and its working.. cheers!;)