Results 1 to 11 of 11

Thread: Sequential work with the port

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2017
    Posts
    30
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    9

    Default Re: Sequential work with the port

    Quote Originally Posted by high_flyer View Post
    did I misunderstand?
    Not certainly in that way.
    In fact, device->open()/close() is just the emission of a signal. I emit a signal switchStateSig, then the slot switchState in the thread of the device is called. I do not control this, I just go further in the thread of the algorithm.
    I can not call directly, because the device and the algorithm are in different threads, so I had to connect them with a signal and a slot.
    This should work like this: from the thread of the algorithm, I call the open/close of the device, I wait for an answer, and only then I go further. Now I just emit a signal and, without waiting for an answer, I go forward. This is very bad. This is the first problem.

    The second problem is that the function waitForReadyRead() unpredictably breaks the whole program. I tried to do this, but I did not get a single byte. Apparently, something is blocked:
    Qt Code:
    1. void ms_delay(int ms){
    2. QElapsedTimer ms_timer;
    3. ms_timer.start();
    4. while(ms_timer.elapsed() < ms){}
    5. return;
    6. }
    7.  
    8. QByteArray ComPort::requestResponse(const QByteArray &data)
    9. {
    10. mutex->lock();
    11. qDebug() << "-------------------------";
    12. if(!serial->isOpen())
    13. open();
    14. int attempts = 1;
    15. QElapsedTimer waitTimer;
    16. readBuf.clear();
    17. while (attempts <= 3) {
    18. if (serial->isWritable())
    19. {
    20. serial->write(data);
    21. waitTimer.restart();
    22. while (waitTimer.elapsed() < 333){
    23. ms_delay(5);
    24. readBuf += serial->readAll();
    25. if (readBuf.size() == 4){
    26. close();
    27. mutex->unlock();
    28. return readBuf;
    29. }
    30. }
    31. readBuf.clear();
    32. qDebug() << "Timeout...";
    33. close();
    34. open();
    35. attempts++;
    36. }
    37. else
    38. {
    39. qDebug() << "Port is not written";
    40. close();
    41. mutex->unlock();
    42. return 0;
    43. }
    44.  
    45. }
    46. close();
    47. mutex->unlock();
    48. return 0;
    49. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Sequential work with the port

    So let me see if I understand:
    Calling the QDevice::waitForReadRead() is not relible on windows, so you don't call it and want to have some other function to call instead that delivers the same functionality.
    Is that correct?
    (All the story around it, how things in other threads are, are not the problem, more the motivation, as far as I can see)

    If so, did you try what I offered in my last post, and if you did, what was the problem?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2017
    Posts
    30
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    9

    Default Re: Sequential work with the port

    Quote Originally Posted by high_flyer View Post
    Is that correct?
    Yes that's right. This is problem number 1. I have not tried your method yet, because no access to the board. I would like to monitor the reception with time, because if my request does not reach board for some reason, she will not answer me, no matter how much time I wait. For this reason, I make repeated requests after the timeout.

    Quote Originally Posted by high_flyer View Post
    All the story around it, how things in other threads are, are not the problem, more the motivation, as far as I can see.
    I can not say with certainty. But I tried to call directly device->switchState(7, true); from algorithm, which got the error that you can not call the methods of another thread, being not in the main thread. Therefore, I connected them with signals.
    Last edited by maratk1n; 18th December 2017 at 21:05.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Sequential work with the port

    because if my request does not reach board for some reason, she will not answer me, no matter how much time I wait. For this reason, I make repeated requests after the timeout.
    or you can set a timeout as well.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2017
    Posts
    30
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    9

    Default Re: Sequential work with the port

    Quote Originally Posted by high_flyer View Post
    or you can set a timeout as well.
    So, how can I do this?..

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Sequential work with the port

    So, how can I do this?..
    there are several ways...
    Probably the least "intrusive" would be to have a timer run and you can check the elapsed time in your while() loop where you are checking the available bytes from the serial device.
    Once the timeout is reached you simply return from your custom waitForReadRead() with false.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Apr 2017
    Posts
    30
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    9

    Default Re: Sequential work with the port

    Quote Originally Posted by high_flyer View Post
    there are several ways...
    Probably the least "intrusive" would be to have a timer run and you can check the elapsed time in your while() loop where you are checking the available bytes from the serial device.
    Once the timeout is reached you simply return from your custom waitForReadRead() with false.
    Thank for you answer. But I already tried to do this way, the port "was silent".
    http://www.qtcentre.org/threads/6904...574#post301574

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Sequential work with the port

    Thank for you answer. But I already tried to do this way, the port "was silent".
    Do you mean by that the the port didn't answer within the time you specified?
    If yes, then try prolonging the timeout.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Apr 2017
    Posts
    30
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    9

    Default Re: Sequential work with the port

    Quote Originally Posted by high_flyer View Post
    Do you mean by that the the port didn't answer within the time you specified?
    If yes, then try prolonging the timeout.
    I tried to increase. Ping using the function waitForReadRead() was 2-5 milliseconds. With the use of a timer, the board absolutely did not answer anything for 333 milliseconds and more. I concluded that data reception is somehow blocked.

Similar Threads

  1. Instructions are NOT sequential in qt/qml?!?
    By JaySDC in forum Qt Quick
    Replies: 9
    Last Post: 24th October 2015, 17:41
  2. serial port command doesn't work
    By hovuquocan1997 in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2015, 16:10
  3. Replies: 2
    Last Post: 26th July 2014, 16:36
  4. Sequential animations on each listitem
    By rama.kesi in forum Qt Quick
    Replies: 3
    Last Post: 12th October 2012, 03:36
  5. Sequential animations on each listitem
    By rama.kesi in forum Qt Quick
    Replies: 0
    Last Post: 8th October 2012, 05:24

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.