Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved how send a character through the Serial Port

    General and Desktop
    5
    9
    2615
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Alan_641
      Alan_641 last edited by

      Hi! I want send a character , for example the letter "a", when i press a button in the GUI through the Serial Port but i don't know how configure the Serial Port, i watch the examples but i don´t understand it for implement in this simple case.
      Someone could give me an example for send the letter "a" to the COM3 when i press a button?
      Thanks

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        Do you mean have slot that does:

        void MyWidget::onButtonPressed()
        {
            _mySerialPort->write("a");
        }
        

        ?

        Do you know the parameters you should set on your serial port ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        Alan_641 1 Reply Last reply Reply Quote 0
        • Alan_641
          Alan_641 @SGaist last edited by

          @SGaist Yes, but i don't know how to configure the serial port: the bauds, the name and the others parameters that it need neither what parameter it need

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @Alan_641 last edited by mrjj

            @Alan_641
            hi
            http://doc.qt.io/qt-5/qtserialport-terminal-example.html
            is a good start example. It has comport config dialog and reading the code
            will show how it sets the parameters.

            serial->setPortName(p.name);
            serial->setBaudRate(p.baudRate);
            serial->setDataBits(p.dataBits);
            serial->setParity(p.parity);
            serial->setStopBits(p.stopBits);
            serial->setFlowControl(p.flowControl);

            You can open and run it directly in Creator.

            kshegunov 1 Reply Last reply Reply Quote 0
            • kshegunov
              kshegunov Moderators @mrjj last edited by kshegunov

              @mrjj @Alan_641
              The values of the parameters you pass to the serial port ultimately depend on the device itself. Serial communication is pretty simplistic (physically) and leaving the technicalities aside, to be able to use the serial port you need to match the baud rate, data bits, stop bits, parity and flow control between the devices (i.e. your computer and whatever you've connected to the serial port).

              Read and abide by the Qt Code of Conduct

              Alan_641 1 Reply Last reply Reply Quote 0
              • Alan_641
                Alan_641 @kshegunov last edited by Alan_641

                @kshegunov
                I do it but now when i try to compiler it show an error: that the main.obj was not found.
                Is not any file .obj in any project that I had seen before.
                I added the code qt += serialport in the .pro file and the QSerialClass header in main.cpp
                How can i fix it?

                1 Reply Last reply Reply Quote 0
                • jsulm
                  jsulm Lifetime Qt Champion last edited by

                  Can you post the whole compiler log?
                  You probably have some compiler errors, so main.obj is not created.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Alan_641 1 Reply Last reply Reply Quote 0
                  • Alan_641
                    Alan_641 @jsulm last edited by

                    @jsulm
                    this is the main.cpp

                    #include "mainwindow.h"
                    #include <QApplication>
                    
                    #include <QTextStream>
                    #include <QCoreApplication>
                    #include <QFile>
                    #include <QtCore>
                    #include <QStringList>
                    #include <QIODevice>
                    #include <iostream>
                    #include <QtSerialPort/QSerialPort>
                    #include <QDebug>
                    #include <Windows.h>
                    #include <QElapsedTimer>
                    
                    QT_USE_NAMESPACE
                    QSerialPort serial;
                    
                    int main(int argc, char *argv[])
                    {
                        QElapsedTimer timer;
                    
                        QApplication a(argc, argv);
                    
                        QByteArray output;
                        serial.setPortName ("COM3");
                        serial.open (QIODevice::ReadOnly);
                        serial.setBaudRate (QSerialPort::Baud9600);
                        serial.setDataBits (QSerialPort::Data8);
                        serial.setParity(QSerialPort::NoParity);
                        serial.setStopBits (QSerialPort::OneStop);
                        serial.setFlowControl (QSerialPort::NoFlowControl);
                    
                        while (!serial.isOpen()) serial.open(QIODevice::ReadOnly);
                        while (true) {
                            output = "a";
                            serial.write(output);
                            serial.flush();
                            timer.start();
                            serial.waitForBytesWritten(100);
                        }
                    
                    
                        MainWindow w;
                        w.show();
                    
                        return a.exec();
                    }
                    //your code here
                    

                    and this is the error that show when I try to compile it:

                    main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QSerialPort::QSerialPort(class QObject *)" (_imp??0QSerialPort@@QEAA@PEAVQObject@@@Z) referenced in function "void __cdecl `dynamic initializer for 'serial''(void)" (??__Eserial@@YAXXZ)

                    1 Reply Last reply Reply Quote 0
                    • jsulm
                      jsulm Lifetime Qt Champion last edited by

                      Do you have this line in your pro file:

                      QT += serialport
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post