Serial Data read from serial port is adding spaces or characters
-
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:
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; } } } } } }
-
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. -
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();
-
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()