[Qt 5.1] QSerialPort Problems,

  • Hi Qt-People!

    in short my environment:
    Microcontroller -> USART -> Bluetooth -> PC with Qt
    PC has Qt 5.1 minGW 32 Windows 8 x64
    Microcontroller is sending well to PC i have checked with Putty.

    I get no Errormessage or any other hint which little devil is in my code ;)

    #include <QCoreApplication>
    #include <QtSerialPort/QtSerialPort>
    #include <QIODevice>
    #include <unistd.h>

    int main(int argc, char *argv[])
    QByteArray incoming="";
    QCoreApplication a(argc, argv);
    QSerialPort mySerialPort;

    QString portname;
            qDebug() << "Device not openable";
            return -1;
    QByteArray sendString;
    sendString = "u";
    if(!mySerialPort.setBaudRate(QSerialPort::Baud115200))          {qDebug() << "Error setting Baud";return -2;}
    if(!mySerialPort.setParity(QSerialPort::NoParity))              {qDebug() << "Error setting Parity";return -3;};
    if(!mySerialPort.setDataBits(QSerialPort::Data8))               {qDebug() << "Error setting Data";return -4;}
    if(!mySerialPort.setStopBits(QSerialPort::OneStop))             {qDebug() << "Error setting Stopbits";return -5;}
    if(!mySerialPort.setFlowControl(QSerialPort::NoFlowControl))    {qDebug() << "Error setting Flow";return -6;}
        qDebug() << "Bytes to TX      :     " << mySerialPort.bytesToWrite();
        qDebug() << "Bytes to RX      :     " << mySerialPort.bytesAvailable();
        if  (  mySerialPort.isWritable() )
            qDebug() << "Device is not writeable";
        if  (  !mySerialPort.flush() )
            qDebug() << "Device is not flushable";
        if(mySerialPort.bytesAvailable() != 0)
            incoming = mySerialPort.readLine();
            qDebug() << "RXed:    " << incoming;
    return a.exec(&#41;;


    Have any one hints to fix get connection to my BT device?

    Best regards,

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The only thing that would be suspicious to me is that you are using an infinite loop before starting Qt's event loop. You should rather use the event driven system to get the data from your serial port.

    Based on the QtSerialPort document, I would also try to retrieve the serial port info from QSerialPortInfo::availablePorts() that correspond to COM6

    Hope it helps

  • I have now puted the code in to a Qthread and run it from a Gui app but with no effect. So i tested it from from a Apple Mac and a other Linux system, but also with no effect. After that i build a communicationtrack with no Bluetooth. There was also no effect the com works very well with old Qt 4.x programm i have written but not with the new QSerialPort class. I think now i will use the Class not in the right way. Is there any proper working examples for the 5.1 QSerialPort? I only found examples for Qext or any older stuff.

    Best regards,

  • Lifetime Qt Champion

    A QSerialPort is a QIODevice so you can use the same concepts:
    connect the readyRead signal to a slot so you get notifications when new data are available and read them in the slot, do the necessary processing etc...

    By removing this infinite loop, you'll have cleaner code

  • 2envii,

    Your code is wrong.

    QtSerialPort to work is required a Qt event-loop in async mode, and waitForXX() methods in blocking mode (as in your code).

    So, just add waitForBytesWritten() after write() and/or waitForReadyRead() before read() method. Methods waitForXX() is necessary to use if you don't use Qt event-loop. Please see examples "Blocking Master/Slave".

    But I wouldn't recommend to use blocking approach. Use non-blocking/asynchronous, as in "Simple Terminal" example.

    PS: And, please, read documentation!

Log in to reply

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