简体   繁体   English

等待数据从Qt的串行端口到达

[英]wait for data arrive from serial port in Qt

I use serial port connection in my qt application. 我在qt应用程序中使用串行端口连接。 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. 我有一个名为serial.cpp的类,它负责串行端口连接。 This class contains 2 queues. 此类包含2个队列。 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. QSerialPort类继承自QIODevice ,该类具有waitFor...方法,这可能是您正在寻找的方法。 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. 如果要异步处理串行端口,请使用readyRead信号,并在连接到该信号的功能中执行读取。 If you don't mind that the operation is blocking, the waitForReadyRead function is what you're looking for. 如果您不介意该操作正在阻塞,则可以使用waitForReadyRead函数。

The good way 好方法

Here's the proper way to do it using Qt's signals and slots mechanism . 这是使用Qt的信号和时隙机制做到这一点的正确方法。 This will not block your GUI and lets your application respond to user actions even while you are waiting for the serial port. 不会阻塞您的GUI,即使您正在等待串行端口,也可以让您的应用程序响应用户的操作。

  1. Connect a function to the bytesWritten signal 将功能连接到bytesWritten写入信号
    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. 将功能连接到readyRead信号从串行端口读取一些数据之后要执行的代码应放在此功能中。
  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. 在某些情况下,您可以像这样进行操作,但是会阻塞 ,这意味着在应用程序等待串行端口时,GUI将冻结。 I don't recommend doing it like this. 我不建议这样做。

  1. Open the port 打开端口
  2. Send data 发送数据
  3. Call waitForBytesWritten 调用waitForBytesWritten
  4. Call waitForReadyRead 调用waitForReadyRead

Working example code 工作示例代码

Qt has a vast amount of working example code . Qt有大量的工作示例代码 There are even examples about how to use QSerialPort , and they are well worth checking out. 甚至还有关于如何使用QSerialPort ,非常值得一试。 You might be most interested in the async writer example and the async reader example . 您可能对异步 编写器示例异步阅读器示例最感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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