copy unsigned char array into QByteArray ..... problems
-
Thanks to all,
in the test code, i am initializing the array with integers.
then i tried to assign or copy into QByteArray.
while copy/paste, missed the lines.int i; for(i=0;i<20;i++){ wbuf[i]=i; } m_writeData =QByteArray::fromRawData(wbuf, sizeof(wbuf));
how to change the above line of code.
Thanks.
-
hi @o6a6r9v1p
QSerialport is derived from QioDevice, therefore write has at least 3 overwrites(those from the base class)- qint64 write(const char *data, qint64 maxSize)
- qint64 write(const char *data)
- qint64 write(const QByteArray &byteArray)
So you can indeed pass it a char array, no need to convert it into a QByteArray
That said, are you sure, about what you do ?
because in here
for(i=0;i<64;i++){ writeData =QByteArray::fromRawData(buf, sizeof(buf)); //ERROR here }
while the loop is running,
buf
will not change, It changes only if you do some hacky stuff!
E.g threads without mutex, processEvent calls etc.@J.Hilk hi,
passed buf[] as you told, as a parameter to QSerialPort(), but it gives same error.
The code is:for(i=0;i<20;i++){ wbuf[i]=i; } const qint64 bytesWritten = serPort->write(wbuf));
the error is:
invalid conversion from "unsigned char*" to "const char*"
can you point to examples or doc pages, if possible.
Thanks.
-
@J.Hilk hi,
passed buf[] as you told, as a parameter to QSerialPort(), but it gives same error.
The code is:for(i=0;i<20;i++){ wbuf[i]=i; } const qint64 bytesWritten = serPort->write(wbuf));
the error is:
invalid conversion from "unsigned char*" to "const char*"
can you point to examples or doc pages, if possible.
Thanks.
@o6a6r9v1p Please read what @raven-worx wrote (hint: casting)
-
@o6a6r9v1p Please read what @raven-worx wrote (hint: casting)
-
@jsulm
hi,
can we do it the way it is done C?
or is it different in C++?I am not good in C++.
Thanks.
const qint64 bytesWritten = serPort->write(reinterpret_cast<char*>(wbuf)));
-
@jsulm
hi,
can we do it the way it is done C?
or is it different in C++?I am not good in C++.
Thanks.
@o6a6r9v1p
take a look herehttp://www.cplusplus.com/doc/tutorial/typecasting/
you can use c-style casting, but you're encuraged to use the cpp variants
-
const qint64 bytesWritten = serPort->write(reinterpret_cast<char*>(wbuf)));
-
@jsulm
hi,
Thanks for the word. this line was used, think i am wrong some where.const qint64 bytesWritten = serPort->write(const QByteArray ((unsigned char*) wbuf));
where did i miss?
Thanks,
@o6a6r9v1p Why do you create a byte array? @J-Hilk already said here that this is not needed.
What is wrong in your code: remove the const before QByteArray. You should learn more about C++. -
@o6a6r9v1p Why do you create a byte array? @J-Hilk already said here that this is not needed.
What is wrong in your code: remove the const before QByteArray. You should learn more about C++. -
@o6a6r9v1p Again: there is NO need for QByteArray.
This line should work as well, did you try?const qint64 bytesWritten = serPort->write(reinterpret_cast<char*>(wbuf)));
-
@o6a6r9v1p Why do you create a byte array? @J-Hilk already said here that this is not needed.
What is wrong in your code: remove the const before QByteArray. You should learn more about C++. -
@jsulm
removed const as told.
still i am getting:invaild conversion from unsigned char* to const char* error.
i know c programming, but not good in c++,
thanks,
@o6a6r9v1p You should read more carefully - I already showed you what you need to do.
- Why do you want to use QByteArray? It is not needed. QByteArray does not care about any order - it is the order you give it.
- You need to cast unsigned char* to char*. See my previous posts to see how, I don't want to write it again and again.
-
@o6a6r9v1p Again: there is NO need for QByteArray.
This line should work as well, did you try?const qint64 bytesWritten = serPort->write(reinterpret_cast<char*>(wbuf)));