I am sending data to Arduino using QtSerialPort. When I read the data the ouput has several not printer characters. I am using a QTexEdit (in left hand) to insert the data to send and other (in right hand) to insert the data to read.
You can see the receive data has some no printer character.
Workflow of sending and receiving data between QtSerialPort and Arduino
Sending Data with QtSerialPort
void MainWindow::sendData(){
QString m_allData = m_sendEdit->toPlainText();
int i = 0;
int size = m_allData.size();
QString line = "";
int c = 0;
while(i < size){
line.append(m_allData[i]);
if(c == 24){
int sended = m_serialPort->write(line.toUtf8(), 24);
m_serialPort->flush();
line.clear();
c = 0;
}
i++;
c++;
}
if(c > 0){
m_serialPort->write(line.toUtf8(), c);
}
m_serialPort->flush();
}
I want to send the data with a buffer size of 24 bytes.
Reading and Sending Data with Arduino
void setup() {
Serial.begin(9600);
}
void loop() {
delay(1000);
if (Serial.available() > 0){
short i = 0;
int size = Serial.available();
String data = Serial.readString();
Serial.print(data);
}
}
The Arduino code is simple.
Receiving Data with QtSerialPort
void MainWindow::readData(){
int c = 0;
char * dataBuffer;
int size = m_serialPort->bytesAvailable();
dataBuffer = new char[size];
c = m_serialPort->read(dataBuffer, size);
m_receiveEdit->setText(m_receiveEdit->toPlainText() + QString::fromUtf8(dataBuffer));
delete dataBuffer;
}
I am trying to read the same data that I send to Arduino. What kind of convertion do I have to do?