Results 1 to 2 of 2

Thread: Problem with QSerialPort

  1. #1
    Join Date
    Jun 2018
    Posts
    20
    Thanks
    4
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Problem with QSerialPort

    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)
    Qt Code:
    1. void Widget::connectToArduino()
    2. {
    3. if(arduino_is_available){
    4. // open and configure the serialport
    5. arduino->setPortName(selectedPort);
    6. arduino->open(QSerialPort::WriteOnly);
    7. arduino->setBaudRate(QSerialPort::Baud9600);
    8. arduino->setDataBits(QSerialPort::Data8);
    9. arduino->setParity(QSerialPort::NoParity);
    10. arduino->setStopBits(QSerialPort::OneStop);
    11. arduino->setFlowControl(QSerialPort::NoFlowControl);
    12.  
    13. arduino->write("#STAR\n");//Code to show message of connection to the pc.
    14.  
    15. connectButton->setText("Disconnect");
    16. enableControls();
    17. }else{
    18. // give error message if not available
    19. QMessageBox::warning(this, "Port error", "Couldn't find the Arduino!");
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    B)
    Qt Code:
    1. void Widget::disconnectFromArduino()
    2. {
    3. arduino_is_available = false;
    4. arduino_port_name="";
    5. selectedPort="";
    6. resetDefaults();
    7. if(arduino->isWritable())
    8. {
    9. qDebug() << "Is writable";
    10. arduino->write("#STOP\n");
    11. }
    12. closePort();
    13. this->repaint();
    14. }
    15. void Widget::closePort()
    16. {
    17. portList.clear();
    18. PortComboBox->clear();
    19. //delete arduino;
    20. arduino->close();
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 24th August 2019 at 08:14. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QSerialPort

    Quote Originally Posted by Eduardo Huerta View Post
    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?

    Quote Originally Posted by Eduardo Huerta View Post
    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,
    _

Similar Threads

  1. QSerialPort responces
    By ad5xj in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2017, 15:14
  2. QSerialPort and QThread
    By maratk1n in forum Qt Programming
    Replies: 12
    Last Post: 5th May 2017, 22:50
  3. QSerialPort
    By fmarques in forum Qt Programming
    Replies: 0
    Last Post: 20th April 2016, 16:04
  4. QserialPort
    By arturs in forum Newbie
    Replies: 0
    Last Post: 13th May 2015, 20:37
  5. QSerialport in multithread
    By snow_starzz in forum Newbie
    Replies: 3
    Last Post: 3rd December 2013, 10:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.