Qt serial communication using UART



  • This post is deleted!

  • Moderators

    You set up the port config in the constructor, but then you don't use it at all, in your button click slot you create a new QSerialPort object (without deleting the one from constructor - that's a memory leak).

    I suggest you remove the port = new QSerialPort() from your button click, and use the single object you create in the constructor. In response to button clicks it should be enough to call port->open() and port->close().

    Also, you don't need the serial variable at all.



  • @segtteee said in Qt serial communication using UART:

    additionaly to what @sierdzio said,
    change your constructor from

    QSerialPort*port=new QSerialPort();

    to

    port = new QSerialPort();

    otherwise you config a new Variable instead of your member variable.



  • @J.Hilk

    Thank you for your reply. I delete "port=new QSerialPort();" and fix from "QSerialPort*port=new QSerialPort();" to "port = new QSerialPort();"

    , insert "port->open(QIODevice::ReadWrite);"

    but how can i remote device when i enter the command at line_Edit . ( like echo ____ > /dev/ttyUSB0 in linux terminal )

    i want to remote device using qt ui line Edit and button .

    please let me know TT


  • Moderators

    QSerialPort is a QIODevice. You can interact with it the same way you interact with QFile, for example. So:

    QByteArray data = port.read();
    port.write("sth");
    
    // Or using text stream
    QTextStream stream(port);
    stream << "some data";
    


  • @segtteee I'm not entirely what you want to do, but I'll take quick look at my crystal ball and write something down:

    To send something via your SieralPort you can do something like this:

    void onSendClicked(){
        QString s("String to Send via Serial");
        QByteArray dataToSend; 
        dataToSend.append(s);
    
        if(->isOpen())
            serialPort->write(dataToSend);
    }
    


  • @sierdzio

    Thank you for your reply. but i can't understand that you say.

    how can i apply "QByteArray data = port.read();" , "port.write("sth");" in my code?

    i think // void MainWindow::lineEdit_command (){ // is that right?
    QByteArray data = port.read();
    port.write("sth");


  • Moderators

    @segtteee said in Qt serial communication using UART:

    Thank you for your reply. but i can't understand that you say.

    Please take a look at some Qt examples to better understand how it works. My code snippets are only an example how to use the QSerialPort, they have little to do with your actual use case.

    So, if I get it right, you want to take the command which is written (by user) into a QLineEdit, and (when user clicks a button) send it via the serial port?

    Then:

    • connect the clicked() signal of the button to a slot you create in your main window class. In your case, that could be void on_pushButton_connect_clicked(); slot, or something else if you want to add another button
    • in the slot, open the port
    • in the slot, write line edit data into your port
    • if needed, add some sanity checking/ validation to the input data. Otherwise users will surely put in some garbage there

    So, in essence, you need something like:

    void MainWindow::on_someButton_clicked() {
      port->open(QSerialPort::ReadWrite);
      port->write(ui->yourLineEditName->text().toLatin1());
    }
    


  • @J.Hilk
    Thank you for your reply.

    you mean if i want use several command , i have to enter code that you tell me one by one?

    otherwise " QString s("String to Send via Serial"); " can understand any commands ? "String to Send via Serial" what mean?



  • Please read any C++ book at first.



  • @sierdzio

    Thaky you for your reply.

    i add
    //void MainWindow::on_pushButton_send_clicked()
    {
    port->open(QSerialPort::ReadWrite);
    port->write(ui->lineEdit_command->text().toLatin1());

    }// in my code and enter the command at lineEdit , but device can't conduct command..

    Is there something else i have to anything for device's behavior?


  • Moderators

    @segtteee said in Qt serial communication using UART:

    in my code and enter the command at lineEdit , but device can't conduct command..

    Well, I can't guess what error codes are you getting, right? I don't even know if you connected to the device correctly. Did it get the command? Did it reply with anything? Have you connected the readyRead() to some slot, or used waitForReadyRead() to wait for the reply?

    I don't give you whole solution for your problem, I'm not here to write the code for you. I'm happy to help you overcome problems and learn.



  • @sierdzio

    okay thank you for your reply .

    i have question , how to check connected device with qt ? only i enter the command and device conduct command?

    when i debuging the code there is no error message and no problem i think . hot to check in qt ?


  • Qt Champions 2017

    @segtteee: I can only recommend you to have a look at the examples (there are a lot of them):

    http://doc.qt.io/qt-5/qtserialport-examples.html

    Take, e.g. the Terminal example http://doc.qt.io/qt-5/qtserialport-terminal-example.html and try to understand how it works. It should be very near to what you want to achive.

    PS: This example also shows how to check for some errors. Depending on the communication protocol, you may have to add more error checks.



  • @aha_1980

    I also saw an example when I wrote this project.

    But they did not come out in detail and I could not understand.

    so i search everyday and raise a question here ....


  • Moderators

    @segtteee said in Qt serial communication using UART:

    i have question , how to check connected device with qt ? only i enter the command and device conduct command?

    First step is to check if port->open() returned true.
    When calling port->write() you can check if the amount of bytes written is the same you sent. If not, something went wrong. Then you can check QSerialPort::error and see what was the problem.

    Another clue might be whether readyRead() is emitted in response to your command. If there is no reply, then again - something might be wrong (or not - that depends on what the command is and which device you connect to).



  • @sierdzio

    Thank you for your answer during long time.

    i try but i can't solve this situation. but i learn many thing thank to your reply.

    i'm really thank you .


  • Qt Champions 2017

    Hi
    For a pretty good example, please see
    http://doc.qt.io/qt-5/qtserialport-terminal-example.html
    It has dialog to select comport properties and allows to send text out of the box.
    Its also pretty good explained.~


  • Qt Champions 2017

    @mrjj Thats what I wrote three days ago...


  • Qt Champions 2017

    @aha_1980
    Oh yes. sorry. i did miss. Clicked from locked thread. Should have read it all :)


  • Qt Champions 2017

    @mrjj said in Qt serial communication using UART:

    @aha_1980
    Oh yes. sorry. i did miss. Clicked from locked thread. Should have read it all :)

    No problem :) (And no need to delete your post)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.