PDA

View Full Version : QIODevice::bytesAvaiable() - error value!



kuzulis
21st May 2009, 06:03
Hello!

I "created" a library for working with serial port to Qt4 in asynchronous mode, while using as a base - QextSerialPort and the full of his altered.

My class is inherited from QIODevice.

Problems arise when working in Qt 4.3.4 under ArchLinux x86_64, which is that if the function bytesAvaiable() call inside a function readDAta() - a function bytesAvaiable() returns an incorrect number of bytes = 16384! o_O

Implementation bytesAvaiable () for POSIX:


qint64 TPosixSerialDevice::bytesAvailable()
{
if (isOpen()) {
int bytesQueued=0;

if (ioctl(fd, FIONREAD, &bytesQueued)==-1) {
TTY_PORTABILITY_DEBUG("TPosixSerialDevice::bytesAvailable->ioctl(FIONREAD)! Error!");
return -1;
}
return bytesQueued + QIODEvice:: bytesAvaiable() ;
}//if isOpen()
TTY_PORTABILITY_DEBUG("TPosixSerialDevice::bytesAvailable->Device is not open! Error!");
return -1;
}


So:
1. If you do it like this:


int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
TSerialDevice *MyDevice; // where TSerialDevice - the heir of QIODEvice!

MyDevice = new TSerialDevice();
if (!MyDevice->open(QIODevice::ReadWrite)) {
return 0;
}
if (MyDevice->waitForReadyRead(3000)) { //wait data 3 sec
cout << "Data is ready" << endl;
int bav = MyDevice->bytesAvailable(); // <========== HERE returning the proper number of bytes in the reception buffer!
}
cout << "End for";
MyDevice->close();
cout << "End Close";

return app.exec();
}

then the function MyDevice-> bytesAvailable () - returns all right!

2. If you do it like this:


int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
TSerialDevice *MyDevice; //where TSerialDevice - the heir of QIODEvice!

MyDevice = new TSerialDevice();
if (!MyDevice->open(QIODevice::ReadWrite)) {
return 0;
}
if (MyDevice->waitForReadyRead(3000)) { //wait data 3 sec
cout << "Data is ready" << endl;
QBytesArray ba = MyDevice->read(); // HERE DOES NOT RIGHT!
}
cout << "End for";
MyDevice->close();
cout << "End Close";

return app.exec();
}

ie if you call read (), which is readData (), which is called bytesAvailable () - it bytesAvailable () returns an incorrect value = 16384!

Implementation readData() for POSIX


qint64 TPosixSerialDevice::readData(char * data, qint64 maxSize)
{
int bytesRead=0;//the number of bytes actually read in one pass
int retVal=0;//the number of bytes actually read all the passages
int bav=0;// the number of bytes ready to read, which are currently in the reception buffer, the serial device
do {
if (maxSize>retVal) {
bav=bytesAvailable(); // <=========== THIS RETURN INCORRECT VALUE = 16384 !!! о_О
if (bav>0) {
if ((maxSize-retVal)<=bav) {
bav=maxSize-retVal;
}
bytesRead = ::read(fd, (void*)(data+retVal), bav);
if (bytesRead==-1) {
TTY_PORTABILITY_DEBUG("TPosixSerialDevice::readData->bytesRead==-1! Error!");
return -1;
}//if read == -1
else {
if (bytesRead==bav) {
retVal+=bytesRead;//with OK
}//if bytesRead==bav
else {
TTY_PORTABILITY_DEBUG("TPosixSerialDevice::readData->(bytesRead!=bav) with OK! Error!");
return -1;
}//else bytesRead!=bav
}//else read
}//if bav>0
else {
TTY_PORTABILITY_DEBUG("TPosixSerialDevice::readData-> (bav<=0)! Error!");
return -1;
}//else bav<=0
}//if maxSize>retVal
else {
return retVal;
}
} while (waitForReadyRead(parameters->charIntervalTimeout));//forward to coming to the reception buffer device next character
return retVal;
}



At the same time when testing in Qt4.1 under Windows - that no error!

Help to understand what's wrong! : (

PS:
source code library and examples can be downloaded at the forum: http://www.prog.org.ru/topic_9537_0.html
there must be pre-registered. Downloads: QSerialDevce.tar.bz2 attached as an attachment.

kuzulis
21st May 2009, 17:30
this my source code attach!

Help me! Please! :(

kuzulis
23rd May 2009, 13:08
People!
Who is someone trying to work with this library? Who has something she has earned? Write to bugs!

wysota
23rd May 2009, 19:37
What is the size reported by QIODevice::bytesAvailable() and what is the size reported by "bytesQueued"?

kuzulis
24th May 2009, 16:04
What is the size reported by QIODevice::bytesAvailable() and what is the size reported by "bytesQueued"?

1. I do not know what is written in an assistant to do so:
[code]
qint64 CustomDevice:: bytesAvailable () const
(
return buffer.size () + QIODevice:: bytesAvailable ();
)
[/ code]
?????
2. I'm reading 10 bytes from the port situation is this:
- Run a test program: examples / release / reader
- Opens a port / dev/ttyS0
- Configure read 10 bytes
- Do waitForReadyRead (5000) / / 5 seconds
- Wait for data to / dev/ttyS0
- Run a test program: examples / release / writer
- Opens a port / dev/ttyUSB0
- Configure write 10 bytes
- Press Enter (while in port / dev/ttyUSB0 recorded 10 bytes)
- At this point in the examples / release / reader - triggered function waitForReadyRead (5000) = true,
which indicates that the / dev/ttyS0 come at least 1 byte
- Then I give the read (10); / /
- Inside the function read (10) is bytesAvailable () and this function returns the number of bytes = 16392
where: QIODevice:: bytesAvailable () = 16384, bytesQueued = 8
- And further back is triggered waitForReadyRead (50) / / inside function read () waiting for the next character
- Inside the function read (10) is bytesAvailable () and this function returns the number of bytes = 16386
where: QIODevice:: bytesAvailable () = 16384, bytesQueued = 2

ie really comes into / dev/ttyS0 10 bytes = 8 + 2

but where the number 16384 is taken - I do not know! o_O

Here is an example of the withdrawal of the test:
[quote]
[root @ myhost reader] # release / reader
Please Enter Device name, specific by OS: / dev/ttyS0
= Defaults parameters =
Device name: / dev/ttyS0
Baud rate: 12
Data bits: 3
Parity: 0
Stop bits: 0
Flow: 0
Char timeout, msec: 50
Trying to open File
Opened File succesfully
Serial device / dev/ttyS0 open in Read Only mode
Please Enter wait timeout for ready read, msec: 5000
Please Enter len data for read, bytes: 10
Starting waiting ready read in time: 18:38:18
bytesQueued = 8
bav = 16392 <======== 16392-8 = 16384 bytes o_O!
bytesRead = 8
TPosixSerialDevice:: readData-> (bytesRead! = Bav) with OK! Error!
bytesQueued = 0
bav = 0
TPosixSerialDevice:: readData-> (bav <= 0)! Error!
Readed is: 0 bytes
Rx:
bytesQueued = 2
bav = 16386 <======== 16386-2 = 16384 tytes o_O!
bytesRead = 2
TPosixSerialDevice:: readData-> (bytesRead! = Bav) with OK! Error!
bytesQueued = 0
bav = 0
TPosixSerialDevice:: readData-> (bav <= 0)! Error!
Readed is: 0 bytes
Rx:
^ C
[root @ myhost reader] #
[/ quote]