Results 1 to 5 of 5

Thread: QIODevice::bytesAvaiable() - error value!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QIODevice::bytesAvaiable() - error value!

    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.

  2. #2
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QIODevice::bytesAvaiable() - error value!

    this my source code attach!

    Help me! Please!
    Attached Files Attached Files

  3. #3
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QIODevice::bytesAvaiable() - error value!

    People!
    Who is someone trying to work with this library? Who has something she has earned? Write to bugs!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QIODevice::bytesAvaiable() - error value!

    What is the size reported by QIODevice::bytesAvailable() and what is the size reported by "bytesQueued"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QIODevice::bytesAvaiable() - error value!

    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]

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.