PDA

View Full Version : Problem with QSerialPort



Eduardo Huerta
23rd August 2019, 03:19
My problem is the following, I have two slot functions connected to a QPushButton that make the connection and disconnect to an arduino . And in the connection function I show a message on a display that arduino is already connected to the PC and in the other function show a message on the display when I disconnect. Then I show the functions A and B, I have reviewed and the problem I have is that the signal does not send the arduino, in the case of the connection function if I do a slot function where it has only arduino -> write(code), the message is received by the arduino. And in the case of disconnection if I remove the function close () I get the message displayed on the display.
Why is the message not sent to the arduino when I send it through the slot functions A and B?

How can I correct these functions?
Thanks for help me.

A)


void Widget::connectToArduino()
{
if(arduino_is_available){
// open and configure the serialport
arduino->setPortName(selectedPort);
arduino->open(QSerialPort::WriteOnly);
arduino->setBaudRate(QSerialPort::Baud9600);
arduino->setDataBits(QSerialPort::Data8);
arduino->setParity(QSerialPort::NoParity);
arduino->setStopBits(QSerialPort::OneStop);
arduino->setFlowControl(QSerialPort::NoFlowControl);

arduino->write("#STAR\n");//Code to show message of connection to the pc.

connectButton->setText("Disconnect");
enableControls();
}else{
// give error message if not available
QMessageBox::warning(this, "Port error", "Couldn't find the Arduino!");
}
}

B)


void Widget::disconnectFromArduino()
{
arduino_is_available = false;
arduino_port_name="";
selectedPort="";
resetDefaults();
if(arduino->isWritable())
{
qDebug() << "Is writable";
arduino->write("#STOP\n");
}
closePort();
this->repaint();
}
void Widget::closePort()
{
portList.clear();
PortComboBox->clear();
//delete arduino;
arduino->close();
}

anda_skoa
24th August 2019, 08:17
My problem is the following, I have two slot functions connected to a QPushButton

You are aware that both slots will be called for each click, right?



Why is the message not sent to the arduino when I send it through the slot functions A and B?

Maybe closing the port aborts sending any bytes that have not been sent yet.
I.e. if write() is not synchronous the stop message might still be on the PC side and close might simply discard it.

Cheers,
_