QSerialPort 正确发送多行

QSerialPort proper sending of many lines

我正在尝试使用 QSerialPort (QT 5.3.1) 将非常大的文件写入串行端口。问题是 - 我一直在发送超过设备可以处理的数量。 Programm 是这样工作的(这个函数每 50ms 调用一次):

void MainWindow::sendNext()
{
    if(sending && !paused && port.isWritable())
    {
        if(currentLine >= gcode.size()) //check if we are at the end of array
        {
            sending = false;
            currentLine = 0;
            ui->sendBtn->setText("Send");
            ui->pauseBtn->setDisabled("true");
            return;
        }
        if(sendLine(gcode.at(currentLine))) currentLine++; //check if this was written to a serial port
        ui->filelines->setText(QString::number(gcode.size()) + QString("/") + QString::number(currentLine) + QString(" Lines"));
        ui->progressBar->setValue(((float)currentLine/gcode.size()) * 100);
    }
}

但它最终会出现缺陷并挂起(在设备上,而不是在 PC 上)。如果我能以某种方式检查设备是否已准备好用于下一行,但我无法在 QSerial 文档中找到类似的东西。 有什么想法吗?

RS232 确实有一些流量控制功能。 检查您的设备是否使用 RTS/CTS,如果是,请更改连接属性以使用硬件流控制。 QSerialPort 还允许使用 dataTerminalReadyrequestToSend

手动检查流量控制线

您可以使用QSerialPort::waitForBytesWritten来确保写入字节。但是这个函数会阻塞线程,建议在新线程中使用它,否则你的主线程会被阻塞,你的应用程序会定期冻结。