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. Serial Data read from serial port is adding spaces or characters
Forum Updated to NodeBB v4.3 + New Features

Serial Data read from serial port is adding spaces or characters

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 643 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.
  • U Offline
    U Offline
    User94
    wrote on 5 May 2022, 09:07 last edited by
    #1

    I'm sending bytes (uint8_t) from arduino to Qt to build captured image.
    I'm reading received bytes as QByteArray using readAll() when serial is available but result is different than the actual data and by comparing text files using "WinMerge" there is some sort of spacing or tabs added in some place of the code like shown in the following picture:
    2fc180b5-fa4c-43b3-8c27-ea7f023e38ab-image.png

    qt code:

        if(SerialState){
            if(serial->isWritable()){
                QString datasend = "1";
                serial->write(datasend.toStdString().c_str());
                QFile ImageFile("./Image.JPG");
                ImageFile.open(QIODevice::WriteOnly);
                QDataStream out(&ImageFile);
    
    //            int counter = 0;
                while(!serial->isDataTerminalReady()){
                    serial->waitForReadyRead(0);
    //                counter++;
    //                if(counter == 65000){
    //                    QMessageBox::critical(this, "Capture Error", "Couldn't read data");
    //                    return;
    //                }
                }
                if(serial->isDataTerminalReady()){
                    QString LineData = ReadInput();
                    qDebug() << LineData;
                    bool WriteStatus = false;
                    while(true){
                        while(!serial->canReadLine()){
                            serial->waitForReadyRead(0);
                        }
                        if(serial->isDataTerminalReady()){
                            QByteArray LineData = ReadData();
                            qDebug() << LineData;
                            printeverything.push_back(LineData);
                            if(LineData.contains("done")){
                                WriteStatus = false;
                                LineData.remove(LineData.indexOf("done"), 5);
                                out << LineData;
                                ImageFile.close();
                                return;
                            }
                            if(WriteStatus){
                                out << LineData;
                            }
                            if(LineData.contains("byte image")){
                                WriteStatus = true;
                                out << LineData;
                            }
                        }
                    }
                }
            }
        }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrdebug
      wrote on 5 May 2022, 09:22 last edited by
      #2

      Are you sending the image in one time, without to split it in packets and without to add a crc byte to each packet in order to check the validity of the packet?
      If you want to verify if the problem is on arduino side or on Qt side you should capture the trasmitted data with a software like this
      http://www.denisgottardello.it/QtComPort/index.php
      or with a serial capture softare that you prefear.

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • U Offline
        U Offline
        User94
        wrote on 5 May 2022, 09:47 last edited by
        #3

        I'm not using crc or any method to check if data transmitted is correct if you want to check the code from arduino side here's the code that sends data and write it on sd at the same time:

        File imgFile = SD.open(filename, FILE_WRITE);
        
          // Get the size of the image (frame) taken  
          uint16_t jpglen = cam.frameLength();
          Serial.print("Storing ");
          Serial.print(jpglen, DEC);
          Serial.print(" byte image.");
        
          int32_t time = millis();
          pinMode(8, OUTPUT);
          // Read all the data up to # bytes!
          byte wCount = 0; // For counting # of writes
          while (jpglen > 0) {
            // read 32 bytes at a time;
            uint8_t *buffer;
            uint8_t bytesToRead = min((uint16_t)32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
            buffer = cam.readPicture(bytesToRead);
            imgFile.write(buffer, bytesToRead);
            Serial.write(buffer, bytesToRead);
            //Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");
            jpglen -= bytesToRead;
          }
          imgFile.close();
        
        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 5 May 2022, 15:43 last edited by
          #4

          Don't use QDataStream to write plain data - it serializes your data so it can be read in later on and therefore adds additional bytes. Use QFile::write()

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3

          1/4

          5 May 2022, 09:07

          • Login

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