No error, but no reading from serial port
-
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.hThere are no errors and the terminal seems to send something out but it doesn't receive anything, is there something im missing?
-
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.hThere are no errors and the terminal seems to send something out but it doesn't receive anything, is there something im missing?
@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 localQSerialPortand opens and closes it. I don't know about the rest of your code, but you are supposed to have a single instance ofQSerialPortpermanently open for both writes & reads.