Results 1 to 2 of 2

Thread: SerialPort Connection Issue

  1. #1
    Join Date
    Feb 2021
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default SerialPort Connection Issue

    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

    Qt Code:
    1. -----mainwindow.cpp------
    2.  
    3. #include "mainwindow.h"
    4. #include "ui_mainwindow.h"
    5. #include "QMessageBox"
    6. #include <QtSerialPort/QtSerialPort>
    7. #include <QtSerialPort/QSerialPort>
    8. #include <QtSerialPort/QSerialPortInfo>
    9. #include <QComboBox>
    10. #include <QList>
    11. #include <QString>
    12. #include <QTextEdit>
    13. #include <QDebug>
    14. #include <QTime>
    15.  
    16.  
    17. QSerialPort serial;
    18. MainWindow::MainWindow(QWidget *parent)
    19. : QMainWindow(parent)
    20. , ui(new Ui::MainWindow)
    21. {
    22. ui->setupUi(this);
    23. QList<QSerialPortInfo> list;
    24. list = QSerialPortInfo::availablePorts();
    25.  
    26. for(int i =0;i<list.length();i++)
    27. {
    28. ui->comboBox->addItem(list[i].portName());
    29. }
    30. }
    31.  
    32. MainWindow::~MainWindow()
    33. {
    34. delete ui;
    35. serial.close();
    36. }
    37.  
    38.  
    39. void MainWindow::on_pushButton_clicked()
    40. {
    41.  
    42. foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
    43. {
    44. //QMessageBox::information(this, "New Box", serialPortInfo.portName());
    45. ui->comboBox->addItem(serialPortInfo.portName());
    46. //serial.setPortName( ui->comboBox->currentText());
    47.  
    48. }
    49. }
    50.  
    51. void MainWindow::on_pushButton_2_clicked()
    52. {
    53. serial : new QSerialPort(this);
    54. //QMessageBox::information(this, "New Box", ui->comboBox->currentText());
    55. // serial.setBaudRate(QSerialPort::UnknownBaud);
    56. // serial.setDataBits(QSerialPort::Data8);
    57. // serial.setParity(QSerialPort::NoParity);
    58. // serial.setStopBits(QSerialPort::OneStop);
    59. // serial.setFlowControl(QSerialPort::NoFlowControl);
    60. // serial.open(QIODevice::ReadOnly);
    61. // serial.open(QIODevice::ReadWrite);
    62. // serial.readAll();
    63. //connect(serial,SIGNAL::readyRead(),this,SLOT(serialReceived()));
    64.  
    65. if(serial.open(QIODevice::ReadWrite)){
    66. QMessageBox::information(this, "New Box", "Baglandi");
    67. }
    68.  
    69. if (serial.open(QSerialPort::ReadOnly))
    70. {
    71. qDebug("SERIAL PORT - OPENED") ;
    72. serial.setBaudRate(QSerialPort::Baud9600);
    73. serial.setDataBits(QSerialPort::Data8);
    74. serial.setParity(QSerialPort::NoParity);
    75. serial.setStopBits(QSerialPort::OneStop);
    76. serial.setFlowControl(QSerialPort::NoFlowControl);
    77. }
    78.  
    79. else
    80. {
    81. qDebug("SERIAL PORT - NOT OPENED") ;
    82. qDebug() << "error code = " << serial.error();
    83. qDebug() << "error string = " << serial.errorString();
    84. }
    85. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. -----main.cpp-----
    2.  
    3. #include "mainwindow.h"
    4. #include <QtSerialPort/QtSerialPort>
    5. #include <QtSerialPort/QSerialPort>
    6. #include <QtSerialPort/QSerialPortInfo>
    7. #include <QApplication>
    8. #include <QComboBox>
    9. #include <QList>
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QApplication a(argc, argv);
    14. MainWindow w;
    15. w.show();
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. -----untitled.pro-----
    2.  
    3. QT += core gui
    4. QT += serialport
    5. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    6.  
    7. TARGET = serial
    8. TEMPLATE = app
    9. CONFIG += c++11
    10.  
    11. # You can make your code fail to compile if it uses deprecated APIs.
    12. # In order to do so, uncomment the following line.
    13. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    14.  
    15. SOURCES += \
    16. main.cpp \
    17. mainwindow.cpp
    18.  
    19. HEADERS += \
    20. mainwindow.h
    21.  
    22. FORMS += \
    23. mainwindow.ui
    24.  
    25. # Default rules for deployment.
    26. qnx: target.path = /tmp/$${TARGET}/bin
    27. else: unix:!android: target.path = /opt/$${TARGET}/bin
    28. !isEmpty(target.path): INSTALLS += target
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. -----debug report-----
    2.  
    3. SERIAL PORT - NOT OPENED
    4. error code = QSerialPort::SerialPortError(DeviceNotFoundError)
    5. error string = "No such file or directory"
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,972
    Thanks
    277
    Thanked 808 Times in 797 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: SerialPort Connection Issue

    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 (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?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    akincio (22nd February 2021)

Similar Threads

  1. Use newest Qt5 serialport library in Qt4?
    By nyaruko in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2014, 00:03
  2. QtSerialport Inconsistant Connection Issue
    By awpitt13 in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2013, 07:14
  3. Communication via SerialPort
    By porterneon in forum Newbie
    Replies: 1
    Last Post: 2nd August 2011, 23:34
  4. Replies: 1
    Last Post: 2nd April 2010, 06:42
  5. qt4 serialport
    By nmn in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2006, 03:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.