user2279559
user2279559

Reputation: 107

QSerialPort proper sending of many lines

I am trying to write really big files to serialport using QSerialPort (QT 5.3.1). The problem is - I keep sending more than device can handle. Programm works like this (this function is called once in 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);
    }
}

But it eventually gets flawed and hangs (on the device, not on the PC). If only I could check somehow if the device is ready or not for next line, but I cant find anything like it in the QSerial docs. Any ideas?

Upvotes: 1

Views: 1009

Answers (2)

Nejat
Nejat

Reputation: 32645

You can use QSerialPort::waitForBytesWritten to ensure that the bytes are written. However this function would block the thread and it's recommended to use it in a new thread, otherwise your main thread would be blocked and your application freezes periodically.

Upvotes: 1

riodoro1
riodoro1

Reputation: 1256

The RS232 does have some flow control capabilities. Check if Your device uses RTS/CTS and if so change the connection properties to use hardware flow control. The QSerialPort also allows for checking the flow control lines manually with dataTerminalReady or requestToSend

Upvotes: 1

Related Questions