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

QSerialPort - transmition errors

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.6k Views 1 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.
  • M Offline
    M Offline
    michelson
    wrote on last edited by SGaist
    #1

    Hello guys,
    i have some problems in reading bytes using QSerialPort. I implemented readyRead() - readAll() signal-slot system but it seems to be working incorectly. I have to read about 3Mb of data (continuous transmition) but the bytes read by readAll() function are all zeros. I tested the device using some terminal application and it seems to work fine. Does anyone experienced similar problem in their work?

    simplified code here:

    char datawrite[] = {0x01,0x02}; // device commands which i should get response from
    QByteArray dataread;
    serial->write(datawrite);
     ...
    // handle readyRead()
     void on_readyRead() {
           dataread.append(serial->readAll());
    }
    

    [edit: fixed coding tags ``` SGaist]

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

      Hi,

      Please post the un-simplified code. From what you posted you might be using two different QByteArrays with the same name so one would stay empty and not the other one.

      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
      • M Offline
        M Offline
        michelson
        wrote on last edited by michelson
        #3

        HEADER:

        #ifndef SESIONPROGRAMING_H
        #define SESIONPROGRAMING_H
        
        #include <QMainWindow>
        #include <QSerialPort>
        
        namespace Ui {
        class SesionPrograming;
        }
        
        class SesionPrograming : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit SesionPrograming(QWidget *parent = 0);
            ~SesionPrograming();
        
        private slots:
            void handleError(QSerialPort::SerialPortError error);
            void openSerialPort();
            void closeSerialPort();
            void writeData(const QByteArray &data);
            void readData();
        
        private:
            QByteArray reciveddata;
            int readcalls;
            char charbuff[1];
            Ui::SesionPrograming *ui;
            QSerialPort *serial;
        
        };
        
        #endif // SESIONPROGRAMING_H
        

        SOURCE:

        #include <QMessageBox>
        #include <QDebug>
        #include <windows.h>
        
        SesionPrograming::SesionPrograming(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::SesionPrograming)
        {
            ui->setupUi(this);
            serial = new QSerialPort(this);
            serial->setBaudRate(QSerialPort::Baud115200);
            serial->setDataBits(QSerialPort::Data8);
            serial->setParity(QSerialPort::NoParity);
            serial->setStopBits(QSerialPort::OneStop);
            serial->setFlowControl(QSerialPort::NoFlowControl);
            serial->setPortName("COM5");
            connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
            connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
                    SLOT(handleError(QSerialPort::SerialPortError)));
            openSerialPort();
            const char commands[] = {0xb0};
            QByteArray writeba(commands);
            writeData(writeba);
        }
        
        SesionPrograming::~SesionPrograming()
        {
            qDebug() << reciveddata.toHex();
            qDebug() << reciveddata;
            qDebug() << reciveddata.size();
            qDebug() << readcalls;
            closeSerialPort();
            delete ui;
        }
        
        void SesionPrograming::handleError(QSerialPort::SerialPortError error)
        {
            if (error == QSerialPort::ResourceError) {
                QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
                closeSerialPort();
            }
        }
        
        void SesionPrograming::writeData(const QByteArray &data)
        {
            serial->write(data);
        }
        
        void SesionPrograming::readData()
        {
            reciveddata.append(serial->readAll());
        }
        void SesionPrograming::openSerialPort()
        {
            if (serial->open(QIODevice::ReadWrite))
            {
                qDebug()<<"połączono z portem com4";
            }
            else
            {
                QMessageBox::critical(this, tr("Error"), serial->errorString());
            }
        }
        
        void SesionPrograming::closeSerialPort()
        {
            if (serial->isOpen())
                serial->close();
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why are you calling serial->clear() ?

          Please enclose your code with coding tags: three back-ticks before and after.

          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
          • M Offline
            M Offline
            michelson
            wrote on last edited by
            #5

            Im sorry, i was messing around with code to work something out, thought there might be some rubbish in the buffer so i thought clearing it may fix the problem (in fact it doesnt)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              michelson
              wrote on last edited by
              #6

              Another strange behaviour here:
              The device has a list of commands i.e
              0x02 - return name (8bytes)
              0x04 - return firmware (12bytes)
              When i write above commands it responds correctly but when i use this 0x80... command which should get me the measurment data (thousands of bytes) it sends only zeros (again, i tested the device in external terminal app and it worked fine...) so in my opinion the length of data is the problem. But why?

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kuzulis
                Qt Champions 2020
                wrote on last edited by kuzulis
                #7

                Why you are think that it is correct?

                    const char commands[] = {0xb0};
                    QByteArray writeba(commands);
                

                QByteArray is not the telepathist, so try this instead:

                    const char commands[] = {0xb0};
                    QByteArray writeba(commands, sizeof(commands));
                

                and check that QByteArray contains desired bytes.

                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