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. No error, but no reading from serial port
Qt 6.11 is out! See what's new in the release blog

No error, but no reading from serial port

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 212 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.
  • A Offline
    A Offline
    agmar
    wrote on last edited by
    #1

    Hi, i was trying to get a read from the serial monitor and this is my code - #include "mainwindow.h"
    #include "./ui_mainwindow.h"

    #include <QApplication>
    #include <QLabel>
    #include <QScrollArea>
    #include <QVBoxLayout>
    #include <QWidget>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QLineEdit>
    #include <QIODevice>

    #include <iostream>

    QString portName;
    QString message;
    QString messagein;
    QString test;
    QByteArray lol;

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),
    m_serial(new QSerialPort (this))
    {

    ui->setupUi(this);
    
    // Connect the "Send" button to the sendMessage() slot
      connect(ui->sendbutton, &QPushButton::clicked, this, &MainWindow::sendMessage);
      connect(ui->lineEdit, &QLineEdit::textChanged, this, &MainWindow::inputGet);
      connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
    
    
    auto infos = QSerialPortInfo::availablePorts();
    if (!infos.empty()) {
        for (const auto &info : infos) {
            std::cout << info.portName().toStdString() << std::endl;
        }
    } else {
        std::cout << "No USB device found" << std::endl;
    
    }
    

    // auto infos = QSerialPortInfo::availablePorts();
    if (!infos.empty()) {
    portName = infos.first().portName();
    } else {
    portName = "No USB device found";
    }

    ui->current_port->setTextFormat(Qt::PlainText);
    ui->current_port->setText(portName);
    

    }

    MainWindow::~MainWindow()
    {

    delete label;
    delete ui;
    

    }

    void MainWindow::readData()
    {
    const QByteArray data = m_serial->readAll();
    lol = data;
    ui->labTest2->setText(test);
    QString resultat = "data received";

    std::cout << resultat.toStdString() << std::endl;
    

    }

    void MainWindow::inputGet()
    {
    test = ui->lineEdit->text();
    ui->labTest->setText(test);
    QString resultato = "linedit";

    std::cout << resultato.toStdString() << std::endl;

    }

    void MainWindow::on_horizontalSlider_sliderReleased()
    {

    }

    void MainWindow::sendMessage()
    {
    QSerialPort serial;
    serial.setPortName(portName); // replace with the name of the serial port
    serial.setBaudRate(QSerialPort::Baud19200);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    if (serial.open(QIODevice::ReadWrite)) {
    QString message = ui->messageEdit->text();
    QByteArray messageData = message.toUtf8();
    serial.write(message.toUtf8());
    serial.waitForBytesWritten(-1);
    serial.close();

    }

    serial.setPortName(portName); // replace with the name of the serial port
    serial.setBaudRate(QSerialPort::Baud19200);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    if (serial.open(QIODevice::ReadWrite)) {

    const QByteArray data = serial.readAll();
    lol = data;
    ui->labTest2->setText(lol);
    QString resultat = "data received";

    std::cout << resultat.toStdString() << std::endl;

    }

    }
    for mainwindow.pp
    #include "mainwindow.h"
    #include "./ui_mainwindow.h"

    #include <QApplication>
    #include <QLabel>
    #include <QScrollArea>
    #include <QVBoxLayout>
    #include <QWidget>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QLineEdit>
    #include <QIODevice>

    #include <iostream>

    QString portName;
    QString message;
    QString messagein;
    QString test;
    QByteArray lol;

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),
    m_serial(new QSerialPort (this))
    {

    ui->setupUi(this);
    
    // Connect the "Send" button to the sendMessage() slot
      connect(ui->sendbutton, &QPushButton::clicked, this, &MainWindow::sendMessage);
      connect(ui->lineEdit, &QLineEdit::textChanged, this, &MainWindow::inputGet);
      connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
    
    
    auto infos = QSerialPortInfo::availablePorts();
    if (!infos.empty()) {
        for (const auto &info : infos) {
            std::cout << info.portName().toStdString() << std::endl;
        }
    } else {
        std::cout << "No USB device found" << std::endl;
    
    }
    

    // auto infos = QSerialPortInfo::availablePorts();
    if (!infos.empty()) {
    portName = infos.first().portName();
    } else {
    portName = "No USB device found";
    }

    ui->current_port->setTextFormat(Qt::PlainText);
    ui->current_port->setText(portName);
    

    }

    MainWindow::~MainWindow()
    {

    delete label;
    delete ui;
    

    }

    void MainWindow::readData()
    {
    const QByteArray data = m_serial->readAll();
    lol = data;
    ui->labTest2->setText(test);
    QString resultat = "data received";

    std::cout << resultat.toStdString() << std::endl;
    

    }

    void MainWindow::inputGet()
    {
    test = ui->lineEdit->text();
    ui->labTest->setText(test);
    QString resultato = "linedit";

    std::cout << resultato.toStdString() << std::endl;

    }

    void MainWindow::on_horizontalSlider_sliderReleased()
    {

    }

    void MainWindow::sendMessage()
    {
    QSerialPort serial;
    serial.setPortName(portName); // replace with the name of the serial port
    serial.setBaudRate(QSerialPort::Baud19200);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    if (serial.open(QIODevice::ReadWrite)) {
    QString message = ui->messageEdit->text();
    QByteArray messageData = message.toUtf8();
    serial.write(message.toUtf8());
    serial.waitForBytesWritten(-1);
    serial.close();

    }

    serial.setPortName(portName); // replace with the name of the serial port
    serial.setBaudRate(QSerialPort::Baud19200);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    if (serial.open(QIODevice::ReadWrite)) {

    const QByteArray data = serial.readAll();
    lol = data;
    ui->labTest2->setText(lol);
    QString resultat = "data received";

    std::cout << resultat.toStdString() << std::endl;

    }

    }
    for mainwindow.h

    There are no errors and the terminal seems to send something out but it doesn't receive anything, is there something im missing?

    JonBJ 1 Reply Last reply
    0
    • A agmar

      Hi, i was trying to get a read from the serial monitor and this is my code - #include "mainwindow.h"
      #include "./ui_mainwindow.h"

      #include <QApplication>
      #include <QLabel>
      #include <QScrollArea>
      #include <QVBoxLayout>
      #include <QWidget>
      #include <QtSerialPort/QSerialPortInfo>
      #include <QLineEdit>
      #include <QIODevice>

      #include <iostream>

      QString portName;
      QString message;
      QString messagein;
      QString test;
      QByteArray lol;

      MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow),
      m_serial(new QSerialPort (this))
      {

      ui->setupUi(this);
      
      // Connect the "Send" button to the sendMessage() slot
        connect(ui->sendbutton, &QPushButton::clicked, this, &MainWindow::sendMessage);
        connect(ui->lineEdit, &QLineEdit::textChanged, this, &MainWindow::inputGet);
        connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
      
      
      auto infos = QSerialPortInfo::availablePorts();
      if (!infos.empty()) {
          for (const auto &info : infos) {
              std::cout << info.portName().toStdString() << std::endl;
          }
      } else {
          std::cout << "No USB device found" << std::endl;
      
      }
      

      // auto infos = QSerialPortInfo::availablePorts();
      if (!infos.empty()) {
      portName = infos.first().portName();
      } else {
      portName = "No USB device found";
      }

      ui->current_port->setTextFormat(Qt::PlainText);
      ui->current_port->setText(portName);
      

      }

      MainWindow::~MainWindow()
      {

      delete label;
      delete ui;
      

      }

      void MainWindow::readData()
      {
      const QByteArray data = m_serial->readAll();
      lol = data;
      ui->labTest2->setText(test);
      QString resultat = "data received";

      std::cout << resultat.toStdString() << std::endl;
      

      }

      void MainWindow::inputGet()
      {
      test = ui->lineEdit->text();
      ui->labTest->setText(test);
      QString resultato = "linedit";

      std::cout << resultato.toStdString() << std::endl;

      }

      void MainWindow::on_horizontalSlider_sliderReleased()
      {

      }

      void MainWindow::sendMessage()
      {
      QSerialPort serial;
      serial.setPortName(portName); // replace with the name of the serial port
      serial.setBaudRate(QSerialPort::Baud19200);
      serial.setDataBits(QSerialPort::Data8);
      serial.setParity(QSerialPort::NoParity);
      serial.setStopBits(QSerialPort::OneStop);
      serial.setFlowControl(QSerialPort::NoFlowControl);
      if (serial.open(QIODevice::ReadWrite)) {
      QString message = ui->messageEdit->text();
      QByteArray messageData = message.toUtf8();
      serial.write(message.toUtf8());
      serial.waitForBytesWritten(-1);
      serial.close();

      }

      serial.setPortName(portName); // replace with the name of the serial port
      serial.setBaudRate(QSerialPort::Baud19200);
      serial.setDataBits(QSerialPort::Data8);
      serial.setParity(QSerialPort::NoParity);
      serial.setStopBits(QSerialPort::OneStop);
      serial.setFlowControl(QSerialPort::NoFlowControl);
      if (serial.open(QIODevice::ReadWrite)) {

      const QByteArray data = serial.readAll();
      lol = data;
      ui->labTest2->setText(lol);
      QString resultat = "data received";

      std::cout << resultat.toStdString() << std::endl;

      }

      }
      for mainwindow.pp
      #include "mainwindow.h"
      #include "./ui_mainwindow.h"

      #include <QApplication>
      #include <QLabel>
      #include <QScrollArea>
      #include <QVBoxLayout>
      #include <QWidget>
      #include <QtSerialPort/QSerialPortInfo>
      #include <QLineEdit>
      #include <QIODevice>

      #include <iostream>

      QString portName;
      QString message;
      QString messagein;
      QString test;
      QByteArray lol;

      MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow),
      m_serial(new QSerialPort (this))
      {

      ui->setupUi(this);
      
      // Connect the "Send" button to the sendMessage() slot
        connect(ui->sendbutton, &QPushButton::clicked, this, &MainWindow::sendMessage);
        connect(ui->lineEdit, &QLineEdit::textChanged, this, &MainWindow::inputGet);
        connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
      
      
      auto infos = QSerialPortInfo::availablePorts();
      if (!infos.empty()) {
          for (const auto &info : infos) {
              std::cout << info.portName().toStdString() << std::endl;
          }
      } else {
          std::cout << "No USB device found" << std::endl;
      
      }
      

      // auto infos = QSerialPortInfo::availablePorts();
      if (!infos.empty()) {
      portName = infos.first().portName();
      } else {
      portName = "No USB device found";
      }

      ui->current_port->setTextFormat(Qt::PlainText);
      ui->current_port->setText(portName);
      

      }

      MainWindow::~MainWindow()
      {

      delete label;
      delete ui;
      

      }

      void MainWindow::readData()
      {
      const QByteArray data = m_serial->readAll();
      lol = data;
      ui->labTest2->setText(test);
      QString resultat = "data received";

      std::cout << resultat.toStdString() << std::endl;
      

      }

      void MainWindow::inputGet()
      {
      test = ui->lineEdit->text();
      ui->labTest->setText(test);
      QString resultato = "linedit";

      std::cout << resultato.toStdString() << std::endl;

      }

      void MainWindow::on_horizontalSlider_sliderReleased()
      {

      }

      void MainWindow::sendMessage()
      {
      QSerialPort serial;
      serial.setPortName(portName); // replace with the name of the serial port
      serial.setBaudRate(QSerialPort::Baud19200);
      serial.setDataBits(QSerialPort::Data8);
      serial.setParity(QSerialPort::NoParity);
      serial.setStopBits(QSerialPort::OneStop);
      serial.setFlowControl(QSerialPort::NoFlowControl);
      if (serial.open(QIODevice::ReadWrite)) {
      QString message = ui->messageEdit->text();
      QByteArray messageData = message.toUtf8();
      serial.write(message.toUtf8());
      serial.waitForBytesWritten(-1);
      serial.close();

      }

      serial.setPortName(portName); // replace with the name of the serial port
      serial.setBaudRate(QSerialPort::Baud19200);
      serial.setDataBits(QSerialPort::Data8);
      serial.setParity(QSerialPort::NoParity);
      serial.setStopBits(QSerialPort::OneStop);
      serial.setFlowControl(QSerialPort::NoFlowControl);
      if (serial.open(QIODevice::ReadWrite)) {

      const QByteArray data = serial.readAll();
      lol = data;
      ui->labTest2->setText(lol);
      QString resultat = "data received";

      std::cout << resultat.toStdString() << std::endl;

      }

      }
      for mainwindow.h

      There are no errors and the terminal seems to send something out but it doesn't receive anything, is there something im missing?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @agmar
      It would be better if you pasted some minimal example of your problematic code. But if you are going to post code as big as this please use the forum's Code tags around code blocks.

      Meanwhile, your MainWindow::sendMessage() at least has a local QSerialPort and opens and closes it. I don't know about the rest of your code, but you are supposed to have a single instance of QSerialPort permanently open for both writes & reads.

      1 Reply Last reply
      4

      • Login

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