QSerialPort许多线路的正确发送

QSerialPort proper sending of many lines

本文关键字:许多线 QSerialPort      更新时间:2023-10-16

我正试图使用QSerialPort(QT 5.3.1)将非常大的文件写入串行端口。问题是,我不断发送超出设备处理能力的文件。程序工作方式如下(此函数在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文档中找不到类似的东西。有什么想法吗?

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

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