Important: Please read the Qt Code of Conduct -

how send a character through the Serial Port

  • 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?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you mean have slot that does:

    void MyWidget::onButtonPressed()


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

  • @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

  • Lifetime Qt Champion

    is a good start example. It has comport config dialog and reading the code
    will show how it sets the parameters.


    You can open and run it directly in Creator.

  • Moderators

    @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).

  • @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?

  • Lifetime Qt Champion

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

  • @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>
    QSerialPort serial;
    int main(int argc, char *argv[])
        QElapsedTimer timer;
        QApplication a(argc, argv);
        QByteArray output;
        serial.setPortName ("COM3"); (QIODevice::ReadOnly);
        serial.setBaudRate (QSerialPort::Baud9600);
        serial.setDataBits (QSerialPort::Data8);
        serial.setStopBits (QSerialPort::OneStop);
        serial.setFlowControl (QSerialPort::NoFlowControl);
        while (!serial.isOpen());
        while (true) {
            output = "a";
        MainWindow w;;
        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)

  • Lifetime Qt Champion

    Do you have this line in your pro file:

    QT += serialport

Log in to reply