PDA

View Full Version : SerialPort Connection Issue



akincio
19th February 2021, 11:48
Im working on a project that have to read and send CANBUS data through USB port. In first step im trying to connect my devices (one for send and one for receive) to Qt. I can list current COMs available but im not able to connect these ports. Im new in Qt and also programming. Im posting my mainwindow.cpp, main.cpp, mainwindow.h, untitled.pro files and debug output. What am i doing wrong? (Im working on Ubuntu on VMware). Qt version: 5.9.5 Ubuntu:18.04




-----mainwindow.cpp------

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox"
#include <QtSerialPort/QtSerialPort>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QComboBox>
#include <QList>
#include <QString>
#include <QTextEdit>
#include <QDebug>
#include <QTime>


QSerialPort serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QList<QSerialPortInfo> list;
list = QSerialPortInfo::availablePorts();

for(int i =0;i<list.length();i++)
{
ui->comboBox->addItem(list[i].portName());
}
}

MainWindow::~MainWindow()
{
delete ui;
serial.close();
}


void MainWindow::on_pushButton_clicked()
{

foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
//QMessageBox::information(this, "New Box", serialPortInfo.portName());
ui->comboBox->addItem(serialPortInfo.portName());
//serial.setPortName( ui->comboBox->currentText());

}
}

void MainWindow::on_pushButton_2_clicked()
{
serial : new QSerialPort(this);
//QMessageBox::information(this, "New Box", ui->comboBox->currentText());
// serial.setBaudRate(QSerialPort::UnknownBaud);
// serial.setDataBits(QSerialPort::Data8);
// serial.setParity(QSerialPort::NoParity);
// serial.setStopBits(QSerialPort::OneStop);
// serial.setFlowControl(QSerialPort::NoFlowControl);
// serial.open(QIODevice::ReadOnly);
// serial.open(QIODevice::ReadWrite);
// serial.readAll();
//connect(serial,SIGNAL::readyRead(),this,SLOT(seria lReceived()));

if(serial.open(QIODevice::ReadWrite)){
QMessageBox::information(this, "New Box", "Baglandi");
}

if (serial.open(QSerialPort::ReadOnly))
{
qDebug("SERIAL PORT - OPENED") ;
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
}

else
{
qDebug("SERIAL PORT - NOT OPENED") ;
qDebug() << "error code = " << serial.error();
qDebug() << "error string = " << serial.errorString();
}
}






-----main.cpp-----

#include "mainwindow.h"
#include <QtSerialPort/QtSerialPort>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QApplication>
#include <QComboBox>
#include <QList>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}







-----untitled.pro-----

QT += core gui
QT += serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = serial
TEMPLATE = app
CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp \
mainwindow.cpp

HEADERS += \
mainwindow.h

FORMS += \
mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target






-----debug report-----

SERIAL PORT - NOT OPENED
error code = QSerialPort::SerialPortError(DeviceNotFoundError)
error string = "No such file or directory"

d_stranz
19th February 2021, 16:00
serial : new QSerialPort(this);

I am not sure what you think this is supposed to do. It isn't assigning anything to the global variable "serial" you already defined.

It is actually defining a labeled statement (https://en.cppreference.com/w/cpp/language/goto) (named "serial"), followed by a line of code which creates a new QSerialPort instance that immediately gets turned into a zombie because the return value from the new() operator isn't saved anywhere.

Global variables are considered a bad thing in C++. You should define "serial" as a member variable of the MainWindow class.


if(serial.open(QIODevice::ReadWrite)){
QMessageBox::information(this, "New Box", "Baglandi");
}

if (serial.open(QSerialPort::ReadOnly))
{


Either you open it read / write or you open it read only. This code tries to do both. If opening for read / write succeeds, then the next clause will fail, because it tries to open an already open port using a different mode.

Have you looked at the Qt Serial Port examples (https://doc.qt.io/qt-5/qtserialport-examples.html)?