Reputation: 2135
How I can write data in serial port, with delay between send's messages? This is my code:
void MainWindow::on_pushButton_Done_clicked()
{
if(sport->isOpen()){
sport->clear();
QString cmd = Phase+Mode;
//Write Stop
sport->write("stop!", 5);
//Write Mode
sport->write(cmd.toStdString().c_str(), cmd.toStdString().length());
//Write Speed
sport->write(Speed.toStdString().c_str(), Speed.toStdString().length());
//Write Direction
sport->write(Direction.toStdString().c_str(), Direction.toStdString().length());
//Run
sport->write("start!", 6);
}
}
My device receives an error message when I call this function.
Thank you.
Upvotes: 1
Views: 3815
Reputation: 37697
Looks like you are trying to program some step motor controller or something similar. Usually in such controllers you should wait for controller response to verify that command was processed properly.
It looks like that your design of code is very bad. Move everything related with this controller to separate class, which has set of slots, something like: starRotateLeftWithSpeed(double)
. Code will be cleaner and it will be easy to use thread if you decide to use methods like waitForBytesWritten
proposed in another answer.
Definitely you should read controller manual more carefully.
Upvotes: 0
Reputation: 48196
2 options:
use waitForBytesWritten
to ensure the bytes are written and then a short sleep
however this will block the thread and will block the gui
the other is using a QTimer to trigger another slot a few times and a field that will indicate what needs to be sent
Upvotes: 1