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:
Qt Code:
  1. qint64 TPosixSerialDevice::bytesAvailable()
  2. {
  3. if (isOpen()) {
  4. int bytesQueued=0;
  5.  
  6. if (ioctl(fd, FIONREAD, &bytesQueued)==-1) {
  7. TTY_PORTABILITY_DEBUG("TPosixSerialDevice::bytesAvailable->ioctl(FIONREAD)! Error!");
  8. return -1;
  9. }
  10. return bytesQueued + QIODEvice:: bytesAvaiable() ;
  11. }//if isOpen()
  12. TTY_PORTABILITY_DEBUG("TPosixSerialDevice::bytesAvailable->Device is not open! Error!");
  13. return -1;
  14. }
To copy to clipboard, switch view to plain text mode 

So:
1. If you do it like this:
Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QCoreApplication app(argc, argv);
  4. TSerialDevice *MyDevice; // where TSerialDevice - the heir of QIODEvice!
  5.  
  6. MyDevice = new TSerialDevice();
  7. if (!MyDevice->open(QIODevice::ReadWrite)) {
  8. return 0;
  9. }
  10. if (MyDevice->waitForReadyRead(3000)) { //wait data 3 sec
  11. cout << "Data is ready" << endl;
  12. int bav = MyDevice->bytesAvailable(); // <========== HERE returning the proper number of bytes in the reception buffer!
  13. }
  14. cout << "End for";
  15. MyDevice->close();
  16. cout << "End Close";
  17.  
  18. return app.exec();
  19. }
To copy to clipboard, switch view to plain text mode 
then the function MyDevice-> bytesAvailable () - returns all right!

2. If you do it like this:
Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QCoreApplication app(argc, argv);
  4. TSerialDevice *MyDevice; //where TSerialDevice - the heir of QIODEvice!
  5.  
  6. MyDevice = new TSerialDevice();
  7. if (!MyDevice->open(QIODevice::ReadWrite)) {
  8. return 0;
  9. }
  10. if (MyDevice->waitForReadyRead(3000)) { //wait data 3 sec
  11. cout << "Data is ready" << endl;
  12. QBytesArray ba = MyDevice->read(); // HERE DOES NOT RIGHT!
  13. }
  14. cout << "End for";
  15. MyDevice->close();
  16. cout << "End Close";
  17.  
  18. return app.exec();
  19. }
To copy to clipboard, switch view to plain text mode 
ie if you call read (), which is readData (), which is called bytesAvailable () - it bytesAvailable () returns an incorrect value = 16384!

Implementation readData() for POSIX
Qt Code:
  1. qint64 TPosixSerialDevice::readData(char * data, qint64 maxSize)
  2. {
  3. int bytesRead=0;//the number of bytes actually read in one pass
  4. int retVal=0;//the number of bytes actually read all the passages
  5. int bav=0;// the number of bytes ready to read, which are currently in the reception buffer, the serial device
  6. do {
  7. if (maxSize>retVal) {
  8. bav=bytesAvailable(); // <=========== THIS RETURN INCORRECT VALUE = 16384 !!! о_О
  9. if (bav>0) {
  10. if ((maxSize-retVal)<=bav) {
  11. bav=maxSize-retVal;
  12. }
  13. bytesRead = ::read(fd, (void*)(data+retVal), bav);
  14. if (bytesRead==-1) {
  15. TTY_PORTABILITY_DEBUG("TPosixSerialDevice::readData->bytesRead==-1! Error!");
  16. return -1;
  17. }//if read == -1
  18. else {
  19. if (bytesRead==bav) {
  20. retVal+=bytesRead;//with OK
  21. }//if bytesRead==bav
  22. else {
  23. TTY_PORTABILITY_DEBUG("TPosixSerialDevice::readData->(bytesRead!=bav) with OK! Error!");
  24. return -1;
  25. }//else bytesRead!=bav
  26. }//else read
  27. }//if bav>0
  28. else {
  29. TTY_PORTABILITY_DEBUG("TPosixSerialDevice::readData-> (bav<=0)! Error!");
  30. return -1;
  31. }//else bav<=0
  32. }//if maxSize>retVal
  33. else {
  34. return retVal;
  35. }
  36. } while (waitForReadyRead(parameters->charIntervalTimeout));//forward to coming to the reception buffer device next character
  37. return retVal;
  38. }
To copy to clipboard, switch view to plain text mode 


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.