0

I'm trying to read and write with serial port. I have created a class , named serial , and connect it with readyRead on mainwindow.cpp.

QObject::connect(frm_serial->arduino,SIGNAL(readyRead()),frm_serial,SLOT(serialRead()));

I'm reading the data when I received it by connecting signal in serial.cpp without any problem.

I'm writing the data in serial.cpp without any problem too.

But when I tried to write data from another class "The program has unexpectedly finished".

Here is the code of connect method in another class

connect(this,SIGNAL(giden_seri(QString)),frm_serial,SLOT(serialWrite(QString)));

and string send code in another class

emit giden_seri(seri_mesaj);

Here is my serialWrite function in serial.cpp

void serial::serialWrite(QString gelen_seri)
{
    arduino->write(gelen_seri.toStdString().c_str());
}

By the way I cant not use any functions of serialDevice, not only write....

Edit:

mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "serial.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

private:
    Ui::MainWindow *ui;
    manuel frm_manuel;
    serial *frm_serial = new serial;
};

#endif // MAINWINDOW_H

serial.h

#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QByteArray>
#include <QString>

class serial : public QObject
{
    Q_OBJECT
public:
    explicit serial(QObject *parent = 0);
    QSerialPort *arduino;
    const QString arduino_seri = "75533353637351110171";
    QString arduino_port_name;
    bool arduino_is_available;

    QString serialBuffer;
    QString bas="cagri",son="deniz";

signals:

public slots:
    void ard_kontrol();

    void serialRead();

    void serialWrite(QString gelen_seri);

};

#endif // SERIAL_H

manuel.h (my another class)

    #ifndef MANUEL_H
    #define MANUEL_H

    #include <QWidget>
    #include <QString>

    #include "serial.h"

    #include <QDebug>
    #include <QByteArray>
    #include <QString>
    #include <QtSerialPort/QSerialPort>

    namespace Ui {
    class manuel;
    }

    class manuel : public QWidget
    {
        Q_OBJECT

    public:
        explicit manuel(QWidget *parent = 0);
        ~manuel();

    private slots:

        void seri_gonder();

    signals:
        void giden_veri(QString s);
        void giden_seri(QString veri);

    private:
        Ui::manuel *ui;
        QString seri_mesaj="";
        bool mesaj_hazir=0; // 0 yok , 1 Hazir
        serial *frm_serial;

    };

    #endif // MANUEL_H

mainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QPixmap>
    #include <QObject>


    bool auto_flag=0;

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        frm_serial->ard_kontrol();

        QObject::connect(frm_serial->arduino,SIGNAL(readyRead()),frm_serial,SLOT(serialRead()));

    }

    MainWindow::~MainWindow()
    {
        delete ui;
    }

serial.cpp

    #include "serial.h"

serial::serial(QObject *parent) :
    QObject(parent)
{
}


void serial::ard_kontrol()
{
    arduino_is_available=false;
    arduino_port_name="";
    serialBuffer="";
    arduino = new QSerialPort;

    foreach (const QSerialPortInfo &serialPortInfo,     QSerialPortInfo::availablePorts()) {
        if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier())
        {
            if(serialPortInfo.serialNumber() == arduino_seri)
            {
                arduino_port_name= serialPortInfo.portName();
                arduino_is_available=true;
            }
        }

    }

    if(arduino_is_available)
    {
        qDebug() << "Arduino var : " << arduino_port_name ;
        arduino->setPortName(arduino_port_name);
        arduino->open(QSerialPort::ReadWrite);
        arduino->setBaudRate(QSerialPort::Baud9600);
        arduino->setDataBits(QSerialPort::Data8);
        arduino->setParity(QSerialPort::NoParity);
        arduino->setStopBits(QSerialPort::OneStop);
        arduino->setFlowControl(QSerialPort::NoFlowControl);
    }

    else
        qDebug() << "Arduino yok";
}

void serial::serialRead()
{
    serialBuffer.append(arduino->readAll());
    if(serialBuffer.indexOf(bas) >=0 && serialBuffer.indexOf(son) >= 0)
    {    serialBuffer=serialBuffer.mid(serialBuffer.indexOf(bas)+bas.length(),serialBuffer.indexOf(son)-(serialBuffer.indexOf(bas)+bas.length()));
        qDebug() << serialBuffer;
        serialBuffer="";
   }
}    

void serial::serialWrite(QString gelen_seri)
{
    arduino->write(gelen_seri.toStdString().c_str());
}

manuel.cpp

#include "manuel.h"
#include "ui_manuel.h"
#include <QDebug>
#include <QPixmap>

manuel::manuel(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::manuel)
{
    ui->setupUi(this);
    frm_serial=new serial();
    connect(this,SIGNAL(giden_veri(QString)),frm_uyari,SLOT(gelen_veri(QString)));
    connect(this,SIGNAL(giden_seri(QString)),frm_serial,SLOT(serialWrite(QString)));
}

manuel::~manuel()
{
    delete ui;
}


void manuel::seri_gonder()
{
    if(mesaj_hazir)
    {
        emit giden_seri(seri_mesaj);
        mesaj_hazir=0;
    }
}

Github repository

bladekel
  • 37
  • 7
  • You could share the complete code. – eyllanesc Apr 03 '17 at 10:03
  • Of course but it can be complex. I found something.When I checked the availability of serail port from another class it says "not opened/writable/readable ...". But when I checked it from its own class its open. And I dont close it anywhere. How can I open the serial from another class? – bladekel Apr 03 '17 at 10:14
  • Please share your code or something simplified but reproduce the error. – eyllanesc Apr 03 '17 at 10:19
  • You could share it through github or a similar medium. – eyllanesc Apr 03 '17 at 10:25
  • I have tried your code and it is difficult to help if it can not be executed since they are missing the .ui file, the .pro file and other files. If you can send it to my email: e.yllanescucho@gmail.com – eyllanesc Apr 03 '17 at 13:01
  • Please minimize your code to remove everything unnecessary to demonstrate your issue. See e.g. [the first snippet in this answer](http://stackoverflow.com/a/41724363/1329652) - it is a complete test case. That answer also demonstrates how to factor out a communications system from the gui. That answer links to other answer(s) that may also be relevant. – Kuba hasn't forgotten Monica Apr 03 '17 at 13:30
  • Use seem to have two serial objects going. You can't expect them both to be able to access the same serial port. – onion Apr 04 '17 at 07:02

0 Answers0