Qt World Summit: Register Today!

Serial Port Not Opening

  • Hi,

    I am trying to write to a serial port printer. I use Qt5.4 and the below code:

    bool SerialPortWriter::openPort(QString portName, QSerialPort::BaudRate actualBaudRate)
        qDebug() << Q_FUNC_INFO << "Invoked";
        bool open = false;
        if (NULL != m_pSerialPort)
            qDebug() << Q_FUNC_INFO << "set name & params";
            qDebug() << Q_FUNC_INFO << "set baudrate:" <<
            qDebug() << Q_FUNC_INFO << "set databits:" <<
            qDebug() << Q_FUNC_INFO << "set parity:" <<
            qDebug() << Q_FUNC_INFO << "set stopbits:" <<
            qDebug() << Q_FUNC_INFO << "set flow contrl:" <<
            open = m_pSerialPort->open(QIODevice::WriteOnly);
        qDebug() << Q_FUNC_INFO << "Exits" << open;
        return open;
    void SerialPortWriter::handleErrorOccurance(QSerialPort::SerialPortError error)
        qDebug() << Q_FUNC_INFO << "Invoked";
        qDebug() << Q_FUNC_INFO << m_pSerialPort->errorString();
        qDebug() << Q_FUNC_INFO << m_pSerialPort->error();
        if (error == QSerialPort::WriteError)
            qDebug() << Q_FUNC_INFO << m_pSerialPort->error();

    I get the error
    void SerialPortWriter::handleErrorOccurance(QSerialPort::SerialPortError) Invoked
    void SerialPortWriter::handleErrorOccurance(QSerialPort::SerialPortError) "Incorrect function."
    void SerialPortWriter::handleErrorOccurance(QSerialPort::SerialPortError) 11
    bool SerialPortWriter::openPort(QString, QSerialPort::BaudRate) Exits false

    Can someone help to know why this happens? Is there anything with the driver in my PC. From windows when I try to test print it works fine..

  • Lifetime Qt Champion


    Are you sure you are using the right set of parameters when configuring the serial port ?

  • @SGaist :
    I have two com ports com2 and com3
    I've installed an application to set the logo in printer and connected to printer with com3. Then the problem started.

    Now I found com2 connects fine and is printing. But com3 doesn't gets connected through Qt (even after restart of OS).
    If i configure a printer in windows "device and printers" to com3 and do test print it will print.

    If I give command mode com3:9600,n,8,1 in command prompt it will say Device COM3 is not currently available.

    Not sure what went wrong with com3.

  • Lifetime Qt Champion

    Maybe an application is locking it

  • @SGaist : Once restarted it shoudn't.. right?

  • Hi
    When you install a serial printer, Windows lock the serial port for any other application, even you don't use the printer.
    You must modify the printer port or uninstall the printer to acces the serial port.

  • void SerialPortWriter::handleErrorOccurance(QSerialPort::SerialPortError) "Incorrect function."

    I suspect that error "Incorrect function" comes from the QSerialPort::open() method (yes, can you check it?). If so, then it is related that QSerialPort trying to query states of DTR/DSR pins when opening. So, if this pins are not supported by the driver, then this method can return this error.

    So, you should try to use Qt 5.4.1 instead.. Maybe this helps. But if not - then try to debug code of QSerialPort himself and let us know where is a problem. Because I do not have your printer hardware.. Also you can try to update a driver of yur printer..

  • @kuzulis_

    I've installed Qt 5.4.1. The same error is still there.

    I'm not able to go into QSerialPort::open() method when I debug my application...
    I have installed with qt source when installed. So can see 'Src' in my installation path. I am not exactly sure how to debug in QSerialPort code. if you could brief I love to proceed .. :)

  • if you could brief I love to proceed .. :)

    1. Download the QtSerialPort sources (e.g. from the 5.4.1 branch)
    2. Run QtCreator and open the project file: qtserialport/qtserialport.pro
    3. Go to "Project->Build->Build Steps -> Make -> Make arguments" and type "all" in field. This allow to build QtSerialPort with examples
    4. Run re-build all
    5. Go to "Project->Build->Run" and select any example to run.
    6. Set break-point to QSerialPort::open() method in source file, e.g. to: "qtserialport/src/serialport/qserialport.cpp"
    7. Run debugging of selected example and try to open device.. Watch for debug steps.

Log in to reply