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 read ? QSerialPort
Forum Updated to NodeBB v4.3 + New Features

How to read ? QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.3k Views 2 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.
  • lordumuzcaL Offline
    lordumuzcaL Offline
    lordumuzca
    wrote on last edited by
    #1

    Arduino send me "degreevalue,distancevalue." .constantly sending different values. I have to write two different places.as the submitted values change, the displayed value must change as well.
    for example:
    place1 = degreevalue
    place2 = distancevalue
    how can i write

    1 Reply Last reply
    0
    • lordumuzcaL Offline
      lordumuzcaL Offline
      lordumuzca
      wrote on last edited by
      #2

      Actually i want code like this. i tried different types but i couldn't write

      void serialEvent (Serial myPort) { // starts reading data from the Serial Port
        // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
        data = myPort.readStringUntil('.');
        data = data.substring(0,data.length()-1);
        
        index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
        angle= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port
        distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance
        
        // converts the String variables into Integer
        iAngle = int(angle);
        iDistance = int(distance);
      }
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mostefa
        wrote on last edited by
        #3

        Hi @lordumuzca

        Maybe you can get inspired from Qt Serial Port Examples

        1 Reply Last reply
        2
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          To add to @mostefa, type casting a string to an it like that is wrong. QString provides dedicated methods for conversion from string to int and other numeric types.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • lordumuzcaL Offline
            lordumuzcaL Offline
            lordumuzca
            wrote on last edited by
            #5
            #include "dialog.h"
            #include "ui_dialog.h"
            #include <QSerialPort>
            #include <QSerialPortInfo>
            #include <QDebug>
            #include <QtWidgets>
            #include <QString>
            #include <string>
            
            
            Dialog::Dialog(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::Dialog)
            {
                ui->setupUi(this);
                ui->degree_lcdNumber->display("---");
                ui->distance_lcdNumber->display("---");
                arduino_is_available = false;
                arduino_port_name = "";
                arduino =new QSerialPort;
                serialBuffer = "";
            
                foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) {
                    if(serialPortInfo.hasVendorIdentifier() &&serialPortInfo.hasProductIdentifier()){
                        if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
                            if(serialPortInfo.productIdentifier()== arduino_uno_product_id){
                                arduino_port_name = serialPortInfo.portName();
                                arduino_is_available = true;
            
                            }
                        }
                    }
            
                }
                if(arduino_is_available){
                    //open and configure the port
                    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);
                    QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived()));
            
                }else{
                    //give error message
                    QMessageBox::warning(this,"Port Error","Couldn't find the Arduino!");
                }
            }
            
            
            Dialog::~Dialog()
            {
                if(arduino->isOpen()){
                    arduino->close();
                }
                delete ui;
            }
            
            void Dialog::serialReceived(){
            
                 QStringList bufferSplit = serialBuffer.split(".");
            
                    serialData = arduino->readAll();
                    serialBuffer += QString::fromStdString(serialData.toStdString());
            
            //      serialBuffer  = ",";
                                  qDebug()<<bufferSplit;
            }
            void Dialog::updateLCD(const QString sensor_reading){
            //    ui->degree_lcdNumber->display(sensor_reading);
            }
            
            

            my output
            alt text

            i want to display before comma in LCD1, after comman is in LCD2. i tried different things but i cant.
            maybe each double is new line like this.maybet better
            "xx,xx"
            "xx,xx"

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You can use a regexp to retrieve both numbers from your string.

              Also note that your conversion to std string are useless, QString already provides handling fro QByteArray.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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