Problem in reading port using QextSerialPort
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:
Code:
port = new QextSerialPort("COM1");
port->setBaudRate(BAUD19200);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_2);
then send AT COMMANDS:
Code:
QString command
="AT\r\n";
//sample command - in Hyperterminal this returns OK byte.clear();
byte.append(command);
port->write(byte);
port->waitForBytesWritten (500);
then try to read the response:
Code:
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';
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...
Re: Problem in reading port using QextSerialPort
did you checked what port->open(QIODevice::ReadWrite); is returning?
Re: Problem in reading port using QextSerialPort
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.
1 Attachment(s)
Re: Problem in reading port using QextSerialPort
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! :-)
Re: Problem in reading port using QextSerialPort
Quote:
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.
Quote:
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
Quote:
2 cutie.monkey, use QSerialDevice :-)
ok, i'll try to use QSerialDevice . hope this one will help..
thanks:)
Re: Problem in reading port using QextSerialPort
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.
Re: Problem in reading port using QextSerialPort
hi, instead of QextSerialPort , im now using QSerialDevice which kuzulis suggessted(tnks to kuzulis), and its working.. cheers!;)