kshfire / qextserialport

Automatically exported from code.google.com/p/qextserialport
Other
0 stars 0 forks source link

On Linux - port->write(QByteArray) fails whit String of NULL-characters? #108

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I have a device that is reset by a string of 5 '\x00' chars. 
For all the data I send to the device I use a QByteArray instance like this one 
here:
port->write(QByteArray("SomeData"));

This works fine unless there is a  '\x00' char in the data. In this case the 
write call fails, because the write() call in QIODevice is implemented as:
inline qint64 write(const QByteArray &data) { 
    return write(data.constData(), data.size()); 
}

but for QByteArray.constdata() the QT documentation says:
" Returns a pointer to the data stored in the byte array. The pointer can be 
used to access the bytes that compose the array. The data is '\0'-terminated. "

So thats why my reset-Command fails:
port->write(QByteArray("\x00\x00\x00\x00\x00"));
simply does not write anything.

I guess you can't really call this a bug, but it was at least a surprise for 
me. Should this be mentioned in the documentation of you library?

Anyway, the solution is obviuos, use the write()-method that accepts a const 
char pointer and the number of bytes to be written. 

In my case :
    port->write("\x00\x00\x00\x00\x00",5);
solved the problem.

Thanks for reading
Eberhard

Original issue reported on code.google.com by eberhard...@gmail.com on 19 Oct 2011 at 3:17

GoogleCodeExporter commented 8 years ago
No, this has nothing with QextSerialPort.

If you want to construct an QByteArray which contians a bytes stream likes 
this: "\x00\x00\x00\x00\x00"

You should write 
QByteArray("\x00\x00\x00\x00\x00", 5);

instead of

QByteArray("\x00\x00\x00\x00\x00");

Original comment by dbzhang...@gmail.com on 10 Nov 2011 at 1:44

GoogleCodeExporter commented 8 years ago
> You should write
> QByteArray("\x00\x00\x00\x00\x00", 5);

No, this won't work. Two QByteArrays one created by 
QByteArray("\x00\x00\x00\x00\x00");
the other created by 
QByteArray("\x00\x00\x00\x00\x00", 5);
contain the same data.

So a call to QByteArray.constdata() will return an emtpy String "" in both 
cases and nothing gets written.

The solution is to use the QIODevice.write(const char *data, int size); method 
which doesn't care about the content found at the const char *data pointer.

Eberhard

Original comment by eberhard...@gmail.com on 10 Nov 2011 at 12:00

GoogleCodeExporter commented 8 years ago
But as I said in my first post; i don't think this is a bug or defect , rather 
a coding gotcha for noobs like me.

Original comment by eberhard...@gmail.com on 10 Nov 2011 at 12:01

GoogleCodeExporter commented 8 years ago
No, you are wrong.

QByteArray("\x00\x00\x00\x00\x00");
equals
QByteArray("\x00\x00\x00\x00\x00", strlen("\x00\x00\x00\x00\x00"));
equals
QByteArray("\x00\x00\x00\x00\x00", 0);

Original comment by dbzhang...@gmail.com on 11 Nov 2011 at 6:03

GoogleCodeExporter commented 8 years ago

Original comment by dbzhang...@gmail.com on 16 Mar 2012 at 9:03