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


  • Lifetime Qt Champion

    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 ?



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


  • Qt Champions 2016

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


  • Qt Champions 2016

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


  • Moderators

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


  • Moderators

    Do you have this line in your pro file:

    QT += serialport
    

Log in to reply
 

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