Results 1 to 10 of 10

Thread: why bytesAvailable() returns '0' when there are answer data from serial port monitor

  1. #1
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question why bytesAvailable() returns '0' when there are answer data from serial port monitor

    hello, everyone. i use qextserialport in my application to realize serial communication.
    i can write data using writeData(const char * data, qint64 maxSize). But the function bytesAvailable () returns '0' while reading, so i can't get any data through readData(char * data, qint64 maxSize). But i can see the answer data from serial port monitor. WHY??
    my code:
    Qt Code:
    1. if(RS232->open(QIODevice::ReadWrite))
    2. {
    3. RS232->writeData(Data,13);
    4. //usleep(500);
    5.  
    6. int i= RS232->QIODevice::bytesAvailable();
    7. if(i > 0)
    8. {
    9. char *pStr=NULL;
    10. RS232->readData(pStr,25);
    11. }
    12. RS232->close();
    13. }
    To copy to clipboard, switch view to plain text mode 
    adding the "usleep(500);" has no use.

    thanks very much for any advice!
    Last edited by to_guliang; 3rd June 2008 at 08:28.

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

    Default Re: why bytesAvailable() returns '0' when there are answer data from serial port moni

    As far as I remember bytesAvailable() doesn't work with QExtSerialPort.

  3. The following user says thank you to wysota for this useful post:

    to_guliang (4th June 2008)

  4. #3
    Join Date
    Apr 2008
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why bytesAvailable() returns '0' when there are answer data from serial port moni

    1.- Wait for X ms, depending of the data length. The serial port isn't so fast...
    2.- Don't use the serial monitor, because it can take the data from the buffer, cleaning it, and no leaving data in it.

    For example:
    Qt Code:
    1. bool threadDatos::RecibirTrama(char *trama, int num_bytes, datos *Datos)
    2. {
    3. msleep(12); // No queda de otra...esta cosa no tiene timeout ¬¬
    4. int i = PuertoSerial->bytesAvailable(); // Leer cuantos bytes hay
    5. if(i == num_bytes) // Si es igual al número de bytes requeridos
    6. {
    7. Datos->pBuffer->acquire(); // Pedir recurso en el buffer
    8. PuertoSerial->read(trama, num_bytes); // Leer el buffer de recepción
    9. Datos->cBuffer->release(); // Liberar recurso de dato valido
    10. mutex->unlock(); // Liberar puerto serial
    11. return TRUE; // Regresar con TRUE
    12. }
    13. else if(i == num_bytes + 5) // Si hay mas, debido a un NCK de SETP
    14. {
    15. Datos->pBuffer->acquire(); // Pedir recurso en el buffer
    16. PuertoSerial->read(trama, 5); // Leer el buffer de recepción
    17. PuertoSerial->read(trama, num_bytes); // Leer el buffer de recepción
    18. Datos->cBuffer->release(); // Liberar recurso de dato valido
    19. mutex->unlock(); // Liberar puerto serial
    20. return TRUE; // Regresar con TRUE
    21. }
    22. mutex->unlock(); // Liberar puerto serial
    23. return FALSE; // Error: devolver FALSE.
    24. }
    To copy to clipboard, switch view to plain text mode 

    PS: excuse for my english...

  5. The following user says thank you to zeratul for this useful post:

    to_guliang (4th June 2008)

  6. #4
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: why bytesAvailable() returns '0' when there are answer data from serial port moni

    thanks, i will try as you say.

  7. #5
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: why bytesAvailable() returns '0' when there are answer data from serial port moni

    Quote Originally Posted by wysota View Post
    As far as I remember bytesAvailable() doesn't work with QExtSerialPort.
    supposed that bytesAvailable() doesn't work ,i amend my code as:

    Qt Code:
    1. RS232->writeData(Data,13); //write data
    2. usleep(120000); //wait
    3.  
    4. int i= RS232->QIODevice::bytesAvailable();//if there are bytes availble to read
    5.  
    6. QString str;
    7. str.setNum(i);
    8. Com->setText(str);//show the bytes availble to read on the 'Com' Button
    9.  
    10. char *pStr=NULL;
    11. RS232->read(pStr,13); //read data from serial port to pStr
    12. Com1->setText(pStr); //show the data on 'Com1' Button
    13. RS232->close();
    To copy to clipboard, switch view to plain text mode 
    i still can't get any data! why?

    to zeratul: i don't use the serial monitor.

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

    Default Re: why bytesAvailable() returns '0' when there are answer data from serial port moni

    I just checked - bytesAvailable() works fine with the serial port class. If it doesn't work for you, there has to be some other reason for that. What did open() return and what did you pass to it?

  9. The following user says thank you to wysota for this useful post:

    to_guliang (4th June 2008)

  10. #7
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: why bytesAvailable() returns '0' when there are answer data from serial port moni

    Quote Originally Posted by wysota View Post
    I just checked - bytesAvailable() works fine with the serial port class. If it doesn't work for you, there has to be some other reason for that. What did open() return and what did you pass to it?
    thanks for your reply
    i have develop a application that could write and read program to the MELSEC FX-3U PLC.
    open() return true. i write command that read program from PLC to the serial port.
    i can observe the answer data from PLC through serial port, that means i have pass my data succeedly to the PLC and the PLC has answer me.
    but the bytesAvailable() always returns '0' and i can't get any data by using readData(char * data, qint64 maxSize).
    i don't know why

    sorry, my expression is poor,Could you understand me? if don't, please tell me, i will try my best to express myself

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

    Default Re: why bytesAvailable() returns '0' when there are answer data from serial port moni

    How exactly do you "observe" the data passing through the port? If you use another application for that, it could be that it steals the data, as it was already suggested.

  12. The following user says thank you to wysota for this useful post:

    to_guliang (4th June 2008)

  13. #9
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: why bytesAvailable() returns '0' when there are answer data from serial port moni

    Quote Originally Posted by wysota View Post
    How exactly do you "observe" the data passing through the port? If you use another application for that, it could be that it steals the data, as it was already suggested.
    i make sure i have write the data succeedly. the PLC can run with my program that i write to it. According to the protocol of the MELSEC-FX PLC, the PLC will answer me some information.
    i can't get any data even if i close the serial port monitor as i have state it after i try as the suggestion.

  14. #10
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: why bytesAvailable() returns '0' when there are answer data from serial port moni

    the problem has been solved. QIODevice::bytesAvailable()
    i can read data the PLC answer, and the bytesAvailable() returns the right value.
    i should use "RS232->bytesAvailable();", not "RS232->QIODevice::bytesAvailable();"

Similar Threads

  1. First attempt to display serial port data on GUI
    By ShaChris23 in forum Newbie
    Replies: 12
    Last Post: 4th May 2007, 09:14
  2. Replies: 16
    Last Post: 7th March 2006, 15:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.