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
) , 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(serialReceived()));
}
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();
}
}
-----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(serialReceived()));
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();
}
}
To copy to clipboard, switch view to plain text mode
-----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[])
{
MainWindow w;
w.show();
return a.exec();
}
-----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();
}
To copy to clipboard, switch view to plain text mode
-----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
-----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
To copy to clipboard, switch view to plain text mode
-----debug report-----
SERIAL PORT - NOT OPENED
error code = QSerialPort::SerialPortError(DeviceNotFoundError)
error string = "No such file or directory"
-----debug report-----
SERIAL PORT - NOT OPENED
error code = QSerialPort::SerialPortError(DeviceNotFoundError)
error string = "No such file or directory"
To copy to clipboard, switch view to plain text mode
Bookmarks