1

I have currently some problems with the QSerialPort: When I am using the function from an example which looks like

QKeyEvent *e;
emit getData(e->text().toLocal8Bit());
connect(console, SIGNAL(getData(QByteArray)), this, SLOT(writeData(QByteArray)));
void MainWindow::writeData(const QByteArray &data)
{
    qDebug() << "Data is to write: " << data;
    serial->write(data);
}

then the receiving device can work with the data. But when I change the function writeData() to

void MainWindow::writeData(const QByteArray &data)
{
    QString a = "Q";
    QByteArray b = a.toLocal8Bit();
    serial->write(b);
}

the receiving device can not work with the received data. Where is the difference between those two approaches?
Update: I found out that apparently the data is only usefully transferred if I press Enter after typing the letters. Somehow the '\n' gets lost in the conversion from QString to QByteArray. How can I keep it?

arc_lupus
  • 3,942
  • 5
  • 45
  • 81

3 Answers3

1

you should add an enter to your Qstring like this

QString a = "Q\x00D";

Pim
  • 393
  • 3
  • 18
0

In the example you have given, there is no "\n" in the QString! It is not getting lost, it is not there in the first place.

If a newline is necessary, then construct the String as QString a = "Q\n".

You can also construct the QByteArray directly from a character array rather than going through a char array -> QString -> QByteArrray conversion sequence, like so:

QByteArray b("Q\n");

EDIT: I realized that your contrived example where you are just sending the letter "Q" is probably a debug attempt, not your real code. In reality, you're getting data in as a QByteArray from some other signal that is emitting a QByteArray. That QByteArray that you are receiving must not include the newline character in it. If you are reading from a file or user input, then that is normal. Most readline-like functions strip off the trailing newline. If it is always necessary to have a newline, you can simply do something like this in your WriteData method:

void MainWindow::writeData(const QByteArray &data)
{
    serial->write(data);
    serial->write("\n");
}

If sometimes the passed-in QByteArray has a newline at the end and sometimes not, and your receiving device cannot handle redundant newlines, then you'd need to check whether data ends with a newline and only write the "\n" if it does not.

ScottG
  • 773
  • 6
  • 18
0

what if you make the QByteArray like this

QByteArray b(&a);
Sime
  • 144
  • 2
  • 14
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Hcorg Oct 29 '15 at 09:47
  • Perhaps my answer should have had more details as to why I think it is the answer. I assume you mean that when you say this doesn't provide an answer. But I was neither trying to criticize the question nor request a clarification. – Sime Oct 30 '15 at 09:03