简体   繁体   中英

wait for data arrive from serial port in Qt

I use serial port connection in my qt application. My problem- I cant get back the control (or the values comes from the comm port) after sending the command.

I have a class named serial.cpp which responsible to serial port connection. This class contains 2 queues. one for save bytes from the comm port and second for the decoded messages. the class has the functions below:

void Serial::sendCommand(QString s)
{
    QString sometext = s;
    QByteArray ba = QByteArray::fromHex(sometext.toLatin1());
    serial->write(ba);
}

void Serial::serialReceived()
{
    QByteArray ba ;
    serialArray = serial->readAll();
    for (int i=0; i<sizeof(serialArray);i++)
    {
        queueBytes.enqueue(serialArray[i]);  // queue for saving the bytes
    }

    QVector<int> vect = queueBytes.toVector();

    packetSize = 6;
    if (vect.size() >= packetSize)
    { // the whole packet arrived
        for (int i =0 ;i<packetSize;i++)
        {
            item = queueBytes.dequeue();
            ba.append(item);
        }
    }
    if (ba.toHex() == "12ee02010176")
        queueMsgs.enqueue("ACK");

    // ... and so on
}

here is the call class:

void Engine::onNewMessageFromAppReceived(int msgId,QString args)
{
    serial->sendCommand("ee1203190209005569");

    while (serial->queueMsgs.size() == 0) // infinite loop-the queue is always empty
    {
        usleep(1);
    }

    QVector<QString> vect2 = serial->queueMsgs.toVector();
    qDebug() << vect2 << "get ack---" ;
}

please your help

The QSerialPort class inherits from QIODevice which has waitFor... methods that may be what you're looking for. Take a look at these docs .

If you want to handle the serial port asynchronously, use the readyRead signal and perform reading in a function that you connected to that signal. If you don't mind that the operation is blocking, the waitForReadyRead function is what you're looking for.

The good way

Here's the proper way to do it using Qt's signals and slots mechanism . This will not block your GUI and lets your application respond to user actions even while you are waiting for the serial port.

  1. Connect a function to the bytesWritten signal
    The code you want to execute after you sent data through the serial port should be placed in this function.
  2. Connect a function to the readyRead signal The code you want to execute after you read some data from the serial port should be placed in this function.
  3. Open the port

The bad way

In some cases you can do it like this, but it's blocking , meaning that your GUI will freeze while your app is waiting for the serial port. I don't recommend doing it like this.

  1. Open the port
  2. Send data
  3. Call waitForBytesWritten
  4. Call waitForReadyRead

Working example code

Qt has a vast amount of working example code . There are even examples about how to use QSerialPort , and they are well worth checking out. You might be most interested in the async writer example and the async reader example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM