Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QSerialPort not getting readyRead signal from UART

QSerialPort not getting readyRead signal from UART

Scheduled Pinned Locked Moved Mobile and Embedded
6 Posts 2 Posters 3.2k Views
  • 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.
  • T Offline
    T Offline
    TentNougat
    wrote on last edited by
    #1

    I'm attempting to implement a serial communication object using QSerialPort on an ARM based processor running WEC7. I am able to send data, but I am not getting a readyRead signal, and any data on the port is not being seen. I have another application written in VS2008 which uses File IO with polling and it is seeing data on the port, so I know my hardware is working. I've reimplemented the VS2008 application to use SetCommMask to process the EV_RXCHAR event, and the WaitCommEvent function to capture the event. I am not seeing this event. Has anyone else run into this issue? I'm presently assuming the UART driver is not sending the event and therefore QSerialPort will not issue the readyRead signal. I'm also assuming that QSerialPort does not support polling, as it does not see data if the readyRead or waitForReadyRead are not processed (verified on a PC through Qt Creator). I've attached the code for my implementation of QSerialPort.

    @
    #include "serialcomm.h"

    SerialComm::SerialComm()
    {
    bDEBUG = false;
    serial = new QSerialPort();

    connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError)));
    connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    
    Open();
    

    }

    void SerialComm::readData()
    {
    QString dataRead = serial->readAll();
    inputBuffer.append(dataRead);

    if(dataRead.right(1)!= ">")    // command terminator from device
    {
        inputReady = false;
    }
    else
    {
        inputReady = true;
        if(bDEBUG)
            qDebug() <&lt; inputBuffer;
    }
    

    }

    void SerialComm::handleError(QSerialPort::SerialPortError error)
    {
    if (error == QSerialPort::ResourceError)
    {
    //QMessageBox::critical(this->parent(), QString("Critical Error"), serial->errorString());
    Close();
    }
    }

    // function to flush the buffers on the COM device
    // RX - receive TX - transmit BOTH - both
    void SerialComm::PurgeComm()
    {
    inputBuffer.clear();
    inputReady = false;
    }

    // function to open the stream tot he COM device
    bool SerialComm::Open()
    {
    serial->setPortName("COM0"); // SoM port

    if (serial->open(QIODevice::ReadWrite))
    {
        serial->setBaudRate(QSerialPort::Baud38400);        // PC port
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        return true;
    }
    else
    {
        serial->close();
        return false;
        //QMessageBox::critical(this, tr("Error"), serial->errorString());
    }
    

    }

    // function to close the stream to the COM device
    void SerialComm::Close()
    {
    serial->close();
    }

    // function to send a string to the COM device transmit buffer
    void SerialComm::Write(QString sBuf)
    {
    if(sentButNotRead)
    {
    QString sTemp;
    Read(sTemp);
    }
    PurgeComm();
    QByteArray dataSend = sBuf.toAscii();//QString(ui->command->currentText()+"\r").toAscii();
    serial->write(dataSend);
    if(bDEBUG)
    qDebug() <<dataSend;
    sentButNotRead = true;
    }

    // function to load the contents of the COM device receive buffer into sBuf
    void SerialComm::Read(QString &sBuf, int iMaxWaitTime)
    {
    sBuf = "";
    QTime startTime;
    startTime.start();
    QTime nowTime = QTime::currentTime();
    QTime endTime = nowTime.addMSecs(iMaxWaitTime);
    while( (!inputReady) && (nowTime < endTime) )
    {
    Delay(3);
    nowTime = QTime::currentTime();
    }
    if(inputReady)
    {
    sentButNotRead = false;
    sBuf = inputBuffer;
    }
    if(nowTime >= endTime)
    {
    sBuf = QString("no response! after %1 ms").arg(startTime.elapsed());
    //qDebug() << "\nCOM ERROR!\n";
    }
    return;
    }

    // delay function---units in milliseconds
    void SerialComm::Delay(int iMS)
    {
    QTime dieTime = QTime::currentTime().addMSecs(iMS);
    while(QTime::currentTime() < dieTime)
    {
    QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    }
    }

    void SerialComm::ReadCommand(QString &sCom)
    {
    ReadCommand(sCom, 50);
    }

    void SerialComm::ReadCommand(QString &sCom, int iMaxWaitTime)
    {
    Read(sCom, iMaxWaitTime);}
    }
    @

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kuzulis
      Qt Champions 2020
      wrote on last edited by
      #2

      QSerialPort are not tested on WEC7.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TentNougat
        wrote on last edited by
        #3

        The QSerialPort object is working on WEC7. It needs to be built separately and included. The issue turned out to be caused by trying to use QString as the input buffer, where QByteArray should have been used. Everything is working as expected now.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TentNougat
          wrote on last edited by
          #4

          The QSerialPort object is working on WEC7. It needs to be built separately and included. The issue turned out to be caused by trying to use QString as the input buffer, where QByteArray should have been used. Everything is working as expected now.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on last edited by
            #5

            Hmm.. It is strange that QtSerialPort works on WinCE, because its sources was broken (may broken, as I know). It is just magic.. :)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by
              #6

              Hmm.. It is strange that QtSerialPort works on WinCE, because its sources was broken (may broken, as I know). It is just magic.. :)

              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