QML Slot不显示串行输入的双值

时间:2017-06-17 20:07:31

标签: qt arduino qml registering

我很难将串行输入“analogread2”转换为double,以QML显示。

我添加了Q_property,main.cpp中的根上下文,我可以在qml中添加该属性,但我不能让它以文本格式显示。

QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
SerialPort serialport;
//engine.rootContext()->setContextProperty("serialport", &serialport);
qmlRegisterType<SerialPort>("SerialPortlib", 1, 0, "SerialPort");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")))

头。

Q_PROPERTY(double newValue MEMBER m_oil_pressure_volt WRITE set_oil_pressure_volt NOTIFY oil_pressure_volt_Changed )

QML

Window {
    visible: true
    width: 640
    height: 480
    id: gauge

    SerialPort{
        // what would I put in here to display the text value (double)
    }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

要进行测试我假设您是通过以下方式之一从arduino发送数据:

Serial.println(data);
Serial.print(data);

假设上面已经实现了serialport类,它有一个名为openDefault的函数,它扫描通过串行连接的设备,并在描述或制造商中搜索单词“Arduino”,如果发现它连接到它。 / p>

完整的代码可在以下网址找到:https://github.com/eyllanesc/stackoverflow/tree/master/Arduino-QML

<强> serialport.h

#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>

class SerialPort : public QObject
{
    Q_OBJECT
    Q_PROPERTY(double oil_pressure_volt READ get_oil_pressure_volt WRITE set_oil_pressure_volt NOTIFY oil_pressure_volt_Changed )

public:
    SerialPort(QObject *parent = 0);
    ~SerialPort();

    double get_oil_pressure_volt() const;
    void set_oil_pressure_volt(double newValue);

public slots:
    void onReadData();

signals:
    void oil_pressure_volt_Changed(double newValue);

private:
    QSerialPort *arduino;

    double mOil_pressure_volt;

    QSerialPortInfo portInfo;

    void openDefault();
};

<强> serialport.cpp

#include "serialport.h"

#include <QDebug>

SerialPort::SerialPort(QObject *parent):QObject(parent)
{

    arduino = new QSerialPort(this);
    connect(arduino, &QSerialPort::readyRead, this, &SerialPort::onReadData);
    openDefault();
}

SerialPort::~SerialPort()
{
    delete arduino;
}

void SerialPort::set_oil_pressure_volt(double newValue)
{
    if (mOil_pressure_volt == newValue)
        return;

    mOil_pressure_volt = newValue;
    emit oil_pressure_volt_Changed(mOil_pressure_volt);
}

void SerialPort::onReadData()
{
    if(arduino->bytesAvailable()>0){

        QByteArray data = arduino->readAll();

        QString value = QString(data).trimmed();
        qDebug()<< value;
        bool ok;
        double val = value.toDouble(&ok);
        if(ok)
            set_oil_pressure_volt(val);
    }
}

void SerialPort::openDefault()
{
    for(auto info: QSerialPortInfo::availablePorts()){
        qDebug()<<info.portName()<<info.description()<<info.manufacturer();
        if(!info.isBusy() && (info.description().contains("Arduino") || info.manufacturer().contains("Arduino"))){
            portInfo = info;
            break;
        }
    }

    if(portInfo.isNull()){
        return;
    }

    arduino->setPortName(portInfo.portName());
    arduino->setBaudRate(QSerialPort::Baud115200);
    arduino->setDataBits(QSerialPort::Data8);
    arduino->setParity(QSerialPort::NoParity);
    arduino->setStopBits(QSerialPort::OneStop);
    arduino->setFlowControl(QSerialPort::NoFlowControl);

    if(arduino->open(QSerialPort::ReadWrite))
        qDebug()<<"Connected to "<< portInfo.manufacturer()<< " on " << portInfo.portName();
    else
        qCritical()<<"Serial Port error: " << arduino->errorString();

}

double SerialPort::get_oil_pressure_volt() const
{
    return mOil_pressure_volt;
}

示例:

<强>的main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "serialport.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    qmlRegisterType<SerialPort>("SerialPortLib", 1, 0, "SerialPort");


    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
    return app.exec();
}

<强> main.qml

import QtQuick 2.6
import QtQuick.Window 2.2
import SerialPortLib 1.0

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

    SerialPort{
        onOil_pressure_voltChanged: {
            tx.text = "%1".arg(oil_pressure_volt);
        }
    }

    Text {
        id: tx
        anchors.fill: parent
        font.family: "Helvetica"
        font.pointSize: 20
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
    }
}