Results 1 to 11 of 11

Thread: how to i synchronize serial read and write without using thread?

  1. #1
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default how to i synchronize serial read and write without using thread?

    I am using qextserialport library for my serial communication. The thing i do is write to serial every 3 seconds. I have used timer that timeouts every 3 seconds and calls a slot for writing. Also i write to serial port on some button clicks. But sometimes my gui freezes may be due to collision between these write commands, not sure though. How do i synchronize this. is there any way of doing it?

  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: how to i synchronize serial read and write without using thread?

    If you have one thread it can only do one thing at a time, it is synchronized by default.

    The event loop can either execute the slot connected to the timer's signal or the slot connected to the button's signal.

    Maybe your freeze is a call blocking, e.g. the write call because there is a limited buffer and it is full, or something like that.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to i synchronize serial read and write without using thread?

    Thanx. Your comments are clear to me. Is there anyway i could block the other events from processing until my current event is being processed? May be this could solve my problem.For example:if my timer timeouts, and its corresponding write code executes and if user presses a button, it would block the processing of the button click event until the previous code is complete?

  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: how to i synchronize serial read and write without using thread?

    That happens automatically. While the timeout is being processed, the main thread is doing that processing. So it can go into the event loop and start processing the user interaction with the button.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to i synchronize serial read and write without using thread?

    If the processing of timeout is handled first, and then my button click is responded, why does it take a lot of time. Sometimes, my GUI freezes and shows not responding.This usually happens when i click my ui pushbutton while the timer event is running. Is there a way i could control it. I have read somewhere about queuing the events like QApplication:rocessEvents(QEventLoop::ExcludeUserInputEvents); could work for me or not?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to i synchronize serial read and write without using thread?

    If you need processEvents() you are likely to be doing something wrong. Why not post some code so we do not have to guess what you are actually doing?

  7. #7
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to i synchronize serial read and write without using thread?

    here are some portion of my code:

    //MaiWindow.cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. timer_for_read=new QTimer(this); //this timer is used for calling continuous_monitor()
    7. QObject::connect(timer_for_read,SIGNAL(timeout()),this,SLOT(continuous_monitor()));
    8. QObject::connect(timer_for_read,SIGNAL(timeout()),timer_for_read,SLOT(start()));//didnt know ways to restart timer again.
    9. start_check[0]=0;
    10.  
    11. }
    12. void MainWindow::on_Start1_clicked()
    13. {
    14. if(!modbus_master.port_init)
    15. modbus_master.init_port();
    16. modbus_master.data_buffer.Reset();//these are for data
    17. modbus_master.data_buffer.Append(50);
    18. while (modbus_master.CheckBusy());
    19. modbus_master.WriteOutputReg(10,1000);
    20. start_check[0]=1;
    21. qDebug()<<timer_for_read->isActive();
    22. if(!timer_for_read->isActive())
    23. timer_for_read->start(5000);
    24. }
    25. void MainWindow::continuous_monitor()
    26. {
    27. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);//dint know where to do this
    28. if(start_check[0]){
    29. while(modbus_master.CheckBusy());// this checks whether the code is listening to response, returns 1 when listening is done
    30. modbus_master.ReadInputReg(10,1000,4);// reads input register// this writes a message to serial port and listens to response
    31. }
    32. if(start_check[1]){
    33. while(modbus_master.CheckBusy());
    34. modbus_master.ReadInputReg(10,1000,4);
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    //ModbuMaster.cpp
    Qt Code:
    1. ModbusMaster::ModbusMaster()
    2. {
    3. timer=new QTimer(this);
    4. QObject::connect(timer,SIGNAL(timeout()),this,SLOT(onTimeOut()));// onTimeout asserts that the response is invalid
    5. QObject::connect(this,SIGNAL(WriteEmit(char*)),this,SLOT(writeData(char*)));
    6. port=new QextSerialPort();
    7. QObject::connect(port,SIGNAL(readyRead()),this,SLOT(readData()));
    8. }
    9.  
    10. void ModbusMaster::readData()
    11. {
    12. qDebug("read data");
    13. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
    14. port->flush();
    15. // char buff[1024];
    16. // qDebug("read data");
    17. QByteArray response=port->readAll();
    18. for(int i=0;i<response.length();i++)
    19. {
    20. ReadCharacterCB((uint8_t)response[i]); //this function has intrepretation of data
    21. }
    22. }
    23. void ModbusMaster::writeData(char* msg)
    24. {
    25. //qDebug("Writing data");
    26. port->write(msg,1);
    27. port->flush();
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 24th January 2014 at 08:02. Reason: add code tags

  8. #8
    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: how to i synchronize serial read and write without using thread?

    First, remove those QApplication:rocessEvents() that you have right now, they are not needed there.
    They currently are in the beginning of slots that are called by the event loop, so there is no point in returning to the event loop again right away.

    Second, you don't need the connect of the timer_for_read to its own slot. A QTImer which has not been put into single shot mode will fire continuously on its own.

    My guess on your blocking problem are the two while loops. Those obviously keep the main thread spinning in there as long as the condition is true.
    Since you are already executing them in a slot that is called regularily, maybe just return if the condition is not true?

    I.e.
    Qt Code:
    1. void MainWindow::continuous_monitor()
    2. {
    3. if(start_check[0]){
    4. if(!modbus_master.CheckBusy()) return;// this checks whether the code is listening to response, returns 1 when listening is done
    5. modbus_master.ReadInputReg(10,1000,4);// reads input register// this writes a message to serial port and listens to response
    6. }
    7. if(start_check[1]){
    8. if(!modbus_master.CheckBusy()) return;
    9. modbus_master.ReadInputReg(10,1000,4);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  9. #9
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to i synchronize serial read and write without using thread?

    I have replaced the while loops with if, but what i observe is the code will now never read the input register. It just skips that part. Also the readyRead signal is not fired at all when i send data using my virtual serial(realterm). I think i am confused between my own stuffs.
    Ok, i'll explain the total motive of my project.
    what i have to do is:
    Write to serialport when buttons are clicked. Also write to serialport continuously every few seconds and receive their response, intrepret it and display in my gui. It seems like i dont get any response. or there's something else wrong. do my readyRead and timer signals interfere with each other?

  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: how to i synchronize serial read and write without using thread?

    The condition is probably the wrong way around. Try
    Qt Code:
    1. if(modbus_master.CheckBusy()) return;
    To copy to clipboard, switch view to plain text mode 

    Not sure why you need that at all, the class seems to be using an event driven I/O (QExtSerialPort) so there should be signals when it becomes read for whatever you are checking.

    Cheers,
    _

  11. #11
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to i synchronize serial read and write without using thread?

    I remove that part completely, it just works fine. Thanks.

Similar Threads

  1. qext-serial-port write problem.
    By rex in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2013, 07:18
  2. Replies: 2
    Last Post: 2nd November 2010, 05:15
  3. Replies: 4
    Last Post: 10th July 2010, 17:34
  4. Replies: 1
    Last Post: 16th June 2009, 09:09
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 14:12

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.