c++qtserial-portg-code3d-printing

QtSerialPort not writing to serial port


I am trying to send Gcode commands to a 3d printer with QtSerialPort. I am able to connect and read the startup gcode the printer sends out but I haven't been able to write a Gcode to make the printer do something. I have read many people's code and mine doesnt look wrong as far as I can tell but I am definitely missing something.

This is what I use to connect to the 3d printer:

void MainWindow::on_connectButton_clicked()
{
    serial->setPortName(ui->portBox->currentText());
    serial->setBaudRate(250000);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);;
    serial->setFlowControl(QSerialPort::NoFlowControl);
    if (serial->open(QIODevice::ReadWrite)) {
       ui->label->setText("Connected to Printer!!");
    } else {
    ui->label->setText("Error: Failed to connect");
    }
}

This is what I am typing to write to the serial port:

QString command = "G28";
QByteArray x = command.toLocal8Bit();
serial->write(x);

This gives me no errors but the printer doesn't react. I tried the Qt serial console example and sent out "G28" and the printer reacts correctly. on the same program I added a button that executes the above code to write the command and it did nothing. That tells me the problem is likely on how I am writing to the port. Anyone have any ideas on what I am doing wrong? Thanks in advance.


Solution

  • As Rafael Posted above, my problem was I was missing \n after G28. Changing it to this solved my issue:

    QString command = "G28\n";
    QByteArray x = command.toLocal8Bit();
    serial->write(x);