[SOLVED]Serial port not sending bytes in my application



  • Hi!

    I created a test app to experiment with QtSerialPort. To verify the behaviour of my app I made a simple embedded device which displays the received bytes on its LCD display. The device is using an FTDI USB-serial converter. The device has been tested carefully in Windows.

    In Linux, I checked with dmesg that the device is listed as ttyUSB0. I gave full permission to everybody to use this device with

    @sudo chmod 0666 /dev/ttyUSB0@

    I tested it with Cutecom and it worked. I also tried with an example project ('Terminal') which came with QtSerialPort package. The 'Terminal' application worked too. However, there is no luck with my app which is very simple and there is not much to go wrong. Here is my code:

    mainwindow.h

    @private:
    Ui::MainWindow *ui;
    QSerialPort *serial;@

    mainwindow.cpp

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    bool status = false;

    QString pname = "ttyUSB0";
    
    serial = new QSerialPort(this);
    serial->setPortName(pname);
    
    status = serial->open(QIODevice::ReadWrite);
    
    if (status){
        serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
        serial->setDataBits(QSerialPort::Data8);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->setParity(QSerialPort::NoParity);
    
        serial->putChar('W');
    }
    else
        serial->close();
    
    serial->close();
    

    }@

    The status variable gave me 'true' which means the port had been opened. But the character 'W' didn't show up on my LCD display nonetheless. I've been eyeballing it for a few days now in vain so any advice would be great. Thank you.

    Edit: the display is OLED not LCD, typo



  • You probably need to read docs for your LCD more thoroughly. I doubt that sending single letter via serial will cause the display to display that letter.



  • [quote author="toptan" date="1363098990"]You probably need to read docs for your LCD more thoroughly. I doubt that sending single letter via serial will cause the display to display that letter.[/quote]

    The external device is working correctly. Trust me, I'm not sending single letters to an OLED display :-)



  • Have you checked whether putChar returns true. After all it is QIODevice of some king. Also you could try write and waitForBytesWritten combo. Something like this:
    @
    QByteArray data("W");
    serial.write(data);
    if (!serial.waitForBytesWritten(waitTimeout)) {
    /* timeout handling */
    }
    @

    I am using array of six 7 segment led displays in my project and QSerialPort works no problem on linux.



  • 2 JustGreg

    Remove serial->close() in end of MainWindow constructor.



  • [quote author="kuzulis" date="1363101362"]2 JustGreg

    Remove serial->close() in end of MainWindow constructor.[/quote]

    Thank you kuzulis. That was the problem, however my app is still not sending the bytes when I'm stepping over the code in debug mode. Only in 'Run' mode. Do you know what can cause this behaviour? It'd be nice to know.

    @toptan: the putChar method returned true, even in debug mode where there were no bytes sent to my device...strange. Thank you for your help, anyway.



  • No problem. I also have the problem in debug mode. I overcame that with a lot of qDebug(), if you find solution for step-by-step debugging please share.



  • bq. That was the problem, however my app is still not sending the bytes when I’m stepping over the code in debug mode. Only in ‘Run’ mode. Do you know what can cause this behaviour? It’d be nice to know.

    Because all I/O operations processing asynchronously in Qt event-loop, so in debug step-by-step event-loop is stopped by step-by-step. :)

    So, nothing strange in this...

    UPD:
    Also, port->write() always returns true, because it write data to internal ring buffer.
    So, for catch errors you can connect to SerialPortError signal.



  • [quote author="toptan" date="1363102507"]No problem. I also have the problem in debug mode. I overcame that with a lot of qDebug(), if you find solution for step-by-step debugging please share.[/quote]

    Right, I'll check qDebug then. I'm new to Qt & Linux so do not expect too much brainpower from me :-)

    [quote author="kuzulis" date="1363102625"]
    Because all I/O operations processing asynchronously in Qt event-loop, so in debug step-by-step event-loop is stopped by step-by-step. :)

    So, nothing strange in this...[/quote]

    Good to know. Thank you and I'll be back soon with my receive event signal-slot questions :-)



  • [quote author="kuzulis" date="1363101362"]2 JustGreg

    Remove serial->close() in end of MainWindow constructor.[/quote]

    One more question: what could cause the putChar process to fail? The close method was too fast and it had already closed the port before the internal buffer was emptied?



  • Yes. At the moment to the call close() does not force flush data.

    So, you can do port->flush() before port->close()

    UPD: On future maybe behavior can change... and flush() will called automatically on close().



  • Ok and thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.