Results 1 to 15 of 15

Thread: Reading/Writing SerialPort and Handle Response

  1. #1
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Reading/Writing SerialPort and Handle Response

    I connected the ReadyRead-Signal from my SerialPort to the Slot

    Qt Code:
    1. void Serial::handlePortReadyRead(){
    2. QByteArray temp = serialPort.readAll();
    3. qDebug()<<"Read from Port" << temp.toHex();
    4. emit ReadFromPort(temp);
    5. }
    To copy to clipboard, switch view to plain text mode 

    When I start a process I connect the ReadFromPort(QByteArray)-Signal to a Slot, where I handle the repsonse from the port.

    Qt Code:
    1. void Process::analyzeFrame(QByteArray temp){
    2. for(int charCount = 0; charCount < temp.count(); charCount++){
    3. char singleInChar = temp.at(charCount);
    4. validationFlag = handleResponse(&response, deviceAddress, FID, singleInChar, false);
    5. break;
    6. }
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    handleResponse returns 0 or 1. If it returns 1 I want the process to continue, 0 means I have to wait. If validationFlag does not get 1 within 100 msecs the process should stop.

    Qt Code:
    1. serialPort.write(dataToSend);
    2. TimerForReadingFromPort->start(100);
    3. do{
    4. QApplication::processEvents();
    5. if(validationFlag == 1){
    6. //analyze response
    7. ...
    8. response.clear();
    9. }
    10. else if(TimerForReadingFromPort->remainingTime() == 0){
    11. ....
    12. return
    13. }
    14. }
    15. while(validationFlag == 0);//end while-Schleife
    To copy to clipboard, switch view to plain text mode 

    So this process is a very long process. Sometimes it get stucked in the middle of the code, I think it is because of the QApplication:rocessEvents().
    Is there any better way?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    You could just start a timer with 100 ms and stop it when the validation gets 1.
    If the timer fires then you stop.

    No need for a loop or anything like that.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    Qt Code:
    1. QTimer *TimerForReadingFromPort = new QTimer;
    2. TimerForReadingFromPort->setSingleShot(true);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. serialPort.write(dataToSend);
    2. TimerForReadingFromPort->start(100);
    3. QApplication::processEvents();
    4. if(validationFlag == 1){
    5. TimerForReadingFromPort->stop();
    6. //analyze response
    7. ...
    8. response.clear();
    9. }
    10. else if(TimerForReadingFromPort->remainingTime() == 0){
    11. ....
    12. return;
    13. }
    To copy to clipboard, switch view to plain text mode 

    You mean like this? I don't know how this should work (it doesn't). The function when the Timer times out doesn't get called...

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    There is no need for calling processEvents() or the other stuff.

    Connect the timer's timeout() signal to a slot.

    Stop the timer when handleResponse() returns 1.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    Qt Code:
    1. QTimer *TimerForReadingFromPort = new QTimer;
    2. TimerForReadingFromPort->setSingleShot(true);
    3. TimerForReadingFromPort->setInterval(100);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ...
    2. connect(TimerForReadingFromPort,SIGNAL(timeout()),this,SLOT(handleTimeout()));
    3. connect(this,SIGNAL(readCompleteFrame()),TimerForReadingFromPort,SLOT(stop()));
    4. ...
    5. serialPort.write(dataToSend);
    6. TimerForReadingFromPort->start();
    7.  
    8. if(validationFlag == 1){
    9. //analyze response
    10. ...
    11. response.clear();
    12. }
    To copy to clipboard, switch view to plain text mode 

    readCompleteFrame is emitted when handleResponse returns 1 and stops the Timer.
    Last edited by mikrocat; 16th November 2015 at 09:14.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    Quote Originally Posted by mikrocat View Post
    readCompleteFrame is emitted when handleResponse returns 1 and stops the Timer.
    Ok. You could also just have stopped the timer.

    Quote Originally Posted by mikrocat View Post
    Because the function to stop the timer gets never called.
    So what happens?
    You get a timeout but never get into handlePortReadyRead()?
    Or you get into handlePortReadyRead() but never into analyzeFrame()?
    Or you get into analyzeFrame() but handleResponse() never return 1?

    Cheers,
    _

  7. #7
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    Okay this is working now. But now I have following problem:

    I have a loop in which i read lines from a file and send it to my device.

    Qt Code:
    1. while(!Line.isEmpty()){
    2. ....
    3. SerialPort.write(dataToSend);
    4. TimerForReadingFromPort->start();
    5.  
    6.  
    7. Line = nextLineFromFile();
    8. ProgressValue += Line.size()+3;
    9. setProgressvalue(ProgressValue);
    10. }
    To copy to clipboard, switch view to plain text mode 

    So how do i wait for the lines get read from the port before I continue the process?

  8. #8
    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: Reading/Writing SerialPort and Handle Response

    Remeber that serial port is only a pipe. Physical reading has no connection to the logical structure of the transmitted data.
    You must define a protocol which will clearly divided transmitted data into records.

  9. #9
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    The protocol is already defined

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    Quote Originally Posted by mikrocat View Post
    So how do i wait for the lines get read from the port before I continue the process?
    You just don't use a loop.

    Instead you process the next line whenever you know that the previous one has completed.

    So a very simple function that reads one line, updates progress, sends the line and starts the timer.
    You call that when you start the whole process and in the response handler when you know that you can now send the next line.

    Cheers,
    _

  11. #11
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    New problem:
    When i click at the ui while the process the Timer times out, I can't click my ui anymore but the process is still going on til its finished.

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    That is very strange.

    Maybe you could post the current state of your code? There have been many iterations since the initial post and it is not at all clear what the current state is.

    Cheers,
    _

  13. #13
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    Hm okay I will try

    Qt Code:
    1. TimerForReadingFromPort = new QTimer();
    2. TimerForReadingFromPort->setSingleShot(true);
    3. TimerForReadingFromPort->setInterval(100);
    To copy to clipboard, switch view to plain text mode 

    got Response is emitted when validatioFlag turns 1, means when I build a valid answer from what i read from the port.
    handleTimeOut opens a MessageBox that says "No Connection" (which appears when i click on the ui).
    Slot getDevicetype() analyses the data read from port. if this was successfull i disconnect gotResponse from the slot and call the next function for reading from the port.
    Qt Code:
    1. ...
    2. connect(TimerForReadingFromPort,SIGNAL(timeout()),this,SLOT(handleTimeOut()));
    3. connect(this,SIGNAL(gotResponse()),this,SLOT(getDevicetype()));
    4.  
    5. ...
    6. serialPort.write(dataToSend);
    7. TimerForReadingFromPort->start();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Proccess::getDevicetype(){
    2. ... //analysig response
    3. disconnect(this,SIGNAL(gotResponse()),0,0);
    4. jumpToBootloader();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Proccess::jumpToBootloader(){
    2. connect(this,SIGNAL(gotResponse()),this,SLOT(clearResponse()));
    3. ...
    4. serialPort.write(dataToSend);
    5. TimerForReadingFromPort->start();
    6. }
    To copy to clipboard, switch view to plain text mode 

    and so on.

  14. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    That looks sensible.

    Have you tried explicitly stopping the timer when gotResponse is emitted?
    E.g. something like
    Qt Code:
    1. connect(TimerForReadingFromPort,SIGNAL(timeout()),this,SLOT(handleTimeOut()));
    2. connect(this,SIGNAL(gotResponse()),TimerForReadingFromPort, SLOT(stop()));
    To copy to clipboard, switch view to plain text mode 
    I.e. each gotResponse() would first stop the timer.

    Not sure why clicking the UI would get you into a timeout situation, but maybe the thing you do upon clicking takes longer than allowed for the timeout?

    Cheers,
    _

  15. #15
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading/Writing SerialPort and Handle Response

    Quote Originally Posted by anda_skoa View Post
    That looks sensible.
    Yes It is.

    Yep that's the way it works. The Timer gets stopped, when the gotResponse() Signal is emitted.

    Thought this is the way you told me to do it. I don't know how it should work else...

Similar Threads

  1. Replies: 2
    Last Post: 17th April 2015, 10:36
  2. Help in reading and writing xml using qt
    By shah_27 in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2013, 08:32
  3. Replies: 2
    Last Post: 4th July 2010, 22:49
  4. Replies: 2
    Last Post: 8th September 2006, 11:35
  5. reading/writing to file
    By QiT in forum Newbie
    Replies: 2
    Last Post: 11th August 2006, 17:21

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.