gpt4 book ai didi

c++ - QSerialPort 正确发送多行

转载 作者:行者123 更新时间:2023-11-30 03:55:58 70 4
gpt4 key购买 nike

我正在尝试使用 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 文档中找到类似的东西。有什么想法吗?

最佳答案

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

关于c++ - QSerialPort 正确发送多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28770862/

70 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com