Results 1 to 2 of 2

Thread: Synchronized writing in two serialport.

  1. #1
    Join Date
    Jul 2018
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Synchronized writing in two serialport.

    Hi All,


    We are using a QT 5.10 and Ubuntu as OS.
    We aim to communicate with an FTDI chip which have two (virtual) serial ports. Here is the initialization of the two ports:
    Qt Code:
    1. port_1->setPortName("ttyUSB0");
    2. port_1->setBaudRate(QSerialPort::Baud9600);
    3. port_1->setDataBits(QSerialPort::Data8);
    4. port_1->setParity(QSerialPort::NoParity);
    5. port_1->setStopBits(QSerialPort::OneStop);
    6. port_1->setFlowControl(QSerialPort::SoftwareControl);
    7.  
    8.  
    9. port_2->setPortName("ttyUSB1");
    10. port_2->setBaudRate(QSerialPort::Baud9600);
    11. port_2->setDataBits(QSerialPort::Data8);
    12. port_2->setParity(QSerialPort::NoParity);
    13. port_2->setStopBits(QSerialPort::OneStop);
    14. port_2->setFlowControl(QSerialPort::SoftwareControl);
    15. port_2->setReadBufferSize(10);
    To copy to clipboard, switch view to plain text mode 

    Opening, reading and Closing port work properly.
    Writing is each port work properly.
    In our case, we always read from one port, but we write in two ports. The writing should be in a port after the other. Here is an example:
    Qt Code:
    1. n_wrote1 = port1->write(cmd1, COUNT);
    2. n_wrote2 = port2->write(cmd2, COUNT);
    3.  
    4. n_wrote3 = port1->write(cmd3, COUNT);
    5. n_wrote4 = port2->write(cmd4, COUNT);
    6.  
    7. n_wrote5 = port1->write(cmd5, COUNT);
    8. n_wrote6 = port2->write(cmd6, COUNT);
    To copy to clipboard, switch view to plain text mode 

    But when we use this previous code in a loop, it's look like that the writing is not anymore synchronized. Which mean that it can happen that the cmd1 and cmd3 is written in a the port1 before the port2 write the cmd2.

    After looking in different post in this forum, I added
    Qt Code:
    1. qApp->processEvents();
    To copy to clipboard, switch view to plain text mode 
    after writing in each ports with
    Qt Code:
    1. port1->flush();
    To copy to clipboard, switch view to plain text mode 
    like in the example below

    Qt Code:
    1. n_wrote1 = port1->write(cmd, COUNT);
    2. port->waitForBytesWritten(30000);
    3. qApp->processEvents();
    4. port->flush();
    5. qApp->processEvents();
    6.  
    7. n_wrote2 = port2->write(cmd, COUNT);
    8. port->waitForBytesWritten(30000);
    9. qApp->processEvents();
    10. port->flush();
    11. qApp->processEvents();
    To copy to clipboard, switch view to plain text mode 

    Applying these changes helped quite a bit, but from time to time these "glitches" still happen. So what i am looking is to undrestand what qApp->processEvents(); is doing exactely in my case? is it ok to call it many times in my code? and if there is a better way to fix this isssue?

    Many thanks for your help.
    Last edited by Atef; 13th July 2018 at 16:33.

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Synchronized writing in two serialport.

    Hi,

    First of all, regarding the last code example you provided, I can't find differences between waitForBytesWritten() and flush(). Both are blocking calls waiting for the data to be written inside low level layer (OS).

    There is maybe another reason why you can't make it work as you wish. Qt doesn't actually write the data to the hardware, the OS do that.

    Assuming you are using Linux, you have driver managing each serial port, this mean Qt can write the data to the serial port file but the OS scheduler and drivers are responsible of writing the data into the hardware and actually sending them (there will be some buffering and delaying in the process).

    I think sending messages inside a loop on two serial ports and expect than they are sent in a specific order over two different hardwares is not possible (at least with a full reliability). You can only control order in a single serial port but not between two serial port.

    Regards,

Similar Threads

  1. Replies: 0
    Last Post: 1st July 2017, 07:02
  2. Reading/Writing SerialPort and Handle Response
    By mikrocat in forum Qt Programming
    Replies: 14
    Last Post: 17th November 2015, 15:10
  3. Replies: 6
    Last Post: 20th October 2014, 10:54
  4. Communication via SerialPort
    By porterneon in forum Newbie
    Replies: 1
    Last Post: 3rd August 2011, 00:34
  5. qt4 serialport
    By nmn in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2006, 04:55

Tags for this Thread

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.