Skip to content

Commit

Permalink
add "unsafe" fall-back-mode
Browse files Browse the repository at this point in the history
For some controllers the welcome message is not received properly, add a
"unsafe" mode to continue in that cases.
Add extra timer handler for first connect with longer delay.
  • Loading branch information
EtlamGit committed Jan 16, 2018
1 parent b3dde9b commit e831826
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
74 changes: 54 additions & 20 deletions source/GrblCommander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "ui_EasyGrblSetup.h"

#include <QSerialPortInfo>

#include <QMessageBox>

GrblCommander::GrblCommander(Ui::EasyGrblSetup *ui)
: ui(ui)
Expand All @@ -25,10 +25,6 @@ GrblCommander::GrblCommander(Ui::EasyGrblSetup *ui)
this, &GrblCommander::comHandleReadyRead);
// connect( &m_serialPort, &QSerialPort::errorOccured,
// this, &SerialCommands::comHandleError);

// timer
connect( &m_timer, &QTimer::timeout,
this, &GrblCommander::handleTimer );
}


Expand All @@ -47,7 +43,42 @@ void GrblCommander::handleTimer()
appendCommand("$G (no-logging)"); // request gcode parser state
}
} else
comDisconnect();
ui->toolButton_disconnect->click();
}

void GrblCommander::handleFirstConnect()
{
if (m_grblFound) {
requestFirstStatus();
} else {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
msgBox.setText("No grbl controller found!");
msgBox.setInformativeText("For safety reasons no communication is send via COM until this message is received.\nYou can disable this on your own risk.");
QAbstractButton* pUnsafe = msgBox.addButton(tr("Unsafe"), QMessageBox::YesRole);
msgBox.addButton(QMessageBox::Ok);
msgBox.exec();
if (msgBox.clickedButton() == pUnsafe) {
m_grblFound = true;
requestFirstStatus();
} else
ui->toolButton_disconnect->click();
}
}

void GrblCommander::requestFirstStatus()
{
// reconnect timer
disconnect(&m_timer, 0, 0, 0);
connect( &m_timer, &QTimer::timeout,
this, &GrblCommander::handleTimer );
m_timer.start(250);

sendRealtime('?'); // request first status
appendCommand("$I (no-logging)"); // request build info
appendCommand("$$ (no-logging)"); // request settings
appendCommand("$G (no-logging)"); // request gcode parser state
appendCommand("$# (no-logging)"); // request gcode parameters
}


Expand Down Expand Up @@ -188,9 +219,9 @@ bool GrblCommander::comConnect()
m_serialPort.setStopBits( QSerialPort::OneStop );

if (m_serialPort.open(QIODevice::ReadWrite)) {
ui->toolButton_rescan ->setVisible(false);
ui->comboBox_com ->setVisible(false);
ui->toolButton_connect->setVisible(false);
ui->toolButton_rescan ->setVisible(false);
ui->comboBox_com ->setVisible(false);
ui->toolButton_connect ->setVisible(false);

ui->label_grbl_version ->setVisible(true);
ui->label_grbl_buffer ->setVisible(true);
Expand All @@ -203,7 +234,12 @@ bool GrblCommander::comConnect()

m_grblFound = false;

m_timer.start(250);
// timer
disconnect(&m_timer, 0, 0, 0);
connect( &m_timer, &QTimer::timeout,
this, &GrblCommander::handleFirstConnect );
m_timer.start(500);

return true;
}

Expand All @@ -213,16 +249,18 @@ bool GrblCommander::comConnect()

void GrblCommander::comDisconnect()
{
disconnect(&m_timer, 0, 0, 0);
m_timer.stop();
m_serialPort.close();

ui->label_grbl_version ->setVisible(false);
ui->label_grbl_buffer ->setVisible(false);
ui->toolButton_disconnect->setVisible(false);

ui->toolButton_rescan ->setVisible(true);
ui->comboBox_com ->setVisible(true);
ui->toolButton_connect->setVisible(true);

ui->toolButton_rescan ->setVisible(true);
ui->comboBox_com ->setVisible(true);
ui->toolButton_connect ->setVisible(true);

ui->groupBox_status->setDisabled(true);
ui->groupBox_jog ->setDisabled(true);
Expand Down Expand Up @@ -322,17 +360,13 @@ void GrblCommander::parseLine(const QString &line)
if (line.left(1) == "$") return parseSetting (line);
if (line.left(1) == "[") return parseMessage(line);

// output all the rest to log ...
if (line.left(5) == "Grbl " && line.contains("['$' for help]")) {
// search for welcome message
if (line.startsWith("Grbl") && line.contains("['$' for help]")) {
// grbl welcome message => reqeust some more information
m_grblFound = true;
sendRealtime('?'); // request first status
appendCommand("$I (no-logging)"); // request build info
appendCommand("$$ (no-logging)"); // request settings
appendCommand("$G (no-logging)"); // request gcode parser state
appendCommand("$# (no-logging)"); // request gcode parameters
}

// output all the rest to log ...
emit receivedMessage(line);
}

Expand Down
2 changes: 2 additions & 0 deletions source/GrblCommander.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ private slots:

// handle stuff at regular timer (every 250ms)
void handleTimer();
void handleFirstConnect();
void requestFirstStatus();

// parse received stuff
void parseLine(const QString &line);
Expand Down

0 comments on commit e831826

Please sign in to comment.