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 to display data on console incoming data from the serial port
Forum Updated to NodeBB v4.3 + New Features

How to display data on console incoming data from the serial port

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.0k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    hmdi
    wrote on last edited by
    #1

    hi,

    my aim write data and read data in serial port on console but I can not display to data on console. How to display data?

    my code:

    @#include <QTextStream>
    #include <QCoreApplication>
    #include <QStringList>
    #include <QtSerialPort/QSerialPort>
    #include <iostream>
    #include <QDebug>
    #include <QByteArray>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QSerialPort serialPort;
    QTextStream standardOutput(stdout);
    
    QString serialPortName = "COM1";
    serialPort.setPortName(serialPortName);
    
    int serialPortBaudRate =  QSerialPort::Baud9600;
    
    if(serialPort.setBaudRate(serialPortBaudRate))
     {
         std::cout<<"asd\n\n"<<serialPortBaudRate<<"\n\n";
     }
    if (!serialPort.open(QIODevice::ReadWrite)) {
        std::cout<<"Failed to open port";
        return 1;
    }
    
    char data;
    
    std::cin>>data;
    
    serialPort.write(&data);
    
    QByteArray readData = serialPort.readAll();
    readData.append(serialPort.readAll());
    
    qDebug()<<readData;
    
    standardOutput << readData<< endl;
    
    return a.exec&#40;&#41;;
    

    }@

    thanks

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You are doing everything in the main. So you may not be reading the data at all from SerialPort. You better work with Signal and Slots. There is readyRead() signal for serial port. You better put this signal and connect to slot. When ever the data comes it calls the slot. Inside the slot you can read the data and print the same. Also you can try with waitForReadyRead() method as well.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        Hi,
        The a.exec() command will start the application event loop, so anything before that (without the call to handle the eventhandler) will not work with signal/slot or events. In your case you dump some data on the seriel port with the write command and then immediate read some data from that port (which is probably empty, cause only usec before you created the port class). Thus your readData will always remain empty.
        Better to setup a MainWindow (or any other type of class) that starts a seriel port and setup a slot for the readyread signal. Then when data is received your slot will be called and there is data in the read buffer.

        Greetz, Jeroen

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hmdi
          wrote on last edited by
          #4

          I don't know use signal and slot but I will learn in a shortest time and I will apply.

          thanks

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            To add to what has already been said, you have to first open QSerialPort and then setup the device

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - 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