Receiving garbage data from serial port

  • I'm using serial port class for transferring data from machine to machine. Before I open the port I define the following settings:

    @Port = new QSerialPort(this);
    Port ->setPortName("COM16");
    Port ->setBaudRate(QSerialPort::Baud115200);
    Port ->setDataBits(QSerialPort::Data8);
    Port ->setParity(QSerialPort::NoParity);
    Port ->setFlowControl(QSerialPort::NoFlowControl);
    Port ->setStopBits(QSerialPort::OneStop);
    Port ->clear();@
    My problem is, that sometimes when I run my app I get from the Serial port garbage data, the issue gets fixed when I open ExtraPutty tool ( on the same port- I get their the correct data and afterwards when I open my application everything is OK and I receive normal data. Probably the ExtraPutty defines some setting that I missed and the settings stays when I open the port from my application. can some one help me figure out what is the issue?

  • Lifetime Qt Champion


    You didn't open the serial port before doing the rest

  • well, I didn't write here the line that I'm opening the port, I open the port after setting these above details.
    for the case it makes sense I'm adding here the code that opens the port:

    @Port->open(QIODevice:: ReadWrite);
    if ((Port->isOpen()))

        connect(Port, SIGNAL(readyRead()), this,SLOT(DataReceived()));
        QMessageBox::critical(this, tr("Error"), Port->errorString());

  • Lifetime Qt Champion

    You have to open it before setting anything

  • Also, you should to check the return from the all setXX() methods.

  • I checked the return. the return is false and the last error message is: "the handle is invalid"

    what does it mean, and what should I do in order to fix it?

  • bq. what does it mean,

    It means that the device isn't opened.

    bq. and what should I do in order to fix it?

    For the old Qt versions (is lower than 5.3.1) it is necessary: at first - try to open device, and second - try to do setXX() methods (but not vice versa!!!).

    Now (for the Qt 5.3.1 and above) in it there is no need: it is possible to do setXX() before device opening.

    But anyway it is necessary always to do check of return codes.

Log in to reply

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