How to display data from uart on Raspberry PI 4

49 Views Asked by At

I'm new in the raspbian bios and programming language, I have problem with receiving data from the raspberry pi 4 uart (ttyS0). I have ADC converter of course, which returns me hexadecimal string with data. The Przemytnik class is for connecting c++ language with qml, I would like to add a few classes in the future that's why I'm using the other class to reconvert data to qml. Sorry for my bad English, I'm still learning ;) I'm using QSerialPort library Could you help me with this? SerialReader.h:

#ifndef SERIALREADER_H
#define SERIALREADER_H

#include <QObject>
#include <QSerialPort>

class SerialReader : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString data READ data NOTIFY dataChanged)

public:
    explicit SerialReader(QObject *parent = nullptr);

    QString data() const;
    Q_INVOKABLE void readData();
    void writeData(const QString &data);

signals:
    void dataChanged();

private:
    QSerialPort m_serialPort;
    QString m_data;
};

#endif // SERIALREADER_H

SerialReader.cpp:

#include <SerialReader.h>
#include <iostream>

SerialReader::SerialReader(QObject *parent) : QObject(parent) {
    m_serialPort.setPortName("/dev/ttyS0");
    m_serialPort.setBaudRate(QSerialPort::Baud115200);
    m_serialPort.setDataBits(QSerialPort::Data8);
    m_serialPort.setParity(QSerialPort::NoParity);
    m_serialPort.setStopBits(QSerialPort::OneStop);
    m_serialPort.setFlowControl(QSerialPort::NoFlowControl);

    if (!m_serialPort.open(QIODevice::ReadOnly)) {
        std::cerr << "Błąd portu szeregowego!";
    }
}

QString SerialReader::data() const { return m_data; }

void SerialReader::readData() {
    if (m_serialPort.isOpen() && m_serialPort.isReadable()) {
        m_data = m_serialPort.readAll();
        emit dataChanged();
    }
}

void SerialReader::writeData(const QString &data) {
    if (m_serialPort.isOpen() && m_serialPort.isWritable()) {
        m_serialPort.write(data.toUtf8());
    }
}

przemytnik.h:

#ifndef PRZEMYTNIK_H
#define PRZEMYTNIK_H

#include <QObject>
#include <SerialReader.h>

class Przemytnik : public QObject
{
    Q_OBJECT
    Q_PROPERTY(SerialReader* serialReader READ serialReader WRITE setSerialReader NOTIFY serialReaderChanged)

public:
    explicit Przemytnik(QObject *parent = nullptr);

    SerialReader* serialReader() const;
    void setSerialReader(SerialReader* serialReader);

signals:
    void serialReaderChanged();

private:
    SerialReader* m_serialReader;
};

#endif // PRZEMYTNIK_H

przemytnik.cpp:

#include <przemytnik.h>

Przemytnik::Przemytnik(QObject *parent) : QObject(parent), m_serialReader(new SerialReader(this)) {
    m_serialReader->readData();
}

SerialReader* Przemytnik::serialReader() const { return m_serialReader; }

void Przemytnik::setSerialReader(SerialReader* serialReader) {
    if (serialReader != m_serialReader) {
        m_serialReader = serialReader;
        emit serialReaderChanged();
    }
}

I also used qmlRegisteredType in main.cpp And my simple main.qml code:

import QtQuick 2.9
import QtQuick.Window 2.3
import port 1.0

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Item{
        Przemytnik{
            id: przemytnik
        }
        Text {
            text: przemytnik.serialReader.data
        }
    }
}

I tried to change the code, but with no results. I don't know what I can do to fix that. I'm hoping that someone can help me.

0

There are 0 best solutions below