Results 1 to 16 of 16

Thread: QtSerialport windows problem

Hybrid View

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

    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,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    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
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 1 Time in 1 Post

    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,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    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
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 1 Time in 1 Post

    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
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 43 Times in 42 Posts

    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
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 1 Time in 1 Post

    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
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 43 Times in 42 Posts

    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
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 1 Time in 1 Post

    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
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 43 Times in 42 Posts

    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
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 1 Time in 1 Post

    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.

Similar Threads

  1. Adding QtSerialPort as a library -
    By HSPalm in forum Newbie
    Replies: 14
    Last Post: 26th June 2014, 17:16
  2. Help to generate QtSerialPort documentation
    By cstr in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2013, 20:04
  3. QtSerialPort--how to recognize end of message
    By davethomaspilot in forum Newbie
    Replies: 4
    Last Post: 3rd December 2012, 18:38
  4. QtSerialPort synchronous approach
    By codein in forum Newbie
    Replies: 2
    Last Post: 4th September 2012, 10:45
  5. cannot build qtserialport
    By banlinhtienphong in forum Qt Programming
    Replies: 1
    Last Post: 16th December 2011, 06: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
  •  
Qt is a trademark of The Qt Company.