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. How to connection serialport outside 'mainwindow.cpp' ?

How to connection serialport outside 'mainwindow.cpp' ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 232 Views 1 Watching
  • 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.
  • B Offline
    B Offline
    bknhatice
    wrote on last edited by
    #1

    Hi,

    I created new MainWindow project. I don't want to do the serialport connection via mainwindow.cpp. But when I want to make a serialport connection over a different class, the project crashes.

    Example:
    cameramodel.h
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    class CameraModel : public QMainWindow
    {
    Q_OBJECT

    public:
    QSerialPort *serialCamera;
    bool isCameraSerial = false;

    }

    cameramodel.cpp:
    #include "cameramodel.h"
    #include "ui_cameramodel.h"

    CameraModel::CameraModel(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CameraModel)
    {
    ui->setupUi(this);
    serialCamera = new QSerialPort(ui->cb_spCportName->currentText());
    serialCamera->setBaudRate(9600);
    serialCamera->setDataBits(QSerialPort::Data8);
    serialCamera->setParity(QSerialPort::NoParity);
    serialCamera->setStopBits(QSerialPort::OneStop);
    serialCamera->setFlowControl(QSerialPort::NoFlowControl);
    if (serialCamera->open(QIODevice::ReadWrite))
    {
    QMessageBox::information(this, tr("Connected"), "Serial Port Opened Successfully");
    }
    else{
    QMessageBox::critical(this, tr("Error"), serialCamera->errorString());
    }
    }
    Project crashed. How to solve ?

    Thanks,

    Pablo J. RoginaP 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @bknhatice said in How to connection serialport outside 'mainwindow.cpp' ?:

      Project crashed

      Use a debugger and see where it crashes, fix the crash.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • B bknhatice

        Hi,

        I created new MainWindow project. I don't want to do the serialport connection via mainwindow.cpp. But when I want to make a serialport connection over a different class, the project crashes.

        Example:
        cameramodel.h
        #include <QtSerialPort/QSerialPort>
        #include <QtSerialPort/QSerialPortInfo>
        class CameraModel : public QMainWindow
        {
        Q_OBJECT

        public:
        QSerialPort *serialCamera;
        bool isCameraSerial = false;

        }

        cameramodel.cpp:
        #include "cameramodel.h"
        #include "ui_cameramodel.h"

        CameraModel::CameraModel(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::CameraModel)
        {
        ui->setupUi(this);
        serialCamera = new QSerialPort(ui->cb_spCportName->currentText());
        serialCamera->setBaudRate(9600);
        serialCamera->setDataBits(QSerialPort::Data8);
        serialCamera->setParity(QSerialPort::NoParity);
        serialCamera->setStopBits(QSerialPort::OneStop);
        serialCamera->setFlowControl(QSerialPort::NoFlowControl);
        if (serialCamera->open(QIODevice::ReadWrite))
        {
        QMessageBox::information(this, tr("Connected"), "Serial Port Opened Successfully");
        }
        else{
        QMessageBox::critical(this, tr("Error"), serialCamera->errorString());
        }
        }
        Project crashed. How to solve ?

        Thanks,

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @bknhatice said in How to connection serialport outside 'mainwindow.cpp' ?:

        ui->cb_spCportName->currentText()

        What is the format of the port name?
        Are you using proper port name format?

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0

        • Login

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