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. Problem with data read from USB port
Forum Updated to NodeBB v4.3 + New Features

Problem with data read from USB port

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 7 Posters 5.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.
  • P Offline
    P Offline
    pj0909
    wrote on 21 Feb 2017, 15:03 last edited by
    #1

    Hello All,

    I am new to Qt and I need your suggestions for my problem. I have a sensor connected to USB port and I want to read the data from the sensor. I tried with QSerialPort and I am getting some random values. Could anyone please advice me where I am going wrong. Below is my source code

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QDebug>
    #include <QString>
    #include <QMessageBox>
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
    
        sensor = new QSerialPort(this); //sensor is included in header file as QSerialPort
        serialBuffer = ""; // included in header file as QString
    
        qDebug() << "Number of ports: " << QSerialPortInfo::availablePorts().length() << "\n";
        foreach(const QSerialPortInfo &serialportinfo, QSerialPortInfo::availablePorts())
        {
            qDebug() << "Description: " <<serialportinfo.description() << "\n";
            qDebug() << "Has vendor id: " <<serialportinfo.hasVendorIdentifier() << "\n";
            qDebug() << "vendor id: " <<serialportinfo.vendorIdentifier() << "\n";
            qDebug() << "Has product id: " <<serialportinfo.hasProductIdentifier() << "\n";
            qDebug() << "prodcut id: " <<serialportinfo.productIdentifier() << "\n";
        }
    
        bool sensor_is_available = false;
        QString sensor_port_name;
    
        foreach(const QSerialPortInfo &serialportinfo, QSerialPortInfo::availablePorts())
        {
            if(serialportinfo.hasProductIdentifier() && serialportinfo.hasVendorIdentifier())
            {
                if((serialportinfo.productIdentifier() == sensor_product_id) && (serialportinfo.vendorIdentifier() == sensor_vendor_id))
                {
                    sensor_is_available = true;
                    sensor_port_name = serialportinfo.portName();
                }
            }
        }
    
        if(sensor_is_available)
        {
            qDebug() << "Found the sensor port\n";
            sensor->setPortName(sensor_port_name);
            sensor->open(QSerialPort::ReadOnly);
            sensor->setBaudRate(QSerialPort::Baud9600);
            sensor->setDataBits(QSerialPort::Data8);
            sensor->setFlowControl(QSerialPort::HardwareControl);
            sensor->setParity(QSerialPort::NoParity);
            sensor->setStopBits(QSerialPort::OneStop);
            QObject::connect(sensor, SIGNAL(readyRead()), this, SLOT(readSerial()));
        }
    
        else{
            qDebug() << "Couldn't find the correct port for the sensor.\n";
            QMessageBox::warning (this, "Serial Port Error", "Couldn't open serial port to sensor");
        }
    }
    
    Dialog::~Dialog()
    {
        if(sensor->isOpen())
            sensor->close();
        delete ui;
    }
    
    void Dialog::readSerial()
    {
        serialData = sensor->readAll(); // serialData included in header file as QByteArray
        serialBuffer += QString::fromStdString(serialData.toStdString());
        ui->textBrowser->setText(serialBuffer);
    
        qDebug() <<serialBuffer;
        qDebug("stopped");
    }
    

    The output which I am getting is as below :

    "\r\u0014f\u0000@\np?>??>?V?>S&?\r\u0014f\u0000@\n?=?'?>?\u0005?>?$??\r\u0014f\u0000ve?????>?f|>????\r\u0014f\u0000ve?=??i>??C>????"
    stopped
    

    I am still figuring out where I went wrong. I read somewhere that data from USB port can be read through serial port functions. Could anyone advice me on the above code?

    Thank you for your time and knowledge.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 21 Feb 2017, 15:09 last edited by
      #2

      the problem is here: serialBuffer += QString::fromStdString(serialData.toStdString());

      how does your sensor encode the data?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      P 1 Reply Last reply 21 Feb 2017, 15:20
      1
      • V VRonin
        21 Feb 2017, 15:09

        the problem is here: serialBuffer += QString::fromStdString(serialData.toStdString());

        how does your sensor encode the data?

        P Offline
        P Offline
        pj0909
        wrote on 21 Feb 2017, 15:20 last edited by
        #3

        @VRonin The sensor is based on capacitive technology. Any change in capacitance gives corresponding values.

        V 1 Reply Last reply 21 Feb 2017, 15:56
        0
        • P pj0909
          21 Feb 2017, 15:20

          @VRonin The sensor is based on capacitive technology. Any change in capacitance gives corresponding values.

          V Offline
          V Offline
          VRonin
          wrote on 21 Feb 2017, 15:56 last edited by
          #4

          @pj0909 said in Problem with data read from USB port:

          values

          This still does not answer my question. how are those values represented in binary format to the serial output? is it a 32 bit integer, a 64 bit double precision float, an ASCII encoded string?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          P 1 Reply Last reply 21 Feb 2017, 16:36
          0
          • V VRonin
            21 Feb 2017, 15:56

            @pj0909 said in Problem with data read from USB port:

            values

            This still does not answer my question. how are those values represented in binary format to the serial output? is it a 32 bit integer, a 64 bit double precision float, an ASCII encoded string?

            P Offline
            P Offline
            pj0909
            wrote on 21 Feb 2017, 16:36 last edited by
            #5

            @VRonin Sorry, I misinterpreted your question. The answer to your question is 64 bit double precision float.

            J K 2 Replies Last reply 22 Feb 2017, 05:37
            0
            • P pj0909
              21 Feb 2017, 16:36

              @VRonin Sorry, I misinterpreted your question. The answer to your question is 64 bit double precision float.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 22 Feb 2017, 05:37 last edited by
              #6

              @pj0909 Then you should use http://doc.qt.io/qt-5/qbytearray.html#toDouble without converting to string:

              bool ok = false;
              serialBuffer += sensor->readAll().toDouble(&ok);
              if (!ok) // error
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • V Offline
                V Offline
                VRonin
                wrote on 22 Feb 2017, 07:34 last edited by VRonin
                #7

                To add to @jsulm, you should also check that serialData contains 64 and only 64 bits of data (i.e. serialData.size()==8) otherwise use QDataStream

                void Dialog::readSerial()
                {
                QDataStream sensorStream(sensor);
                QVector<double> readValues;
                for(;;){
                sensorStream.startTransaction();
                double readValue=0;
                sensorStream >> readValue;
                if(sensorStream.commitTransaction())
                readValues.push_back(readValue);
                else
                break;
                }
                qDebug() << "data read: " << readValues;
                }
                

                P.S.
                If your sensor sends the data in little endian order, you'll have to set the QDataStream byte order http://doc.qt.io/qt-5/qdatastream.html#setByteOrder

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2
                • P pj0909
                  21 Feb 2017, 16:36

                  @VRonin Sorry, I misinterpreted your question. The answer to your question is 64 bit double precision float.

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 22 Feb 2017, 07:43 last edited by
                  #8

                  @pj0909 said in Problem with data read from USB port:

                  The answer to your question is 64 bit double precision float.

                  Then you need to read the data as binary, not as text! Something like this (warning: the code isn't really portable):

                  QDataStream in(&serialData, QIODevice::ReadOnly);
                  
                  double value;
                  in >> value;

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  3
                  • P Offline
                    P Offline
                    pj0909
                    wrote on 22 Feb 2017, 12:42 last edited by
                    #9

                    I will implement the suggestions in the code and will see the output from the sensor. Will let you all know if its working correctly or still there is a problem. Thank you for the suggestions.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pj0909
                      wrote on 28 Feb 2017, 09:51 last edited by
                      #10

                      Finally I solved my problem. I set the floating point precision in addition to the suggestions given above and now I am reading the data as expected.

                      Thank you all for the suggestions.

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        namduc
                        wrote on 6 Apr 2020, 11:06 last edited by
                        #11

                        Hi all!
                        I have the same problem, I need to communicate with a USB card reader, but I don't recognize it when i plug it with my PC ( Win 7)
                        serialportinfo.hasProductIdentifier() and serialportinfo.hasVendorIdentifier() all return false;
                        Who can help me? Thanks!

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kuzulis
                          Qt Champions 2020
                          wrote on 6 Apr 2020, 12:17 last edited by
                          #12

                          What's relation between the serial ports and the USB card readers?

                          1 Reply Last reply
                          2
                          • N Offline
                            N Offline
                            namduc
                            wrote on 7 Apr 2020, 01:58 last edited by
                            #13

                            Reader Card connect directly to the computer using the USB port
                            Are there any libraries supporting this?
                            Thanks!

                            aha_1980A 1 Reply Last reply 7 Apr 2020, 05:54
                            0
                            • N namduc
                              7 Apr 2020, 01:58

                              Reader Card connect directly to the computer using the USB port
                              Are there any libraries supporting this?
                              Thanks!

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on 7 Apr 2020, 05:54 last edited by
                              #14

                              Hi @namduc,

                              there is exactly one: libusb

                              Regards

                              Qt has to stay free or it will die.

                              1 Reply Last reply
                              6

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved