Results 1 to 15 of 15

Thread: Arduino serial input qml display text, conflict of previous declaration error.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2017
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Arduino serial input qml display text, conflict of previous declaration error.

    Hi Guys,

    Ive been working on a project trying to read the analog input from an Arduino board through serial, then display this on a qml gui in text form first and then move onto a rotating gauge.

    I completed the process using qdebug at each step, so I'm reading the input until I come to the Q_PROPERTY stage and I get this error.

    Qt Code:
    1. C:\Users\Stephn\Documents\nissan300zx\serialport.h:48: error: 'double SerialPort::oil_pressure_volt' conflicts with a previous declaration
    2. double oil_pressure_volt;
    To copy to clipboard, switch view to plain text mode 
    ^

    Qt Code:
    1. #ifndef SERIALPORT_H
    2. #define SERIALPORT_H
    3.  
    4. #include <QObject>
    5. #include <QQuickItem>
    6. #include <QSerialPort>
    7. #include <QSerialPortInfo>
    8. #include <QByteArray>
    9.  
    10. class SerialPort : public QObject
    11. {
    12. Q_OBJECT
    13. Q_PROPERTY(double oil_pressure_volt READ oil_pressure_volt WRITE set_oil_pressure_volt NOTIFY oil_pressure_volt_Changed)
    14.  
    15. public:
    16. SerialPort(QObject *parent = 0);
    17. SerialPort(QString);
    18.  
    19. double oil_pressure_volt() const
    20. {
    21. return m_oil_pressure_volt;
    22. }
    23.  
    24. public slots:
    25. void analogRead2();
    26. void updateOilPressure(QString);
    27.  
    28. void set_oil_pressure_volt(double oil_pressure_volt)
    29. {
    30. if (m_oil_pressure_volt == oil_pressure_volt)
    31. return;
    32.  
    33. m_oil_pressure_volt = oil_pressure_volt;
    34. emit oil_pressure_volt_Changed(oil_pressure_volt);
    35. }
    36.  
    37. signals:
    38.  
    39. void oil_pressure_volt_Changed(double oil_pressure_volt);
    40.  
    41. private:
    42. QSerialPort *arduino;
    43. static const quint16 arduino_uno_vendor_id = 0x2341;
    44. static const quint16 arduino_uno_product_id = 0x0001;
    45. QByteArray serialData;
    46. QString serialBuffer;
    47. QString parsed_data;
    48. double oil_pressure_volt;
    49.  
    50. double m_oil_pressure_volt;
    51. };
    52.  
    53. #endif // SERIALPORT_H
    To copy to clipboard, switch view to plain text mode 

    much appreciated for any help you can give me.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    The Q_PROPERTY macro defines the member variable oil_pressure_volt for you. Line 48 is adding a duplicate definition and thus causing the error. Delete that line. Get rid of m_oil_pressure_volt also, since it is just a duplicate of the property.

    You would confuse yourself less if you didn't use the name "oil_pressure_volt" everywhere. C++ doesn't care what the argument names are, so in the set method, use "newValue" or something like that. Likewise in your definition of the signal.

    If you want the -name- of the property that is exposed to QML to be something different from the member variable name, then use the MEMBER form of Q_PROPERTY:

    Qt Code:
    1. Q_PROPERTY( double oilPressureVoltage MEMBER m_oil_pressure_volt WRITE set_oil_pressure_volt NOTIFY oil_pressure_volt_Changed )
    To copy to clipboard, switch view to plain text mode 

    You will refer to it as "oilPressureVoltage" from QML.
    Last edited by d_stranz; 15th June 2017 at 21:26.
    <=== 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. #3
    Join Date
    Jun 2017
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    Much appreciated for clarifying that... and after adding your code and trying I still couldn't see anything in the main.qml.

    I'm using the "double oil_pressure_volt" in my serialport.cpp code below, it takes my 10 bit analog input and turns it into a voltage reading. Would I be better doing the conversion in the qml?

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QGuiApplication app(argc, argv);
    4. QQmlApplicationEngine engine;
    5. SerialPort serialport;
    6. qmlRegisterType<SerialPort>("SerialPortlib", 1, 0, "SerialPort");
    7. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    8.  
    9. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class SerialPort : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY( double oilPressureVoltage MEMBER m_oil_pressure_volt WRITE set_oil_pressure_volt NOTIFY oil_pressure_volt_Changed )
    5.  
    6. public:
    7. SerialPort(QObject *parent = 0);
    8. SerialPort(QString);
    9.  
    10. public slots:
    11. void analogRead2();
    12. void updateOilPressure(QString);
    13.  
    14. void set_oil_pressure_volt(double oilPressureVoltage)
    15. {
    16. if (m_oilPressureVoltage == oilPressureVoltage)
    17. return;
    18.  
    19. m_oilPressureVoltage = oilPressureVoltage;
    20. emit oil_pressure_volt_Changed(oilPressureVoltage);
    21. }
    22.  
    23. signals:
    24.  
    25. void oil_pressure_volt_Changed(double oilPressureVoltage);
    26.  
    27. private:
    28. QSerialPort *arduino;
    29. static const quint16 arduino_uno_vendor_id = 0x2341;
    30. static const quint16 arduino_uno_product_id = 0x0001;
    31. QByteArray serialData;
    32. QString serialBuffer;
    33. QString parsed_data;
    34. double oil_pressure_volt;
    35. double m_oilPressureVoltage;
    36. };
    37.  
    38. #endif // SERIALPORT_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void SerialPort::analogRead2()
    2. {
    3. QStringList buffer_split = serialBuffer.split(",");
    4. if(buffer_split.length() < 3)
    5. {
    6. serialData = arduino->readAll();
    7. serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
    8. serialData.clear();
    9. }else{
    10. serialBuffer = "";
    11. qDebug() << buffer_split << "\n";
    12. parsed_data = buffer_split[1];
    13. oil_pressure_volt = (0.0048) * (parsed_data.toDouble());
    14. qDebug() << "Pressure: " << oil_pressure_volt << "\n";
    15. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. import QtQuick 2.5
    2. import QtQuick.Window 2.2
    3. import QtQuick.Extras 1.4
    4. import QtQuick.Controls 2.0
    5. import SerialPortlib 1.0
    6.  
    7. Window {
    8. id: gauge
    9. visible: true
    10. width: 640
    11. height: 480
    12.  
    13. TextField {
    14. id: textField
    15. x: 250
    16. y: 280
    17. text: SerialPort.oilPressureVoltage
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by StevieGardiner; 16th June 2017 at 08:15.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    First change line
    Qt Code:
    1. oil_pressure_volt = (0.0048) * (parsed_data.toDouble());
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. set_oil_pressure_volt((0.0048) * (parsed_data.toDouble()));
    To copy to clipboard, switch view to plain text mode 
    P.S. Try to figure out why.

  5. #5
    Join Date
    Jun 2017
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    Hi Lesiok, thanks for you help, I changed this line with nothing still in the qml.

  6. #6
    Join Date
    Jun 2017
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    An update guys, after changing to this in the main.cpp, I managed to display a "0" which I think is the first reading from the analogRead2, then the rest is the voltage.

    Qt Code:
    1. engine.rootContext()->setContextProperty("serialport", &serialport);
    To copy to clipboard, switch view to plain text mode 

    I would think id need to add this code to a loop, and advice guys is much appreciated.

    Qt Code:
    1. void set_oil_pressure_volt(double oilPressureVoltage)
    2. {
    3. if (m_oilPressureVoltage != oilPressureVoltage)
    4. return;
    5.  
    6. m_oilPressureVoltage = oilPressureVoltage;
    7. emit oil_pressure_volt_Changed(oilPressureVoltage);
    8. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    How is SerialPort::analogRead2() activated ?

  8. #8
    Join Date
    Jun 2017
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    By the QObject::Connect on line 43

    Qt Code:
    1. #include "serialport.h"
    2. #include <QSerialPort>
    3. #include <QSerialPortInfo>
    4. #include <string>
    5. #include <QDebug>
    6. #include <QQuickView>
    7. #include <QQmlApplicationEngine>
    8. #include <QApplication>
    9. #include <QtQuickWidgets/QQuickWidget>
    10.  
    11. SerialPort::SerialPort(QObject *parent)
    12. {
    13. arduino = new QSerialPort(this);
    14.  
    15. serialBuffer = "";
    16. parsed_data = "";
    17. oil_pressure_volt = 0.0;
    18.  
    19. bool arduino_is_available = false;
    20. QString arduino_uno_port_name;
    21.  
    22. foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
    23. // check if the serialport has both a product identifier and a vendor identifier
    24. if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()){
    25. // check if the product ID and the vendor ID match those of the arduino uno
    26. if((serialPortInfo.productIdentifier() == arduino_uno_product_id)
    27. && (serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id)){
    28. arduino_is_available = true; // arduino uno is available on this port
    29. arduino_uno_port_name = serialPortInfo.portName();
    30. }
    31. }
    32. }
    33.  
    34. if(arduino_is_available){
    35. qDebug() << "Found the arduino port...\n";
    36. arduino->setPortName(arduino_uno_port_name);
    37. arduino->open(QSerialPort::ReadOnly);
    38. arduino->setBaudRate(QSerialPort::Baud57600);
    39. arduino->setDataBits(QSerialPort::Data8);
    40. arduino->setFlowControl(QSerialPort::NoFlowControl);
    41. arduino->setParity(QSerialPort::NoParity);
    42. arduino->setStopBits(QSerialPort::OneStop);
    43. QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(analogRead2()));
    44.  
    45. }else{
    46. qDebug() << "Couldn't find the correct port for the arduino.\n";
    47. //QMessageBox::information(this, "Serial Port Error", "Couldn't open serial port to arduino.");
    48. }
    49.  
    50. }
    51. void SerialPort::analogRead2()
    52. {
    53. QStringList buffer_split = serialBuffer.split(",");
    54. if(buffer_split.length() < 3)
    55. {
    56. serialData = arduino->readAll();
    57. serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
    58. serialData.clear();
    59. }else{
    60. serialBuffer = "";
    61. qDebug() << buffer_split << "\n";
    62. parsed_data = buffer_split[1];
    63. oil_pressure_volt = (0.0048) * (parsed_data.toDouble());
    64. //set_oil_pressure_volt((0.0048) * (parsed_data.toDouble()));
    65. qDebug() << "Pressure: " << oil_pressure_volt << "\n";
    66. }
    67. }
    68.  
    69. void SerialPort::updateOilPressure(QString sensor_reading)
    70. {
    71.  
    72. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    And on qDebug you have a series of messages "Pressure: ...." ?
    BTW : I see that you have not figured out why it should be line 64 and not 63. Line 63 changes oil_pressure_volt but not generate signal oil_pressure_volt_Changed.

  10. #10
    Join Date
    Jun 2017
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    Yes on qDebug, this is so I could see the input in *.cpp
    I just tried again by applying line 64 and commenting out line 63, and the qml still shows "0"

    I think I need to apply a loop somewhere,

  11. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Arduino serial input qml display text, conflict of previous declaration error.

    I'm not using QML so I don't know how to use signal oil_pressure_volt_Changed in QML.

Similar Threads

  1. Replies: 0
    Last Post: 5th December 2013, 06:46
  2. Replies: 2
    Last Post: 5th March 2013, 17:40
  3. Replies: 12
    Last Post: 24th July 2012, 08:19
  4. linker error during signal declaration
    By riarioriu3 in forum Newbie
    Replies: 1
    Last Post: 28th June 2012, 00:02
  5. error ISO C++ forbids declaration of 'obj' with no type
    By naturalpsychic in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2011, 06:23

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.