Results 1 to 16 of 16

Thread: QtSerialport windows problem

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QtSerialport windows problem

    hi i write a code for reading serial port in qt 5.1.1 and my code work great in linux but not in windows xp/7, i send a packet to my device and wait for response sync like below code

    Qt Code:
    1. QByteArray packet;
    2. if(m_serial.waitForReadyRead(readTimeout))
    3. {
    4. QByteArray header = m_serial.read(3);
    5. if(header.length() == 3)
    6. {
    7. int len = header.at(2);
    8. if (len <= 0)
    9. {
    10. emit errorhappend("Timeout get header, happend in get header");
    11. return QByteArray();
    12. }
    13.  
    14. packet = m_serial.read(len + 1);
    15. int pos = 0;
    16.  
    17. if(packet.length() == len + 1)
    18. {
    19. packet.prepend(header);
    20. return packet;
    21. }
    22.  
    23. while (m_serial.waitForReadyRead(readTimeout)) {
    24. pos = packet.length();
    25. packet += m_serial.read(len + 1 - pos);
    26. if(packet.length() == len + 1)
    27. {
    28. packet.prepend(header);
    29. return packet;
    30. }
    31. }
    32.  
    33. emit errorhappend("Timeout get responce, happend in get packet ("
    34. + QString::number(packet.length())
    35. + "," +
    36. QString::number(pos) + "," + QString::number(len) + ")");
    37. }
    38. else
    39. emit errorhappend("Timeout get responce, cant get header");
    40. }
    41.  
    42. return QByteArray();
    To copy to clipboard, switch view to plain text mode 
    if i get errorstring() of serial port after first waitforreadyread condition and i get "Unknown Error" in windows
    serial port seems open correctly and write correctly too this is my write code
    Qt Code:
    1. if(!m_serial.isOpen())
    2. return false;
    3.  
    4. m_serial.write(writeBlocks(blockNo, bcount, data, devAddr));
    5. if(m_serial.waitForBytesWritten(writeTimeout))
    6. {
    7. StatusCodes status = Error_Other;
    8. bool val = responceNormal(&status);
    9.  
    10. if(status == Normal && val)
    11. return true;
    12. else
    13. {
    14. emit errorhappend("Previously error happend in Write function, Status(" + statusName(status) + ")");
    15. }
    16. }
    17. else
    18. emit timeout("Write, write data time out");
    19.  
    20. return false;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    waitForReadyRaed() == true means that you have received at least one character. As well it can be two or more characters but this does not mean that you can read the three characters. So You must change logic of lines 2, 3, 4 and 5.

  3. #3
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    tnx for your comment but its not important now, my program in windows dont get even one character , i have "Unknown error" on my serial port , why???? this seems like a bug, i read somewhere waitForReadyRaed() in windows must be run in another thread!! is it true?? i wonder nobody have this problem before

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    Quote Originally Posted by danics View Post
    ...i read somewhere waitForReadyRaed() in windows must be run in another thread!! is it true??
    No, it is not true.

  5. #5
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    seems its a bug in this library for windows, i currently test with mingw and vs2012 too but no difference, some times its read serial but after restart system its not read anything

  6. #6
    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: QtSerialport windows problem

    Hi.

    This "Unknown Error" with an SerialPortError::NoError mean that occured a Timeout error. In Qt 5.2 will be introduced an TimeoutError code.

    Thus, at present it is "bug" which shall be fixed in future Qt 5.2.

    I would advise to you to rewrite your code on non-blocking approach since blocking's approach implementation in QSerialPort has still some errors.

  7. #7
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    Hi,
    are you sure if i change my code (many code) to non-blocking, that will be work in windows??
    i dont want create any pressure here , i mean non-blocking approch isnt a threaded blocking way?? am i wrong?
    tnx

  8. #8
    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: QtSerialport windows problem

    Non-blocking approach is use of signals/slots (event-based), e.g. see Terminal example. There is no need for the waitForXXX() methods and additional threads.

  9. #9
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    i know that, i mean behind the scene in main code of qt serial how implement non-blocking approch, it is not a threaded blocking approch and then emit a signal?

  10. #10
    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: QtSerialport windows problem

    I don't understand, what it isn't clear to you?

    Do not use waitForXX methods, and for:

    * reading - just use an readyRead() signal -> bytesAvailable(), if need -> read()
    * writing - just use write() -> bytesWritten() singal, if need
    * for waiting response - just use an QTimer to detect a wait timeout

    1. write()
    2. run a wait timer
    3. handle or readyRead() or timeout() signal

    .. what is problems?

  11. #11
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    sorry for my bad english if you confused and tnx for your consideration,
    i know how implement non-blocking approach in my code but as i said i must change so many code!!!!
    so i ask myself why non-blocking approch must work in windows? i mean whats the code of qtserialport library written with some skilled programmers, for emitting a signal like readyRead() for me?
    Did they create a thread and listen on port until some bytes come and send a signal? so they listen function is'nt waitForReadyRead() ?
    if they use this function for emitting a signal then non-blocking approach must not work in windows too !! i think. (cause in blocking approach dont work)
    if they usen't this technic i can use non-blocking approach, right?. now you understand why i ask?
    if you see my replies Lesiok, he says to me running a blocking approach in another thread dont effect on my result.

  12. #12
    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: QtSerialport windows problem

    so i ask myself why non-blocking approch must work in windows?
    And why not? o_O

    i mean whats the code of qtserialport library written with some skilled programmers, for emitting a signal like readyRead() for me?
    Yes, it is a main Qt (QIODevice) feature. Any classes, derived from QIODevice do it. Please, look documentation.

    Did they create a thread and listen on port until some bytes come and send a signal?
    Not, thread is not used. Is used an Qt-event loop. Please see how it work in sources of QtSerialPort...

    so they listen function is'nt waitForReadyRead() ?
    Are "listening" an Win32 events from the device. Though, the word "listening" here isn't pertinent...

    if they use this function for emitting a signal then non-blocking approach must not work in windows too !! i think. (cause in blocking approach dont work)
    Here I didn't understand your thought.

    if they usen't this technic i can use non-blocking approach, right?. now you understand why i ask?
    No, I don't understand. Please, read documentation on QIODevice and so on.

    if you see my replies Lesiok, he says to me running a blocking approach in another thread dont effect on my result.
    The methods waitForXX() usually shall be used in other thread because if they are used in main thread - that main thread will be blocked (will be blocked an main GUI Qt-event loop and so on) and you got freezes.

    Besides, I say to you, as the QtSerialPort developer - that the waitForXXX() methods still have some errors in implementation. Therefore I recommend not to use them.

  13. #13
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    Quote Originally Posted by danics
    i mean behind the scene in main code of qt serial how implement non-blocking approch, it is not a threaded blocking approch and then emit a signal?
    Quote Originally Posted by kuzulis View Post
    Not, thread is not used. Is used an Qt-event loop. Please see how it work in sources of QtSerialPort...
    thanks for your usefull answer
    Last edited by danics; 10th September 2013 at 16:11.

  14. #14
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    just for others known
    non-blocking approach dont work either in windosw xp/7 but run in linux

  15. #15
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    Hi,

    non-blocking approach dont work either in windosw xp/7 but run in linux
    I have an application working on Windows XP using QtSerialPort on Qt 4.8.1 with non-blocking approach and it works perfectly.
    Òscar Llarch i Galán

  16. #16
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialport windows problem

    i m using Qt 5.1.1

Similar Threads

  1. Adding QtSerialPort as a library -
    By HSPalm in forum Newbie
    Replies: 14
    Last Post: 26th June 2014, 16:16
  2. Help to generate QtSerialPort documentation
    By cstr in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2013, 19:04
  3. QtSerialPort--how to recognize end of message
    By davethomaspilot in forum Newbie
    Replies: 4
    Last Post: 3rd December 2012, 17:38
  4. QtSerialPort synchronous approach
    By codein in forum Newbie
    Replies: 2
    Last Post: 4th September 2012, 09:45
  5. cannot build qtserialport
    By banlinhtienphong in forum Qt Programming
    Replies: 1
    Last Post: 16th December 2011, 05:56

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.