Results 1 to 5 of 5

Thread: Qserialdevice 2.0 waitForBytesWritten halfduplex question

  1. #1
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Qserialdevice 2.0 waitForBytesWritten halfduplex question

    I have a question, writing a Qt GUI and I have the RTS line connected to a RS-485 driver transmit/receive control signal. I am sending out x number of bytes, I use the setRts(1) to make it go high (start of transmisson) and setRts(0) when I finish (as direction control). Does the waitForBytesWritten method work? I want it to wait until all the bytes have been transmitted out of the serial port, so I can setRts low. I see that sometimes the RTS signal is going low before all the bytes have been transferred. Any one have any ideas?

    Do I have to set it up in unbuffered mode?

    Here is a snippet of my code. Thanks in advance.

    Qt Code:
    1. void TraceDialog::procSendButtonClick()
    2. {
    3. QByteArray data;
    4. data.append(ui->lineEdit->text());
    5. m_port->setRts(1);
    6. if (data.size() > 0) {
    7. m_port->write(data);
    8. printTrace(data, false);
    9. ui->lbError->setText(QString::number(m_port->error()));
    10. }
    11. // Add in sleep timer
    12.  
    13. while(m_port->waitForBytesWritten(15)); // Time out for 15 ms
    14.  
    15. m_port->setRts(0);
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 12th February 2012 at 22:07. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qserialdevice 2.0 waitForBytesWritten halfduplex question

    To be honest, this method waitForBytesWritten() has not been tested.

    >Do I have to set it up in unbuffered mode?
    Try it.

  3. #3
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qserialdevice 2.0 waitForBytesWritten halfduplex question

    Hi Kuzulis,

    I see your the expert in qserialdevice. I added a delay (depends on bytes sent and baud rate) to change the RTS signal. Seems to work.

    I have another question I want to receive back a variable string of bytes. The first byte contains the amount (max is 255) is there a way to read one byte and then read x bytes depending on the first byte? In the code below I see the first byte with a msgsize of 15, but I don't see the 15 bytes of data. I know they are coming over because I can see them on the logic analzer. Do you know what is wrong?

    Thanks

    void TraceDialog:rocReadyRead()
    {
    while(m_port->bytesAvailable() < 1) {} //wait for datacount

    char msgsize[1];
    // Read the first byte to determine amount of bytes
    qint64 datacnt = m_port->read(msgsize, 1);

    char datamsg[32];
    while(m_port->bytesAvailable() == 15) {} //wait for datacount
    qint64 data2cnt = m_port->read(datamsg, 15);
    //QByteArray data = m_port->readAlll();

    //printTrace(data, true);
    ui->lbError->setText(QString::number(m_port->error()));
    }

  4. #4
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qserialdevice 2.0 waitForBytesWritten halfduplex question

    Do not use call waiting with a "while", because of this, you're slow/freeze Qt event loop.

    Use signals and slots.

    To find out about the possibility of reading the first 15 bytes, for example, can after first emitted signal readyRead(), make to run QTimer, with wait timeout limit. Thus, after each emitting readyRead() to check the number of bytes available for reading bytesAVailable(), and if it is >= 15 then reset QTimer and read the data; if <15 then do nothing (wait for the next readyRead()). If worked signal from QTimer, then the limit has expired timeout - then do something.

    Ie this approach the analysis and reading the input data will be executed asynchronously, as if with a delay in phase relative to their real receive.

    So it must be done!

  5. #5
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qserialdevice 2.0 waitForBytesWritten halfduplex question

    Thanks for your help, this works without the timer. Going to add in a timeout if I don't get the message in (baud_rate*numbytes) time to reset msgsize back to zero. I will never use while loops in a slot.

    void MainWidget:rocSerialDataReceive()
    {
    if (this->initTraceWidget() && this->serial && this->serial->isOpen()) {

    if ((msgsize == 0) && (serial->bytesAvailable() >= 1))
    {
    QByteArray msg_size = this->serial->read(1);
    msgsize = msg_size[0];
    }
    if ((msgsize != 0) && (this->serial->bytesAvailable() >= msgsize))
    {
    QByteArray data = this->serial->read(msgsize);
    msgsize = 0;
    this->traceWidget->printTrace(data, true);
    }
    }
    }

Similar Threads

  1. QSerialDevice enumerator deletion question
    By marcvanriet in forum Qt Programming
    Replies: 4
    Last Post: 28th December 2011, 02:48
  2. QSerialDevice on Symbian
    By hubbobubbo in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2011, 20:00
  3. QSerialDevice
    By clinisbut in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2009, 14:30
  4. question about QSerialDevice (baudrate)
    By mastupristi in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2009, 09:54
  5. waitForBytesWritten
    By smalls in forum Qt Programming
    Replies: 8
    Last Post: 1st March 2006, 17:42

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.