Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how send a character through the Serial Port
QtWS25 Last Chance

how send a character through the Serial Port

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 3.3k Views
  • 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_641A Offline
    Alan_641A Offline
    Alan_641
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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_641A 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        Alan_641A Offline
        Alan_641A Offline
        Alan_641
        wrote on last edited by
        #3

        @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

        mrjjM 1 Reply Last reply
        0
        • Alan_641A Alan_641

          @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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

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

          kshegunovK 1 Reply Last reply
          0
          • mrjjM 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.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @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_641A 1 Reply Last reply
            0
            • kshegunovK 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).

              Alan_641A Offline
              Alan_641A Offline
              Alan_641
              wrote on last edited by Alan_641
              #6

              @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
              0
              • jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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_641A 1 Reply Last reply
                0
                • jsulmJ jsulm

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

                  Alan_641A Offline
                  Alan_641A Offline
                  Alan_641
                  wrote on last edited by
                  #8

                  @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
                  0
                  • jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved