Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Serial Port Not Opening
Forum Updated to NodeBB v4.3 + New Features

Serial Port Not Opening

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 4.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abin
    wrote on last edited by Abin
    #1

    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";
            m_pSerialPort->setPortName(portName);
            qDebug() << Q_FUNC_INFO << "set baudrate:" <<
                        m_pSerialPort->setBaudRate(actualBaudRate);
            qDebug() << Q_FUNC_INFO << "set databits:" <<
                        m_pSerialPort->setDataBits(QSerialPort::Data8);
            qDebug() << Q_FUNC_INFO << "set parity:" <<
                        m_pSerialPort->setParity(QSerialPort::NoParity);
            qDebug() << Q_FUNC_INFO << "set stopbits:" <<
                        m_pSerialPort->setStopBits(QSerialPort::OneStop);
            qDebug() << Q_FUNC_INFO << "set flow contrl:" <<
                        m_pSerialPort->setFlowControl(QSerialPort::HardwareControl);
            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(qPrintable(m_pSerialPort->errorString()));
            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..

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

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

        A Offline
        A Offline
        Abin
        wrote on last edited by Abin
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Maybe an application is locking it

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • SGaistS SGaist

            Maybe an application is locking it

            A Offline
            A Offline
            Abin
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              difix
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kuzulis_
                wrote on last edited by
                #7

                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..

                A 1 Reply Last reply
                0
                • K kuzulis_

                  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..

                  A Offline
                  A Offline
                  Abin
                  wrote on last edited by
                  #8

                  @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 .. :)

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kuzulis_
                    wrote on last edited by kuzulis_
                    #9

                    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.
                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved